diff --git a/sql_app/crud.py b/sql_app/crud.py index cb89580..3c16713 100644 --- a/sql_app/crud.py +++ b/sql_app/crud.py @@ -239,3 +239,15 @@ def update_user_status(db: Session, user: models.User, state: bool): db.add(user) db.commit() 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) diff --git a/sql_app/main.py b/sql_app/main.py index 17a4510..f089f16 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -6,7 +6,7 @@ from sqlalchemy.orm import Session from . import crud, models, schemas, auth_helper, init_db from .database import SessionLocal, engine -from .utils import get_db +from .utils import get_db, EMERG_SMOKE, EMERG_TEMP from typing import List from datetime import timedelta, datetime @@ -400,6 +400,11 @@ def polling_method_for_room_monitor(request: schemas.MonitorUpdateReadings, status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials") 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) return request diff --git a/sql_app/models.py b/sql_app/models.py index 168da0f..831ba73 100644 --- a/sql_app/models.py +++ b/sql_app/models.py @@ -69,7 +69,6 @@ class DoorAccessLog(Base): 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) @@ -87,4 +86,14 @@ class UserConnectionHistory(Base): timestamp = Column(DateTime) # 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. \ No newline at end of file diff --git a/sql_app/utils.py b/sql_app/utils.py index c8ccf88..ea7648d 100644 --- a/sql_app/utils.py +++ b/sql_app/utils.py @@ -6,4 +6,7 @@ def get_db(): try: yield db finally: - db.close() \ No newline at end of file + db.close() + +EMERG_TEMP = 50 +EMERG_SMOKE = 1000 \ No newline at end of file