feat(routers): add observed router inventory and scoped controls
Separate router discovery, evidence and authorized control from device runtime. Add Node/Core contracts and reusable operator surfaces.
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
"""Router observation and explicit allowlisted, runtime-bound controls."""
|
||||
import json
|
||||
|
||||
from fastapi import Depends, HTTPException, Request, Response
|
||||
|
||||
from k1link.fleet.registry import FleetRegistry
|
||||
from k1link.fleet.trust import PairingError
|
||||
|
||||
|
||||
def register(parent, local_operator):
|
||||
@parent.get("/{vehicle_id}/routers")
|
||||
def read(vehicle_id: str, response: Response, fleet: FleetRegistry = Depends(local_operator)):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
with fleet.lock:
|
||||
return fleet.routers.view(fleet.find(vehicle_id))
|
||||
except PairingError:
|
||||
raise HTTPException(404, "Аппарат не найден.") from None
|
||||
|
||||
@parent.get("/{vehicle_id}/routers/journal")
|
||||
def export(vehicle_id: str, fleet: FleetRegistry = Depends(local_operator)):
|
||||
try:
|
||||
with fleet.lock:
|
||||
state = fleet.routers.view(fleet.find(vehicle_id))
|
||||
body = {"schema": "missioncore.router-journal/v1", **{k: state[k] for k in ("fresh", "received_at", "events", "journal") if k in state}}
|
||||
return Response(json.dumps(body, ensure_ascii=False, indent=2), media_type="application/json", headers={"Cache-Control": "no-store", "Content-Disposition": 'attachment; filename="router-journal.json"'})
|
||||
except PairingError:
|
||||
raise HTTPException(404, "Аппарат не найден.") from None
|
||||
|
||||
@parent.post("/{vehicle_id}/routers/access")
|
||||
async def access(vehicle_id: str, request: Request, response: Response, fleet: FleetRegistry = Depends(local_operator)):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
body = await request.body()
|
||||
if len(body) > 4096:
|
||||
raise ValueError
|
||||
return fleet.routers.authorize(fleet, vehicle_id, json.loads(body))
|
||||
except PairingError as error:
|
||||
raise HTTPException(409, str(error)) from None
|
||||
except (ValueError, TypeError):
|
||||
raise HTTPException(422, "Проверьте данные доступа.") from None
|
||||
|
||||
@parent.post("/{vehicle_id}/routers/control")
|
||||
async def control(vehicle_id: str, request: Request, response: Response, fleet: FleetRegistry = Depends(local_operator)):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
body = await request.body()
|
||||
if len(body) > 2048:
|
||||
raise ValueError
|
||||
return fleet.routers.control(fleet, vehicle_id, json.loads(body))
|
||||
except PairingError as error:
|
||||
raise HTTPException(409, str(error)) from None
|
||||
except (ValueError, TypeError):
|
||||
raise HTTPException(422, "Проверьте команду роутеру.") from None
|
||||
|
||||
@parent.get("/{vehicle_id}/routers/access/{operation_id}")
|
||||
def operation(vehicle_id: str, operation_id: str, response: Response, fleet: FleetRegistry = Depends(local_operator)):
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
try:
|
||||
with fleet.lock:
|
||||
return fleet.routers.operation(fleet.find(vehicle_id), operation_id)
|
||||
except PairingError:
|
||||
raise HTTPException(404, "Аппарат не найден.") from None
|
||||
Reference in New Issue
Block a user