From 17fc9cc6bf922e38befa2406a37cbe07b32badd6 Mon Sep 17 00:00:00 2001 From: root Date: Wed, 29 Apr 2026 15:57:08 +0200 Subject: [PATCH] Core API reintegrated --- app.py | 1 + routes/reslevis.py | 53 +++++++++++++++++++++++++++++++++++++++++++++ schemas/reslevis.py | 22 +++++++++++++++++++ 3 files changed, 76 insertions(+) diff --git a/app.py b/app.py index c922fd3..f1b234b 100644 --- a/app.py +++ b/app.py @@ -153,6 +153,7 @@ async def stop_mqtt_monitor(): async def local_then_core(request: Request, call_next): internal_core_proxy_paths = { "/reslevis/updateAlarm", + "/reslevis/updateCoreSettings", } # only proxy CRUD for Reslevis (change prefix or methods if needed) if ( diff --git a/routes/reslevis.py b/routes/reslevis.py index bcbc8f2..0812c32 100644 --- a/routes/reslevis.py +++ b/routes/reslevis.py @@ -24,6 +24,8 @@ from schemas.reslevis import ( TrackerZoneItem, SettingItem, GuiConfigItem, + CoreSettingsItem, + CoreSettingsUpdateItem, ) from logica_reslevis.gateway import GatewayJsonRepository @@ -46,6 +48,7 @@ from security import get_current_user CORE_BASE_URL = config_env.CORE_API_URL.rstrip("/") ALERTS_CORE_BASE_URL = "http://localhost:1902" TRACKS_CORE_BASE_URL = "http://localhost:1902" +SETTINGS_CORE_BASE_URL = "http://localhost:1902" CORE_TIMEOUT = 2.0 # secondi async def sync_core_get(request: Request) -> None: @@ -622,3 +625,53 @@ def updateGuiConfig(item: GuiConfigItem): def removeGuiConfig(gui_config_id: str): gui_config_repo.remove(gui_config_id) return {"message": "OK"} + + +@router.get( + "/getCoreSettings", + response_model=List[CoreSettingsItem], + tags=["Reslevis"], + dependencies=[Depends(get_current_user)], +) +async def getCoreSettings(): + async with httpx.AsyncClient(timeout=CORE_TIMEOUT) as client: + resp = await client.get(f"{SETTINGS_CORE_BASE_URL}/reslevis/settings") + + if resp.status_code >= 400: + detail = resp.text.strip() or "CORE settings 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( + "/updateCoreSettings", + tags=["Reslevis"], + dependencies=[Depends(get_current_user)], +) +async def updateCoreSettings(item: CoreSettingsUpdateItem): + async with httpx.AsyncClient(timeout=CORE_TIMEOUT) as client: + resp = await client.patch( + f"{SETTINGS_CORE_BASE_URL}/reslevis/settings", + json=item.model_dump(exclude_none=True), + ) + + if resp.status_code >= 400: + detail = resp.text.strip() or "CORE settings 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"} diff --git a/schemas/reslevis.py b/schemas/reslevis.py index 47b675c..4971fc5 100644 --- a/schemas/reslevis.py +++ b/schemas/reslevis.py @@ -193,6 +193,28 @@ class ConfigItem(BaseModel): propery: List[str] offset: str +class CoreSettingsItem(BaseModel): + ID: int + current_algorithm: str + location_confidence: int + last_seen_threshold: int + beacon_metric_size: int + HA_send_interval: int + HA_send_changes_only: bool + RSSI_enforce_threshold: bool + RSSI_min_threshold: int + + +class CoreSettingsUpdateItem(BaseModel): + current_algorithm: str + last_seen_threshold: int + beacon_metric_size: int + HA_send_interval: int + HA_send_changes_only: bool + RSSI_enforce_threshold: bool + RSSI_min_threshold: int + location_confidence: Optional[int] = None + class CalibrationMetadata(BaseModel): pixel_ratio: float calibrated: bool