Ver código fonte

Added Alarm API

alarm_API
Lorenzo Pollutri 3 semanas atrás
pai
commit
9d51bb2e37
3 arquivos alterados com 74 adições e 1 exclusões
  1. +8
    -1
      app.py
  2. +53
    -0
      routes/reslevis.py
  3. +13
    -0
      schemas/reslevis.py

+ 8
- 1
app.py Ver arquivo

@@ -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:


+ 53
- 0
routes/reslevis.py Ver arquivo

@@ -16,6 +16,8 @@ from schemas.reslevis import (
OperatorItem,
SubjectItem,
AlarmItem,
AlarmCoreItem,
AlarmStatusUpdateItem,
TrackItem,
TrackHistoryItem,
TrackerZoneItem,
@@ -38,6 +40,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

@@ -425,6 +428,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],


+ 13
- 0
schemas/reslevis.py Ver arquivo

@@ -107,6 +107,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


Carregando…
Cancelar
Salvar