diff --git a/validate.py b/validate.py index 1e4c5d2..1f83345 100644 --- a/validate.py +++ b/validate.py @@ -3,6 +3,7 @@ import sys from datetime import datetime import time +import json import RPi.GPIO as IO import zmq @@ -51,8 +52,12 @@ def read_loop(): socket = context.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, "") socket.connect(SCANSERVADDRESS) - # TODO: Open the access database - + # Open the access database + access_raw = open("access.json").read() + try: + access = json.loads(access_raw) + except: + raise SystemExit("Could not load access file!") # Initialize the PWM pin for opening the door pwm_pin = setup_pwm() # Open the log file in append mode @@ -63,21 +68,23 @@ def read_loop(): while True: # Read in the ID scanned_id = socket.recv() - # TODO: Check if the ID is authorized - authorized = True - - + # Determine ID authorization + authorized = id in access and "authorized" in access[id] and access[id]["authorized"] + user = access[id]["user"] if id in access and "user" in access[id] else "unknown id" # If the user is not authorized, deny access if not authorized: - s = timestamped("##### denied\n") + s = timestamped("Denied {} ({})\n".format(id, user)) log_file.write(s) + print s, # If the user is authorized, perform the unlock procedure else: - s = timestamped("##### validated\n") + s = timestamped("Validated {} {}\n".format(id, user)) log_file.write(s) print s, # TODO: Play open tone - # TODO: Run unlock procedure + if "sound" in access[id]: + pass + # Run unlock procedure unlock_door(pwm_pin) if __name__ == "__main__":