2020-02-20 00:45:27 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.INFO)
|
|
|
|
|
|
|
|
from multiprocessing import Process, Queue
|
|
|
|
from queue import Empty
|
2020-02-20 02:11:38 +00:00
|
|
|
import RPi.GPIO as GPIO
|
2020-02-23 01:03:41 +00:00
|
|
|
import os
|
2020-02-20 02:11:38 +00:00
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
import serial
|
2020-02-20 00:45:27 +00:00
|
|
|
import time
|
2020-02-23 01:19:53 +00:00
|
|
|
from signal import *
|
2020-02-20 00:45:27 +00:00
|
|
|
|
2020-02-27 23:07:23 +00:00
|
|
|
import secrets
|
|
|
|
|
2020-02-23 01:03:41 +00:00
|
|
|
DEBUG = os.environ.get('DEBUG', False)
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
RELAY_PIN = 17
|
|
|
|
RFID_EN_PIN = 27
|
|
|
|
CARDS_FILE = 'card_data.json'
|
|
|
|
OPEN_DURATION = 4
|
|
|
|
|
2020-02-20 02:11:38 +00:00
|
|
|
API_STATS = 'https://api.my.protospace.ca/stats/'
|
|
|
|
API_DOOR = 'https://api.my.protospace.ca/door/'
|
|
|
|
API_SEEN = lambda x: 'https://api.my.protospace.ca/door/{}/seen/'.format(x)
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
ser = None
|
|
|
|
|
2020-02-23 01:19:53 +00:00
|
|
|
def unlock_door():
|
|
|
|
GPIO.output(RELAY_PIN, GPIO.HIGH)
|
|
|
|
GPIO.output(RFID_EN_PIN, GPIO.HIGH)
|
|
|
|
|
|
|
|
time.sleep(OPEN_DURATION)
|
|
|
|
|
|
|
|
GPIO.output(RELAY_PIN, GPIO.LOW)
|
|
|
|
GPIO.output(RFID_EN_PIN, GPIO.LOW)
|
|
|
|
|
|
|
|
def lock_door_on_exit(*args):
|
|
|
|
logging.info('Exiting, locking door...')
|
|
|
|
GPIO.output(RELAY_PIN, GPIO.LOW)
|
|
|
|
GPIO.output(RFID_EN_PIN, GPIO.LOW)
|
|
|
|
os._exit(0)
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
def init():
|
|
|
|
global ser, cards
|
|
|
|
|
|
|
|
GPIO.setwarnings(False)
|
|
|
|
GPIO.setmode(GPIO.BCM)
|
|
|
|
GPIO.setup(RELAY_PIN, GPIO.OUT)
|
|
|
|
GPIO.output(RELAY_PIN, GPIO.LOW)
|
|
|
|
GPIO.setup(RFID_EN_PIN, GPIO.OUT)
|
|
|
|
GPIO.output(RFID_EN_PIN, GPIO.LOW)
|
|
|
|
logging.info('GPIO initialized')
|
|
|
|
|
|
|
|
ser = serial.Serial(port='/dev/ttyAMA0', baudrate=2400, timeout=0.1)
|
|
|
|
logging.info('Serial initialized')
|
|
|
|
|
2020-02-23 01:19:53 +00:00
|
|
|
for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
|
|
|
|
signal(sig, lock_door_on_exit)
|
|
|
|
logging.info('Signals initialized')
|
2020-02-20 00:45:27 +00:00
|
|
|
|
|
|
|
def reader_thread(card_data_queue):
|
|
|
|
recent_scans = {}
|
|
|
|
|
|
|
|
with open(CARDS_FILE, 'r') as f:
|
|
|
|
card_data = json.load(f)
|
|
|
|
logging.info('Read {} card numbers from disk'.format(str(len(card_data))))
|
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
card_data = card_data_queue.get_nowait()
|
|
|
|
except Empty:
|
|
|
|
pass
|
|
|
|
|
|
|
|
card = ser.readline()
|
|
|
|
if not card: continue
|
|
|
|
|
2022-01-23 02:04:12 +00:00
|
|
|
try:
|
|
|
|
card = card.decode().strip()
|
|
|
|
except UnicodeDecodeError:
|
|
|
|
continue
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
if len(card) != 10: continue
|
|
|
|
|
|
|
|
# debounce card scans
|
|
|
|
now = time.time()
|
|
|
|
if card in recent_scans:
|
|
|
|
if now - recent_scans[card] < 5.0:
|
|
|
|
continue
|
|
|
|
recent_scans[card] = now
|
|
|
|
|
|
|
|
logging.info('Read card: ' + card)
|
|
|
|
|
|
|
|
if card in card_data:
|
|
|
|
logging.info('Card recognized')
|
|
|
|
else:
|
|
|
|
logging.info('Card not recognized, denying access')
|
|
|
|
continue
|
|
|
|
|
|
|
|
logging.info('DOOR ACCESS - Card: {} | Name: {}'.format(
|
|
|
|
card, card_data[card],
|
|
|
|
))
|
|
|
|
|
|
|
|
unlock_door()
|
|
|
|
|
2020-02-20 02:11:38 +00:00
|
|
|
try:
|
|
|
|
res = requests.post(API_SEEN(card), timeout=2)
|
|
|
|
res.raise_for_status()
|
|
|
|
except BaseException as e:
|
|
|
|
logging.error('Problem POSTing seen: {} - {}'.format(e.__class__.__name__, str(e)))
|
|
|
|
continue
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
def update_thread(card_data_queue):
|
2020-02-20 02:11:38 +00:00
|
|
|
last_card_change = None
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
while True:
|
2020-02-20 02:11:38 +00:00
|
|
|
time.sleep(5)
|
2020-02-20 00:45:27 +00:00
|
|
|
|
2020-02-20 02:11:38 +00:00
|
|
|
try:
|
|
|
|
res = requests.get(API_STATS, timeout=5)
|
|
|
|
res.raise_for_status()
|
|
|
|
res = res.json()
|
|
|
|
except BaseException as e:
|
|
|
|
logging.error('Problem GETting stats: {} - {}'.format(e.__class__.__name__, str(e)))
|
|
|
|
continue
|
|
|
|
|
|
|
|
if res['last_card_change'] == last_card_change:
|
|
|
|
continue
|
|
|
|
last_card_change = res['last_card_change']
|
|
|
|
|
|
|
|
logging.info('Cards changed, pulling update from API')
|
|
|
|
|
|
|
|
try:
|
2020-02-27 23:07:23 +00:00
|
|
|
headers = {'Authorization': 'Bearer ' + secrets.DOOR_API_KEY}
|
|
|
|
res = requests.get(API_DOOR, headers=headers, timeout=5)
|
2020-02-20 02:11:38 +00:00
|
|
|
res.raise_for_status()
|
|
|
|
res = res.json()
|
|
|
|
except BaseException as e:
|
|
|
|
logging.error('Problem GETting door: {} - {}'.format(e.__class__.__name__, str(e)))
|
2020-02-27 23:07:23 +00:00
|
|
|
last_card_change = None
|
2020-02-20 02:11:38 +00:00
|
|
|
continue
|
|
|
|
|
|
|
|
logging.info('Got {} cards from API'.format(str(len(res))))
|
|
|
|
card_data_queue.put(res)
|
|
|
|
|
|
|
|
logging.info('Writing data to file')
|
|
|
|
with open(CARDS_FILE, 'w') as f:
|
|
|
|
json.dump(res, f)
|
|
|
|
|
|
|
|
def watchdog_thread():
|
|
|
|
while True:
|
|
|
|
with open('/dev/watchdog', 'w') as wdt:
|
|
|
|
wdt.write('1')
|
|
|
|
time.sleep(1)
|
2020-02-20 00:45:27 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
logging.info('Initializing...')
|
|
|
|
init()
|
|
|
|
|
|
|
|
card_data = Queue()
|
|
|
|
|
|
|
|
Process(target=reader_thread, args=(card_data,)).start()
|
|
|
|
Process(target=update_thread, args=(card_data,)).start()
|
2020-02-23 01:03:41 +00:00
|
|
|
if not DEBUG: Process(target=watchdog_thread).start()
|