feat(fleet): preserve operator VESC integration before final driver merge
This commit is contained in:
@@ -112,6 +112,7 @@ from k1link.web.advanced_laboratory_api import build_advanced_laboratory_router
|
||||
from k1link.web.artifact_health_api import build_artifact_health_router
|
||||
from k1link.web.compute_contour_api import build_compute_contour_router
|
||||
from k1link.web.fleet_api import router as fleet_router
|
||||
from k1link.web.rover_api import router as rover_router
|
||||
from k1link.web.device_plugin_composition import load_installed_device_plugins
|
||||
from k1link.web.e30_engineering_api import build_e30_engineering_router
|
||||
from k1link.web.e30_human_review_api import build_e30_human_review_router
|
||||
@@ -1004,6 +1005,7 @@ app.add_middleware(ResponseCompressionMiddleware, minimum_size=1_024, compressle
|
||||
|
||||
|
||||
app.include_router(fleet_router)
|
||||
app.include_router(rover_router)
|
||||
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
|
||||
@@ -6,12 +6,12 @@ import asyncio
|
||||
import ipaddress
|
||||
import json
|
||||
from contextlib import suppress
|
||||
from typing import Annotated
|
||||
from typing import Annotated, Literal
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request, Response
|
||||
from fastapi.responses import StreamingResponse
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from pydantic import BaseModel, ConfigDict, Field, StrictBool
|
||||
|
||||
from k1link.fleet.registry import FleetRegistry
|
||||
from k1link.fleet.trust import PairingError
|
||||
@@ -51,6 +51,42 @@ class AddRequest(BaseModel):
|
||||
router = APIRouter(prefix="/api/v1/fleet", tags=["fleet"])
|
||||
|
||||
|
||||
class BoardLayoutChange(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
section: Literal["computer", "settings", "devices"]
|
||||
open: StrictBool
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}/board-layout")
|
||||
def board_layout_read(vehicle_id: str, response: Response,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)]):
|
||||
from k1link.fleet.board_layout import read
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
with fleet.lock:
|
||||
fleet.find(vehicle_id)
|
||||
return read(fleet.root, vehicle_id)
|
||||
except PairingError as error:
|
||||
raise HTTPException(404, str(error)) from None
|
||||
except (OSError, ValueError):
|
||||
raise HTTPException(500, "Не удалось прочитать раскладку аппарата.") from None
|
||||
|
||||
|
||||
@router.patch("/{vehicle_id}/board-layout")
|
||||
def board_layout_patch(vehicle_id: str, body: BoardLayoutChange, response: Response,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)]):
|
||||
from k1link.fleet.board_layout import update
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
with fleet.lock:
|
||||
fleet.find(vehicle_id)
|
||||
return update(fleet.root, vehicle_id, body.section, body.open)
|
||||
except PairingError as error:
|
||||
raise HTTPException(404, str(error)) from None
|
||||
except (OSError, ValueError):
|
||||
raise HTTPException(500, "Не удалось сохранить раскладку аппарата.") from None
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}/monitor")
|
||||
def board_monitor(vehicle_id: str, response: Response,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)],
|
||||
@@ -141,6 +177,30 @@ def sensor_command(
|
||||
raise HTTPException(409, str(error)) from None
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}/devices/{device_id}/configurations")
|
||||
def vesc_configurations(vehicle_id: str, device_id: str, response: Response,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)], before: int = 0):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
with fleet.lock:
|
||||
node = fleet.find(vehicle_id)["node_id"]
|
||||
return fleet.vesc_archive.listing(node, device_id, before)
|
||||
except (PairingError, ValueError):
|
||||
raise HTTPException(404, "История конфигураций недоступна.") from None
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}/devices/{device_id}/configurations/{version_id}")
|
||||
def vesc_configuration(vehicle_id: str, device_id: str, version_id: str, response: Response,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)]):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
with fleet.lock:
|
||||
node = fleet.find(vehicle_id)["node_id"]
|
||||
return fleet.vesc_archive.read(node, device_id, version_id)
|
||||
except (PairingError, ValueError):
|
||||
raise HTTPException(404, "Версия конфигурации не найдена.") from None
|
||||
|
||||
|
||||
@router.get("/{vehicle_id}/devices/operations/{operation_id}")
|
||||
def sensor_operation(
|
||||
vehicle_id: str, operation_id: str, fleet: Annotated[FleetRegistry, Depends(local_operator)]
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
"""Planning-profile preparation and preview; never an acquisition endpoint."""
|
||||
|
||||
import gzip
|
||||
from typing import Literal
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Query, Response
|
||||
from fastapi import APIRouter, HTTPException, Query, Request, Response
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import BaseModel, ConfigDict, Field
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
|
||||
@@ -47,8 +49,25 @@ def build_planning_live_router(service):
|
||||
raise HTTPException(409, str(exc)) from exc
|
||||
|
||||
@router.get("/active")
|
||||
async def active():
|
||||
return await call(service.get)
|
||||
async def active(request: Request):
|
||||
accepts_gzip = "gzip" in request.headers.get("accept-encoding", "")
|
||||
|
||||
def encoded():
|
||||
# A completed report can contain megabytes of numerical evidence.
|
||||
# Returning its dict sends it through FastAPI's recursive encoder
|
||||
# and gzip on the event loop, delaying unrelated control requests.
|
||||
# Keep the exact JSON contract, but finish both operations here.
|
||||
response = JSONResponse(service.get(), headers={"Cache-Control": "no-store"})
|
||||
if accepts_gzip and len(response.body) >= 1024:
|
||||
return Response(
|
||||
gzip.compress(response.body, compresslevel=5, mtime=0),
|
||||
media_type="application/json",
|
||||
headers={"Content-Encoding": "gzip", "Vary": "Accept-Encoding",
|
||||
"Cache-Control": "no-store"},
|
||||
)
|
||||
return response
|
||||
|
||||
return await call(encoded)
|
||||
|
||||
@router.get("")
|
||||
async def history():
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
"""Local operator BFF for the paired Node's bounded control channel."""
|
||||
from typing import Annotated
|
||||
from fastapi import APIRouter, Depends, HTTPException, Response
|
||||
from k1link.fleet.registry import FleetRegistry
|
||||
from k1link.fleet.trust import PairingError
|
||||
from .fleet_api import local_operator
|
||||
|
||||
router = APIRouter(prefix="/api/v1/fleet", tags=["fleet"])
|
||||
|
||||
def node(fleet, vehicle):
|
||||
with fleet.lock:
|
||||
row = fleet.find(vehicle)
|
||||
if row["enrollment"] != "paired":
|
||||
raise ValueError("Борт не привязан.")
|
||||
return row["node_id"]
|
||||
|
||||
@router.get("/{vehicle_id}/rover")
|
||||
def rover_state(vehicle_id: str, response: Response,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)]):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
return fleet.rover_control.view(node(fleet, vehicle_id))
|
||||
except (ValueError, PairingError) as error:
|
||||
raise HTTPException(409, str(error)) from None
|
||||
|
||||
@router.post("/{vehicle_id}/rover/arm")
|
||||
def rover_arm(vehicle_id: str, body: dict,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)]):
|
||||
try:
|
||||
return fleet.rover_control.arm(node(fleet, vehicle_id), body)
|
||||
except (ValueError, PairingError) as error:
|
||||
raise HTTPException(409, str(error)) from None
|
||||
|
||||
@router.post("/{vehicle_id}/rover/command")
|
||||
def rover_command(vehicle_id: str, body: dict,
|
||||
fleet: Annotated[FleetRegistry, Depends(local_operator)]):
|
||||
try:
|
||||
return fleet.rover_control.command(node(fleet, vehicle_id), body)
|
||||
except (ValueError, PairingError) as error:
|
||||
raise HTTPException(409, str(error)) from None
|
||||
Reference in New Issue
Block a user