diff --git a/app.py b/app.py index c922fd3..9667ecf 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 8efc3b4..44d7db7 100644 --- a/routes/reslevis.py +++ b/routes/reslevis.py @@ -23,6 +23,8 @@ from schemas.reslevis import ( TrackHistoryItem, TrackerZoneItem, SettingItem, + CoreSettingsItem, + CoreSettingsUpdateItem, ) from logica_reslevis.gateway import GatewayJsonRepository @@ -44,6 +46,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://127.0.0.1:1902" CORE_TIMEOUT = 2.0 # secondi async def sync_core_get(request: Request) -> None: @@ -591,3 +594,53 @@ def updateSetting(item: SettingItem): def removeSetting(setting_id: str): setting_repo.remove(setting_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 c1ee856..a3c109a 100644 --- a/schemas/reslevis.py +++ b/schemas/reslevis.py @@ -82,6 +82,29 @@ class SettingItem(BaseModel): debugFields: Optional[List[str]] = None language: 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 OperatorItem(BaseModel): id: UUID name: str