basic client for tinance2

This commit is contained in:
Matthew Frost 2023-10-31 22:47:31 +01:00
parent 42659310cf
commit 386c5e91fe

View file

@ -4,31 +4,38 @@ import binascii
import threading import threading
import time import time
ser = serial.Serial('/dev/tty.usbserial-0001', 9600) # Constants
tinance2_reader_identifier = "TEST-READER-1" SERIAL_PORT = '/dev/tty.usbserial-0001'
tinance2_reader_accesskey = "4f95166a-8a15-4963-bad8-35c7047b4269" 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 = { reader_info = {
"enabled": False, "enabled": False,
"mode": None "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): def handle_log_msg(prefix, value):
print(f"Received LOG_MSG: {value}") print(f"Received LOG_MSG: {value}")
def check_valid_card(prefix, 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": if value == "0:0":
print("Received invalid card ID (0:0), ignoring...")
return 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: if response.status_code == 200:
print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully") print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully")
@ -41,10 +48,9 @@ def check_valid_card(prefix, value):
print(response.text) print(response.text)
def fetch_initial_reader_config(): def fetch_initial_reader_config():
global reader_info
try: try:
url = 'http://localhost:8000/accesscontrol/api/readerinfo' url = f'{API_BASE_URL}readerinfo'
response = requests.get(url, headers=headers) response = requests.get(url, headers=HEADERS)
if response.status_code == 200: if response.status_code == 200:
data = response.json() data = response.json()
@ -54,11 +60,10 @@ def fetch_initial_reader_config():
print(f"Error fetching initial reader config: {e}") print(f"Error fetching initial reader config: {e}")
def reader_config_check(): def reader_config_check():
global reader_info
while True: while True:
print("Checking HTTP endpoint for new reader config...") print("Checking HTTP endpoint for new reader config...")
url = 'http://localhost:8000/accesscontrol/api/readerinfo' url = f'{API_BASE_URL}readerinfo'
response = requests.get(url, headers=headers) response = requests.get(url, headers=HEADERS)
if response.status_code == 200: if response.status_code == 200:
data = response.json() data = response.json()
@ -112,12 +117,8 @@ def serial_router():
print(f"Received non-UTF-8 data: {hex_data}") print(f"Received non-UTF-8 data: {hex_data}")
if __name__ == "__main__": if __name__ == "__main__":
headers = { ser = serial.Serial(SERIAL_PORT, BAUD_RATE)
"Content-Type": "application/json",
"X-Identifier": tinance2_reader_identifier,
"X-Access-Key": tinance2_reader_accesskey
}
# Fetch the initial reader configuration # Fetch the initial reader configuration
fetch_initial_reader_config() fetch_initial_reader_config()