sql_app: Respond to emergency events record db entry

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-11 18:05:52 +03:00
parent e1a7c4023b
commit 20dfa6dcc4
4 changed files with 32 additions and 3 deletions

View File

@ -239,3 +239,15 @@ def update_user_status(db: Session, user: models.User, state: bool):
db.add(user) db.add(user)
db.commit() db.commit()
db.refresh(user) db.refresh(user)
def record_emergancy_entry(db: Session, monitor_data: schemas.MonitorUpdateReadings, monitor_id: int):
new_entry : models.EmergancyNotice = models.EmergancyNotice(
monitor_id=monitor_id,
people=monitor_data.people,
temperature=monitor_data.temperature,
smoke_sensor_reading=monitor_data.smoke_sensor_reading,
timestamp=datetime.now()
)
db.add(new_entry)
db.commit()
db.refresh(new_entry)

View File

@ -6,7 +6,7 @@ from sqlalchemy.orm import Session
from . import crud, models, schemas, auth_helper, init_db from . import crud, models, schemas, auth_helper, init_db
from .database import SessionLocal, engine from .database import SessionLocal, engine
from .utils import get_db from .utils import get_db, EMERG_SMOKE, EMERG_TEMP
from typing import List from typing import List
from datetime import timedelta, datetime from datetime import timedelta, datetime
@ -400,6 +400,11 @@ def polling_method_for_room_monitor(request: schemas.MonitorUpdateReadings,
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, device) crud.record_room_sensor_data(db, request, device)
if request.temperature >= EMERG_TEMP or request.smoke_sensor_reading >= EMERG_SMOKE:
crud.record_emergancy_entry(db, request, device.id)
print("********EMERGENCY AT %s********" % device.description)
# Call into a hook to notify with room and people
print(request) print(request)
return request return request

View File

@ -69,7 +69,6 @@ class DoorAccessLog(Base):
class RoomSensorData(Base): class RoomSensorData(Base):
__tablename__ = "room_sensor_data" __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) reading_id = Column(Integer, primary_key=True, index=True)
humidity = Column(Integer) humidity = Column(Integer)
people = Column(Integer) people = Column(Integer)
@ -87,4 +86,14 @@ class UserConnectionHistory(Base):
timestamp = Column(DateTime) timestamp = Column(DateTime)
# TODO: add ip # TODO: add ip
class EmergancyNotice(Base):
__tablename__ = "emergency_notice"
id = Column(Integer, primary_key=True)
monitor_id = Column(Integer, ForeignKey("monitors.id"), index=True)
people = Column(Integer)
temperature = Column(Integer)
smoke_sensor_reading = Column(Integer)
timestamp = Column(DateTime)
# TODO: Add table to store active sessions. May periodically clear. # TODO: Add table to store active sessions. May periodically clear.

View File

@ -7,3 +7,6 @@ def get_db():
yield db yield db
finally: finally:
db.close() db.close()
EMERG_TEMP = 50
EMERG_SMOKE = 1000