sql_app: data: Endpoint to fetch sensor data

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-08 13:01:47 +03:00
parent c2048d8dba
commit fdb5be583b
3 changed files with 37 additions and 6 deletions

View File

@ -185,3 +185,7 @@ def record_user_connection(db: Session, user: models.User, time: datetime):
db.add(entry) db.add(entry)
db.commit() db.commit()
db.refresh(entry) 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

View File

@ -2,7 +2,8 @@
from . import crud, main, schemas, auth_helper from . import crud, main, schemas, auth_helper
from decouple import config from decouple import config
from .database import SessionLocal from .database import SessionLocal
from datetime import timedelta from datetime import timedelta, datetime
from random import randint
db = SessionLocal() db = SessionLocal()
@ -63,9 +64,29 @@ def init_monitor():
def init_allowance(): def init_allowance():
crud.create_user_link_to_iot(db, 1, 1) 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(): def init():
init_user() init_user()
init_door() init_door()
init_monitor() init_monitor()
init_allowance() init_allowance()
init_sensor_data()

View File

@ -204,7 +204,7 @@ def read_user(user_id: int, db: Session = Depends(get_db)):
detail="User not found") detail="User not found")
return db_user 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)): def allow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)):
user = crud.get_user(db, request.user_id) user = crud.get_user(db, request.user_id)
if not user: 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) crud.increment_door_access_list_counter(db, iot_entity)
return 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)): def disallow_user_for_iot_entity_by_id(request: schemas.UserAllowForIotEntityRequestByID, db: Session = Depends(get_db)):
user = crud.get_user(db, request.user_id) user = crud.get_user(db, request.user_id)
if not user: 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) crud.increment_door_access_list_counter(db, iot_entity)
return 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)): 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) user = crud.get_user_by_username(db, request.username)
if not user: if not user:
@ -263,11 +263,11 @@ def allow_user_for_iot_entity_by_name(request: schemas.UserAllowForIotEntityRequ
return 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)): def deactiveate_user(user_id: int, db:Session = Depends(get_db)):
return 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)): def deactiveate_user(user_id: int, db:Session = Depends(get_db)):
return return
@ -304,6 +304,12 @@ def get_access_log_history_for_user(request : schemas.UserAccessLogRequestUserna
def get_room_data(db: Session = Depends(get_db)): def get_room_data(db: Session = Depends(get_db)):
return crud.get_room_data_now(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']) @app.post("/iotdevice/door/status", response_model=schemas.IotDoorPollingResponse, tags=['Iot'])
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)):