users: Add open door command from user application
Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
parent
d8bb92a48a
commit
34f27c32e8
@ -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??
|
@ -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()
|
||||
@ -56,3 +61,9 @@ def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
|
||||
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
|
@ -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)):
|
||||
|
@ -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):
|
||||
|
@ -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
|
||||
|
||||
@ -54,3 +55,10 @@ class UserAllowForIotEntityRequestByID(BaseModel):
|
||||
class UserAllowForIotEntityRequestByUsername(BaseModel):
|
||||
username: str
|
||||
description: str
|
||||
|
||||
class OpenDoorRequestBase(BaseModel):
|
||||
username: str
|
||||
bluetooth_mac: str
|
||||
|
||||
class OpenDoorRequestTime(OpenDoorRequestBase):
|
||||
time_minutes: int
|
Loading…
Reference in New Issue
Block a user