basic tinance2 example

This commit is contained in:
Matthew Frost 2023-10-31 18:54:21 +01:00
parent 93dc75f66a
commit 5e93a8c1b7
2 changed files with 70 additions and 2 deletions

View file

@ -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:

View file

@ -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()