Initial commit: ICU Patient Display Management System
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException, status
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
from sqlalchemy.future import select
|
||||
from typing import List
|
||||
|
||||
from app.core.database import get_db
|
||||
from app.models.user import User
|
||||
from app.core.dependencies import get_current_user
|
||||
from app.schemas.display import DisplayCreate, DisplayUpdate, DisplayResponse, ESP32Heartbeat, ESP32HeartbeatResponse, ESP32DisplayData
|
||||
from app.services.display_service import display_service
|
||||
from app.repositories.display_repo import display_repo
|
||||
|
||||
from app.models.room import ICURoom
|
||||
from app.models.patient import Patient
|
||||
from app.models.vitals import Vitals
|
||||
|
||||
router = APIRouter(tags=["displays"])
|
||||
|
||||
@router.get("/api/displays", response_model=List[DisplayResponse])
|
||||
async def read_displays(
|
||||
skip: int = 0, limit: int = 100,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
return await display_repo.get_all(db, skip=skip, limit=limit)
|
||||
|
||||
@router.post("/api/displays", response_model=DisplayResponse)
|
||||
async def register_device(
|
||||
device_in: DisplayCreate,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user)
|
||||
):
|
||||
return await display_service.register_device(db, obj_in=device_in)
|
||||
|
||||
@router.post("/api/device/heartbeat", response_model=ESP32HeartbeatResponse)
|
||||
async def device_heartbeat(
|
||||
heartbeat: ESP32Heartbeat,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
try:
|
||||
pending_update = await display_service.process_heartbeat(db, heartbeat)
|
||||
return {"pending_update": pending_update}
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=401, detail=str(e))
|
||||
|
||||
@router.get("/api/display/{display_id}", response_model=List[ESP32DisplayData])
|
||||
async def get_display_data(
|
||||
display_id: str,
|
||||
db: AsyncSession = Depends(get_db)
|
||||
):
|
||||
device = await display_repo.get_by_device_id(db, display_id)
|
||||
if not device:
|
||||
raise HTTPException(status_code=404, detail="Device not found")
|
||||
|
||||
device.pending_update = False
|
||||
await db.commit()
|
||||
|
||||
# Fetch up to 5 most recently updated patients
|
||||
patient_result = await db.execute(
|
||||
select(Patient).order_by(Patient.last_updated.desc()).limit(5)
|
||||
)
|
||||
patients = patient_result.scalars().all()
|
||||
|
||||
if not patients:
|
||||
return [ESP32DisplayData(
|
||||
patient_name="No Patients Found",
|
||||
status="Empty"
|
||||
)]
|
||||
|
||||
display_data_list = []
|
||||
|
||||
for patient in patients:
|
||||
# Get the Room for this patient
|
||||
room_name = "N/A"
|
||||
if patient.room_id:
|
||||
room_result = await db.execute(select(ICURoom).filter(ICURoom.id == patient.room_id))
|
||||
room = room_result.scalars().first()
|
||||
if room:
|
||||
room_name = room.room_number
|
||||
|
||||
# Get the latest Vitals for this patient
|
||||
vitals_result = await db.execute(
|
||||
select(Vitals).filter(Vitals.patient_id == patient.id).order_by(Vitals.updated_at.desc())
|
||||
)
|
||||
latest_vitals = vitals_result.scalars().first()
|
||||
|
||||
display_data_list.append(
|
||||
ESP32DisplayData(
|
||||
patient_name=patient.patient_name,
|
||||
age=patient.age,
|
||||
gender=patient.gender,
|
||||
blood_group=patient.blood_group,
|
||||
doctor=patient.doctor_name,
|
||||
room=room_name,
|
||||
heart_rate=latest_vitals.heart_rate if latest_vitals else None,
|
||||
spo2=latest_vitals.spo2 if latest_vitals else None,
|
||||
blood_pressure=latest_vitals.blood_pressure if latest_vitals else None,
|
||||
temperature=latest_vitals.temperature if latest_vitals else None,
|
||||
respiration_rate=latest_vitals.respiration_rate if latest_vitals else None,
|
||||
diagnosis=patient.diagnosis,
|
||||
status=patient.status,
|
||||
food_instructions=patient.food_instructions or "No outside food allowed",
|
||||
visiting_rules=patient.visiting_rules or "2 times visits",
|
||||
visiting_hours=patient.visiting_hours or "9 - 11 AM, 4 - 6 PM",
|
||||
restrictions=patient.restrictions or "Dogs are not allowed",
|
||||
last_updated=latest_vitals.updated_at.isoformat() if latest_vitals else patient.last_updated.isoformat() if patient.last_updated else None
|
||||
)
|
||||
)
|
||||
|
||||
return display_data_list
|
||||
Reference in New Issue
Block a user