From ee1b76d04356ca839a450a99a5fe859ced48f78a Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Wed, 20 Jan 2016 11:59:21 -0500 Subject: [PATCH] Clean up reading keys I can easily cut out a function call to verify / validate keys. I also added in a check for if the key is alphanumeric, to cut down on exceptions caused by garbage data. Signed-off-by: Anna Schumaker --- doors.py | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/doors.py b/doors.py index f2cde24..cb434d7 100755 --- a/doors.py +++ b/doors.py @@ -124,7 +124,9 @@ def clear_cache(): CACHED_KEYS.clear() THIS_MONTH = today.month -def valid_key(key): +def verify_key(key): + print("Verifying key: %s" % key) + GPIO.output(YELLOW_LED, ON); clear_cache() if key in CACHED_KEYS: print("Using cached key") @@ -132,14 +134,6 @@ def valid_key(key): return True return ping_server(key) -# Blocks for 5 seconds before resetting the door -def verify_key(key): - GPIO.output(YELLOW_LED, ON); - if valid_key(key) == True: - unlock_door() - time.sleep(5) - lock_door() - def ping_keys(): if len(NEED_PING) > 0: ping_server(NEED_PING[0]) @@ -157,18 +151,23 @@ import serial RFID_SERIAL = serial.Serial(RFID_PATH, 2400, timeout=1) -def _do_read_rfid(): +def read_key(): string = RFID_SERIAL.read(12) + RFID_SERIAL.flushInput() # ignore errors, no data if len(string) > 0: key = string[1:11].decode() #exclude start x0A and stop x0D bytes - print("Read key: %s" % key) - verify_key(key) - RFID_SERIAL.flushInput() # ignore errors, no data - ping_keys() + if key.isalnum(): + return key; + return None def read_rfid(): try: - _do_read_rfid() + key = read_key() + if key and verify_key(key): + unlock_door() + time.sleep(5) # block for 5 seconds before resetting door + lock_door() + ping_keys() except Exception as e: print(e) lock_door()