stm32-fmt-code/access_control_python/access_control_mqtt.py

68 lines
1.8 KiB
Python
Raw Permalink Normal View History

2023-11-14 11:45:43 +00:00
from espmega.espmega_r3 import ESPMega_standalone as ESPMega
2023-11-14 15:55:26 +00:00
from time import sleep, perf_counter
from time import sleep as delay
2023-11-06 06:53:10 +00:00
2023-11-13 12:12:30 +00:00
class access_control:
2023-11-17 13:46:16 +00:00
"""
This class provides methods to control access to a door using an ESPMega board.
"""
2023-11-06 09:10:46 +00:00
def __init__(self):
2023-11-17 13:46:16 +00:00
"""
Initializes an instance of the access_control class.
"""
2023-11-14 11:45:43 +00:00
self.plc = ESPMega("/facescan","192.168.0.239",1883)
2023-11-06 06:53:10 +00:00
def get_scan_state(self) -> bool:
2023-11-17 13:46:16 +00:00
"""
Returns the state of the scan sensor.
Returns:
bool: True if the scan sensor is active, False otherwise.
"""
2023-11-14 11:45:43 +00:00
return self.plc.digital_read(1)
2023-11-17 13:46:16 +00:00
def lock_door(self):
"""
Locks the door if it is closed.
"""
2023-11-14 11:45:43 +00:00
if(self.plc.digital_read(0)):
2023-11-14 15:55:26 +00:00
self.plc.digital_write(0,0)
2023-11-14 11:45:43 +00:00
2023-11-17 13:46:16 +00:00
def unlock_door(self):
"""
Unlocks the door if it is closed.
"""
2023-11-14 11:45:43 +00:00
if(self.plc.digital_read(0)):
2023-11-14 15:55:26 +00:00
self.plc.digital_write(0,1)
2023-11-17 13:46:16 +00:00
2023-11-14 11:45:43 +00:00
def get_door_state(self) -> bool:
2023-11-17 13:46:16 +00:00
"""
Returns the state of the door.
Returns:
bool: True if the door is closed, False otherwise.
"""
2023-11-14 11:45:43 +00:00
return self.plc.digital_read(0)
2023-11-17 13:46:16 +00:00
# def activate_alarm(self):
# """
# Activates the alarm.
# """
# self.plc.digital_write(2,1)
2023-11-14 15:55:26 +00:00
2023-11-17 13:46:16 +00:00
def activate_alarm(self):
"""
Activates the LED alarm.
"""
2023-12-11 05:05:24 +00:00
self.plc.digital_write(8,1)
2023-11-14 15:55:26 +00:00
while(True):
self.plc.digital_write(1,round((perf_counter()*2)%1))
print(round((perf_counter()*2)%1))
2023-11-17 13:46:16 +00:00
delay(0.3)
2023-11-14 15:55:26 +00:00
if self.get_door_state():
self.lock_door()
2023-11-17 13:46:16 +00:00
self.plc.digital_write(1,0)
2023-12-11 05:05:24 +00:00
self.plc.digital_write(8,0)
2023-11-14 15:55:26 +00:00
break