diff --git a/python/example-processor.py b/python/example-processor.py new file mode 100644 index 0000000..c1a5257 --- /dev/null +++ b/python/example-processor.py @@ -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() \ No newline at end of file