users: Add open door command from user application

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-04-18 01:43:12 +03:00
parent d8bb92a48a
commit 34f27c32e8
Signed by: Hesham
GPG Key ID: 74876157D199B09E
5 changed files with 39 additions and 4 deletions

View File

@ -8,6 +8,6 @@
- Open for 1H
- Open to 1:30PM
- Set schedual
- [ ] Issue door open command
- [ ] Make functions to gen a IotEntity token
- [X] Issue door open command
- [X] Make functions to gen a IotEntity token
- [ ] Save prefix of token to allow/revoke token??

View File

@ -4,6 +4,11 @@ from sqlalchemy.orm import Session
from . import models, schemas, crypto, auth_helper
# TODO: Data we can collect or log
# - Last user connection (link to user)
# - Last Iot Entity Connection (link to IotEntity)
# - Any open request (link to user)
# - Any polling from IotEntity? Maybe to much data
def get_user(db: Session, user_id: int):
return db.query(models.User).filter(models.User.id == user_id).first()
@ -55,4 +60,10 @@ def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
db.add(new_link)
db.commit()
db.refresh(new_link)
return True
def set_open_door_request(db: Session, iot_entity_id: int):
device = get_iot_entity(db, iot_entity_id)
setattr(device, "open_request", True)
db.refresh(device)
return True

View File

@ -173,6 +173,22 @@ def get_iot_access_list_for_user(db: Session = Depends(get_db), current_user: sc
user = crud.get_user_by_username(db, current_user.username)
return user.authorized_devices
@app.post("/users/open",tags=['Users'])
def issue_open_door_command(command: schemas.OpenDoorRequestBase,
db: Session = Depends(get_db),
current_user: schemas.User = Depends(get_current_active_user)):
err = HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,
detail="Unauthrized to open")
device = crud.get_iot_entity_by_bluetooth_mac(db, command.bluetooth_mac)
if not device: raise err
# TODO: Use database search rather then this linear search
user = crud.get_user(db, current_user.id)
for dev in user.authorized_devices:
if dev.bluetooth_mac == device.bluetooth_mac:
crud.set_open_door_request(db, device.id)
return device
raise err
@app.post("/users/tkn", response_model=schemas.Token, tags=['Users'])
@app.post("/tkn", response_model=schemas.Token, tags=['Users'])
def login_for_access_token(form_data: OAuth2PasswordRequestForm = Depends(), db: Session = Depends(get_db)):

View File

@ -23,7 +23,7 @@ class IotEntity(Base):
id = Column(Integer, primary_key=True, index=True)
bluetooth_mac = Column(String(512))
description = Column(String(512))
open_request = Column(Boolean, default=False)
authorized_users = relationship("User", secondary= 'user_iot_link')
class UserAuthToIoTDev(Base):

View File

@ -23,6 +23,7 @@ class IotEntity(IotEntityBase):
description: str
bluetooth_mac: str
#authorized_users: List[User] = []
open_request: bool # Flag to open
class Config:
orm_mode = True
@ -53,4 +54,11 @@ class UserAllowForIotEntityRequestByID(BaseModel):
class UserAllowForIotEntityRequestByUsername(BaseModel):
username: str
description: str
description: str
class OpenDoorRequestBase(BaseModel):
username: str
bluetooth_mac: str
class OpenDoorRequestTime(OpenDoorRequestBase):
time_minutes: int