From 921b255c6dc246f958a4e6c8cedbbbf4f26897f9 Mon Sep 17 00:00:00 2001 From: Jaculabilis Date: Sat, 27 Jan 2018 20:19:59 -0600 Subject: [PATCH] Moved access load code to read loop --- validate.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/validate.py b/validate.py index 1725d36..d757b44 100644 --- a/validate.py +++ b/validate.py @@ -52,12 +52,6 @@ def read_loop(): socket = context.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, "") socket.connect(SCANSERVADDRESS) - # 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 @@ -68,6 +62,18 @@ def read_loop(): while True: # Read in the ID code = socket.recv() + + # Load the access database + # As long as the access list is small and the speed is unimportant, + # the flexibility and update ability is worth reading from disk. + raw = open("access.json", "r").read() + try: + access = json.loads(raw) + except: + log_file.write(timestamped("Could not load access file!")) + sys.stderr.write("Could not load access file!") + access = {} + # Determine ID authorization if code not in access: authorized = False @@ -75,11 +81,13 @@ def read_loop(): else: authorized = "authorized" in access[code] and access[code]["authorized"] user = access[code]["name"] if "name" in access[code] else code + # If the user is not authorized, deny access if not authorized: s = timestamped("Denied {} ({})\n".format(code, user)) log_file.write(s) print s, + # If the user is authorized, perform the unlock procedure else: s = timestamped("Validated {} ({})\n".format(code, user))