diff --git a/python/example-tinance2.py b/python/example-tinance2.py index 29d91cb..72fc511 100644 --- a/python/example-tinance2.py +++ b/python/example-tinance2.py @@ -1,27 +1,24 @@ import serial import requests import binascii +import threading +import time -# Replace 'COMX' with the appropriate COM port on Windows or '/dev/ttyUSBX' on Linux -ser = serial.Serial('/dev/tty.usbserial-0001', 9600) # Adjust the baud rate as needed +ser = serial.Serial('/dev/tty.usbserial-0001', 9600) tinance2_reader_identifier = "TEST-READER-1" tinance2_reader_accesskey = "4f95166a-8a15-4963-bad8-35c7047b4269" -# Function to handle LOG_MSG -def handle_log_msg(prefix,value): +def handle_log_msg(prefix, value): print(f"Received LOG_MSG: {value}") -# Function to send HTTP POST -def send_http_post(prefix,value): +def send_http_post(prefix, value): + headers = { + "Content-Type": "application/json", + "X-Identifier": tinance2_reader_identifier, + "X-Access-Key": tinance2_reader_accesskey + } - headers = { - "Content-Type" : "application/json", - "X-Identifier" : tinance2_reader_identifier, - "X-Access-Key" : tinance2_reader_accesskey - } - - - url = 'http://localhost:8000/accesscontrol/api/check-card-id' # Replace with your actual API endpoint + url = 'http://localhost:8000/accesscontrol/api/check-card-id' data_to_post = {"full_card_id": value} if value == "0:0": @@ -36,9 +33,34 @@ def send_http_post(prefix,value): print(f"Failed to send FULL_CARD_ID via HTTP POST. Status code: {response.status_code}") print(response.text) -try: - while True: # Run the main loop indefinitely - data = ser.readline() # Read a line of data from the serial port +def reader_config_check(): + previous_enabled = None # Initialize to None + while True: + print("Checking HTTP endpoint for new reader config...") + # Replace with your HTTP GET endpoint URL + url = 'http://localhost:8000/accesscontrol/api/readerinfo' + response = requests.get(url, headers=headers) + + if response.status_code == 200: + data = response.json() + enabled = data.get("enabled", False) + + if enabled != previous_enabled: + print("New reader enabled status: ", enabled) + if enabled: + print("Enabling reader...") + ser.write("enable".encode('utf-8')) + else: + print("Disabling reader...") + ser.write("disable".encode('utf-8')) + previous_enabled = enabled # Update the previous_enabled status + + time.sleep(5) # Check every 15 seconds + + +def card_auth(): + while True: + data = ser.readline() try: data = data.decode('utf-8') if '=' in data: @@ -54,15 +76,27 @@ try: } if prefix in actions: - actions[prefix](prefix,value) + actions[prefix](prefix, value) else: print(f"Unknown prefix: {prefix}") except UnicodeDecodeError: - # Handle non-UTF-8 encoded data hex_data = binascii.hexlify(data).decode('utf-8') print(f"Received non-UTF-8 data: {hex_data}") -except Exception as e: - print(f"Error: {e}") -finally: - # Remember to close the serial port when done: - ser.close() \ No newline at end of file + +if __name__ == "__main__": + headers = { + "Content-Type": "application/json", + "X-Identifier": tinance2_reader_identifier, + "X-Access-Key": tinance2_reader_accesskey + } + + # Create and start the two threads + card_auth_thread = threading.Thread(target=card_auth) + reader_config_check_thread = threading.Thread(target=reader_config_check) + + card_auth_thread.start() + reader_config_check_thread.start() + + # Wait for both threads to finish (which is never in this case since they run indefinitely) + card_auth_thread.join() + reader_config_check_thread.join()