sql_app: main: User access list now includes room sensor data

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-07 12:56:10 +03:00
parent 317b3825c3
commit bf14c97cb6
Signed by: Hesham
GPG Key ID: 74876157D199B09E
3 changed files with 27 additions and 4 deletions

View File

@ -40,7 +40,7 @@ def get_access_log_for_door_by_door_mac(db: Session, bluetooth_mac : str):
def get_access_log_for_user_by_id(db: Session, id : str):
return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.user_id == id).all()
def get_room_data_now(db: Session):
def get_room_data_now(db: Session) -> models.RoomSensorData:
return db.query(models.RoomSensorData)[-1]
def create_user(db: Session, user: schemas.UserCreate):
@ -144,7 +144,6 @@ def record_room_sensor_data(db: Session, entry: schemas.IotMonitorRoomInfo):
db.refresh(db_item)
def increment_door_access_list_counter(db: Session, iot_entity: models.IotEntity):
#setattr(iot_entity, "acces_list_counter", iot_entity.acces_list_counter + 1)
iot_entity.acces_list_counter = iot_entity.acces_list_counter + 1
db.add(iot_entity)
db.commit()

View File

@ -196,10 +196,28 @@ def get_access_log_history_for_user(request : schemas.UserAccessLogRequestUserna
if not user: raise HTTPException(status_code=404, detail="User not found")
return crud.get_access_log_for_user_by_id(db, user.id)
@app.get("/users/acesslist/", response_model=List[schemas.IotEntity], tags=['Users'])
@app.get("/users/acesslist/", response_model=List[schemas.RoomOverview], tags=['Users'])
def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: schemas.User = Depends(get_current_active_user)):
user = crud.get_user_by_username(db, current_user.username)
return user.authorized_devices
access_list = list()
for device in user.authorized_devices:
dev_db : models.IotEntity = device
sensors = crud.get_room_data_now(db)
if not sensors: raise HTTPException(status_code=500, detail="No Room link")
entry : schemas.RoomOverview = schemas.RoomOverview(
id=dev_db.id,
description=dev_db.description,
bluetooth_mac=dev_db.bluetooth_mac,
open_request=dev_db.open_request,
time_seconds=dev_db.time_seconds,
acces_list_counter=dev_db.acces_list_counter,
humidity=sensors.humidity,
people=sensors.people,
temperature=sensors.temperature,
smoke_sensor_reading=sensors.smoke_sensor_reading
)
access_list.append(entry)
return access_list
@app.get("/admin/roominfo/now/", tags=['Admin'])
def get_room_data(db: Session = Depends(get_db)):

View File

@ -110,3 +110,9 @@ class UserAccessLogRequestEmail(BaseModel):
class UserAccessLogRequestID(BaseModel):
id : int
class RoomOverview(IotEntity):
humidity : int
people : int
temperature : int
smoke_sensor_reading : int