From 5e93a8c1b75877e19492b403fc95a342b11d8789 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Tue, 31 Oct 2023 18:54:21 +0100 Subject: [PATCH] basic tinance2 example --- python/example-processor.py | 4 +-- python/example-tinance2.py | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 python/example-tinance2.py diff --git a/python/example-processor.py b/python/example-processor.py index a02dfff..d8110dd 100644 --- a/python/example-processor.py +++ b/python/example-processor.py @@ -15,10 +15,10 @@ def send_http_post(prefix,value): data_to_post = {'data': value} response = requests.post(url, data=data_to_post) - if value is "0:0": + if value == "0:0": return - if response.status_code is 200: + if response.status_code == 200: print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully") ser.write("unlock".encode('utf-8')) else: diff --git a/python/example-tinance2.py b/python/example-tinance2.py new file mode 100644 index 0000000..29d91cb --- /dev/null +++ b/python/example-tinance2.py @@ -0,0 +1,68 @@ +import serial +import requests +import binascii + +# 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 +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): + print(f"Received LOG_MSG: {value}") + +# Function to send HTTP POST +def send_http_post(prefix,value): + + 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 + data_to_post = {"full_card_id": value} + + if value == "0:0": + return + + response = requests.post(url, json=data_to_post, headers=headers) + + if response.status_code == 200: + print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully") + ser.write("unlock".encode('utf-8')) + else: + 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 + try: + data = data.decode('utf-8') + if '=' in data: + prefix, value = data.split('=') + prefix = prefix.strip() + value = value.strip() + + actions = { + "LOG_MSG": handle_log_msg, + "FULL_CARD_ID": send_http_post, + "DOOR_DISABLED_STATUS": handle_log_msg, + "DOOR_LOCK_STATUS": handle_log_msg + } + + if prefix in actions: + 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