Capability report: PUT /devices/{id}/report, a Capabilities section, offline devices degraded
A device reports its board, screen, camera and ports from its own credential (16 KB, schema 1, replacing the last report), and the flows are validated against it again. The node panel turns a reported port's config schema into a form, a reported screen is a canvas preset and a touch screen's first report turns Touch friendly on. An offline device node is degraded: amber on the canvas, a warning on Home, never a fault. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0138N7k5PT4dcR1iEqXaaQ9k
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
a9d453c9d9
commit
ad76200d3d
@@ -18,8 +18,13 @@ device nodes in published flows wire its own ports to, and nothing else.
|
||||
Deleting the device revokes it, and so does unpairing, which bumps the
|
||||
device's nonce and leaves what it shows as it was.
|
||||
|
||||
``/panels/pair``, ``/panels/pair/{code}`` and ``GET /panels/{id}`` are what the
|
||||
openpanel firmware calls, and stay as aliases until every device has updated.
|
||||
Once paired, a device says what it has with ``PUT /devices/{id}/report``: its
|
||||
board, its screen and the ports it serves, which the device node's ports are
|
||||
checked against.
|
||||
|
||||
``/panels/pair``, ``/panels/pair/{code}`` and ``GET /panels/{id}`` are what
|
||||
openpanel firmware from before devices calls, and stay as aliases until every
|
||||
device has updated.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
@@ -34,7 +39,7 @@ import httpx
|
||||
import redis
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from pydantic import BaseModel, Field, field_validator
|
||||
from pydantic import BaseModel, Field, ValidationError, field_validator
|
||||
|
||||
from fluksio.api.deps import (
|
||||
CurrentUser,
|
||||
@@ -52,6 +57,7 @@ from fluksio.flow.devices import (
|
||||
DeviceExists,
|
||||
DeviceIo,
|
||||
DeviceNotFound,
|
||||
DeviceReport,
|
||||
DeviceStore,
|
||||
is_lan,
|
||||
presence,
|
||||
@@ -88,6 +94,10 @@ RESERVED = {"pair", "pending"}
|
||||
#: How much of what a device says about itself is kept.
|
||||
HELLO_LENGTH = 64
|
||||
|
||||
#: The most a capability report may weigh. A panel with every header pin
|
||||
#: reported is a few kB.
|
||||
REPORT_MAX = 16 * 1024
|
||||
|
||||
|
||||
class _Pending(BaseModel):
|
||||
"""A device waiting to be told what it is."""
|
||||
@@ -314,6 +324,7 @@ class DevicePublic(DeviceDocument):
|
||||
last_seen: datetime | None = None
|
||||
#: What the device says it has, as it last said so. None until it reports.
|
||||
report: dict[str, Any] | None = None
|
||||
reported_at: datetime | None = None
|
||||
|
||||
|
||||
class DevicesPublic(BaseModel):
|
||||
@@ -499,15 +510,19 @@ async def read_devices(store: DeviceStoreDep) -> Any:
|
||||
"""Every device, what each one shows, and whether it is connected."""
|
||||
|
||||
def gather() -> list[DevicePublic]:
|
||||
return [
|
||||
DevicePublic(
|
||||
**_document(device, store).model_dump(),
|
||||
online=presence.online(device.id),
|
||||
last_seen=presence.last_seen(device.id),
|
||||
report=store.state(device.id).report,
|
||||
found = []
|
||||
for device in store.list():
|
||||
state = store.state(device.id)
|
||||
found.append(
|
||||
DevicePublic(
|
||||
**_document(device, store).model_dump(),
|
||||
online=presence.online(device.id),
|
||||
last_seen=presence.last_seen(device.id),
|
||||
report=state.report,
|
||||
reported_at=state.reported_at,
|
||||
)
|
||||
)
|
||||
for device in store.list()
|
||||
]
|
||||
return found
|
||||
|
||||
data = await run_in_threadpool(gather)
|
||||
return DevicesPublic(
|
||||
@@ -596,6 +611,57 @@ panels_router.add_api_route(
|
||||
)
|
||||
|
||||
|
||||
@router.put(
|
||||
"/{device_id}/report",
|
||||
response_model=Message,
|
||||
dependencies=[Depends(get_current_user)],
|
||||
)
|
||||
async def put_report(device_id: str, request: Request, store: DeviceStoreDep) -> Any:
|
||||
"""A device says what it has: its board, its screen, its ports.
|
||||
|
||||
Only the device itself, through its own credential, which ``_device_may``
|
||||
holds to its own id. The report replaces the one before it, and the flows
|
||||
are validated again, since a device node's ports are checked against it.
|
||||
|
||||
The first report of a touch screen makes the device touch friendly, as
|
||||
the switch would have been set by hand. Later ones leave the setting to
|
||||
whoever changed it since.
|
||||
"""
|
||||
device: DeviceDef | None = getattr(request.state, "device", None)
|
||||
if device is None or device.id != device_id:
|
||||
raise HTTPException(
|
||||
status_code=403, detail="Only the device itself reports what it has"
|
||||
)
|
||||
raw = bytearray()
|
||||
async for chunk in request.stream():
|
||||
raw += chunk
|
||||
if len(raw) > REPORT_MAX:
|
||||
raise HTTPException(
|
||||
status_code=413,
|
||||
detail=f"A report is at most {REPORT_MAX // 1024} KB",
|
||||
)
|
||||
try:
|
||||
report = DeviceReport.model_validate_json(bytes(raw))
|
||||
except ValidationError as exc:
|
||||
raise HTTPException(
|
||||
status_code=422,
|
||||
detail=exc.errors(
|
||||
include_url=False, include_input=False, include_context=False
|
||||
),
|
||||
)
|
||||
first = await run_in_threadpool(store.report, device_id, report)
|
||||
if first and report.display and report.display.touch and not device.touch:
|
||||
with suppress(StaleVersion, DeviceNotFound):
|
||||
await run_in_threadpool(
|
||||
store.write,
|
||||
device.model_copy(update={"touch": True}),
|
||||
device.version,
|
||||
)
|
||||
_changed()
|
||||
await _recheck(request)
|
||||
return Message(message=f"{len(report.ports)} ports reported")
|
||||
|
||||
|
||||
@router.put(
|
||||
"/{device_id}",
|
||||
response_model=DeviceDef,
|
||||
|
||||
Reference in New Issue
Block a user