From b40ca07f357504981cb7a4fc1abbaec0c3ab80ab Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sun, 23 Feb 2020 01:19:53 +0000 Subject: [PATCH] Lock door whenever any thread is terminated --- main.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/main.py b/main.py index aa8eef4..8a83cd8 100644 --- a/main.py +++ b/main.py @@ -11,6 +11,7 @@ import json import requests import serial import time +from signal import * DEBUG = os.environ.get('DEBUG', False) @@ -25,6 +26,21 @@ API_SEEN = lambda x: 'https://api.my.protospace.ca/door/{}/seen/'.format(x) ser = None +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 lock_door_on_exit(*args): + logging.info('Exiting, locking door...') + GPIO.output(RELAY_PIN, GPIO.LOW) + GPIO.output(RFID_EN_PIN, GPIO.LOW) + os._exit(0) + def init(): global ser, cards @@ -39,14 +55,9 @@ def init(): ser = serial.Serial(port='/dev/ttyAMA0', baudrate=2400, timeout=0.1) logging.info('Serial initialized') -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) + for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM): + signal(sig, lock_door_on_exit) + logging.info('Signals initialized') def reader_thread(card_data_queue): recent_scans = {}