example controller app on desktop

This commit is contained in:
Matthew Frost 2023-10-31 10:23:21 +01:00
parent ba50a00eec
commit 573fb1c829

View file

@ -0,0 +1,53 @@
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
def handle_log_msg(prefix,value):
print(f"Received LOG_MSG: {value}")
# 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):
url = 'https://your-api-endpoint.com' # Replace with your actual API endpoint
data_to_post = {'data': value}
response = requests.post(url, data=data_to_post)
if response.status_code is 200:
print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully")
else:
print(f"Failed to send FULL_CARD_ID via HTTP POST. Status code: {response.status_code}")
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
}
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()