-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwriteCard.py
More file actions
executable file
·169 lines (130 loc) · 5.03 KB
/
writeCard.py
File metadata and controls
executable file
·169 lines (130 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/env python
# -*- coding: utf8 -*-
# if no params, writes list of members without cards
# if --full param, writes list of all members in system, shames member
# if id param, writes id of member without a card to the next card
# if id and --replace, writes id of member replacement card to next card, shames member
import RPi.GPIO as GPIO
import MFRC522
import signal
import MySQLdb
import ConfigParser
import argparse
import sys
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
# hook up the argparser
parser = argparse.ArgumentParser()
parser.add_argument(
'--full',
help='Show all members, including members who already have cards.',
action="store_true",
default=False
)
parser.add_argument(
'--replace',
help='Replace a card for a member; suggested donation $10.',
action="store_true",
default=False
)
parser.add_argument('id', help='Member id to write to card.', default=None, nargs='?')
args = parser.parse_args()
# parse the config:
config = ConfigParser.ConfigParser()
config.read("/home/pi/prms-door/door.ini")
# connect the database
db = MySQLdb.connect(
config.get('db', 'server'),
config.get('db', 'user'),
config.get('db', 'password'),
config.get('db', 'database')
)
db.autocommit(True)
curs = db.cursor()
# no id, show list
if not args.id:
query = 'SELECT id as member_id, name, account_status as status FROM thelist'
if not args.full:
query += ' WHERE has_card = 0'
curs.execute(query)
if curs.rowcount:
print("ID\t\tName\t\t\tStatus")
result = curs.fetchall()
for row in result:
print(str(row[0]) + "\t" + row[1] + "\t\t" + row[2])
print("Use -h to show all options.")
sys.exit()
# id, verify and write card
query = """SELECT account_status, has_card, name FROM thelist WHERE id = %s"""
if not args.replace:
query += " AND has_card = 0"
curs.execute(query, (args.id))
if curs.rowcount != 1:
print "Error - invalid id. " + str(curs.rowcount) + " members found."
sys.exit()
status, has_card, name = curs.fetchone()
if str(status) != 'Active':
print("User's membership status is currently " + str(status) + ", cannot create card.")
sys.exit()
if has_card:
print("Replacing a card for " + name + " again? Please request a $10 donation to cover the cost.")
# valid id, replacement is flagged appropriately, let's write the card
print("Ready - please swipe the new card.")
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
cardreader = MFRC522.MFRC522()
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# Scan for cards
(status,TagType) = cardreader.MFRC522_Request(cardreader.PICC_REQIDL)
# If a card is found
if status == cardreader.MI_OK:
print "Card detected, preparing to write."
# Get the UID of the card
(status,uid) = cardreader.MFRC522_Anticoll()
# If we have the UID, continue
if status == cardreader.MI_OK:
# Print UID
#print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
cardreader.MFRC522_SelectTag(uid, False)
# Authenticate
card_status = cardreader.MFRC522_Auth(cardreader.PICC_AUTHENT1A, 8, key, uid)
# Check if authenticated
if card_status == cardreader.MI_OK:
# Variable for the data to write
data = []
# Fill the data with 0xFF
for x in range(0,8):
data.append(0xFF)
for i in args.id:
data.append(int(i))
cardreader.MFRC522_Write(8, data, True)
#read data to confirm
data = cardreader.MFRC522_Read(8, True)
print("written, card data read:")
print(str(data))
cardreader.MFRC522_StopCrypto1()
memberId = ''.join(map(str, data[len(data)/2:]))
if memberId != args.id:
print "Write error; unable to confirm member id written properly. Please try again."
print "Expected: " + args.id + "\t\tFound: " + memberId
print "If this error continues, please file a bug."
sys.exit()
curs.execute("""INSERT INTO event_log VALUES (%s, %s, CURRENT_TIMESTAMP)""",(args.id, 'card written'))
curs.execute ("""UPDATE thelist SET has_card=true, last_updated=CURRENT_TIMESTAMP WHERE id=%s""",(args.id))
db.commit()
print("Id " + memberId + " for '" + name + "' confirmed written to card.")
# Make sure to stop reading for cards
continue_reading = False
db.close()
else:
print "Card authentication error - status: "+str(card_status)