Moved access load code to read loop

This commit is contained in:
Jaculabilis 2018-01-27 20:19:59 -06:00
parent 534656df64
commit 921b255c6d
1 changed files with 14 additions and 6 deletions

View File

@ -52,12 +52,6 @@ def read_loop():
socket = context.socket(zmq.SUB) socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "") socket.setsockopt(zmq.SUBSCRIBE, "")
socket.connect(SCANSERVADDRESS) 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 # Initialize the PWM pin for opening the door
pwm_pin = setup_pwm() pwm_pin = setup_pwm()
# Open the log file in append mode # Open the log file in append mode
@ -68,6 +62,18 @@ def read_loop():
while True: while True:
# Read in the ID # Read in the ID
code = socket.recv() 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 # Determine ID authorization
if code not in access: if code not in access:
authorized = False authorized = False
@ -75,11 +81,13 @@ def read_loop():
else: else:
authorized = "authorized" in access[code] and access[code]["authorized"] authorized = "authorized" in access[code] and access[code]["authorized"]
user = access[code]["name"] if "name" in access[code] else code user = access[code]["name"] if "name" in access[code] else code
# If the user is not authorized, deny access # If the user is not authorized, deny access
if not authorized: if not authorized:
s = timestamped("Denied {} ({})\n".format(code, user)) s = timestamped("Denied {} ({})\n".format(code, user))
log_file.write(s) log_file.write(s)
print s, print s,
# If the user is authorized, perform the unlock procedure # If the user is authorized, perform the unlock procedure
else: else:
s = timestamped("Validated {} ({})\n".format(code, user)) s = timestamped("Validated {} ({})\n".format(code, user))