2022-03-03 13:49:26 +01:00
|
|
|
# CRUD (Create, Read, Update, Delete) from db
|
|
|
|
|
2022-06-05 15:25:20 +02:00
|
|
|
from sqlalchemy import select, join
|
2022-03-03 13:49:26 +01:00
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
2022-04-13 04:24:06 +02:00
|
|
|
from . import models, schemas, crypto, auth_helper
|
2022-03-03 13:49:26 +01:00
|
|
|
|
2022-05-23 12:46:09 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
2022-04-18 00:43:12 +02:00
|
|
|
# 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
|
2022-03-03 13:49:26 +01:00
|
|
|
|
2022-06-05 15:25:20 +02:00
|
|
|
def get_user(db: Session, user_id: int) -> models.User:
|
2022-04-18 00:54:33 +02:00
|
|
|
return db.query(models.User).get(user_id)
|
2022-03-03 13:49:26 +01:00
|
|
|
|
2022-04-13 07:34:12 +02:00
|
|
|
def get_iot_entity(db: Session, id: int):
|
2022-04-18 01:39:45 +02:00
|
|
|
return db.query(models.IotEntity).get(id)
|
2022-03-03 13:49:26 +01:00
|
|
|
|
2022-04-14 00:37:30 +02:00
|
|
|
def get_iot_entity_by_description(db: Session, description: str):
|
|
|
|
return db.query(models.IotEntity).filter(models.IotEntity.description == description).first()
|
|
|
|
|
2022-06-05 15:25:20 +02:00
|
|
|
def get_iot_entity_by_bluetooth_mac(db: Session, bluetooth_mac: str) -> models.IotEntity:
|
2022-04-17 20:34:50 +02:00
|
|
|
return db.query(models.IotEntity).filter(models.IotEntity.bluetooth_mac == bluetooth_mac).first()
|
|
|
|
|
2022-06-05 15:25:20 +02:00
|
|
|
def get_user_by_email(db: Session, email: str) -> models.User:
|
2022-03-03 13:49:26 +01:00
|
|
|
return db.query(models.User).filter(models.User.email == email).first()
|
|
|
|
|
2022-06-05 15:25:20 +02:00
|
|
|
def get_user_by_username(db: Session, username: str) -> models.User:
|
2022-04-04 01:02:29 +02:00
|
|
|
return db.query(models.User).filter(models.User.username == username).first()
|
|
|
|
|
2022-03-03 13:49:26 +01:00
|
|
|
def get_users(db: Session, skip: int = 0, limit: int = 100):
|
|
|
|
return db.query(models.User).offset(skip).limit(limit).all()
|
|
|
|
|
2022-05-23 18:02:54 +02:00
|
|
|
def get_access_log_for_door_by_door_mac(db: Session, bluetooth_mac : str):
|
|
|
|
return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.iot_dev_bluetooth_mac == bluetooth_mac).all()
|
|
|
|
|
2022-05-23 18:44:04 +02:00
|
|
|
def get_access_log_for_user_by_id(db: Session, id : str):
|
|
|
|
return db.query(models.DoorAccessLog).filter(models.DoorAccessLog.user_id == id).all()
|
2022-05-23 18:02:54 +02:00
|
|
|
|
2022-06-07 11:56:10 +02:00
|
|
|
def get_room_data_now(db: Session) -> models.RoomSensorData:
|
2022-05-23 20:04:19 +02:00
|
|
|
return db.query(models.RoomSensorData)[-1]
|
|
|
|
|
2022-03-03 13:49:26 +01:00
|
|
|
def create_user(db: Session, user: schemas.UserCreate):
|
2022-03-16 20:46:50 +01:00
|
|
|
key = crypto.gen_new_key(user.password)
|
|
|
|
salt = key[1]
|
|
|
|
hashed_pass = key[0]
|
2022-06-05 15:25:20 +02:00
|
|
|
db_user = models.User(email=user.email,
|
|
|
|
username=user.username,
|
|
|
|
hashed_password=hashed_pass,
|
|
|
|
passwd_salt=salt)
|
2022-03-03 13:49:26 +01:00
|
|
|
db.add(db_user)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(db_user)
|
|
|
|
return db_user
|
|
|
|
|
2022-06-08 11:00:12 +02:00
|
|
|
def update_user_password(db: Session, user: models.User, request: schemas.UserUpdatePassword):
|
|
|
|
key = crypto.gen_new_key(request.password)
|
|
|
|
salt = key[1]
|
|
|
|
hashed_pass = key[0]
|
|
|
|
user.passwd_salt = salt
|
|
|
|
user.hashed_password = hashed_pass
|
|
|
|
db.add(user)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(user)
|
|
|
|
|
2022-03-03 13:49:26 +01:00
|
|
|
def get_iot_entities(db: Session, skip: int = 0, limit: int = 100):
|
|
|
|
return db.query(models.IotEntity).offset(skip).limit(limit).all()
|
|
|
|
|
|
|
|
|
2022-04-13 07:34:12 +02:00
|
|
|
def create_iot_entity(db: Session, iot_entity: schemas.IotEntityCreate):
|
2022-04-13 08:39:25 +02:00
|
|
|
db_item = models.IotEntity(bluetooth_mac=iot_entity.bluetooth_mac,
|
|
|
|
description=iot_entity.description)
|
2022-03-03 13:49:26 +01:00
|
|
|
db.add(db_item)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(db_item)
|
|
|
|
return db_item
|
2022-04-13 07:34:12 +02:00
|
|
|
|
|
|
|
def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
|
|
|
|
# Ensure link is not already present and it does not allow duplicates
|
2022-06-05 16:10:35 +02:00
|
|
|
link = db.query(models.UserAuthToIoTDev).filter(models.UserAuthToIoTDev.user_id == user_id).filter(models.UserAuthToIoTDev.iot_id == iot_dev_id).first()
|
2022-04-18 01:39:45 +02:00
|
|
|
if link: return True
|
2022-06-05 16:10:35 +02:00
|
|
|
new_link = models.UserAuthToIoTDev(user_id=user_id,
|
|
|
|
iot_id=iot_dev_id,
|
|
|
|
timestamp=datetime.now())
|
2022-04-13 07:34:12 +02:00
|
|
|
db.add(new_link)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(new_link)
|
2022-04-18 00:43:12 +02:00
|
|
|
return True
|
|
|
|
|
2022-04-18 01:39:45 +02:00
|
|
|
def remove_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
|
|
|
|
# Ensure link is not already present and it does not allow duplicates
|
2022-06-06 15:33:02 +02:00
|
|
|
link = (db.query(models.UserAuthToIoTDev)
|
|
|
|
.filter(models.UserAuthToIoTDev.user_id == user_id)
|
|
|
|
.filter(models.UserAuthToIoTDev.iot_id == iot_dev_id)
|
|
|
|
.first())
|
2022-04-18 01:39:45 +02:00
|
|
|
if not link: return True
|
|
|
|
db.delete(link)
|
|
|
|
db.flush()
|
|
|
|
db.commit()
|
|
|
|
#db.refresh(link)
|
|
|
|
return True
|
|
|
|
|
2022-05-23 13:58:30 +02:00
|
|
|
def set_open_door_request(db: Session, iot_entity_id: int, time_seconds : int):
|
2022-04-18 00:43:12 +02:00
|
|
|
device = get_iot_entity(db, iot_entity_id)
|
2022-04-18 00:48:44 +02:00
|
|
|
setattr(device, "open_request", True)
|
2022-05-23 13:58:30 +02:00
|
|
|
if time_seconds < 1:
|
|
|
|
time_seconds = 10 # Magic number move to global constant
|
|
|
|
setattr(device, "time_seconds", time_seconds)
|
2022-04-18 00:48:44 +02:00
|
|
|
db.add(device)
|
|
|
|
db.commit()
|
2022-04-18 00:43:12 +02:00
|
|
|
db.refresh(device)
|
2022-05-23 12:46:09 +02:00
|
|
|
return True
|
|
|
|
|
2022-06-07 14:48:28 +02:00
|
|
|
def set_close_door_request(db: Session, iot_id: int):
|
|
|
|
device : models.IotEntity = get_iot_entity(db, iot_id)
|
|
|
|
device.force_close = True
|
|
|
|
db.add(device)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(device)
|
|
|
|
return True
|
|
|
|
|
|
|
|
def clear_close_door_request(db: Session, iot_id: int):
|
|
|
|
device : models.IotEntity = get_iot_entity(db, iot_id)
|
|
|
|
device.force_close = False
|
|
|
|
db.add(device)
|
|
|
|
db.commit()
|
|
|
|
|
2022-06-05 15:25:20 +02:00
|
|
|
def set_user_last_token(db: Session, username: str, token: str):
|
|
|
|
user : models.User = get_user_by_username(db, username)
|
|
|
|
user.last_token = token
|
|
|
|
db.add(user)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(user)
|
|
|
|
return True
|
|
|
|
|
2022-06-07 17:58:23 +02:00
|
|
|
def set_door_state(db: Session, iot_device: models.IotEntity, state: bool):
|
|
|
|
iot_device.state = state
|
|
|
|
db.add(iot_device)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(iot_device)
|
|
|
|
|
2022-06-05 15:25:20 +02:00
|
|
|
def get_user_last_token(db: Session, username: str):
|
|
|
|
user : models.User = get_user_by_username(db, username)
|
|
|
|
return user.last_token # This method is bad security practice.
|
|
|
|
|
2022-05-23 13:36:29 +02:00
|
|
|
def clear_open_door_request(db: Session, iot_entity_id: int):
|
|
|
|
device = get_iot_entity(db, iot_entity_id)
|
|
|
|
setattr(device, "open_request", False)
|
2022-05-23 13:58:30 +02:00
|
|
|
setattr(device, "time_seconds", 10)
|
2022-05-23 13:36:29 +02:00
|
|
|
db.add(device)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(device)
|
|
|
|
return True
|
|
|
|
|
2022-05-23 12:46:09 +02:00
|
|
|
def record_door_access_log(db: Session, entry: schemas.DoorAccessLog):
|
|
|
|
db_item = models.DoorAccessLog(user_id=entry.user_id,
|
2022-06-07 14:48:28 +02:00
|
|
|
iot_id=entry.iot_id,
|
|
|
|
command=entry.command,
|
|
|
|
timestamp=entry.timestamp)
|
2022-05-23 12:46:09 +02:00
|
|
|
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()
|
2022-06-06 15:38:35 +02:00
|
|
|
db.refresh(db_item)
|
|
|
|
|
|
|
|
def increment_door_access_list_counter(db: Session, iot_entity: models.IotEntity):
|
|
|
|
iot_entity.acces_list_counter = iot_entity.acces_list_counter + 1
|
|
|
|
db.add(iot_entity)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(iot_entity)
|
2022-06-07 15:11:39 +02:00
|
|
|
|
|
|
|
def record_user_connection(db: Session, user: models.User, time: datetime):
|
|
|
|
entry = models.UserConnectionHistory(user_id=user.id, timestamp=time)
|
|
|
|
db.add(entry)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(entry)
|