forked from makeitlabs/doorbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdoor.py
executable file
·89 lines (64 loc) · 2 KB
/
door.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
from time import sleep
import re
import setup
import traceback
from setup import botlog, reader, SERIAL_BAUD_RATE
import authenticate
import door_hw
botlog.info( '%s Starting.' % setup.botname)
reader.initialize(baud_rate=setup.SERIAL_BAUD_RATE)
blinker = 0
while True :
try :
blinker += 1
if blinker%10 == 0 :
door_hw.green(False)
sleep(.1)
door_hw.green(True)
# Get a card
#
rfid_str = reader.get_card()
if not rfid_str :
continue
botlog.debug( 'RFID string >>%s<<' % rfid_str)
# Several things can happen:
# 1. some error in reading.
# 2. good card, not allowed
# 3. good card, allowed: yay!
# 4. bad card from some reason
# 5. unknown card
try :
rfid = int(rfid_str)
access = authenticate.get_access(rfid)
if access :
(username,allowed) = access
if 'allowed' in allowed :
#3, yay!
#
botlog.info('%s allowed' % username)
# open the door
#
door_hw.open_sesame()
else :
#2
# access failed. blink the red
#
botlog.warning('%s DENIED' % username)
door_hw.blink_red()
else :
#5
botlog.warning('Unknown card %s ' % rfid_str)
door_hw.blink_red()
except :
#4
# bad input catcher, also includes bad cards
#
botlog.warning('bad card %s ' % rfid_str)
door_hw.blink_red(4)
except :
e = traceback.format_exc().splitlines()[-1]
botlog.error('door loop unexpected exception: %s' % e)
pass
sleep(3)
reader.flush() # needed for serial reader that keeps sending stuff.
# 0001258648