Lock door whenever any thread is terminated
This commit is contained in:
		
							
								
								
									
										27
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										27
									
								
								main.py
									
									
									
									
									
								
							| @@ -11,6 +11,7 @@ import json | |||||||
| import requests | import requests | ||||||
| import serial | import serial | ||||||
| import time | import time | ||||||
|  | from signal import * | ||||||
|  |  | ||||||
| DEBUG = os.environ.get('DEBUG', False) | 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 | 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(): | def init(): | ||||||
|     global ser, cards |     global ser, cards | ||||||
|  |  | ||||||
| @@ -39,14 +55,9 @@ def init(): | |||||||
|     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') | ||||||
|  |  | ||||||
| def unlock_door(): |     for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM): | ||||||
|     GPIO.output(RELAY_PIN, GPIO.HIGH) |         signal(sig, lock_door_on_exit) | ||||||
|     GPIO.output(RFID_EN_PIN, GPIO.HIGH) |     logging.info('Signals initialized') | ||||||
|  |  | ||||||
|     time.sleep(OPEN_DURATION) |  | ||||||
|  |  | ||||||
|     GPIO.output(RELAY_PIN, GPIO.LOW) |  | ||||||
|     GPIO.output(RFID_EN_PIN, GPIO.LOW) |  | ||||||
|  |  | ||||||
| def reader_thread(card_data_queue): | def reader_thread(card_data_queue): | ||||||
|     recent_scans = {} |     recent_scans = {} | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user