From 386c5e91fe3ae822436f3cc86c2f7b36eda11723 Mon Sep 17 00:00:00 2001 From: Matthew Frost Date: Tue, 31 Oct 2023 22:47:31 +0100 Subject: [PATCH] basic client for tinance2 --- python/example-tinance2.py | 51 +++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/python/example-tinance2.py b/python/example-tinance2.py index 71afc9a..6f8796b 100644 --- a/python/example-tinance2.py +++ b/python/example-tinance2.py @@ -4,31 +4,38 @@ import binascii import threading import time -ser = serial.Serial('/dev/tty.usbserial-0001', 9600) -tinance2_reader_identifier = "TEST-READER-1" -tinance2_reader_accesskey = "4f95166a-8a15-4963-bad8-35c7047b4269" +# Constants +SERIAL_PORT = '/dev/tty.usbserial-0001' +BAUD_RATE = 9600 +TINANCE2_READER_IDENTIFIER = "TEST-READER-1" +TINANCE2_READER_ACCESS_KEY = "4f95166a-8a15-4963-bad8-35c7047b4269" +API_BASE_URL = 'http://localhost:8000/accesscontrol/api/' + +# Reader configuration reader_info = { "enabled": False, "mode": None } +# Headers for API requests +HEADERS = { + "Content-Type": "application/json", + "X-Identifier": TINANCE2_READER_IDENTIFIER, + "X-Access-Key": TINANCE2_READER_ACCESS_KEY +} + def handle_log_msg(prefix, value): print(f"Received LOG_MSG: {value}") def check_valid_card(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' - data_to_post = {"full_card_id": value} - if value == "0:0": + print("Received invalid card ID (0:0), ignoring...") return - response = requests.post(url, json=data_to_post, headers=headers) + url = f'{API_BASE_URL}check-card-id' + data_to_post = {"full_card_id": value} + + 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") @@ -41,10 +48,9 @@ def check_valid_card(prefix, value): print(response.text) def fetch_initial_reader_config(): - global reader_info try: - url = 'http://localhost:8000/accesscontrol/api/readerinfo' - response = requests.get(url, headers=headers) + url = f'{API_BASE_URL}readerinfo' + response = requests.get(url, headers=HEADERS) if response.status_code == 200: data = response.json() @@ -54,11 +60,10 @@ def fetch_initial_reader_config(): print(f"Error fetching initial reader config: {e}") def reader_config_check(): - global reader_info while True: print("Checking HTTP endpoint for new reader config...") - url = 'http://localhost:8000/accesscontrol/api/readerinfo' - response = requests.get(url, headers=headers) + url = f'{API_BASE_URL}readerinfo' + response = requests.get(url, headers=HEADERS) if response.status_code == 200: data = response.json() @@ -112,12 +117,8 @@ def serial_router(): print(f"Received non-UTF-8 data: {hex_data}") if __name__ == "__main__": - headers = { - "Content-Type": "application/json", - "X-Identifier": tinance2_reader_identifier, - "X-Access-Key": tinance2_reader_accesskey - } - + ser = serial.Serial(SERIAL_PORT, BAUD_RATE) + # Fetch the initial reader configuration fetch_initial_reader_config()