users/open: pass in time for door to stay open

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-05-23 14:58:30 +03:00
parent e9925d32b4
commit 575ba9452a
4 changed files with 13 additions and 5 deletions

View File

@ -75,9 +75,12 @@ def remove_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
#db.refresh(link)
return True
def set_open_door_request(db: Session, iot_entity_id: int):
def set_open_door_request(db: Session, iot_entity_id: int, time_seconds : int):
device = get_iot_entity(db, iot_entity_id)
setattr(device, "open_request", True)
if time_seconds < 1:
time_seconds = 10 # Magic number move to global constant
setattr(device, "time_seconds", time_seconds)
db.add(device)
db.commit()
db.refresh(device)
@ -86,6 +89,7 @@ def set_open_door_request(db: Session, iot_entity_id: int):
def clear_open_door_request(db: Session, iot_entity_id: int):
device = get_iot_entity(db, iot_entity_id)
setattr(device, "open_request", False)
setattr(device, "time_seconds", 10)
db.add(device)
db.commit()
db.refresh(device)

View File

@ -170,7 +170,7 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc
return user.authorized_devices
@app.post("/users/open", tags=['Users'])
def issue_open_door_command(command: schemas.OpenDoorRequestBase,
def issue_open_door_command(command: schemas.OpenDoorRequestTime,
db: Session = Depends(get_db),
current_user: schemas.User = Depends(get_current_active_user)):
err = HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
@ -181,7 +181,7 @@ def issue_open_door_command(command: schemas.OpenDoorRequestBase,
user = crud.get_user(db, current_user.id)
for dev in user.authorized_devices:
if dev.bluetooth_mac == device.bluetooth_mac:
crud.set_open_door_request(db, device.id)
crud.set_open_door_request(db, device.id, command.time_seconds)
log_entry = schemas.DoorAccessLog(user_id=current_user.id,
door_bluetooth_mac=command.bluetooth_mac,
time=datetime.now())
@ -218,7 +218,8 @@ def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest,
response : schemas.IotDoorPollingResponse = schemas.IotDoorPollingResponse(
open_command=device.open_request,
acces_list_counter=0)
acces_list_counter=0,
time_seconds=device.time_seconds)
# Reset open_request to False
crud.clear_open_door_request(db, device.id)
return response

View File

@ -24,6 +24,7 @@ class IotEntity(Base):
bluetooth_mac = Column(String(512))
description = Column(String(512))
open_request = Column(Boolean, default=False)
time_seconds = Column(Integer, default=10)
authorized_users = relationship("User", secondary= 'user_iot_link')
class UserAuthToIoTDev(Base):

View File

@ -25,6 +25,7 @@ class IotEntity(IotEntityBase):
bluetooth_mac: str
#authorized_users: List[User] = []
open_request: bool # Flag to open
time_seconds: int
class Config:
orm_mode = True
@ -62,7 +63,7 @@ class OpenDoorRequestBase(BaseModel):
bluetooth_mac: str
class OpenDoorRequestTime(OpenDoorRequestBase):
time_minutes: int
time_seconds: int
# Device sends this periodcally
class IotDoorPollingRequest(BaseModel):
@ -74,6 +75,7 @@ class IotDoorPollingRequest(BaseModel):
class IotDoorPollingResponse(BaseModel):
open_command : bool
acces_list_counter : int
time_seconds : int
class IotMonitorRoomInfo(BaseModel):
humidity : int