main: add iot functions for monitor and door
- Echo back for monitor for now. - Needs database tables and linkage Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
3302c4dc39
commit
dc4e3ef8d6
@ -46,3 +46,13 @@ def create_iot_dev_token(data: dict):
|
|||||||
to_encode = data.copy()
|
to_encode = data.copy()
|
||||||
encoded_jwt = jwt.encode(to_encode, JWT_SECRET, algorithm=JWT_ALGO)
|
encoded_jwt = jwt.encode(to_encode, JWT_SECRET, algorithm=JWT_ALGO)
|
||||||
return encoded_jwt
|
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
|
@ -11,7 +11,6 @@ from datetime import timedelta
|
|||||||
import jwt
|
import jwt
|
||||||
|
|
||||||
models.Base.metadata.create_all(bind=engine)
|
models.Base.metadata.create_all(bind=engine)
|
||||||
#oauth2_scheme = OAuth2PasswordBearer(tokenUrl="tkn")
|
|
||||||
oauth = OAuth2PasswordBearer(tokenUrl="tkn")
|
oauth = OAuth2PasswordBearer(tokenUrl="tkn")
|
||||||
|
|
||||||
app = FastAPI()
|
app = FastAPI()
|
||||||
@ -50,7 +49,6 @@ def get_current_active_user(current_user: schemas.User = Depends(get_current_use
|
|||||||
return current_user
|
return current_user
|
||||||
|
|
||||||
def get_current_iot_device(current_device: schemas.IotBluetoothMac = Depends(),
|
def get_current_iot_device(current_device: schemas.IotBluetoothMac = Depends(),
|
||||||
token: str = Depends(oauth),
|
|
||||||
db: Session = Depends(get_db)):
|
db: Session = Depends(get_db)):
|
||||||
credentials_exception = HTTPException(
|
credentials_exception = HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
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"}
|
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
|
@ -61,4 +61,22 @@ class OpenDoorRequestBase(BaseModel):
|
|||||||
bluetooth_mac: str
|
bluetooth_mac: str
|
||||||
|
|
||||||
class OpenDoorRequestTime(OpenDoorRequestBase):
|
class OpenDoorRequestTime(OpenDoorRequestBase):
|
||||||
time_minutes: int
|
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
|
Loading…
Reference in New Issue
Block a user