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 <Anna.Schumaker@Netapp.com>
This commit is contained in:
Anna Schumaker 2016-01-20 11:59:21 -05:00
parent a95db28f5c
commit ee1b76d043
1 changed files with 14 additions and 15 deletions

View File

@ -124,7 +124,9 @@ def clear_cache():
CACHED_KEYS.clear() CACHED_KEYS.clear()
THIS_MONTH = today.month THIS_MONTH = today.month
def valid_key(key): def verify_key(key):
print("Verifying key: %s" % key)
GPIO.output(YELLOW_LED, ON);
clear_cache() clear_cache()
if key in CACHED_KEYS: if key in CACHED_KEYS:
print("Using cached key") print("Using cached key")
@ -132,14 +134,6 @@ def valid_key(key):
return True return True
return ping_server(key) 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(): def ping_keys():
if len(NEED_PING) > 0: if len(NEED_PING) > 0:
ping_server(NEED_PING[0]) ping_server(NEED_PING[0])
@ -157,18 +151,23 @@ import serial
RFID_SERIAL = serial.Serial(RFID_PATH, 2400, timeout=1) RFID_SERIAL = serial.Serial(RFID_PATH, 2400, timeout=1)
def _do_read_rfid(): def read_key():
string = RFID_SERIAL.read(12) string = RFID_SERIAL.read(12)
RFID_SERIAL.flushInput() # ignore errors, no data
if len(string) > 0: if len(string) > 0:
key = string[1:11].decode() #exclude start x0A and stop x0D bytes key = string[1:11].decode() #exclude start x0A and stop x0D bytes
print("Read key: %s" % key) if key.isalnum():
verify_key(key) return key;
RFID_SERIAL.flushInput() # ignore errors, no data return None
ping_keys()
def read_rfid(): def read_rfid():
try: 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: except Exception as e:
print(e) print(e)
lock_door() lock_door()