gnss/gnss/api/routes/gnss.py
2025-12-23 11:28:19 +05:00

61 lines
2.1 KiB
Python

from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel, Field
from ..dependencies import get_gnss_service
from ... import config
from ...services.gnss import GNSSService, RectangularPathSource, SerialNMEASource, SimulatedPathSource
router = APIRouter(tags=["gnss"])
@router.get("/gnss/state")
async def gnss_state(gnss: GNSSService = Depends(get_gnss_service)) -> dict:
return {"status": "ok", "gnss": gnss.describe()}
class SetGNSSModeRequest(BaseModel):
mode: str = Field(pattern="^(simulated|serial)$")
@router.post("/gnss/mode")
async def set_gnss_mode(payload: SetGNSSModeRequest, gnss: GNSSService = Depends(get_gnss_service)) -> dict:
mode = payload.mode.lower()
if mode == "serial":
source = SerialNMEASource(port=config.GNSS_PORT, baudrate=config.GNSS_BAUD, timeout=config.GNSS_TIMEOUT)
await gnss.set_source(source)
return {"status": "ok", "gnss": gnss.describe()}
start_lat, start_lon = config.SIM_START_POINT
path = (getattr(config, "SIM_PATH", "circle") or "circle").lower()
if path == "rectangle":
source = RectangularPathSource(
center_lat=start_lat,
center_lon=start_lon,
width_m=config.SIM_RECT_WIDTH_METERS,
height_m=config.SIM_RECT_HEIGHT_METERS,
step_m=config.SIM_STEP_METERS,
alt_m=config.SIM_ALT_METERS,
)
else:
source = SimulatedPathSource(center_lat=start_lat, center_lon=start_lon, radius_m=config.SIM_RADIUS_METERS)
await gnss.set_source(source)
return {"status": "ok", "gnss": gnss.describe()}
class UpdateSimulatedCenterRequest(BaseModel):
lat: float = Field(ge=-90, le=90)
lon: float = Field(ge=-180, le=180)
@router.post("/gnss/sim/center")
async def update_simulated_center(
payload: UpdateSimulatedCenterRequest,
gnss: GNSSService = Depends(get_gnss_service),
) -> dict:
try:
await gnss.update_simulated_center(lat=payload.lat, lon=payload.lon)
except TypeError as exc:
raise HTTPException(status_code=400, detail=str(exc))
return {"status": "ok", "gnss": gnss.describe()}