Added data storage tables and types

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-05-23 13:46:09 +03:00
parent 3bf883b6ac
commit bae51c3f67
4 changed files with 66 additions and 9 deletions

View File

@ -4,6 +4,8 @@ from sqlalchemy.orm import Session
from . import models, schemas, crypto, auth_helper from . import models, schemas, crypto, auth_helper
from datetime import datetime
# TODO: Data we can collect or log # TODO: Data we can collect or log
# - Last user connection (link to user) # - Last user connection (link to user)
# - Last Iot Entity Connection (link to IotEntity) # - Last Iot Entity Connection (link to IotEntity)
@ -79,4 +81,22 @@ def set_open_door_request(db: Session, iot_entity_id: int):
db.add(device) db.add(device)
db.commit() db.commit()
db.refresh(device) db.refresh(device)
return True return True
def record_door_access_log(db: Session, entry: schemas.DoorAccessLog):
db_item = models.DoorAccessLog(user_id=entry.user_id,
iot_dev_bluetooth_mac=entry.door_bluetooth_mac,
timestamp=entry.time)
db.add(db_item)
db.commit()
db.refresh(db_item)
def record_room_sensor_data(db: Session, entry: schemas.IotMonitorRoomInfo):
db_item = models.RoomSensorData(humidity=entry.humidity,
people=entry.people,
temperature=entry.temperature,
smoke_sensor_reading=entry.smoke_sensor_reading,
timestamp=datetime.now())
db.add(db_item)
db.commit()
db.refresh(db_item)

View File

@ -7,7 +7,7 @@ from . import crud, models, schemas, auth_helper
from .database import SessionLocal, engine from .database import SessionLocal, engine
from typing import List from typing import List
from datetime import timedelta from datetime import timedelta, datetime
import jwt import jwt
models.Base.metadata.create_all(bind=engine) models.Base.metadata.create_all(bind=engine)
@ -182,6 +182,10 @@ def issue_open_door_command(command: schemas.OpenDoorRequestBase,
for dev in user.authorized_devices: for dev in user.authorized_devices:
if dev.bluetooth_mac == device.bluetooth_mac: if dev.bluetooth_mac == device.bluetooth_mac:
crud.set_open_door_request(db, device.id) crud.set_open_door_request(db, device.id)
log_entry = schemas.DoorAccessLog(user_id=current_user.id,
door_bluetooth_mac=command.bluetooth_mac,
time=datetime.now())
crud.record_door_access_log(db, log_entry)
return device return device
raise err raise err
@ -222,9 +226,9 @@ def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest,
def polling_method_for_room_monitor(request: schemas.IotMonitorRoomInfo, def polling_method_for_room_monitor(request: schemas.IotMonitorRoomInfo,
db: Session = Depends(get_db)): db: Session = Depends(get_db)):
device : schemas.IotEntity = auth_helper.valid_iot_token(request.token, db) device : schemas.IotEntity = auth_helper.valid_iot_token(request.token, db)
# if not device: if not device:
# raise HTTPException( raise HTTPException(
# status_code=status.HTTP_401_UNAUTHORIZED, status_code=status.HTTP_401_UNAUTHORIZED,
# detail="Could not validate credentials") detail="Could not validate credentials")
crud.record_room_sensor_data(db, request)
return request return request

View File

@ -1,4 +1,4 @@
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.orm import relationship from sqlalchemy.orm import relationship
from .database import Base from .database import Base
@ -31,3 +31,21 @@ class UserAuthToIoTDev(Base):
user_id = Column(Integer, ForeignKey('user_accounts.id'), primary_key=True, index=True) user_id = Column(Integer, ForeignKey('user_accounts.id'), primary_key=True, index=True)
iot_entity_id = Column(Integer, ForeignKey('iot_entities.id'), primary_key=True, index=True) iot_entity_id = Column(Integer, ForeignKey('iot_entities.id'), primary_key=True, index=True)
class DoorAccessLog(Base):
__tablename__ = "door_access_log"
user_id = Column(Integer, ForeignKey('user_accounts.id'), primary_key=True, index=True)
iot_dev_bluetooth_mac = Column(Integer, ForeignKey('iot_entities.id'), primary_key=True, index=True)
timestamp = Column(DateTime)
class RoomSensorData(Base):
__tablename__ = "room_sensor_data"
# Data is now not related to a room. We should have a construct for rooms
reading_id = Column(Integer, primary_key=True, index=True)
humidity = Column(Integer)
people = Column(Integer)
temperature = Column(Integer)
smoke_sensor_reading = Column(Integer)
timestamp = Column(DateTime)

View File

@ -1,6 +1,7 @@
from typing import Any, List, Optional from typing import Any, List, Optional
from pydantic import BaseModel from pydantic import BaseModel
from datetime import datetime
class IotEntityBase(BaseModel): class IotEntityBase(BaseModel):
@ -79,4 +80,18 @@ class IotMonitorRoomInfo(BaseModel):
people : int people : int
temperature : int temperature : int
smoke_sensor_reading : int smoke_sensor_reading : int
token: str token: str
class Config:
orm_mode = True
class IotMonitorRoomInfoTimestamped(IotMonitorRoomInfo):
time: datetime
class Config:
orm_mode = True
class DoorAccessLog(BaseModel):
user_id: int
door_bluetooth_mac: str
time: datetime
class Config:
orm_mode = True