Handle reading the cards

This commit is contained in:
Tanner Collin 2020-01-05 05:16:42 +00:00
parent 1eb7b8c508
commit 47f41e17e1
2 changed files with 38 additions and 12 deletions

49
main.py
View File

@ -6,44 +6,71 @@ logging.basicConfig(
import os import os
import json import json
import sys import sys
#import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import serial import serial
import time import time
RELAY_PIN = 17 RELAY_PIN = 17
RFID_EN_PIN = 27 RFID_EN_PIN = 27
CARDS_FILE = 'cardexport.json' CARDS_FILE = 'cardexport.json'
OPEN_DURATION = 5
ser = None ser = None
recent = {} recent = {}
cards = {} cards = {}
def init(): def init():
global ser global ser, cards
#GPIO.setwarnings(False) GPIO.setwarnings(False)
#GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
#GPIO.setup(RELAY_PIN, GPIO.OUT) GPIO.setup(RELAY_PIN, GPIO.OUT)
#GPIO.output(RELAY_PIN, GPIO.LOW) GPIO.output(RELAY_PIN, GPIO.LOW)
#GPIO.setup(RFID_EN_PIN, GPIO.OUT) GPIO.setup(RFID_EN_PIN, GPIO.OUT)
#GPIO.output(RFID_EN_PIN, GPIO.LOW) GPIO.output(RFID_EN_PIN, GPIO.LOW)
logging.info('GPIO initialized.') logging.info('GPIO initialized.')
#ser = serial.Serial(port='/dev/ttyAMA0', baudrate=2400, timeout=0.1) ser = serial.Serial(port='/dev/ttyAMA0', baudrate=2400, timeout=0.1)
logging.info('Serial initialized.') logging.info('Serial initialized.')
with open(CARDS_FILE, 'r') as f: with open(CARDS_FILE, 'r') as f:
cards = json.load(f) cards = json.load(f)
logging.info('Read in {} card numbers.'.format(str(len(cards)))) logging.info('Read in {} card numbers.'.format(str(len(cards))))
def unlock_door():
GPIO.output(RELAY_PIN, GPIO.HIGH)
GPIO.output(RFID_EN_PIN, GPIO.HIGH)
time.sleep(OPEN_DURATION)
GPIO.output(RELAY_PIN, GPIO.LOW)
GPIO.output(RFID_EN_PIN, GPIO.LOW)
def handle_card_read(card):
if card in cards:
logging.info('Card recognized.')
member = cards[card]
else:
logging.info('Card not recognized. Denying access.')
return
if member['enabled']:
logging.info('Member active.')
else:
logging.info('Member not active. Denying access.')
return
logging.info('DOOR ACCESS - Card: {} | Name: {} | id: {}'.format(
card, member['name'], member['id']))
unlock_door()
def loop(): def loop():
card = ser.readline() card = ser.readline()
if not card: if not card:
return return
card = card.strip() card = card.decode().strip()
if len(card) != 10: if len(card) != 10:
return return
@ -54,7 +81,7 @@ def loop():
return return
recent[card] = now recent[card] = now
logging.info('Read card # ' + card) logging.info('\nRead card #' + card)
handle_card_read(card) handle_card_read(card)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -1,2 +1 @@
pkg-resources==0.0.0
pyserial==3.4 pyserial==3.4