From dc4e3ef8d63aa47bcaa6ee21463dcfc1f0e9cdd3 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Sun, 8 May 2022 22:09:06 +0300 Subject: [PATCH] main: add iot functions for monitor and door - Echo back for monitor for now. - Needs database tables and linkage Signed-off-by: HeshamTB --- sql_app/auth_helper.py | 10 ++++++++++ sql_app/main.py | 27 +++++++++++++++++++++++++-- sql_app/schemas.py | 20 +++++++++++++++++++- 3 files changed, 54 insertions(+), 3 deletions(-) diff --git a/sql_app/auth_helper.py b/sql_app/auth_helper.py index bbc75d2..5f41f4a 100644 --- a/sql_app/auth_helper.py +++ b/sql_app/auth_helper.py @@ -46,3 +46,13 @@ def create_iot_dev_token(data: dict): to_encode = data.copy() encoded_jwt = jwt.encode(to_encode, JWT_SECRET, algorithm=JWT_ALGO) return encoded_jwt + +def valid_iot_token(token : str, db: Session): + try: + payload = jwt.decode(token, JWT_SECRET, algorithms=[JWT_ALGO]) + except jwt.DecodeError: + return None + + mac_signed = payload.get("bluetooth_mac") + device = crud.get_iot_entity_by_bluetooth_mac(db, mac_signed) + return device \ No newline at end of file diff --git a/sql_app/main.py b/sql_app/main.py index 7b50911..ec56b6b 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -11,7 +11,6 @@ from datetime import timedelta import jwt models.Base.metadata.create_all(bind=engine) -#oauth2_scheme = OAuth2PasswordBearer(tokenUrl="tkn") oauth = OAuth2PasswordBearer(tokenUrl="tkn") app = FastAPI() @@ -50,7 +49,6 @@ def get_current_active_user(current_user: schemas.User = Depends(get_current_use return current_user def get_current_iot_device(current_device: schemas.IotBluetoothMac = Depends(), - token: str = Depends(oauth), db: Session = Depends(get_db)): credentials_exception = HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, @@ -204,4 +202,29 @@ def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: return {"access_token": access_token, "token_type": "bearer"} +@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)): + # We need db session + # API schema request + # API schema response + device: schemas.IotEntity = auth_helper.valid_iot_token(request.token, db) + if not device: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Could not validate credentials") + + response : schemas.IotEntityPollingResponse(open_command=device.open_request, + acces_list_counter=0) + return response +@app.post("/iotdevice/monitor/status", tags=['Iot']) +def polling_method_for_room_monitor(request: schemas.IotMonitorRoomInfo, + db: Session = Depends(get_db)): + device : schemas.IotEntity = auth_helper.valid_iot_token(request.token, db) + if not device: + raise HTTPException( + status_code=status.HTTP_401_UNAUTHORIZED, + detail="Could not validate credentials") + + return request \ No newline at end of file diff --git a/sql_app/schemas.py b/sql_app/schemas.py index f4bc6db..59753d0 100644 --- a/sql_app/schemas.py +++ b/sql_app/schemas.py @@ -61,4 +61,22 @@ class OpenDoorRequestBase(BaseModel): bluetooth_mac: str class OpenDoorRequestTime(OpenDoorRequestBase): - time_minutes: int \ No newline at end of file + time_minutes: int + +# Device sends this periodcally +class IotDoorPollingRequest(BaseModel): + bluetooth_mac : str + token : str + class Config: + orm_mode = True + +class IotDoorPollingResponse(BaseModel): + open_command : bool + acces_list_counter : int + +class IotMonitorRoomInfo(BaseModel): + humidity : int + people : int + tempreture : int + smoke_sensor_reading : int + token: str \ No newline at end of file