From 1eb7b8c508321e193c7f705ae12bef0cdfcbe782 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 4 Jan 2020 21:05:01 -0700 Subject: [PATCH] Start door controller fix --- .gitignore | 108 +++++++++++++++++++++++++++++++++++++++++++++++ main.py | 69 ++++++++++++++++++++++++++++++ requirements.txt | 2 + 3 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..51a898a --- /dev/null +++ b/.gitignore @@ -0,0 +1,108 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ + +# Editor +*.swp +*.swo + +# DB +db.sqlite3 + +cardexport.json diff --git a/main.py b/main.py new file mode 100644 index 0000000..9f9f86e --- /dev/null +++ b/main.py @@ -0,0 +1,69 @@ +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...') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4386cea --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +pkg-resources==0.0.0 +pyserial==3.4