Fix onboard WebKit preview and archive board telemetry locally

This commit is contained in:
DCCONSTRUCTIONS
2026-09-08 01:37:52 +03:00
parent d8afb61229
commit f0802d2713
46 changed files with 2623 additions and 40 deletions
@@ -382,6 +382,15 @@ def create_app(repository_root: Path):
peers = NodeMediaPeers(bridge.rerun, bridge.service.camera_preview)
sensor = NodeK1Sensor(bridge, peers)
from fastapi.responses import Response
from k1link.viewer.node_local_media import start_local, read_local
async def preview_request(request):
data = await request.body()
if len(data) > 4096:
raise ValueError("Preview request exceeds bound")
return await request.json()
@asynccontextmanager
async def lifespan(_app):
yield
@@ -398,6 +407,30 @@ def create_app(repository_root: Path):
async def inventory(request: Request):
return await sensor.inventory(request.headers["X-Node-Id"])
@app.post("/local-preview")
async def preview(request: Request):
try:
command = await preview_request(request)
if datetime.fromisoformat(command["deadline_at"]) <= datetime.now(UTC):
raise ValueError("Preview request expired")
await sensor.admit_preview(command, request.headers["X-Node-Id"])
identifier = await start_local(peers, command['parameters'])
data = await read_local(peers, identifier, command['parameters'].get('after', 0))
return Response(data, media_type="application/vnd.missioncore.preview",
headers={"Cache-Control": "no-store"})
except (ValueError, KeyError, TypeError):
return JSONResponse({"error": "Preview unavailable"}, status_code=409)
@app.post("/local-preview/read")
async def preview_read(request: Request):
try:
data = await preview_request(request)
payload = await read_local(peers, data['peer_id'], data['after'])
return Response(payload, media_type="application/vnd.missioncore.preview",
headers={"Cache-Control": "no-store"})
except (ValueError, KeyError, TypeError):
return JSONResponse({"error": "Preview expired"}, status_code=409)
@app.get("/prepare-safe")
async def prepare_safe():
state = await sensor.raw_state()
@@ -132,6 +132,17 @@ class NodeK1Sensor:
"expected_state_revision": control["state_revision"],
}
async def admit_preview(self, command, node_id):
item = project_sensor(await self.raw_state(), node_id)
if (not item or command.get("action_id") != "offer"
or command.get("session", {}).get("device_id") != item["id"]
or command["session"].get("session_id") != item["snapshot"]["context"]["session_id"]
or item["snapshot"]["acquisition"] != "streaming"
or not item["control"]["acquisition_id"]
or command.get("parameters", {}).get("acquisition_id") != item["control"]["acquisition_id"]):
raise ValueError("Acquisition is not active or changed")
return item
async def execute(self, command, node_id):
state = await self.raw_state()
item = project_sensor(state, node_id)