Changed read to zmq socket and added PWM activation
This commit is contained in:
parent
3bfdfead1d
commit
47e75ecd1a
|
@ -99,3 +99,7 @@ ENV/
|
||||||
|
|
||||||
# mypy
|
# mypy
|
||||||
.mypy_cache/
|
.mypy_cache/
|
||||||
|
|
||||||
|
# Access files
|
||||||
|
access.log
|
||||||
|
access.json
|
41
validate.py
41
validate.py
|
@ -4,8 +4,16 @@ import sys
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import time
|
import time
|
||||||
import RPi.GPIO as IO
|
import RPi.GPIO as IO
|
||||||
|
import zmq
|
||||||
|
|
||||||
|
# Magic numbers for PWM activation
|
||||||
MOTOR_PIN = 19
|
MOTOR_PIN = 19
|
||||||
|
FREQUENCY = 100
|
||||||
|
DUTYCYCLE = 100
|
||||||
|
DURATION = 1.0
|
||||||
|
# Address of the scanning service
|
||||||
|
SCANSERVADDRESS = "tcp://localhost:5000"
|
||||||
|
|
||||||
|
|
||||||
def timestamped(s):
|
def timestamped(s):
|
||||||
"""
|
"""
|
||||||
|
@ -21,42 +29,53 @@ def setup_pwm():
|
||||||
IO.setwarnings(False)
|
IO.setwarnings(False)
|
||||||
IO.setmode(IO.BCM)
|
IO.setmode(IO.BCM)
|
||||||
IO.setup(MOTOR_PIN, IO.OUT)
|
IO.setup(MOTOR_PIN, IO.OUT)
|
||||||
p = IO.PWM(MOTOR_PIN, 100) # Second argument is frequency, I think
|
p = IO.PWM(MOTOR_PIN, FREQUENCY)
|
||||||
return p
|
return p
|
||||||
|
|
||||||
def unlock_door(p):
|
def unlock_door(p):
|
||||||
"""
|
"""
|
||||||
Uses PWM to turn the motor connected to the door handle.
|
Uses PWM to turn the motor connected to the door handle.
|
||||||
"""
|
"""
|
||||||
p.start(50)
|
try:
|
||||||
time.sleep(1.0)
|
p.start(DUTYCYCLE)
|
||||||
p.stop()
|
xtime.sleep(DURATION)
|
||||||
|
finally:
|
||||||
|
p.stop()
|
||||||
|
|
||||||
def read_loop():
|
def read_loop():
|
||||||
"""
|
"""
|
||||||
Simple REPL skeleton for validating scanned IDs.
|
Simple REPL skeleton for validating scanned IDs.
|
||||||
"""
|
"""
|
||||||
|
# Initialize zmq socket for reading in scanned barcodes
|
||||||
|
context = zmq.Context()
|
||||||
|
socket = context.socket(zmq.SUB)
|
||||||
|
socket.setsockopt(zmq.SUBSCRIBE, "")
|
||||||
|
socket.connect(SCANSERVADDRESS)
|
||||||
|
# TODO: Open the access database
|
||||||
|
|
||||||
|
# Initialize the PWM pin for opening the door
|
||||||
|
pwm_pin = setup_pwm()
|
||||||
# Open the log file in append mode
|
# Open the log file in append mode
|
||||||
log_file = open("./access.log", "a")
|
log_file = open("./access.log", "a")
|
||||||
log_file.write(timestamped("=== Door security initiated ===\n"))
|
log_file.write(timestamped("=== Door security initiated ===\n"))
|
||||||
# Initialize the PWM pin
|
|
||||||
pwm_pin = setup_pwm()
|
|
||||||
|
|
||||||
# Begin the loop
|
# Begin the loop
|
||||||
while True:
|
while True:
|
||||||
# TODO: Read in the ID
|
# Read in the ID
|
||||||
|
scanned_id = socket.recv()
|
||||||
# TODO: Check if the ID is authorized
|
# TODO: Check if the ID is authorized
|
||||||
authorized = False
|
authorized = True
|
||||||
|
|
||||||
|
|
||||||
# If the user is not authorized, deny access
|
# If the user is not authorized, deny access
|
||||||
if input not in authorized:
|
if not authorized:
|
||||||
s = timestamped("##### denied\n")
|
s = timestamped("##### denied\n")
|
||||||
log_file.write(s)
|
log_file.write(s)
|
||||||
# If the user is authorized, perform the unlock procedure
|
# If the user is authorized, perform the unlock procedure
|
||||||
else:
|
else:
|
||||||
s = timestamped("##### <Username> validated")
|
s = timestamped("##### <Username> validated\n")
|
||||||
log_file.write(s)
|
log_file.write(s)
|
||||||
|
print s,
|
||||||
# TODO: Play open tone
|
# TODO: Play open tone
|
||||||
# TODO: Run unlock procedure
|
# TODO: Run unlock procedure
|
||||||
unlock_door(pwm_pin)
|
unlock_door(pwm_pin)
|
||||||
|
|
Loading…
Reference in New Issue