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 datetime import datetime
# TODO: Data we can collect or log
# - Last user connection (link to user)
# - 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.commit()
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 typing import List
from datetime import timedelta
from datetime import timedelta, datetime
import jwt
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:
if dev.bluetooth_mac == device.bluetooth_mac:
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
raise err
@ -222,9 +226,9 @@ def polling_method_for_iot_entity(request: schemas.IotDoorPollingRequest,
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")
if not device:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials")
crud.record_room_sensor_data(db, 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 .database import Base
@ -31,3 +31,21 @@ class UserAuthToIoTDev(Base):
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)
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 pydantic import BaseModel
from datetime import datetime
class IotEntityBase(BaseModel):
@ -79,4 +80,18 @@ class IotMonitorRoomInfo(BaseModel):
people : int
temperature : 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