From fdb5be583b5a5b4a9d3ca412bf3013dd7c1d52ec Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 8 Jun 2022 13:01:47 +0300 Subject: [PATCH] sql_app: data: Endpoint to fetch sensor data Signed-off-by: HeshamTB --- sql_app/crud.py | 4 ++++ sql_app/init_db.py | 23 ++++++++++++++++++++++- sql_app/main.py | 16 +++++++++++----- 3 files changed, 37 insertions(+), 6 deletions(-) diff --git a/sql_app/crud.py b/sql_app/crud.py index fe704e3..ad2b23f 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -185,3 +185,7 @@ def record_user_connection(db: Session, user: models.User, time: datetime): db.add(entry) db.commit() db.refresh(entry) + +def get_all_sensor_data_for_room(db: Session, room_id: int): + data = db.query(models.RoomSensorData).offset(0).limit(50).all() + return data \ No newline at end of file diff --git a/sql_app/init_db.py b/sql_app/init_db.py index 27857fa..341ea1a 100644 --- a/sql_app/init_db.py +++ b/sql_app/init_db.py @@ -2,7 +2,8 @@ from . import crud, main, schemas, auth_helper from decouple import config from .database import SessionLocal -from datetime import timedelta +from datetime import timedelta, datetime +from random import randint db = SessionLocal() @@ -63,9 +64,29 @@ def init_monitor(): def init_allowance(): crud.create_user_link_to_iot(db, 1, 1) +def init_sensor_data(): + # humidity : int + # people : int + # temperature : int + # smoke_sensor_reading : int + # token: str + + for i in range(50): + room_data = \ + schemas.\ + IotMonitorRoomInfo\ + (humidity=randint(20, 80), + people=randint(0, 10), + temperature=randint(18, 27), + smoke_sensor_reading=randint(150, 700), + token='dummy') + crud.record_room_sensor_data(db, room_data) + + def init(): init_user() init_door() init_monitor() init_allowance() + init_sensor_data() \ No newline at end of file diff --git a/sql_app/main.py b/sql_app/main.py index a183260..eded5e6 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -204,7 +204,7 @@ def read_user(user_id: int, db: Session = Depends(get_db)): detail="User not found") return db_user -@app.post("/admin/users/allowdevice/id", tags=['Admin']) +@app.patch("/admin/users/allowdevice/id", tags=['Admin']) def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): user = crud.get_user(db, request.user_id) if not user: @@ -224,7 +224,7 @@ def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReques crud.increment_door_access_list_counter(db, iot_entity) return -@app.post("/admin/users/disallowdevice/id", tags=['Admin']) +@app.patch("/admin/users/disallowdevice/id", tags=['Admin']) def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)): user = crud.get_user(db, request.user_id) if not user: @@ -244,7 +244,7 @@ def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityReq crud.increment_door_access_list_counter(db, iot_entity) return -@app.post("/admin/users/allowdevice/name", tags=['Admin']) +@app.patch("/admin/users/allowdevice/name", tags=['Admin']) def allow_user_for_iot_entity_by_name(request: schemas.UserAllowForIotEntityRequestByUsername, db: Session = Depends(get_db)): user = crud.get_user_by_username(db, request.username) if not user: @@ -263,11 +263,11 @@ def allow_user_for_iot_entity_by_name(request: schemas.UserAllowForIotEntityRequ return -@app.post("/admin/users/{user_id}/deactiveate", tags=['Admin']) +@app.patch("/admin/users/{user_id}/deactiveate", tags=['Admin']) def deactiveate_user(user_id: int, db:Session = Depends(get_db)): return -@app.post("/admin/users/{user_id}/activeate", tags=['Admin']) +@app.patch("/admin/users/{user_id}/activeate", tags=['Admin']) def deactiveate_user(user_id: int, db:Session = Depends(get_db)): return @@ -304,6 +304,12 @@ def get_access_log_history_for_user(request : schemas.UserAccessLogRequestUserna def get_room_data(db: Session = Depends(get_db)): return crud.get_room_data_now(db) +@app.get("/admin/roominfo/history/sensors/{room_id}", tags=['Admin']) +def get_all_sensor_history(room_id: int, + api_key: APIKey = Depends(auth_helper.valid_api_key), + db: Session = Depends(get_db)): + return crud.get_all_sensor_data_for_room(db, room_id) + @app.post("/iotdevice/door/status", response_model=schemas.IotDoorPollingResponse, tags=['Iot']) def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest, db: Session = Depends(get_db)):