Add debug logging, move settings to secrets.py
This commit is contained in:
		
							
								
								
									
										33
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										33
									
								
								main.py
									
									
									
									
									
								
							@@ -1,12 +1,14 @@
 | 
			
		||||
import os
 | 
			
		||||
 | 
			
		||||
DEBUG = os.environ.get('DEBUG', False)
 | 
			
		||||
import logging
 | 
			
		||||
logging.basicConfig(
 | 
			
		||||
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
 | 
			
		||||
    level=logging.INFO)
 | 
			
		||||
    level=logging.DEBUG if DEBUG else logging.INFO)
 | 
			
		||||
 | 
			
		||||
from multiprocessing import Process, Queue
 | 
			
		||||
from queue import Empty
 | 
			
		||||
import RPi.GPIO as GPIO
 | 
			
		||||
import os
 | 
			
		||||
import json
 | 
			
		||||
import requests
 | 
			
		||||
import serial
 | 
			
		||||
@@ -15,17 +17,12 @@ from signal import *
 | 
			
		||||
 | 
			
		||||
import secrets
 | 
			
		||||
 | 
			
		||||
DEBUG = os.environ.get('DEBUG', False)
 | 
			
		||||
 | 
			
		||||
RELAY_PIN = 17
 | 
			
		||||
RFID_EN_PIN = 27
 | 
			
		||||
CARDS_FILE = 'card_data.json'
 | 
			
		||||
OPEN_DURATION = 4
 | 
			
		||||
 | 
			
		||||
API_STATS = 'https://api.my.protospace.ca/stats/'
 | 
			
		||||
API_DOOR = 'https://api.my.protospace.ca/door/'
 | 
			
		||||
API_SEEN = lambda x: 'https://api.my.protospace.ca/door/{}/seen/'.format(x)
 | 
			
		||||
 | 
			
		||||
ser = None
 | 
			
		||||
 | 
			
		||||
def unlock_door():
 | 
			
		||||
@@ -54,7 +51,7 @@ def init():
 | 
			
		||||
    GPIO.output(RFID_EN_PIN, GPIO.LOW)
 | 
			
		||||
    logging.info('GPIO initialized')
 | 
			
		||||
 | 
			
		||||
    ser = serial.Serial(port='/dev/ttyAMA0', baudrate=2400, timeout=0.1)
 | 
			
		||||
    ser = serial.Serial(port='/dev/ttyAMA0', baudrate=secrets.BAUD_RATE, timeout=0.1)
 | 
			
		||||
    logging.info('Serial initialized')
 | 
			
		||||
 | 
			
		||||
    for sig in (SIGABRT, SIGILL, SIGINT, SIGSEGV, SIGTERM):
 | 
			
		||||
@@ -75,14 +72,22 @@ def reader_thread(card_data_queue):
 | 
			
		||||
            pass
 | 
			
		||||
 | 
			
		||||
        card = ser.readline()
 | 
			
		||||
        if not card: continue
 | 
			
		||||
 | 
			
		||||
        if not card:
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        logging.debug('Raw card read: %s', card)
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            card = card.decode().strip()
 | 
			
		||||
        except UnicodeDecodeError:
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        if len(card) != 10: continue
 | 
			
		||||
        logging.debug('Card read string: %s', card)
 | 
			
		||||
 | 
			
		||||
        if len(card) != 10:
 | 
			
		||||
            logging.debug('Card length not 10, skipping')
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        # debounce card scans
 | 
			
		||||
        now = time.time()
 | 
			
		||||
@@ -106,7 +111,7 @@ def reader_thread(card_data_queue):
 | 
			
		||||
        unlock_door()
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            res = requests.post(API_SEEN(card), timeout=2)
 | 
			
		||||
            res = requests.post(secrets.API_SEEN(card), timeout=2)
 | 
			
		||||
            res.raise_for_status()
 | 
			
		||||
        except BaseException as e:
 | 
			
		||||
            logging.error('Problem POSTing seen: {} - {}'.format(e.__class__.__name__, str(e)))
 | 
			
		||||
@@ -119,13 +124,15 @@ def update_thread(card_data_queue):
 | 
			
		||||
        time.sleep(5)
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            res = requests.get(API_STATS, timeout=5)
 | 
			
		||||
            res = requests.get(secrets.API_STATS, timeout=5)
 | 
			
		||||
            res.raise_for_status()
 | 
			
		||||
            res = res.json()
 | 
			
		||||
        except BaseException as e:
 | 
			
		||||
            logging.error('Problem GETting stats: {} - {}'.format(e.__class__.__name__, str(e)))
 | 
			
		||||
            continue
 | 
			
		||||
 | 
			
		||||
        logging.debug('Previous last change time: %s, current: %s', last_card_change, res['last_card_change'])
 | 
			
		||||
 | 
			
		||||
        if res['last_card_change'] == last_card_change:
 | 
			
		||||
            continue
 | 
			
		||||
        last_card_change = res['last_card_change']
 | 
			
		||||
@@ -134,7 +141,7 @@ def update_thread(card_data_queue):
 | 
			
		||||
 | 
			
		||||
        try:
 | 
			
		||||
            headers = {'Authorization': 'Bearer ' + secrets.DOOR_API_KEY}
 | 
			
		||||
            res = requests.get(API_DOOR, headers=headers, timeout=5)
 | 
			
		||||
            res = requests.get(secrets.API_DOOR, headers=headers, timeout=5)
 | 
			
		||||
            res.raise_for_status()
 | 
			
		||||
            res = res.json()
 | 
			
		||||
        except BaseException as e:
 | 
			
		||||
 
 | 
			
		||||
@@ -3,5 +3,4 @@ chardet==3.0.4
 | 
			
		||||
idna==2.9
 | 
			
		||||
pyserial==3.4
 | 
			
		||||
requests==2.23.0
 | 
			
		||||
RPi.GPIO==0.7.0
 | 
			
		||||
urllib3==1.25.8
 | 
			
		||||
 
 | 
			
		||||
@@ -1,3 +1,11 @@
 | 
			
		||||
# Door cards API key
 | 
			
		||||
# should be equal to the auth token value set in Spaceport
 | 
			
		||||
DOOR_API_KEY = ''
 | 
			
		||||
 | 
			
		||||
# Door API routes
 | 
			
		||||
API_STATS = 'https://api.my.protospace.ca/stats/'
 | 
			
		||||
API_DOOR = 'https://api.my.protospace.ca/door/'
 | 
			
		||||
API_SEEN = lambda x: 'https://api.my.protospace.ca/door/{}/seen/'.format(x)
 | 
			
		||||
 | 
			
		||||
# Card reader settings
 | 
			
		||||
BAUD_RATE = 2400
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user