2023-08-29 23:02:28 +00:00
|
|
|
import os
|
2020-02-20 00:45:27 +00:00
|
|
|
import logging
|
|
|
|
logging.basicConfig(
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
level=logging.INFO)
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
TEST = os.environ.get('TEST', False)
|
|
|
|
DEBUG = os.environ.get('DEBUG', False)
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
from multiprocessing import Process, Queue
|
|
|
|
from queue import Empty
|
2020-02-20 02:11:38 +00:00
|
|
|
import json
|
|
|
|
import requests
|
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
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if not TEST:
|
|
|
|
import serial
|
|
|
|
import RPi.GPIO as GPIO
|
|
|
|
|
2020-02-27 23:07:23 +00:00
|
|
|
import secrets
|
|
|
|
|
2020-02-23 01:03:41 +00:00
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
RELAY_PIN = 17
|
|
|
|
RFID_EN_PIN = 27
|
|
|
|
CARDS_FILE = 'card_data.json'
|
|
|
|
OPEN_DURATION = 4
|
2023-08-29 23:02:28 +00:00
|
|
|
VALID_PACKAGES = [
|
|
|
|
'Maker',
|
|
|
|
'Maker Plus',
|
|
|
|
'Maker Pro',
|
|
|
|
#'Storage bin rental',
|
|
|
|
#'Backyard Rental Spot',
|
|
|
|
'IndiCity Laser Space Rental',
|
|
|
|
#'Shipping container rental',
|
|
|
|
'Day Pass Holder',
|
|
|
|
'Access to everything 24/7',
|
|
|
|
'Barter Membership',
|
|
|
|
'Loft Member',
|
|
|
|
]
|
|
|
|
|
|
|
|
TEST_PIPE = '/tmp/airlock'
|
|
|
|
os.remove(TEST_PIPE)
|
|
|
|
|
|
|
|
API_MEMBERS = 'https://fabman.io/api/v1/members?limit=1000&embed=key&embed=activePackages&includeKeyToken=true'
|
2020-02-20 02:11:38 +00:00
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
ser = None
|
|
|
|
|
2020-02-23 01:19:53 +00:00
|
|
|
def unlock_door():
|
2023-08-29 23:02:28 +00:00
|
|
|
logging.info('Unlocking door...')
|
2020-02-23 01:19:53 +00:00
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if not TEST:
|
|
|
|
GPIO.output(RELAY_PIN, GPIO.HIGH)
|
|
|
|
GPIO.output(RFID_EN_PIN, GPIO.HIGH)
|
2020-02-23 01:19:53 +00:00
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
time.sleep(OPEN_DURATION)
|
|
|
|
|
|
|
|
GPIO.output(RELAY_PIN, GPIO.LOW)
|
|
|
|
GPIO.output(RFID_EN_PIN, GPIO.LOW)
|
2020-02-23 01:19:53 +00:00
|
|
|
|
|
|
|
def lock_door_on_exit(*args):
|
|
|
|
logging.info('Exiting, locking door...')
|
2023-08-29 23:02:28 +00:00
|
|
|
|
|
|
|
if not TEST:
|
|
|
|
GPIO.output(RELAY_PIN, GPIO.LOW)
|
|
|
|
GPIO.output(RFID_EN_PIN, GPIO.LOW)
|
|
|
|
|
2020-02-23 01:19:53 +00:00
|
|
|
os._exit(0)
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
def init():
|
|
|
|
global ser, cards
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if not TEST:
|
|
|
|
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')
|
|
|
|
|
|
|
|
if TEST:
|
|
|
|
os.mkfifo(TEST_PIPE)
|
|
|
|
logging.info('Test pipe initialized')
|
|
|
|
else:
|
|
|
|
ser = serial.Serial(port='/dev/ttyAMA0', baudrate=2400, timeout=0.1)
|
|
|
|
logging.info('Serial initialized')
|
2020-02-20 00:45:27 +00:00
|
|
|
|
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:
|
2023-08-29 23:02:28 +00:00
|
|
|
cards = json.load(f)
|
|
|
|
logging.info('Read {} cards from disk'.format(len(cards)))
|
2020-02-20 00:45:27 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
2023-08-29 23:02:28 +00:00
|
|
|
cards = card_data_queue.get_nowait()
|
2020-02-20 00:45:27 +00:00
|
|
|
except Empty:
|
|
|
|
pass
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if TEST:
|
|
|
|
with open(TEST_PIPE, 'r') as pipe:
|
|
|
|
card = pipe.readline()
|
|
|
|
else:
|
|
|
|
card = ser.readline()
|
|
|
|
|
2020-02-20 00:45:27 +00:00
|
|
|
if not card: continue
|
|
|
|
|
2022-01-23 02:04:12 +00:00
|
|
|
try:
|
|
|
|
card = card.decode().strip()
|
2023-08-29 23:02:28 +00:00
|
|
|
except AttributeError:
|
|
|
|
card = card.strip()
|
2022-01-23 02:04:12 +00:00
|
|
|
except UnicodeDecodeError:
|
|
|
|
continue
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if len(card) != 14: continue
|
2020-02-20 00:45:27 +00:00
|
|
|
|
|
|
|
# debounce card scans
|
|
|
|
now = time.time()
|
|
|
|
if card in recent_scans:
|
|
|
|
if now - recent_scans[card] < 5.0:
|
2023-08-29 23:02:28 +00:00
|
|
|
logging.info('Debounce skipping card scan')
|
2020-02-20 00:45:27 +00:00
|
|
|
continue
|
|
|
|
recent_scans[card] = now
|
|
|
|
|
|
|
|
logging.info('Read card: ' + card)
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if card in cards:
|
2020-02-20 00:45:27 +00:00
|
|
|
logging.info('Card recognized')
|
|
|
|
else:
|
|
|
|
logging.info('Card not recognized, denying access')
|
|
|
|
continue
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
card_data = cards[card]
|
|
|
|
|
|
|
|
logging.info('Card belongs to: %s', card_data['name'])
|
|
|
|
|
|
|
|
if not any(package in card_data['packages'] for package in VALID_PACKAGES):
|
|
|
|
logging.info('No valid packages found: %s', str(card_data['packages']))
|
|
|
|
continue
|
|
|
|
|
|
|
|
logging.info('DOOR ACCESS GRANTED - Card: %s | Name: %s', card, card_data['name'])
|
2020-02-20 00:45:27 +00:00
|
|
|
|
|
|
|
unlock_door()
|
|
|
|
|
2023-08-29 23:02:28 +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
|
|
|
|
|
|
|
|
def get_cards(card_data_queue):
|
|
|
|
try:
|
|
|
|
headers = {'Authorization': 'Bearer ' + secrets.FABMAN_API_KEY}
|
|
|
|
res = requests.get(API_MEMBERS, headers=headers, timeout=10)
|
|
|
|
res.raise_for_status()
|
|
|
|
res = res.json()
|
|
|
|
except BaseException as e:
|
|
|
|
logging.exception('Problem GETting Fabman API: {} - {}'.format(e.__class__.__name__, str(e)))
|
|
|
|
return
|
|
|
|
|
|
|
|
members = res
|
|
|
|
cards = {}
|
|
|
|
|
|
|
|
logging.info('Got {} members from API'.format(str(len(res))))
|
|
|
|
|
|
|
|
for member in members:
|
|
|
|
if member['state'] != 'active':
|
2020-02-20 02:11:38 +00:00
|
|
|
continue
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
packages = []
|
2020-02-20 02:11:38 +00:00
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
for member_packages in member['_embedded']['memberPackages']:
|
|
|
|
package = member_packages['_embedded']['package']
|
2020-02-20 00:45:27 +00:00
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if package['state'] != 'active':
|
|
|
|
continue
|
|
|
|
|
|
|
|
packages.append(package['name'])
|
|
|
|
|
|
|
|
key = member['_embedded']['key']
|
|
|
|
|
|
|
|
if not key:
|
2020-02-20 02:11:38 +00:00
|
|
|
continue
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
if key['state'] != 'active':
|
2020-02-20 02:11:38 +00:00
|
|
|
continue
|
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
token = key['token']
|
|
|
|
name = '{} {} ({})'.format(member['firstName'], member['lastName'], member['memberNumber'])
|
2020-02-20 02:11:38 +00:00
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
cards[token] = dict(name=name, packages=packages)
|
|
|
|
|
|
|
|
|
|
|
|
logging.info('Processed {} cards'.format(len(cards)))
|
|
|
|
|
|
|
|
card_data_queue.put(cards)
|
|
|
|
|
|
|
|
logging.info('Writing data to file')
|
|
|
|
with open(CARDS_FILE, 'w') as f:
|
|
|
|
json.dump(cards, f, indent=4)
|
|
|
|
|
|
|
|
def update_thread(card_data_queue):
|
|
|
|
if not DEBUG: time.sleep(10)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
logging.info('Updating cards...')
|
|
|
|
get_cards(card_data_queue)
|
2020-02-20 02:11:38 +00:00
|
|
|
|
2023-08-29 23:02:28 +00:00
|
|
|
time.sleep(300)
|
2020-02-20 02:11:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
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()
|