You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
1.4 KiB
70 lines
1.4 KiB
4 years ago
|
import logging
|
||
|
logging.basicConfig(
|
||
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
||
|
level=logging.INFO)
|
||
|
|
||
|
import os
|
||
|
import json
|
||
|
import sys
|
||
|
#import RPi.GPIO as GPIO
|
||
|
import serial
|
||
|
import time
|
||
|
|
||
|
RELAY_PIN = 17
|
||
|
RFID_EN_PIN = 27
|
||
|
CARDS_FILE = 'cardexport.json'
|
||
|
|
||
|
ser = None
|
||
|
recent = {}
|
||
|
cards = {}
|
||
|
|
||
|
def init():
|
||
|
global ser
|
||
|
|
||
|
#GPIO.setwarnings(False)
|
||
|
#GPIO.setmode(GPIO.BCM)
|
||
|
#GPIO.setup(RELAY_PIN, GPIO.OUT)
|
||
|
#GPIO.output(RELAY_PIN, GPIO.LOW)
|
||
|
#GPIO.setup(RFID_EN_PIN, GPIO.OUT)
|
||
|
#GPIO.output(RFID_EN_PIN, GPIO.LOW)
|
||
|
logging.info('GPIO initialized.')
|
||
|
|
||
|
#ser = serial.Serial(port='/dev/ttyAMA0', baudrate=2400, timeout=0.1)
|
||
|
logging.info('Serial initialized.')
|
||
|
|
||
|
with open(CARDS_FILE, 'r') as f:
|
||
|
cards = json.load(f)
|
||
|
logging.info('Read in {} card numbers.'.format(str(len(cards))))
|
||
|
|
||
|
|
||
|
|
||
|
def loop():
|
||
|
card = ser.readline()
|
||
|
if not card:
|
||
|
return
|
||
|
|
||
|
card = card.strip()
|
||
|
if len(card) != 10:
|
||
|
return
|
||
|
|
||
|
now = time.time()
|
||
|
if card in recent:
|
||
|
if now - recent[card] < 5.0:
|
||
|
recent[card] = now
|
||
|
return
|
||
|
recent[card] = now
|
||
|
|
||
|
logging.info('Read card # ' + card)
|
||
|
handle_card_read(card)
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
logging.info('Initializing...')
|
||
|
init()
|
||
|
|
||
|
logging.info('Running main loop...')
|
||
|
try:
|
||
|
while True:
|
||
|
loop()
|
||
|
except KeyboardInterrupt:
|
||
|
logging.info('Exiting...')
|