-
Notifications
You must be signed in to change notification settings - Fork 10
/
doorbot.py
executable file
·226 lines (176 loc) · 6.74 KB
/
doorbot.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
#!/usr/bin/env python
import sys, os, time, serial, ConfigParser, json, logging
import argparse
from logging.handlers import SysLogHandler
from announcer import *
from relay import *
from RFUID import rfid
def ConfigObject(name):
clsname = config.get('doorbot', name)
cls = globals()[clsname]
params = dict(config.items(clsname))
return cls(**params)
class Doorbot(object):
def __init__(self):
self.mTime = 0
self.lastCard = None
self.cards = {}
self.cardFile = config.get('doorbot', 'cardfile')
self.relay = ConfigObject('relay')
self.announcer = ConfigObject('announcer')
self.reader = None
def reload_cards(self):
try:
currentMtime = os.path.getmtime(self.cardFile)
except IOError, e:
logging.critical('Cannot read card file: %s', e)
raise
if self.mTime != currentMtime:
logging.debug('Loading card table, mtime %d', currentMtime)
self.mTime = 0
newCards = {}
with open(self.cardFile) as f:
users = json.load(f)
for user in users:
if user.get('subscribed', True) == True:
for card in user['cards']:
card = card.encode('utf-8').upper()
nick = user['nick'].encode('utf-8')
newCards[card] = nick
self.cards = newCards
self.mTime = currentMtime
logging.info('Loaded %d cards', len(self.cards))
def check_card(self):
try:
for tag in self.reader.pn532.scan():
uid = tag.uid.upper()
if self.lastCard == uid:
return
self.on_card(uid)
self.lastCard = uid
except rfid.NoCardException:
self.lastCard = None
return
def on_card(self, uid):
self.reload_cards()
try:
person = self.cards[uid]
logging.info('%s authorised as %s', uid, person)
self.reader.green_on()
logging.debug('Triggering door relay')
self.relay.openDoor(config.getfloat('doorbot', 'open_duration'))
if args.foreground:
logging.info('Would announce to network')
else:
logging.info('Announcing to network')
self.announcer.send('RFID', uid, person)
except KeyError, e:
logging.warn('%s not authorised', uid)
self.reader.red_on()
if hasattr(self.relay, 'flashBad'):
logging.debug('Sending unauthorised flash')
self.relay.flashBad()
if args.foreground:
logging.info('Would announce failure to network')
else:
logging.info('Announcing failure to network')
self.announcer.send('RFID', uid, '')
logging.debug('Sleeping to avoid bounce')
time.sleep(2) # To avoid read bounces if the card is _just_ in range
def run(self):
self.reload_cards()
if args.foreground:
logging.info('Would announce start')
else:
logging.info('Announcing start')
self.announcer.send('START', '', '')
logging.debug('Starting outer loop')
# If the reader or relay disappears temporarily, we can recover safely
while True:
try:
self.reader = rfid.Pcsc.reader()
self.reader.open()
logging.info('PCSC firmware: %s', self.reader.pn532.firmware())
self.relay.connect()
except Exception, e:
logging.critical('Error during initialisation: %s', e)
raise
else:
self.main_loop()
# If it was working, give it some time to settle
time.sleep(5)
finally:
self.relay.disconnect()
self.reader.close()
def main_loop(self):
logging.debug('Starting main loop')
while True:
try:
self.check_card()
self.reader.leds_off()
time.sleep(0.2)
if self.relay.checkBell():
logging.info("Doorbell pressed")
self.relay.red_on()
if not args.foreground:
self.announcer.send('BELL', '', '')
if hasattr(self.relay, 'flashOK'):
self.relay.flashOK()
# Wait for button to be released
time.sleep(0.5)
except (serial.SerialException, serial.SerialTimeoutException), e:
logging.warn('Serial error during poll: %s', e)
return
except Exception, e:
logging.critical('Unexpected error during poll: %s', e)
# sometimes the card reader goes away and dosn't come back, if so we
# get "Error PC01 selecting card" forever, i don't think we can fix
# this from here, so lets just quit
# we could send a message to the net that we've failed.
# but the announcer api is clunky, i can has json re-write pls?
raise
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-f', '--foreground', action='store_true')
args = parser.parse_args()
return args
def open_config():
global config
config = ConfigParser.ConfigParser()
config.read((
'doorbot.conf',
sys.path[0] + '/doorbot.conf',
'/etc/doorbot.conf'
))
def set_logger():
if args.foreground:
logging.basicConfig(format='%(asctime)s %(levelname)-8s %(message)s', level=logging.DEBUG)
else:
logfac = config.get('doorbot', 'logfacility')
logfac = SysLogHandler.facility_names[logfac]
logger = logging.root
logger.setLevel(logging.DEBUG)
syslog = SysLogHandler(address='/dev/log', facility=logfac)
formatter = logging.Formatter('Doorbot[%(process)d]: %(levelname)-8s %(message)s')
syslog.setFormatter(formatter)
logger.addHandler(syslog)
def daemonise():
from daemon import DaemonContext
from pidfile import PidFile
daemon = DaemonContext(pidfile=PidFile("/var/run/doorbot.pid"))
daemon.open()
logging.info('Daemonised doorbot')
if __name__ == "__main__":
args = parse_args()
open_config()
set_logger()
if not args.foreground:
daemonise()
doorbot = Doorbot()
try:
doorbot.run()
# Top-level handlers because we're daemonised
except Exception, e:
logging.exception("Exception in main loop: %s" % e)
except:
logging.exception("Non-Exception caught")