2015-04-07 13:50:29 -04:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
|
|
|
|
2015-04-07 14:23:15 -04:00
|
|
|
import socket
|
|
|
|
##################
|
|
|
|
# #
|
|
|
|
# Default values #
|
|
|
|
# #
|
|
|
|
##################
|
|
|
|
|
|
|
|
CONTROLLER = socket.gethostname()
|
|
|
|
SERVER = "localhost"
|
|
|
|
RFID_PATH = "/dev/ttyUSB0"
|
2015-04-22 17:07:21 -04:00
|
|
|
YELLOW_LED = 7
|
2015-04-23 16:48:17 -04:00
|
|
|
GREEN_LED = 11
|
|
|
|
RED_LED = 13
|
2015-04-07 13:50:29 -04:00
|
|
|
DOOR_STRIKE = 15
|
|
|
|
|
2015-04-07 14:03:58 -04:00
|
|
|
|
|
|
|
|
2015-04-07 14:23:15 -04:00
|
|
|
import os
|
2015-04-07 13:50:29 -04:00
|
|
|
####################
|
|
|
|
# #
|
2015-04-07 14:23:15 -04:00
|
|
|
# Read doorrc file #
|
2015-04-07 13:50:29 -04:00
|
|
|
# #
|
|
|
|
####################
|
|
|
|
|
2015-04-09 15:41:08 -04:00
|
|
|
for conf in [ "/etc/doorsrc", "./doorsrc" ]:
|
2015-04-07 14:23:15 -04:00
|
|
|
if os.path.exists(conf):
|
|
|
|
with open(conf) as f:
|
2015-04-09 15:41:08 -04:00
|
|
|
exec(compile(f.read(), "doorsrc", 'exec'))
|
2015-04-07 14:23:15 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
2015-04-07 13:50:29 -04:00
|
|
|
import atexit
|
|
|
|
import RPi.GPIO as GPIO
|
2015-04-07 14:23:15 -04:00
|
|
|
####################
|
|
|
|
# #
|
|
|
|
# Set up GPIO Pins #
|
|
|
|
# #
|
|
|
|
####################
|
2015-04-07 13:50:29 -04:00
|
|
|
|
|
|
|
GPIO.setmode(GPIO.BOARD)
|
|
|
|
atexit.register(GPIO.cleanup)
|
|
|
|
|
|
|
|
GPIO.setup(DOOR_STRIKE, GPIO.OUT)
|
|
|
|
GPIO.setup(RED_LED, GPIO.OUT)
|
|
|
|
GPIO.setup(GREEN_LED, GPIO.OUT)
|
2015-04-22 17:07:21 -04:00
|
|
|
GPIO.setup(YELLOW_LED, GPIO.OUT)
|
2017-06-19 15:57:12 -04:00
|
|
|
GPIO.output(YELLOW_LED, GPIO.OUT)
|
2015-04-07 13:50:29 -04:00
|
|
|
|
2015-04-22 17:13:45 -04:00
|
|
|
|
2015-04-23 10:28:04 -04:00
|
|
|
OFF = GPIO.LOW
|
|
|
|
ON = GPIO.HIGH
|
2015-04-07 13:50:29 -04:00
|
|
|
|
|
|
|
|
2015-04-23 08:41:50 -04:00
|
|
|
def unlock_door():
|
|
|
|
print("Door is unlocked");
|
2015-04-22 17:13:45 -04:00
|
|
|
GPIO.output(DOOR_STRIKE, ON)
|
|
|
|
GPIO.output(RED_LED, OFF)
|
|
|
|
GPIO.output(GREEN_LED, ON)
|
2015-04-07 13:50:29 -04:00
|
|
|
|
2015-04-23 10:18:58 -04:00
|
|
|
def lock_door():
|
|
|
|
print("Door is locked");
|
2015-04-22 17:13:45 -04:00
|
|
|
GPIO.output(DOOR_STRIKE, OFF)
|
|
|
|
GPIO.output(RED_LED, ON)
|
|
|
|
GPIO.output(GREEN_LED, OFF)
|
2015-04-23 16:48:50 -04:00
|
|
|
lock_door()
|
2015-04-22 17:13:45 -04:00
|
|
|
|
2015-04-22 21:10:57 -04:00
|
|
|
# Turn all LEDs on
|
|
|
|
def leds_on():
|
|
|
|
GPIO.output(RED_LED, ON)
|
|
|
|
GPIO.output(GREEN_LED, ON)
|
|
|
|
|
|
|
|
# Turn all LEDs off
|
|
|
|
def leds_off():
|
|
|
|
GPIO.output(RED_LED, OFF)
|
|
|
|
GPIO.output(GREEN_LED, OFF)
|
|
|
|
|
2015-04-23 10:44:44 -04:00
|
|
|
def blink_leds():
|
|
|
|
print("Blinking LEDs")
|
|
|
|
for i in range(5):
|
|
|
|
if (i % 2) == 0:
|
|
|
|
leds_on()
|
|
|
|
else:
|
|
|
|
leds_off()
|
|
|
|
time.sleep(1)
|
|
|
|
lock_door()
|
|
|
|
|
2015-04-07 13:50:29 -04:00
|
|
|
|
2015-04-07 14:03:58 -04:00
|
|
|
|
|
|
|
|
2015-04-07 14:23:15 -04:00
|
|
|
import time
|
2016-01-12 10:51:48 -05:00
|
|
|
import datetime
|
2015-04-07 14:23:15 -04:00
|
|
|
from urllib import request
|
2015-04-07 13:50:29 -04:00
|
|
|
#############################################
|
|
|
|
# #
|
|
|
|
# Verify a key with openings.workantile.com #
|
|
|
|
# #
|
|
|
|
#############################################
|
2016-01-12 10:23:53 -05:00
|
|
|
CACHED_KEYS = set()
|
2016-01-20 12:03:04 -05:00
|
|
|
NEED_FLUSH = []
|
2016-01-12 10:51:48 -05:00
|
|
|
THIS_MONTH = datetime.date.today().month
|
2016-01-12 10:23:53 -05:00
|
|
|
|
|
|
|
def ping_server(key):
|
2017-06-19 15:57:12 -04:00
|
|
|
ret = False
|
|
|
|
|
2016-01-19 13:32:23 -05:00
|
|
|
print("Pinging server with key: %s (%s/%s)" % (key, SERVER, key))
|
2017-06-19 15:57:12 -04:00
|
|
|
GPIO.output(YELLOW_LED, ON);
|
2016-01-19 13:32:23 -05:00
|
|
|
with request.urlopen("%s/%s" % (SERVER, key)) as f:
|
2017-06-19 15:57:12 -04:00
|
|
|
ret = f.read().decode() == "OK"
|
|
|
|
if ret:
|
2016-01-12 10:23:53 -05:00
|
|
|
CACHED_KEYS.add(key)
|
|
|
|
else:
|
|
|
|
CACHED_KEYS.remove(key)
|
2017-06-19 15:57:12 -04:00
|
|
|
GPIO.output(YELLOW_LED, OFF);
|
|
|
|
return ret
|
2016-01-12 10:23:53 -05:00
|
|
|
|
2016-01-12 10:51:48 -05:00
|
|
|
def clear_cache():
|
|
|
|
global THIS_MONTH
|
|
|
|
|
|
|
|
today = datetime.date.today()
|
|
|
|
if today.month != THIS_MONTH:
|
2016-01-19 13:30:19 -05:00
|
|
|
print("Clearing key cache")
|
2016-01-12 10:51:48 -05:00
|
|
|
CACHED_KEYS.clear()
|
|
|
|
THIS_MONTH = today.month
|
|
|
|
|
2016-01-20 11:59:21 -05:00
|
|
|
def verify_key(key):
|
|
|
|
print("Verifying key: %s" % key)
|
2016-01-12 10:51:48 -05:00
|
|
|
clear_cache()
|
2016-01-12 10:23:53 -05:00
|
|
|
if key in CACHED_KEYS:
|
|
|
|
print("Using cached key")
|
2016-01-20 12:03:04 -05:00
|
|
|
NEED_FLUSH.append(key)
|
2016-01-12 10:23:53 -05:00
|
|
|
return True
|
|
|
|
return ping_server(key)
|
2015-04-07 13:50:29 -04:00
|
|
|
|
2016-01-20 12:03:04 -05:00
|
|
|
def flush_keys():
|
|
|
|
if len(NEED_FLUSH) > 0:
|
|
|
|
ping_server(NEED_FLUSH[0])
|
|
|
|
NEED_FLUSH.pop(0)
|
2016-01-12 10:23:53 -05:00
|
|
|
|
2015-04-07 14:27:02 -04:00
|
|
|
|
|
|
|
#python-pyserial package. Not sure we need this. Grabbed based on
|
|
|
|
#http://allenmlabs.blogspot.se/2013/01/raspberry-pi-parallax-rfid-reader.html
|
|
|
|
import serial
|
|
|
|
###################
|
|
|
|
# #
|
|
|
|
# Run RFID Reader #
|
|
|
|
# #
|
|
|
|
###################
|
|
|
|
|
|
|
|
RFID_SERIAL = serial.Serial(RFID_PATH, 2400, timeout=1)
|
|
|
|
|
2016-01-20 11:59:21 -05:00
|
|
|
def read_key():
|
2016-01-20 13:10:10 -05:00
|
|
|
RFID_SERIAL.reset_input_buffer() # Clear input buffer before reading
|
2015-04-07 14:05:50 -04:00
|
|
|
string = RFID_SERIAL.read(12)
|
2017-06-19 16:32:53 -04:00
|
|
|
if len(string) == 12 and string[0] == 0xA and string[-1] == 0xD:
|
|
|
|
key = string[1:11].decode() #exclude start 0xA and stop 0xD bytes
|
2016-01-20 11:59:21 -05:00
|
|
|
if key.isalnum():
|
|
|
|
return key;
|
|
|
|
return None
|
2015-04-09 09:33:27 -04:00
|
|
|
|
2015-04-23 10:44:44 -04:00
|
|
|
def read_rfid():
|
|
|
|
try:
|
2016-01-20 11:59:21 -05:00
|
|
|
key = read_key()
|
|
|
|
if key and verify_key(key):
|
|
|
|
unlock_door()
|
|
|
|
time.sleep(5) # block for 5 seconds before resetting door
|
2017-06-19 16:32:53 -04:00
|
|
|
lock_door()
|
2016-01-20 12:03:04 -05:00
|
|
|
flush_keys()
|
2015-04-23 10:44:44 -04:00
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
lock_door()
|
|
|
|
blink_leds()
|
2015-04-09 09:33:27 -04:00
|
|
|
|
2015-04-22 21:10:57 -04:00
|
|
|
|
2015-04-22 20:44:08 -04:00
|
|
|
|
2015-04-23 10:44:44 -04:00
|
|
|
def loop():
|
2015-04-22 20:44:08 -04:00
|
|
|
while True:
|
2015-04-23 10:44:44 -04:00
|
|
|
read_rfid()
|
2015-04-22 20:44:08 -04:00
|
|
|
|
2015-04-23 10:44:44 -04:00
|
|
|
if __name__ == "__main__":
|
|
|
|
loop()
|