-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
522 lines (435 loc) · 14.7 KB
/
main.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/python3
import serial
import click
import re
import logging
import time
import threading
import io
import queue
import traceback
import paho.mqtt.client as mqtt
import cherrypy
import os
from pathlib import Path
from datetime import datetime
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
mqtt_client = mqtt.Client()
db_filename = None
# Configuration
GPIO_RPI_OK = 22
GPIO_GSM_OK = 23
GPIO_OPEN = 27
# Sim800 board
GPIO_GSM_PWR = 17
GPIO_GSM_RST = 18
# MQTT host
MQTT_HOST = ""
event_queue = queue.SimpleQueue()
class Filter:
def match(self, time, number):
"""Return whether this filter matches the incoming request"""
return False
def label(self):
"""Return the label for this request, or None if no label"""
return None
class TimeFilter(Filter):
def __init__(self, start, end):
"""Start and end are minutes from midnight on sunday. This always repeats weekly"""
self.start = start
self.end = end
def match(self, time, number):
now = time.weekday()
now = now * 24 + time.hour
now = now * 60 + time.minute
if self.start > self.end:
return now >= self.start or now <= self.end
else:
return now >= self.start and now <= self.end
class NumberFilter(Filter):
def __init__(self, number, label):
self.label_ = label
self.number = number
def match(self, time, number):
return number == self.number
def label(self):
return self.label_
class Sim800Thread(threading.Thread):
def __init__(self, *, name="SIM800", device="/dev/ttyAMA0"):
super().__init__(name=name)
self.daemon = True
self.device_name = device
self.raw_device = serial.Serial(
port = self.device_name,
baudrate = 115200,
timeout = 1,
inter_byte_timeout = 0.1,
)
self.device = self.raw_device
# Start by trying to shut down the device, so that it can be woken up later
self.device.write(b"AT+CPOWD=1\n")
while self.device.readline() != b'':
pass
logger.info("GSM halted")
def run(self):
# Wait for connection
connected = False
while not connected:
self.device.write(b"AT\n")
for line in self.device:
logger.debug("GSM: %s", repr(line))
if line == b"OK\r\n":
connected = True
break
logger.info("GSM active")
# Set up the line
# Wait until the device is done booting
time.sleep(5)
self.raw_device.timeout = 10
#self.device.write(b"ATQ0V1E1+CREG=1;+CLIP=1;+CPIN=1111\n")
self.device.write(b"ATQ0V1E1+CREG=1;+CLIP=1\n")
logger.info("GSM initialized")
# It's a PITA to analyze the results, so just drop into the wait loop
while True:
line = self.device.readline()
logger.debug("GSM: %s", repr(line))
if line == b"":
# Toggle LED
self.device.write(b"AT\n")
continue
if line == b"OK\r\n":
event_queue.put(("GSM_OK", []))
continue
m = re.match(br"\+CREG: *(\d+)\r\n", line)
if m:
event_queue.put(("CREG", [int(m.group(1))]))
continue
m = re.match(br"\+CLIP: *([^\r\n]+)\r\n", line)
if m:
# Parse CLIP. Format is
# num:str,type:int,subnum:str,subtype,pbentry:str,valid:int
# We only really care about the first field
num = re.match(br'"([^"]*)",.*', m.group(1))
if num is not None:
event_queue.put(("RING", [num.group(1)]))
continue
logger.fatal("GSM ended")
class TickThread(threading.Thread):
def __init__(self, rate=0.1):
super().__init__(name="Tick")
self.daemon = True
self.rate = rate
def run(self):
while True:
time.sleep(self.rate)
event_queue.put(("HEARTBEAT", []))
class OpenerThread(threading.Thread):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.daemon = True
self.semaphore = threading.Semaphore(value=0)
def run(self):
while True:
self.semaphore.acquire()
logger.info("Opening")
GPIO.output(GPIO_OPEN, True)
time.sleep(1)
GPIO.output(GPIO_OPEN, False)
opener = OpenerThread(name="Opener")
class WebGatekeeper(threading.Thread):
def __init__(self):
super().__init__(name='WebGatekeeper')
self.daemon = True
@cherrypy.expose
def index(self):
return '<meta http-equiv="refresh" content="0;URL=\'/static/index.html\'" />'
@cherrypy.expose
def open_sesame(self):
logger.info('Opening gate from MQTT command')
if mqtt_client.is_connected():
mqtt_client.publish("hsg/gatekeeper/open", "web")
else:
logger.warning("Could not publish to MQTT. Attempting to reconnect...")
try:
mqtt_client.connect(MQTT_HOST)
mqtt_client.publish("hsg/gatekeeper/open", "web")
except:
logger.error("Could not connect to MQTT.")
opener.semaphore.release()
return '<meta http-equiv="refresh" content="0;URL=\'/static/opened.html\'" />'
def run(self):
conf = {
'/': {
'tools.sessions.on': True,
'tools.staticdir.root': os.path.abspath(os.getcwd())
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': './static'
}
}
logger.info('Starting WebGatekeeper frontend')
cherrypy.config.update(
{
'server.socket_host': '0.0.0.0',
}
)
cherrypy.quickstart(self, '/', conf)
def init():
global cached_db
cached_db = load_database()
GPIO.setup([
GPIO_RPI_OK,
GPIO_GSM_OK,
GPIO_OPEN,
GPIO_GSM_PWR
], GPIO.OUT, initial = GPIO.LOW)
GPIO.output(GPIO_RPI_OK, True)
# Spawn SIM800 listener
Sim800Thread().start()
# Start up sim800
GPIO.output(GPIO_GSM_PWR, True)
time.sleep(1.5)
GPIO.output(GPIO_GSM_PWR, False)
# Start timer
TickThread().start()
opener.start()
class Heartbeat:
PAT_HEARTBEAT = [
(1, True),
(1, False),
(1, True),
(7, False),
]
PAT_SLOW = [
(5, True),
(5, False),
]
PAT_FAST = [
(2, True),
(2, False),
]
PAT_VSLOW = [
(8, True),
(2, False),
]
PAT_OFF = [
(1, False),
]
PAT_ON = [
(1, True),
]
PAT_SOS = [(2, True), (2, False)] * 3 + [(6, True), (2, False)] * 3 + [(2,True), (2, False)] * 3 + [(6, False)]
def __init__(self, pin, active_low=False):
self.pin = pin
self.active_low = active_low
GPIO.setup(pin, GPIO.OUT)
self.pattern = None
self.set_mode(self.PAT_OFF)
def set_mode(self, pattern):
if self.pattern is pattern:
# Don't change the pattern if it would be to the current state
return
self.pattern = pattern
self.pos = -1
self.delay = 0
def pulse(self):
self.delay = self.delay - 1
if self.delay < 0:
self.pos = (self.pos + 1) % len(self.pattern)
self.delay, state = self.pattern[self.pos]
GPIO.output(self.pin, state ^ self.active_low)
gsm_ok = Heartbeat(GPIO_GSM_OK)
rpi_ok = Heartbeat(GPIO_RPI_OK)
rpi_ok.set_mode(Heartbeat.PAT_HEARTBEAT)
def clock_now():
return time.clock_gettime(time.CLOCK_MONOTONIC)
def loop():
last_gsm_ok = clock_now()
regstate = 0
while True:
event, args = event_queue.get()
#print(event, repr(args))
if event == "GSM_OK":
last_gsm_ok = clock_now()
elif event == "CREG":
logger.info("Registration state: %d", args[0])
regstate = args[0]
elif event == "RING":
handle_ring(args[0])
elif event == "HEARTBEAT":
# Update GSM_OK state
if clock_now() - last_gsm_ok > 30:
# GSM is out to lunch
gsm_ok.set_mode(Heartbeat.PAT_OFF)
elif regstate == 0:
# Not registered, not searching
gsm_ok.set_mode(Heartbeat.PAT_OFF)
elif regstate == 1:
# Registered, home network
gsm_ok.set_mode(Heartbeat.PAT_SLOW)
elif regstate == 2:
# Not registered, searching
gsm_ok.set_mode(Heartbeat.PAT_FAST)
elif regstate == 3:
# Registration denied
gsm_ok.set_mode(Heartbeat.PAT_SOS)
elif regstate == 5:
# Roaming
gsm_ok.set_mode(Heartbeat.PAT_VSLOW)
else:
gsm_ok.set_mode(Heartbeat.PAT_OFF)
gsm_ok.pulse()
rpi_ok.pulse()
def handle_ring(number):
global cached_db
if mqtt_client.is_connected():
mqtt_client.publish("hsg/gatekeeper/ring", 1)
else:
logger.warning("Could not publish to MQTT. Attempting to reconnect...")
try:
mqtt_client.connect(MQTT_HOST)
mqtt_client.publish("hsg/gatekeeper/ring", 1)
except:
logger.error("Could not connect to MQTT.")
logger.info("Received call from %s", number)
try:
number = number.decode("ascii")
except UnicodeDecodeError:
return
# Load the database
try:
db = load_database()
except Exception as e:
logger.exception("Failed to load config")
db = cached_db
else:
cached_db = db
now = datetime.now()
accept = False
label = None
if now.weekday() == 3 and now.hour >= 18:
label = "unknown, open day"
accept = True
if os.path.exists('/tmp/eventmode'):
label = "unknown, event mode"
accept = True
for filt in db:
if filt.match(now, number):
accept = True
if label == None or "unknown" in label:
label = filt.label()
if accept:
# Open door
if mqtt_client.is_connected():
mqtt_client.publish("hsg/gatekeeper/open", label or "anon")
else:
logger.warning("Could not publish to MQTT. Attempting to reconnect...")
try:
mqtt_client.connect(MQTT_HOST)
mqtt_client.publish("hsg/gatekeeper/open", label or "anon")
except:
logger.error("Could not connect to MQTT.")
logger.info("Door opened for: %s", label)
opener.semaphore.release()
def handle_mqtt_cmd(client, userdata, msg):
logger.info("Received MQTT command '%s' on topic '%s'", str(msg.payload), msg.topic)
if msg.payload.decode('utf-8') == 'open':
logger.info('Opening gate from MQTT command')
mqtt_client.publish("hsg/gatekeeper/open", "mqtt")
opener.semaphore.release()
if msg.payload.decode('utf-8') == 'eventmode?':
logger.info('Someone queried event mode state. Publishing.')
if os.path.exists('/tmp/eventmode'):
mqtt_client.publish("hsg/gatekeeper/eventmode", "1")
else:
mqtt_client.publish("hsg/gatekeeper/eventmode", "0")
if msg.payload.decode('utf-8') == 'enable_eventmode':
logger.info('Enabling event mode')
Path('/tmp/eventmode').touch()
if os.path.exists('/tmp/eventmode'):
mqtt_client.publish("hsg/gatekeeper/eventmode", "1")
else:
mqtt_client.publish("hsg/gatekeeper/eventmode", "0")
if msg.payload.decode('utf-8') == 'disable_eventmode':
logger.info('Disabling event mode')
try:
os.remove('/tmp/eventmode')
except:
logger.info('Failed to disable event mode - already disabled?')
if os.path.exists('/tmp/eventmode'):
mqtt_client.publish("hsg/gatekeeper/eventmode", "1")
else:
mqtt_client.publish("hsg/gatekeeper/eventmode", "0")
def handle_mqtt_connect(client, userdata, flags, rc):
logger.info("Connected to MQTT server")
client.subscribe("hsg/gatekeeper/cmd")
def load_database():
# TODO: fill this in
with open(db_filename, "rt") as f:
filters = []
for rawline in f:
line = rawline.strip().split('#')[0].split()
if len(line) == 0:
continue
elif line[0] == "*":
# Date pattern
daystart = int(line[1]) * 60 * 24
stime = parse_time(line[2]) + daystart
etime = parse_time(line[3]) + daystart
filters.append(TimeFilter(stime, etime))
elif line[0].startswith("+"):
num = line[0][1:]
if len(line) > 1:
label = " ".join(line[1:])
else:
label = None
filters.append(NumberFilter(num, label))
else:
logger.warning("DB: Don't know what to do with line %r", rawline)
return filters
def configure_log(use_journald, verbosity):
global logger
levels = [
logging.WARN,
logging.INFO,
logging.DEBUG
]
if verbosity >= len(levels):
verbosity = -1
handlers = []
if use_journald:
import systemd.journal
handlers.append(systemd.journal.JournalHandler())
else:
handlers.append(logging.StreamHandler())
# TODO: File handler
logging.basicConfig(level = levels[verbosity], handlers=handlers)
logger = logging.getLogger("main")
@click.command()
@click.option("--journald/--no-journald", default=False)
@click.option("-v", '--verbose', count=True)
@click.option("-d", "--database", required=True)
@click.option("-m", "--mqtt")
@click.option('--web/--no-web', default=False)
def main(journald, verbose, database, mqtt, web):
global db_filename, MQTT_HOST
db_filename = database
configure_log(journald, verbose)
if mqtt:
MQTT_HOST = mqtt
try:
mqtt_client.connect(mqtt)
except Exception:
logger.error("Failed to connect to MQTT - will try again later.")
mqtt_client.on_connect = handle_mqtt_connect
mqtt_client.on_message = handle_mqtt_cmd
mqtt_client.loop_start()
if web:
WebGatekeeper().start()
init()
loop()
if __name__ == "__main__":
main()