forked from TinkerMill/tinkerAccess
/
BADGE_SCAN_APP.py
295 lines (247 loc) · 8.93 KB
/
BADGE_SCAN_APP.py
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import threading
import Queue
import time
import lcdModule as LCD
import badgeScanModule as SCAN
import dataBaseAccess as DB
import RPi.GPIO as GPIO
#!/usr/bin/python
import sys
from select import select
deviceAccessQ = Queue.Queue()
LcdQ= Queue.Queue()
eventQ = Queue.Queue()
EVENT_MSG_NULL = 0
EVENT_MSG_RESET = 1
EVENT_MSG_BADGE_SCAN = 2
EVENT_MSG_USER_GRANTED_ACCESS = 3
EVENT_MSG_TIMER_EXPIRE = 4
KILL_APP_PIN = 13
RESET_PIN = 16
LOCK_PIN = 17
############################################################
def taskResetButton():
GPIO.setup(RESET_PIN, GPIO.IN)
GPIO.setup(KILL_APP_PIN, GPIO.IN)
resetPinVal = GPIO.LOW
killAppPinVal = GPIO.LOW
while 1:
time.sleep(0.1)
resetPinVal = GPIO.input(RESET_PIN)
if resetPinVal == GPIO.HIGH:
print "\nIn taskResetButton - reset button pressed"
eventQ.put( (EVENT_MSG_RESET,"LOGGING OFF",time.time()) )
resetPinVal = GPIO.LOW
time.sleep(3)
resetPinVal = GPIO.input(KILL_APP_PIN)
if killAppPinVal == GPIO.HIGH:
print "\n\nKILL APP!!!\n\n"
taskLCD_ID.exit()
taskBadgeScan_ID.exit()
taskDeviceAccessControl_ID.exit()
return 0;
LCD.lcd_init()
def taskLCD():
while 1:
#print "in taskLCD"
time.sleep(0.3)
if not LcdQ.empty():
LcdText = LcdQ.get()
print "-------------------------------LCD Display:", LcdText
LCD.lcd_string(LcdText[0],LCD.LCD_LINE_1)
LCD.lcd_string(LcdText[1],LCD.LCD_LINE_2)
def taskBadgeScan():
retcode = SCAN.scanInit()
while 1:
badgeId = SCAN.watchPort() # wait here until a badge is scanned
eventQ.put( (EVENT_MSG_BADGE_SCAN,badgeId, 0 ) )
print "\nIn taskBadgeScan - badge scanned event", badgeId
EXPIRED = "00:00"
def taskDeviceAccessControl():
time_until_expire = ('INIT',EXPIRED)
while 1:
time.sleep(0.3)
if not deviceAccessQ.empty():
time_until_expire = deviceAccessQ.get()
print "in taskDeviceAccessControl ", time_until_expire
if time_until_expire[1] == EXPIRED:
GPIO.output(LOCK_PIN, False)
else:
GPIO.output(LOCK_PIN, True)
#########################################################
STATE_INIT = 0
STATE_IDLE = 1
STATE_VALIDATE_BADGE = 2
STATE_USER_LOGGED_IN = 3
STATE_IN_USE = 4
STATE_LOGGING_OFF = 5
STATE_TIMER_EXPIRE = 6
STATE_INVALID_USER = 7
STATE_FAILURE = 0xf
def stateMachine():
print "Main State Machine"
curState = STATE_INIT
eventMsg = (EVENT_MSG_NULL,"","")
prevEventMsg = eventMsg
while(1):
time.sleep(0.5)
if not eventQ.empty():
prevEventMsg = eventMsg
eventMsg = eventQ.get()
if curState == STATE_INIT:
curState = state_init(eventMsg)
elif curState == STATE_IDLE:
curState = state_idle(eventMsg)
elif curState == STATE_VALIDATE_BADGE:
curState = state_validate_user_badge(eventMsg)
elif curState == STATE_USER_LOGGED_IN:
curState = state_user_logged_in(eventMsg)
elif curState == STATE_INVALID_USER:
eventMsg = prevEventMsg
curState = state_user_logged_in(eventMsg)
eventQ.put( eventMsg)
elif curState == STATE_LOGGING_OFF:
curState = state_logoff_user(eventMsg)
#####################################
#
#####################################
def state_init(eventMsg):
print "state_init = ", eventMsg
LcdQ.put( ("WELCOME",time.ctime(time.time())) )
time.sleep(1)
if eventMsg[0] == EVENT_MSG_BADGE_SCAN:
nextState = STATE_VALIDATE_BADGE
else:
nextState = STATE_IDLE
return nextState
def state_idle(eventMsg):
print "state_idle = ", eventMsg
LcdQ.put( ("SCAN YOUR BADGE", time.ctime(time.time())) )
time.sleep(1)
if eventMsg[0] == EVENT_MSG_BADGE_SCAN:
nextState = STATE_VALIDATE_BADGE
elif eventMsg[0] == EVENT_MSG_RESET:
nextState = STATE_INIT
else:
nextState = STATE_IDLE
return nextState
def state_validate_user_badge(eventMsg):
print "state_validate_user_badge = ", eventMsg
LcdQ.put( ("CHECKING BADGE", time.ctime(time.time())) )
if eventMsg[0] == EVENT_MSG_NULL:
nextState = STATE_IDLE
elif eventMsg[0] == EVENT_MSG_RESET:
nextState = STATE_INIT
elif eventMsg[0] == EVENT_MSG_BADGE_SCAN:
dbUserValues = {}
dbUserValues = DB.queryUserNameFromBadgeId(eventMsg[1])
if (float(dbUserValues['time']) > 0 ):
timeend = float(dbUserValues['time']) +time.time()
eventQ.put( (EVENT_MSG_USER_GRANTED_ACCESS, dbUserValues['username'], timeend ) )
nextState = STATE_USER_LOGGED_IN
else:
LcdQ.put( (dbUserValues['username'], ("NOT PERMITTED") ) )
time.sleep(5)
nextState = STATE_IDLE
elif eventMsg[0] == EVENT_MSG_TIMER_EXPIRE:
nextState = STATE_IDLE
else:
nextState = STATE_IDLE
return nextState
def state_user_logged_in(eventMsg):
print "state_user_logged_in = ", eventMsg
if eventMsg[0] == EVENT_MSG_RESET:
deviceAccessQ.put( ("LOGGING OFF", ("00:00") ) )
nextState = STATE_LOGGING_OFF
elif eventMsg[0] == EVENT_MSG_USER_GRANTED_ACCESS:
logged_in_user_name = eventMsg[1]
tempname = logged_in_user_name
time_end = eventMsg[2]
if time_end < time.time():
time_remaining = 0;
else:
time_remaining = time_end-time.time()
print " time remaining",(time.ctime( time_remaining ) )[14:19]
if (time.ctime( time_remaining ) )[14:19] == EXPIRED:
LcdQ.put( (logged_in_user_name, (time.ctime( time_remaining ) )[14:19]) )
deviceAccessQ.put( (logged_in_user_name, (time.ctime( time_remaining ) )[14:19]) )
nextState = STATE_LOGGING_OFF
else:
LcdQ.put( (logged_in_user_name, (time.ctime( time_remaining ) )[14:19]) )
deviceAccessQ.put( (logged_in_user_name, (time.ctime( time_remaining ) )[14:19]) )
nextState = STATE_USER_LOGGED_IN
elif eventMsg[0] == EVENT_MSG_BADGE_SCAN:
dbUserValues = {}
dbUserValues = DB.queryUserNameFromBadgeId(eventMsg[1])
if (float(dbUserValues['time']) > 0 ):
nextState = STATE_VALIDATE_BADGE
else:
LcdQ.put( (dbUserValues['username'], "NOT AUTH TO USE" ) )
time.sleep(5)
nextState = STATE_INVALID_USER
elif eventMsg[0] == EVENT_MSG_TIMER_EXPIRE:
nextState = STATE_LOGGING_OFF
else:
nextState = STATE_IDLE
return nextState
def state_logoff_user(eventMsg):
print "state_loggoff_user = ", eventMsg
LcdQ.put( ("LOGOFF USER", time.ctime(time.time())) )
time.sleep(1)
if eventMsg[0] == EVENT_MSG_RESET:
nextState = STATE_INIT
else:
nextState = STATE_IDLE
return nextState
def state_error_report(eventMsg):
print "state_XXXX = ", eventMsg
LcdQ.put( ("FAILED", eventMsg[1]) )
if eventMsg[0] == EVENT_MSG_RESET:
nextState = STATE_INIT
elif eventMsg[0] == EVENT_MSG_BADGE_SCAN:
nextState = STATE_VALIDATE_BADGE
else:
nextState = STATE_FAILURE
return nextState
############################
###########################
# state XXXX
#############################
def state_XXXX_(eventMsg):
print "state_XXXX = ", eventMsg
LcdQ.put( ("XXXX", time.ctime(time.time())) )
if eventMsg[0] == EVENT_MSG_NULL:
nextState = STATE_IDLE
elif eventMsg[0] == EVENT_MSG_RESET:
nextState = STATE_IDLE
elif eventMsg[0] == EVENT_MSG_BADGE_SCAN:
nextState = STATE_IDLE
elif eventMsg[0] == EVENT_MSG_TIMER_EXPIRE:
nextState = STATE_IDLE
else:
nextState = STATE_IDLE
return nextState
############################
##########################################################
#### using geany ~/../../etc/rc.local
#### to autolauncn
def main():
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(LOCK_PIN, GPIO.OUT)
taskResetButton_ID=threading.Thread( group=None, target=taskResetButton, name="taskResetButton", args=(), kwargs=None, verbose=None)
taskResetButton_ID.start()
taskLCD_ID=threading.Thread( group=None, target=taskLCD, name="taskLCD", args=(), kwargs=None, verbose=None)
taskLCD_ID.start()
taskBadgeScan_ID=threading.Thread( group=None, target=taskBadgeScan, name="taskBadgeScan", args=(), kwargs=None, verbose=None)
taskBadgeScan_ID.start()
taskDeviceAccessControl_ID=threading.Thread( group=None, target=taskDeviceAccessControl, name="taskDeviceAccessControl", args=(), kwargs=None, verbose=None)
taskDeviceAccessControl_ID.start()
stateMachine()
main()