diff --git a/app.py b/app.py index 5ff3837..c922fd3 100644 --- a/app.py +++ b/app.py @@ -151,8 +151,15 @@ async def stop_mqtt_monitor(): #ResLevis CORE middleware @app.middleware("http") async def local_then_core(request: Request, call_next): + internal_core_proxy_paths = { + "/reslevis/updateAlarm", + } # only proxy CRUD for Reslevis (change prefix or methods if needed) - if request.url.path.startswith("/reslevis/") and request.method in {"POST", "PUT", "DELETE", "PATCH"}: + if ( + request.url.path.startswith("/reslevis/") + and request.method in {"POST", "PUT", "DELETE", "PATCH"} + and request.url.path not in internal_core_proxy_paths + ): body = await request.body() # raw body preserved local_resp = await call_next(request) # local storage runs here if local_resp.status_code >= 400: diff --git a/routes/reslevis.py b/routes/reslevis.py index 7fe2871..8efc3b4 100644 --- a/routes/reslevis.py +++ b/routes/reslevis.py @@ -17,6 +17,8 @@ from schemas.reslevis import ( OperatorItem, SubjectItem, AlarmItem, + AlarmCoreItem, + AlarmStatusUpdateItem, TrackItem, TrackHistoryItem, TrackerZoneItem, @@ -40,6 +42,7 @@ from security import get_current_user #CORE SYNC CORE_BASE_URL = config_env.CORE_API_URL.rstrip("/") +ALERTS_CORE_BASE_URL = "http://localhost:1902" TRACKS_CORE_BASE_URL = "http://localhost:1902" CORE_TIMEOUT = 2.0 # secondi @@ -456,6 +459,56 @@ def removeTrack(track_id: str): return {"message": "OK"} +@router.get( + "/getAlarms", + response_model=List[AlarmCoreItem], + tags=["Reslevis"], + dependencies=[Depends(get_current_user)], +) +async def getAlarms(): + async with httpx.AsyncClient(timeout=CORE_TIMEOUT) as client: + resp = await client.get(f"{ALERTS_CORE_BASE_URL}/reslevis/alerts") + + if resp.status_code >= 400: + detail = resp.text.strip() or "CORE alerts request failed" + raise HTTPException(status_code=resp.status_code, detail=detail) + + try: + payload = resp.json() + except ValueError as exc: + raise HTTPException(status_code=502, detail="Invalid CORE response") from exc + + if not isinstance(payload, list): + raise HTTPException(status_code=502, detail="Unexpected CORE response type") + + return payload + + +@router.put( + "/updateAlarm", + tags=["Reslevis"], + dependencies=[Depends(get_current_user)], +) +async def updateAlarm(item: AlarmStatusUpdateItem): + async with httpx.AsyncClient(timeout=CORE_TIMEOUT) as client: + resp = await client.patch( + f"{ALERTS_CORE_BASE_URL}/reslevis/alerts/{item.id}", + json={"status": item.status}, + ) + + if resp.status_code >= 400: + detail = resp.text.strip() or "CORE alert update failed" + raise HTTPException(status_code=resp.status_code, detail=detail) + + if not resp.content: + return {"message": "OK"} + + try: + return resp.json() + except ValueError: + return {"message": "OK"} + + @router.get( "/getOperators", response_model=List[OperatorItem], diff --git a/schemas/reslevis.py b/schemas/reslevis.py index bffa38a..c1ee856 100644 --- a/schemas/reslevis.py +++ b/schemas/reslevis.py @@ -118,6 +118,19 @@ class AlarmItem(BaseModel): zoneName: Optional[str] = None building: Optional[UUID] = None + +class AlarmCoreItem(BaseModel): + id: UUID + tracker_id: UUID + type: str + status: str + timestamp: str + + +class AlarmStatusUpdateItem(BaseModel): + id: UUID + status: str + class TrackItem(BaseModel): id: UUID timestamp: Optional[str] = None