decent start
This commit is contained in:
parent
a6e37daf78
commit
42659310cf
1 changed files with 51 additions and 20 deletions
|
@ -7,11 +7,15 @@ import time
|
||||||
ser = serial.Serial('/dev/tty.usbserial-0001', 9600)
|
ser = serial.Serial('/dev/tty.usbserial-0001', 9600)
|
||||||
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"
|
||||||
|
reader_info = {
|
||||||
|
"enabled": False,
|
||||||
|
"mode": None
|
||||||
|
}
|
||||||
|
|
||||||
def handle_log_msg(prefix, value):
|
def handle_log_msg(prefix, value):
|
||||||
print(f"Received LOG_MSG: {value}")
|
print(f"Received LOG_MSG: {value}")
|
||||||
|
|
||||||
def send_http_post(prefix, value):
|
def check_valid_card(prefix, value):
|
||||||
headers = {
|
headers = {
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Identifier": tinance2_reader_identifier,
|
"X-Identifier": tinance2_reader_identifier,
|
||||||
|
@ -28,37 +32,61 @@ def send_http_post(prefix, value):
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully")
|
print(f"Sent FULL_CARD_ID: {value} via HTTP POST successfully")
|
||||||
ser.write("unlock".encode('utf-8'))
|
if reader_info.get("mode") == "LATCH":
|
||||||
|
ser.write("unlock".encode('utf-8'))
|
||||||
|
elif reader_info.get("mode") == "TOGGLE":
|
||||||
|
ser.write("toggle".encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
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)
|
||||||
|
|
||||||
def reader_config_check():
|
def fetch_initial_reader_config():
|
||||||
previous_enabled = None # Initialize to None
|
global reader_info
|
||||||
while True:
|
try:
|
||||||
print("Checking HTTP endpoint for new reader config...")
|
|
||||||
# Replace with your HTTP GET endpoint URL
|
|
||||||
url = 'http://localhost:8000/accesscontrol/api/readerinfo'
|
url = 'http://localhost:8000/accesscontrol/api/readerinfo'
|
||||||
response = requests.get(url, headers=headers)
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
if response.status_code == 200:
|
if response.status_code == 200:
|
||||||
data = response.json()
|
data = response.json()
|
||||||
enabled = data.get("enabled", False)
|
reader_info["enabled"] = data.get("enabled", False)
|
||||||
|
reader_info["mode"] = data.get("mode")
|
||||||
if enabled != previous_enabled:
|
except Exception as e:
|
||||||
print("New reader enabled status: ", enabled)
|
print(f"Error fetching initial reader config: {e}")
|
||||||
if enabled:
|
|
||||||
|
def reader_config_check():
|
||||||
|
global reader_info
|
||||||
|
while True:
|
||||||
|
print("Checking HTTP endpoint for new reader config...")
|
||||||
|
url = 'http://localhost:8000/accesscontrol/api/readerinfo'
|
||||||
|
response = requests.get(url, headers=headers)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
data = response.json()
|
||||||
|
new_enabled = data.get("enabled", False)
|
||||||
|
new_mode = data.get("mode")
|
||||||
|
|
||||||
|
if new_enabled != reader_info["enabled"]:
|
||||||
|
print("New reader enabled status: ", new_enabled)
|
||||||
|
if new_enabled:
|
||||||
print("Enabling reader...")
|
print("Enabling reader...")
|
||||||
ser.write("enable".encode('utf-8'))
|
ser.write("enable".encode('utf-8'))
|
||||||
else:
|
else:
|
||||||
print("Disabling reader...")
|
print("Disabling reader...")
|
||||||
ser.write("disable".encode('utf-8'))
|
ser.write("disable".encode('utf-8'))
|
||||||
previous_enabled = enabled # Update the previous_enabled status
|
reader_info["enabled"] = new_enabled
|
||||||
|
|
||||||
time.sleep(5) # Check every 15 seconds
|
if new_mode != reader_info["mode"]:
|
||||||
|
print("Reader mode has changed: ", new_mode)
|
||||||
|
if new_mode == "LATCH" and reader_info["mode"] == "TOGGLE":
|
||||||
|
print("Transitioned from TOGGLE to LATCH, sending 'lock' over serial...")
|
||||||
|
ser.write("lock".encode("utf-8"))
|
||||||
|
reader_info["mode"] = new_mode
|
||||||
|
|
||||||
def card_auth():
|
if new_mode == "LATCH" and reader_info["mode"] == "TOGGLE":
|
||||||
|
print("Transitioned from LATCH to TOGGLE, sending 'unlock' over serial...")
|
||||||
|
time.sleep(15) # Check every 15 seconds
|
||||||
|
|
||||||
|
def serial_router():
|
||||||
while True:
|
while True:
|
||||||
data = ser.readline()
|
data = ser.readline()
|
||||||
try:
|
try:
|
||||||
|
@ -70,7 +98,7 @@ def card_auth():
|
||||||
|
|
||||||
actions = {
|
actions = {
|
||||||
"LOG_MSG": handle_log_msg,
|
"LOG_MSG": handle_log_msg,
|
||||||
"FULL_CARD_ID": send_http_post,
|
"FULL_CARD_ID": check_valid_card,
|
||||||
"DOOR_DISABLED_STATUS": handle_log_msg,
|
"DOOR_DISABLED_STATUS": handle_log_msg,
|
||||||
"DOOR_LOCK_STATUS": handle_log_msg
|
"DOOR_LOCK_STATUS": handle_log_msg
|
||||||
}
|
}
|
||||||
|
@ -90,13 +118,16 @@ if __name__ == "__main__":
|
||||||
"X-Access-Key": tinance2_reader_accesskey
|
"X-Access-Key": tinance2_reader_accesskey
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Fetch the initial reader configuration
|
||||||
|
fetch_initial_reader_config()
|
||||||
|
|
||||||
# Create and start the two threads
|
# Create and start the two threads
|
||||||
card_auth_thread = threading.Thread(target=card_auth)
|
serial_router_thread = threading.Thread(target=serial_router)
|
||||||
reader_config_check_thread = threading.Thread(target=reader_config_check)
|
reader_config_check_thread = threading.Thread(target=reader_config_check)
|
||||||
|
|
||||||
card_auth_thread.start()
|
serial_router_thread.start()
|
||||||
reader_config_check_thread.start()
|
reader_config_check_thread.start()
|
||||||
|
|
||||||
# Wait for both threads to finish (which is never in this case since they run indefinitely)
|
# Wait for both threads to finish (which is never in this case since they run indefinitely)
|
||||||
card_auth_thread.join()
|
serial_router_thread.join()
|
||||||
reader_config_check_thread.join()
|
reader_config_check_thread.join()
|
||||||
|
|
Loading…
Reference in a new issue