|
|
|
@@ -1,4 +1,6 @@ |
|
|
|
from fastapi import APIRouter, Depends, HTTPException |
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Request |
|
|
|
import httpx |
|
|
|
import config_env |
|
|
|
from typing import List |
|
|
|
|
|
|
|
from schemas.reslevis import ( |
|
|
|
@@ -27,7 +29,35 @@ from logica_reslevis.tracker_zone import TrackerZoneJsonRepository |
|
|
|
|
|
|
|
from security import get_current_user |
|
|
|
|
|
|
|
router = APIRouter() |
|
|
|
#CORE SYNC |
|
|
|
CORE_BASE_URL = config_env.CORE_API_URL.rstrip("/") |
|
|
|
CORE_TIMEOUT = 2.0 # secondi |
|
|
|
|
|
|
|
async def sync_core_get(request: Request) -> None: |
|
|
|
if request.method != "GET": |
|
|
|
return |
|
|
|
|
|
|
|
repo = CORE_GET_REPO_BY_PATH.get(request.url.path) |
|
|
|
if repo is None: |
|
|
|
return |
|
|
|
|
|
|
|
try: |
|
|
|
async with httpx.AsyncClient(timeout=CORE_TIMEOUT) as client: |
|
|
|
resp = await client.get( |
|
|
|
f"{CORE_BASE_URL}{request.url.path}", |
|
|
|
params=request.query_params, |
|
|
|
) |
|
|
|
|
|
|
|
if 200 <= resp.status_code < 300: |
|
|
|
data = resp.json() |
|
|
|
if isinstance(data, list): |
|
|
|
repo._write_all(data) # aggiorna i file locali |
|
|
|
except (httpx.RequestError, ValueError): |
|
|
|
# CORE giù o risposta non valida -> uso il file locale |
|
|
|
pass |
|
|
|
|
|
|
|
|
|
|
|
router = APIRouter(dependencies=[Depends(sync_core_get)]) |
|
|
|
|
|
|
|
gateway_repo = GatewayJsonRepository() |
|
|
|
building_repo = BuildingJsonRepository() |
|
|
|
@@ -40,6 +70,18 @@ alarm_repo = AlarmJsonRepository() |
|
|
|
track_repo = TrackJsonRepository() |
|
|
|
tracker_zone_repo = TrackerZoneJsonRepository() |
|
|
|
|
|
|
|
CORE_GET_REPO_BY_PATH = { |
|
|
|
"/reslevis/getGateways": gateway_repo, |
|
|
|
"/reslevis/getBuildings": building_repo, |
|
|
|
"/reslevis/getFloors": floor_repo, |
|
|
|
"/reslevis/getZones": zone_repo, |
|
|
|
"/reslevis/getTrackers": tracker_repo, |
|
|
|
"/reslevis/getTrackerZones": tracker_zone_repo, |
|
|
|
"/reslevis/getTracks": track_repo, |
|
|
|
"/reslevis/getOperators": operator_repo, |
|
|
|
"/reslevis/getSubjects": subject_repo, |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@router.get( |
|
|
|
"/getGateways", |
|
|
|
@@ -103,10 +145,10 @@ def removeBuilding(building_id: str): |
|
|
|
tags=["Reslevis"], |
|
|
|
dependencies=[Depends(get_current_user)], |
|
|
|
) |
|
|
|
|
|
|
|
def getFloors(): |
|
|
|
return floor_repo.list() |
|
|
|
|
|
|
|
|
|
|
|
@router.post("/postFloor", tags=["Reslevis"], dependencies=[Depends(get_current_user)]) |
|
|
|
def postFloor(item: FloorItem): |
|
|
|
floor_repo.add(item) |
|
|
|
|