sql_app: Introduce force_close flag for Iot Door

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-07 15:48:28 +03:00
parent 4789b2778c
commit d9f3a9da4e
Signed by: Hesham
GPG Key ID: 74876157D199B09E
4 changed files with 53 additions and 9 deletions

View File

@ -104,6 +104,20 @@ def set_open_door_request(db: Session, iot_entity_id: int, time_seconds : int):
db.refresh(device) db.refresh(device)
return True 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): def set_user_last_token(db: Session, username: str, token: str):
user : models.User = get_user_by_username(db, username) user : models.User = get_user_by_username(db, username)
user.last_token = token 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): def record_door_access_log(db: Session, entry: schemas.DoorAccessLog):
db_item = models.DoorAccessLog(user_id=entry.user_id, db_item = models.DoorAccessLog(user_id=entry.user_id,
iot_dev_bluetooth_mac=entry.door_bluetooth_mac, iot_id=entry.iot_id,
timestamp=entry.time) command=entry.command,
timestamp=entry.timestamp)
db.add(db_item) db.add(db_item)
db.commit() db.commit()
db.refresh(db_item) db.refresh(db_item)

View File

@ -98,12 +98,32 @@ def issue_open_door_command(command: schemas.OpenDoorRequestTime,
if dev.bluetooth_mac == device.bluetooth_mac: if dev.bluetooth_mac == device.bluetooth_mac:
crud.set_open_door_request(db, device.id, command.time_seconds) crud.set_open_door_request(db, device.id, command.time_seconds)
log_entry = schemas.DoorAccessLog(user_id=current_user.id, log_entry = schemas.DoorAccessLog(user_id=current_user.id,
door_bluetooth_mac=command.bluetooth_mac, iot_id=device.id,
time=datetime.now()) command="OPEN",
timestamp=datetime.now())
crud.record_door_access_log(db, log_entry) crud.record_door_access_log(db, log_entry)
return device return device
raise err 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("/users/tkn", response_model=schemas.Token, tags=['Users'])
@app.post("/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)): 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, def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest,
db: Session = Depends(get_db)): 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: if not device:
raise HTTPException( raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
@ -271,9 +291,11 @@ def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest,
response : schemas.IotDoorPollingResponse = schemas.IotDoorPollingResponse( response : schemas.IotDoorPollingResponse = schemas.IotDoorPollingResponse(
open_command=device.open_request, open_command=device.open_request,
acces_list_counter=0, acces_list_counter=0,
time_seconds=device.time_seconds) time_seconds=device.time_seconds,
force_close=device.force_close)
# Reset open_request to False # Reset open_request to False
crud.clear_open_door_request(db, device.id) crud.clear_open_door_request(db, device.id)
crud.clear_close_door_request(db, device.id)
return response return response
@app.post("/iotdevice/monitor/status", tags=['Iot']) @app.post("/iotdevice/monitor/status", tags=['Iot'])

View File

@ -27,6 +27,7 @@ class IotEntity(Base):
open_request = Column(Boolean, default=False) open_request = Column(Boolean, default=False)
time_seconds = Column(Integer, default=10) time_seconds = Column(Integer, default=10)
acces_list_counter = Column(Integer, default=0) 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") authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices")
class UserAuthToIoTDev(Base): class UserAuthToIoTDev(Base):
@ -41,7 +42,8 @@ class DoorAccessLog(Base):
entry_id = Column(Integer, primary_key=True, index=True) entry_id = Column(Integer, primary_key=True, index=True)
user_id = Column(Integer, ForeignKey('user_accounts.id')) 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) timestamp = Column(DateTime)
class RoomSensorData(Base): class RoomSensorData(Base):

View File

@ -26,6 +26,7 @@ class IotEntity(IotEntityBase):
#authorized_users: List[User] = [] #authorized_users: List[User] = []
open_request: bool # Flag to open open_request: bool # Flag to open
time_seconds: int time_seconds: int
force_close: bool
acces_list_counter: int acces_list_counter: int
class Config: class Config:
orm_mode = True orm_mode = True
@ -66,6 +67,8 @@ class OpenDoorRequestBase(BaseModel):
class OpenDoorRequestTime(OpenDoorRequestBase): class OpenDoorRequestTime(OpenDoorRequestBase):
time_seconds: int time_seconds: int
class CloseDoorRequest(OpenDoorRequestBase):
pass
# Device sends this periodcally # Device sends this periodcally
class IotDoorPollingRequest(BaseModel): class IotDoorPollingRequest(BaseModel):
bluetooth_mac : str bluetooth_mac : str
@ -77,6 +80,7 @@ class IotDoorPollingResponse(BaseModel):
open_command : bool open_command : bool
acces_list_counter : int acces_list_counter : int
time_seconds : int time_seconds : int
force_close: bool
class IotMonitorRoomInfo(BaseModel): class IotMonitorRoomInfo(BaseModel):
humidity : int humidity : int
@ -94,8 +98,9 @@ class IotMonitorRoomInfoTimestamped(IotMonitorRoomInfo):
class DoorAccessLog(BaseModel): class DoorAccessLog(BaseModel):
user_id: int user_id: int
door_bluetooth_mac: str iot_id: str
time: datetime command: str
timestamp: datetime
class Config: class Config:
orm_mode = True orm_mode = True