-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrfidReader.py
More file actions
executable file
·262 lines (235 loc) · 7.74 KB
/
Copy pathrfidReader.py
File metadata and controls
executable file
·262 lines (235 loc) · 7.74 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#!/usr/bin/python
import subprocess # for calling beep for piezo
import RPi.GPIO as GPIO
import time # for sleep
import glob
import os
import logging
import config
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.FileHandler('/var/log/nfc.log')
handler.setLevel(logging.INFO)
formatter = logging.Formatter('(%(levelname)s) %(asctime)s %(message)s','%Y-%m-%d %H:%M:%S')
handler.setFormatter(formatter)
logger.addHandler(handler)
RED_LED = 22
BLUE_LED = 17
GREEN_LED = 27
DOOR_LATCH = 25
FILE_CMD_PATH = "/opt/nfc/doorcmds/"
FILE_CMD_GLOB = "/opt/nfc/doorcmds/*"
FILE_CMD_OPEN = "/opt/nfc/doorcmds/open"
FILE_CMD_INIT = "/opt/nfc/doorcmds/init"
def log(data):
print time.strftime("%Y-%b-%d:%H:%M:%S"),":",data
import nfc
import database
localDB = None
remoteDB = None
def init_gpio():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(GREEN_LED, GPIO.OUT) # green LED
GPIO.setup(BLUE_LED, GPIO.OUT) # blue LED
GPIO.setup(RED_LED, GPIO.OUT) # red LED
GPIO.setup(DOOR_LATCH, GPIO.OUT) # relay
GPIO.output(GREEN_LED, False) # green LED
GPIO.output(BLUE_LED, False) # blue LED
GPIO.output(RED_LED, False) # red LED
if (nodeType == "INTERNAL_DOOR"):
GPIO.output(DOOR_LATCH, True) # relay
else:
GPIO.output(DOOR_LATCH, False) # relay
def init():
global localDB
global remoteDB
global nodeType
global lastAutoUnlockTime
lastAutoUnlockTime = time.time()
localDB = database.MemberDatabase(config.localDBHost,config.localDBUser,config.localDBPass,config.localDBDatabase)
remoteDB = database.MemberDatabase(config.remoteDBHost,config.remoteDBUser,config.remoteDBPass,config.remoteDBDatabase,config.remoteDBPort,config.remoteDBSSL)
nodeType = localDB.get_node_type()
init_gpio()
clearFileCmds()
subprocess.call(["/usr/local/bin/beep"])
nfc.init()
def ledUpdate(mode):
if (mode == "bad hash"):
GPIO.output(RED_LED, True)
GPIO.output(GREEN_LED, True)
time.sleep(0.1)
GPIO.output(RED_LED, False)
GPIO.output(GREEN_LED, False)
time.sleep(0.1)
GPIO.output(RED_LED, True)
GPIO.output(GREEN_LED, True)
time.sleep(0.1)
GPIO.output(RED_LED, False)
GPIO.output(GREEN_LED, False)
elif (mode == "bad serial"):
GPIO.output(RED_LED, True)
time.sleep(0.2)
GPIO.output(RED_LED, False)
time.sleep(0.2)
GPIO.output(RED_LED, True)
time.sleep(0.2)
GPIO.output(RED_LED, False)
elif (mode == "disabled"):
GPIO.output(RED_LED, True)
GPIO.output(BLUE_LED, True)
time.sleep(0.1)
GPIO.output(RED_LED, False)
GPIO.output(BLUE_LED, False)
time.sleep(0.1)
GPIO.output(RED_LED, True)
GPIO.output(BLUE_LED, True)
time.sleep(0.1)
GPIO.output(RED_LED, False)
GPIO.output(BLUE_LED, False)
else:
GPIO.output(RED_LED, True)
time.sleep(0.4)
GPIO.output(RED_LED, False)
time.sleep(0.4)
GPIO.output(RED_LED, True)
time.sleep(0.4)
GPIO.output(RED_LED, False)
def open_door(unlocked):
GPIO.output(DOOR_LATCH, True) # relay
logger.info("Door unlocked")
subprocess.call(["/usr/local/bin/beep"])
# Turn on success LED
GPIO.output(GREEN_LED, True)
time.sleep(5)
# relock door after 5 seconds
if not unlocked:
GPIO.output(DOOR_LATCH, False) # relay
logger.info("Door locked")
# Turn off success LED
GPIO.output(GREEN_LED, False)
def init_card():
GPIO.output(BLUE_LED, True)
GPIO.output(RED_LED, True)
logger.info("Waiting for card to initialze")
card = nfc.initCard()
if (card == None):
# Turn off blue, to leave red on showing error
GPIO.output(BLUE_LED, False)
GPIO.output(GREEN_LED, False)
GPIO.output(RED_LED, True)
time.sleep(2)
GPIO.output(RED_LED, False)
return False
if remoteDB.check():
# Check to see if card exists
cardData = localDB.get_card_data(card[0])
if cardData != None:
remoteDB.update_hash(cardData[0],card[1])
GPIO.output(RED_LED, False)
GPIO.output(BLUE_LED, False)
return True
# Add card to database
logger.info("Adding new card to database")
remoteDB.add_card(card[0],card[1])
GPIO.output(RED_LED, False)
GPIO.output(BLUE_LED, False)
return False
def clearFileCmds():
cmds = glob.glob(FILE_CMD_GLOB)
for cmd in cmds:
os.remove(cmd)
def checkAutoUnlockedState():
global lastAutoUnlockTime
if (time.time()-lastAutoUnlockTime > 1):
unlocked = localDB.check_auto_open()
if (unlocked and (GPIO.input(DOOR_LATCH)==0)) or (not unlocked and (GPIO.input(DOOR_LATCH)==1)):
logger.info("Auto Unlock state changed to %s"%(unlocked))
if (unlocked):
# Unlock the Door
GPIO.output(DOOR_LATCH, True) # relay
logger.info("Door unlocked")
else:
# Lock the Door
GPIO.output(DOOR_LATCH, False) # relay
logger.info("Door locked")
lastAutoUnlockTime = time.time()
def checkFileCmd():
# Check to see if a local file is commanding action and if so do It
cmds = glob.glob(FILE_CMD_GLOB)
# Split off the filename from the full path if anything is returned
if cmds:
user_id = None
try:
fh = open(cmds[0],'r')
user_id = fh.readline().rstrip()
fh.close()
except:
logger.info("Failed to read user id from command file")
user_id = No # Check name for a valid command, and execute
if (cmds[0] == FILE_CMD_OPEN):
remoteDB.log(None,user_id,"Remote Door Unlock","REMOTE_CMD")
logger.info("Remote unlock from user "+user_id)
open_door(False)
os.remove(cmds[0])
elif (cmds[0] == FILE_CMD_INIT):
remoteDB.log(None,user_id,"Remote Card Init","REMOTE_CMD")
init_card()
os.remove(cmds[0])
time.sleep(1)
return
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
def poll():
card = None
unlocked = False
lastTime = time.time()
logger.info("Waiting for new card")
while (card == None):
card = nfc.getCard()
checkFileCmd()
checkAutoUnlockedState()
localDB.periodic_database_ping()
remoteDB.periodic_database_ping()
logger.info("Card found: serial="+card[0]+" hash="+card[1])
if not localDB.check():
logger.info("Local database not connected!")
return None
card_id = localDB.get_valid_card(card[0],card[1],remoteDB)
if (is_number(card_id) == False):
ledUpdate("bad serial")
return False
# Update card hash if configured
if (config.rehash):
new_hash = nfc.generateNewHash(card[1])
if (nfc.writeHash(new_hash[0])):
# Update database
remoteDB.update_hash(card_id,new_hash[1])
else:
# Set fault bit on card
remoteDB.update_hash(card_id,card[1],1)
ledUpdate("bad hash")
return False
# Check for access
access_status = localDB.check_valid_access(card_id, remoteDB)
if (access_status != True):
ledUpdate(access_status)
return False
else:
# Log Success
#db_log(cardData[0],cardData[1],"Access Granted")
#log("Access Granted: Card ID=%s User ID=%s" % (card_id,cardData[1]))
# Open Door
open_door(unlocked)
return True
# Main
init()
while(1):
if poll() is None:
time.sleep(1)
print "Exited while(1)? We must be done?"
nfc.close()