55 lines
1.3 KiB
Python
55 lines
1.3 KiB
Python
import pigpio
|
|
import time
|
|
import logging
|
|
import json
|
|
|
|
from wiegand import decoder
|
|
|
|
class gate:
|
|
def __init__(self, pi, relay_pin):
|
|
self.pi = pi
|
|
self.relay_pin = relay_pin
|
|
|
|
# set the pin to high to keep the gate locked
|
|
pi.write(relay_pin, 1)
|
|
|
|
# unlocks the gate for a number of seconds defined by timeout
|
|
def unlock_gate(self, timeout):
|
|
self.pi.write(self.relay_pin, 0)
|
|
time.sleep(timeout)
|
|
self.pi.write(self.relay_pin, 1)
|
|
|
|
def load_secrets():
|
|
with open("./secrets.json") as secrets:
|
|
return json.loads(secrets.read())
|
|
|
|
if __name__ == "__main__":
|
|
secrets = load_secrets()
|
|
pi = pigpio.pi()
|
|
gate = gate(pi, 4)
|
|
|
|
current_code = ""
|
|
def callback(bits, value):
|
|
global current_code
|
|
# indicates keypad press
|
|
if bits == 4:
|
|
current_code = current_code[-3:] + str(value)
|
|
if len(current_code) == 4:
|
|
# check if we have a valid code
|
|
if current_code in secrets['codes']:
|
|
gate.unlock_gate(5)
|
|
current_code = ""
|
|
# indicates a card swipe
|
|
elif bits == 37:
|
|
current_code = ""
|
|
str_value = str(value)
|
|
if str_value in secrets['cards']:
|
|
gate.unlock_gate(5)
|
|
|
|
logging.info("bits={} value={}".format(bits, value))
|
|
|
|
w = decoder(pi, 2, 3, callback)
|
|
time.sleep(1000)
|
|
w.cancel()
|
|
pi.stop()
|