stm32-fmt-code/access_control_python/access_control.py

99 lines
3.1 KiB
Python
Raw Permalink Normal View History

2023-09-17 08:27:41 +00:00
import serial
2023-09-17 09:57:40 +00:00
import time
2023-09-17 08:27:41 +00:00
from threading import Thread
class access_control:
2023-09-17 09:57:40 +00:00
_in_payloads = []
2023-09-17 08:27:41 +00:00
_read_buffer = []
2023-09-30 08:23:10 +00:00
_door_state: bool = False
2023-09-17 08:27:41 +00:00
serial_adapter: serial.Serial
2023-09-30 08:23:10 +00:00
_scan_active: bool = False
_read_active = True
2023-09-17 08:27:41 +00:00
def __init__(self, serial_port: str):
2023-09-23 06:03:15 +00:00
self.serial_adapter = serial.Serial(serial_port,baudrate=9600)
2023-09-30 08:23:10 +00:00
Thread(target=self.read_serial).start()
Thread(target=self._process_payload).start()
#Thread(target=self._state_requester).start()
def _state_requester(self):
2023-09-17 08:27:41 +00:00
while True:
2023-09-30 08:23:10 +00:00
if self._read_active:
self._request_door_state()
self._request_scan_state()
time.sleep(1)
def read_serial(self):
while True:
if self.serial_adapter.in_waiting>0:
in_byte = self.serial_adapter.read(1)
if(in_byte==b'\xFF'):
self._in_payloads.append(self._read_buffer)
self._read_buffer = []
else:
self._read_buffer.append(in_byte)
time.sleep(0.01)
2023-09-17 09:57:40 +00:00
def _process_payload(self):
while True:
self._process_payload_once()
2023-09-17 10:40:31 +00:00
time.sleep(0.01)
2023-09-17 09:57:40 +00:00
def _process_payload_once(self):
if(len(self._in_payloads)>0):
payload = self._in_payloads.pop(0)
2023-09-30 08:23:10 +00:00
if len(payload)>0:
if(payload[0]==b'\x01'):
if(payload[1]==b'\x00'):
self._door_state = True
elif(payload[1]==b'\01'):
self._door_state = False
elif(payload[0]==b'\x02'):
if(payload[1]==b'\x00'):
self._scan_active = False
elif(payload[1]==b'\01'):
self._scan_active = True
2023-09-17 10:40:31 +00:00
2023-09-17 08:27:41 +00:00
def light_on(self):
packet = bytearray()
packet.append(0x00)
packet.append(0x01)
packet.append(0xFF)
self.serial_adapter.write(packet)
def light_off(self):
packet = bytearray()
packet.append(0x00)
packet.append(0x00)
packet.append(0xFF)
self.serial_adapter.write(packet)
2023-09-17 09:57:40 +00:00
def _request_door_state(self):
2023-09-17 09:18:33 +00:00
packet = bytearray()
packet.append(0x01)
packet.append(0xFF)
2023-09-17 09:57:40 +00:00
self.serial_adapter.write(packet)
2023-09-30 08:23:10 +00:00
def _request_scan_state(self):
packet = bytearray()
packet.append(0x03)
packet.append(0xFF)
self.serial_adapter.write(packet)
2023-09-17 09:57:40 +00:00
def get_door_state(self) -> bool:
2023-09-17 10:40:31 +00:00
return self._door_state
2023-09-30 08:23:10 +00:00
def get_scan_state(self) -> bool:
return self._scan_active
2023-09-17 10:40:31 +00:00
def lock_door(self):
packet = bytearray()
packet.append(0x02)
packet.append(0x01)
packet.append(0xFF)
self.serial_adapter.write(packet)
def unlock_door(self):
packet = bytearray()
packet.append(0x02)
packet.append(0x00)
packet.append(0xFF)
self.serial_adapter.write(packet)