56 lines
No EOL
1.9 KiB
Python
56 lines
No EOL
1.9 KiB
Python
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
|
|
|
|
# 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 value == "0:0":
|
|
return
|
|
|
|
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}")
|
|
|
|
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() |