68 lines
1.8 KiB
Python
68 lines
1.8 KiB
Python
from espmega.espmega_r3 import ESPMega_standalone as ESPMega
|
|
from time import sleep, perf_counter
|
|
from time import sleep as delay
|
|
|
|
class access_control:
|
|
"""
|
|
This class provides methods to control access to a door using an ESPMega board.
|
|
"""
|
|
|
|
def __init__(self):
|
|
"""
|
|
Initializes an instance of the access_control class.
|
|
"""
|
|
self.plc = ESPMega("/facescan","192.168.0.239",1883)
|
|
|
|
def get_scan_state(self) -> bool:
|
|
"""
|
|
Returns the state of the scan sensor.
|
|
|
|
Returns:
|
|
bool: True if the scan sensor is active, False otherwise.
|
|
"""
|
|
return self.plc.digital_read(1)
|
|
|
|
def lock_door(self):
|
|
"""
|
|
Locks the door if it is closed.
|
|
"""
|
|
if(self.plc.digital_read(0)):
|
|
self.plc.digital_write(0,0)
|
|
|
|
def unlock_door(self):
|
|
"""
|
|
Unlocks the door if it is closed.
|
|
"""
|
|
if(self.plc.digital_read(0)):
|
|
self.plc.digital_write(0,1)
|
|
|
|
def get_door_state(self) -> bool:
|
|
"""
|
|
Returns the state of the door.
|
|
|
|
Returns:
|
|
bool: True if the door is closed, False otherwise.
|
|
"""
|
|
return self.plc.digital_read(0)
|
|
|
|
# def activate_alarm(self):
|
|
# """
|
|
# Activates the alarm.
|
|
# """
|
|
# self.plc.digital_write(2,1)
|
|
|
|
def activate_alarm(self):
|
|
"""
|
|
Activates the LED alarm.
|
|
"""
|
|
self.plc.digital_write(8,1)
|
|
while(True):
|
|
self.plc.digital_write(1,round((perf_counter()*2)%1))
|
|
print(round((perf_counter()*2)%1))
|
|
delay(0.3)
|
|
if self.get_door_state():
|
|
self.lock_door()
|
|
self.plc.digital_write(1,0)
|
|
self.plc.digital_write(8,0)
|
|
break
|
|
|