2023-10-31 09:23:21 +00:00
|
|
|
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)
|
2023-10-31 15:16:10 +00:00
|
|
|
|
2023-10-31 17:54:21 +00:00
|
|
|
if value == "0:0":
|
2023-10-31 15:16:10 +00:00
|
|
|
return
|
|
|
|
|
2023-10-31 17:54:21 +00:00
|
|
|
if response.status_code == 200:
|
2023-10-31 09:23:21 +00:00
|
|
|
print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully")
|
2023-10-31 09:26:02 +00:00
|
|
|
ser.write("unlock".encode('utf-8'))
|
2023-10-31 09:23:21 +00:00
|
|
|
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,
|
2023-10-31 09:26:02 +00:00
|
|
|
"DOOR_DISABLED_STATUS": handle_log_msg,
|
|
|
|
"DOOR_LOCK_STATUS": handle_log_msg
|
2023-10-31 09:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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()
|