reader controller
This commit is contained in:
parent
5e93a8c1b7
commit
a6e37daf78
1 changed files with 58 additions and 24 deletions
|
@ -1,27 +1,24 @@
|
||||||
import serial
|
import serial
|
||||||
import requests
|
import requests
|
||||||
import binascii
|
import binascii
|
||||||
|
import threading
|
||||||
|
import time
|
||||||
|
|
||||||
# Replace 'COMX' with the appropriate COM port on Windows or '/dev/ttyUSBX' on Linux
|
ser = serial.Serial('/dev/tty.usbserial-0001', 9600)
|
||||||
ser = serial.Serial('/dev/tty.usbserial-0001', 9600) # Adjust the baud rate as needed
|
|
||||||
tinance2_reader_identifier = "TEST-READER-1"
|
tinance2_reader_identifier = "TEST-READER-1"
|
||||||
tinance2_reader_accesskey = "4f95166a-8a15-4963-bad8-35c7047b4269"
|
tinance2_reader_accesskey = "4f95166a-8a15-4963-bad8-35c7047b4269"
|
||||||
|
|
||||||
# Function to handle LOG_MSG
|
def handle_log_msg(prefix, value):
|
||||||
def handle_log_msg(prefix,value):
|
|
||||||
print(f"Received LOG_MSG: {value}")
|
print(f"Received LOG_MSG: {value}")
|
||||||
|
|
||||||
# Function to send HTTP POST
|
def send_http_post(prefix, value):
|
||||||
def send_http_post(prefix,value):
|
headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"X-Identifier": tinance2_reader_identifier,
|
||||||
|
"X-Access-Key": tinance2_reader_accesskey
|
||||||
|
}
|
||||||
|
|
||||||
headers = {
|
url = 'http://localhost:8000/accesscontrol/api/check-card-id'
|
||||||
"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}
|
data_to_post = {"full_card_id": value}
|
||||||
|
|
||||||
if value == "0:0":
|
if value == "0:0":
|
||||||
|
@ -36,9 +33,34 @@ def send_http_post(prefix,value):
|
||||||
print(f"Failed to send FULL_CARD_ID via HTTP POST. Status code: {response.status_code}")
|
print(f"Failed to send FULL_CARD_ID via HTTP POST. Status code: {response.status_code}")
|
||||||
print(response.text)
|
print(response.text)
|
||||||
|
|
||||||
try:
|
def reader_config_check():
|
||||||
while True: # Run the main loop indefinitely
|
previous_enabled = None # Initialize to None
|
||||||
data = ser.readline() # Read a line of data from the serial port
|
while True:
|
||||||
|
print("Checking HTTP endpoint for new reader config...")
|
||||||
|
# Replace with your HTTP GET endpoint URL
|
||||||
|
url = 'http://localhost:8000/accesscontrol/api/readerinfo'
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json()
|
||||||
|
enabled = data.get("enabled", False)
|
||||||
|
|
||||||
|
if enabled != previous_enabled:
|
||||||
|
print("New reader enabled status: ", enabled)
|
||||||
|
if enabled:
|
||||||
|
print("Enabling reader...")
|
||||||
|
ser.write("enable".encode('utf-8'))
|
||||||
|
else:
|
||||||
|
print("Disabling reader...")
|
||||||
|
ser.write("disable".encode('utf-8'))
|
||||||
|
previous_enabled = enabled # Update the previous_enabled status
|
||||||
|
|
||||||
|
time.sleep(5) # Check every 15 seconds
|
||||||
|
|
||||||
|
|
||||||
|
def card_auth():
|
||||||
|
while True:
|
||||||
|
data = ser.readline()
|
||||||
try:
|
try:
|
||||||
data = data.decode('utf-8')
|
data = data.decode('utf-8')
|
||||||
if '=' in data:
|
if '=' in data:
|
||||||
|
@ -54,15 +76,27 @@ try:
|
||||||
}
|
}
|
||||||
|
|
||||||
if prefix in actions:
|
if prefix in actions:
|
||||||
actions[prefix](prefix,value)
|
actions[prefix](prefix, value)
|
||||||
else:
|
else:
|
||||||
print(f"Unknown prefix: {prefix}")
|
print(f"Unknown prefix: {prefix}")
|
||||||
except UnicodeDecodeError:
|
except UnicodeDecodeError:
|
||||||
# Handle non-UTF-8 encoded data
|
|
||||||
hex_data = binascii.hexlify(data).decode('utf-8')
|
hex_data = binascii.hexlify(data).decode('utf-8')
|
||||||
print(f"Received non-UTF-8 data: {hex_data}")
|
print(f"Received non-UTF-8 data: {hex_data}")
|
||||||
except Exception as e:
|
|
||||||
print(f"Error: {e}")
|
if __name__ == "__main__":
|
||||||
finally:
|
headers = {
|
||||||
# Remember to close the serial port when done:
|
"Content-Type": "application/json",
|
||||||
ser.close()
|
"X-Identifier": tinance2_reader_identifier,
|
||||||
|
"X-Access-Key": tinance2_reader_accesskey
|
||||||
|
}
|
||||||
|
|
||||||
|
# Create and start the two threads
|
||||||
|
card_auth_thread = threading.Thread(target=card_auth)
|
||||||
|
reader_config_check_thread = threading.Thread(target=reader_config_check)
|
||||||
|
|
||||||
|
card_auth_thread.start()
|
||||||
|
reader_config_check_thread.start()
|
||||||
|
|
||||||
|
# Wait for both threads to finish (which is never in this case since they run indefinitely)
|
||||||
|
card_auth_thread.join()
|
||||||
|
reader_config_check_thread.join()
|
||||||
|
|
Loading…
Reference in a new issue