From 575ba9452a6caa6e3cc1a371cf43ed1ff92b2fe4 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 23 May 2022 14:58:30 +0300 Subject: [PATCH] users/open: pass in time for door to stay open Signed-off-by: HeshamTB --- sql_app/crud.py | 6 +++++- sql_app/main.py | 7 ++++--- sql_app/models.py | 1 + sql_app/schemas.py | 4 +++- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index 2da236c..ee47a36 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -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) diff --git a/sql_app/main.py b/sql_app/main.py index 8045b76..5070e0e 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -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 diff --git a/sql_app/models.py b/sql_app/models.py index 7e0e6a2..ec11725 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -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): diff --git a/sql_app/schemas.py b/sql_app/schemas.py index cb22fea..fb0fd44 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -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