diff --git a/sql_app/crud.py b/sql_app/crud.py index edf0278..2144d12 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -104,6 +104,20 @@ def set_open_door_request(db: Session, iot_entity_id: int, time_seconds : int): db.refresh(device) return True +def set_close_door_request(db: Session, iot_id: int): + device : models.IotEntity = get_iot_entity(db, iot_id) + device.force_close = True + db.add(device) + db.commit() + db.refresh(device) + return True + +def clear_close_door_request(db: Session, iot_id: int): + device : models.IotEntity = get_iot_entity(db, iot_id) + device.force_close = False + db.add(device) + db.commit() + def set_user_last_token(db: Session, username: str, token: str): user : models.User = get_user_by_username(db, username) user.last_token = token @@ -127,8 +141,9 @@ def clear_open_door_request(db: Session, iot_entity_id: int): def record_door_access_log(db: Session, entry: schemas.DoorAccessLog): db_item = models.DoorAccessLog(user_id=entry.user_id, - iot_dev_bluetooth_mac=entry.door_bluetooth_mac, - timestamp=entry.time) + iot_id=entry.iot_id, + command=entry.command, + timestamp=entry.timestamp) db.add(db_item) db.commit() db.refresh(db_item) diff --git a/sql_app/main.py b/sql_app/main.py index 0269b54..7be744d 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -98,12 +98,32 @@ def issue_open_door_command(command: schemas.OpenDoorRequestTime, if dev.bluetooth_mac == device.bluetooth_mac: 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()) + iot_id=device.id, + command="OPEN", + timestamp=datetime.now()) crud.record_door_access_log(db, log_entry) return device raise err +@app.post("/users/close", tags=['Users']) +def issue_close_door_command(command: schemas.CloseDoorRequest, + db: Session = Depends(get_db), + current_user: schemas.User = Depends(get_current_active_user)): + err = HTTPException(status.HTTP_401_UNAUTHORIZED, + detail="Unaithrized to close") + device = crud.get_iot_entity_by_bluetooth_mac(db, command.bluetooth_mac) + if not device: raise err + user = crud.get_user(db, current_user.id) + for dev in user.authorized_devices: + if dev.bluetooth_mac == device.bluetooth_mac: + crud.set_close_door_request(db, device.id) + log_entry = schemas.DoorAccessLog(user_id=current_user.id, + iot_id=device.id, + command="CLOSE", + timestamp=datetime.now()) + crud.record_door_access_log(db, log_entry) + return device + @app.post("/users/tkn", response_model=schemas.Token, tags=['Users']) @app.post("/tkn", response_model=schemas.Token, tags=['Users']) def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)): @@ -262,7 +282,7 @@ def get_room_data(db: Session = Depends(get_db)): def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, db: Session = Depends(get_db)): - device: schemas.IotEntity = auth_helper.valid_iot_token(request.token, db) + device: models.IotEntity = auth_helper.valid_iot_token(request.token, db) if not device: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, @@ -271,9 +291,11 @@ def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, response : schemas.IotDoorPollingResponse = schemas.IotDoorPollingResponse( open_command=device.open_request, acces_list_counter=0, - time_seconds=device.time_seconds) + time_seconds=device.time_seconds, + force_close=device.force_close) # Reset open_request to False crud.clear_open_door_request(db, device.id) + crud.clear_close_door_request(db, device.id) return response @app.post("/iotdevice/monitor/status", tags=['Iot']) diff --git a/sql_app/models.py b/sql_app/models.py index 89ecbdc..2690cf7 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -27,6 +27,7 @@ class IotEntity(Base): open_request = Column(Boolean, default=False) time_seconds = Column(Integer, default=10) acces_list_counter = Column(Integer, default=0) + force_close = Column(Boolean, default=False) authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices") class UserAuthToIoTDev(Base): @@ -41,7 +42,8 @@ class DoorAccessLog(Base): entry_id = Column(Integer, primary_key=True, index=True) user_id = Column(Integer, ForeignKey('user_accounts.id')) - iot_dev_bluetooth_mac = Column(Integer, ForeignKey('iot_entities.id')) + iot_id = Column(Integer, ForeignKey('iot_entities.id')) + command = Column(String(16)) timestamp = Column(DateTime) class RoomSensorData(Base): diff --git a/sql_app/schemas.py b/sql_app/schemas.py index e10ea4d..24b60b3 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -26,6 +26,7 @@ class IotEntity(IotEntityBase): #authorized_users: List[User] = [] open_request: bool # Flag to open time_seconds: int + force_close: bool acces_list_counter: int class Config: orm_mode = True @@ -66,6 +67,8 @@ class OpenDoorRequestBase(BaseModel): class OpenDoorRequestTime(OpenDoorRequestBase): time_seconds: int +class CloseDoorRequest(OpenDoorRequestBase): + pass # Device sends this periodcally class IotDoorPollingRequest(BaseModel): bluetooth_mac : str @@ -77,6 +80,7 @@ class IotDoorPollingResponse(BaseModel): open_command : bool acces_list_counter : int time_seconds : int + force_close: bool class IotMonitorRoomInfo(BaseModel): humidity : int @@ -94,8 +98,9 @@ class IotMonitorRoomInfoTimestamped(IotMonitorRoomInfo): class DoorAccessLog(BaseModel): user_id: int - door_bluetooth_mac: str - time: datetime + iot_id: str + command: str + timestamp: datetime class Config: orm_mode = True