From 3bcb333df6abb428681415e60fed5109b365412a Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Tue, 7 Jun 2022 18:58:23 +0300 Subject: [PATCH] sql_app: Added state to Iot Door Signed-off-by: HeshamTB --- sql_app/crud.py | 6 ++++++ sql_app/main.py | 2 ++ sql_app/models.py | 3 +++ sql_app/schemas.py | 1 + 4 files changed, 12 insertions(+) diff --git a/sql_app/crud.py b/sql_app/crud.py index ffdac1d..2c53795 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -126,6 +126,12 @@ def set_user_last_token(db: Session, username: str, token: str): db.refresh(user) return True +def set_door_state(db: Session, iot_device: models.IotEntity, state: bool): + iot_device.state = state + db.add(iot_device) + db.commit() + db.refresh(iot_device) + def get_user_last_token(db: Session, username: str): user : models.User = get_user_by_username(db, username) return user.last_token # This method is bad security practice. diff --git a/sql_app/main.py b/sql_app/main.py index 2ebd041..6d56bfb 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -304,6 +304,8 @@ def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, # Reset open_request to False crud.clear_open_door_request(db, device.id) crud.clear_close_door_request(db, device.id) + crud.set_door_state(db, device, device.state) + return response @app.post("/iotdevice/monitor/status", tags=['Iot']) diff --git a/sql_app/models.py b/sql_app/models.py index f68662c..fbc22e9 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -28,6 +28,7 @@ class IotEntity(Base): time_seconds = Column(Integer, default=10) acces_list_counter = Column(Integer, default=0) force_close = Column(Boolean, default=False) + state = Column(Boolean, default=False) # True is open, False is closed authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices") class UserAuthToIoTDev(Base): @@ -63,4 +64,6 @@ class UserConnectionHistory(Base): reading_id = Column(Integer, primary_key=True) user_id = Column(Integer,ForeignKey("user_accounts.id"), index=True) timestamp = Column(DateTime) + # TODO: add ip + # TODO: Add table to store active sessions. May periodically clear. \ No newline at end of file diff --git a/sql_app/schemas.py b/sql_app/schemas.py index 24b60b3..5db9c40 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -72,6 +72,7 @@ class CloseDoorRequest(OpenDoorRequestBase): # Device sends this periodcally class IotDoorPollingRequest(BaseModel): bluetooth_mac : str + state: int token : str class Config: orm_mode = True