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.
This commit is contained in:
@@ -38,7 +38,13 @@ from pydantic import BaseModel, Field, TypeAdapter, ValidationError, field_valid
|
||||
|
||||
from fluksio.flow.dashboards import DashboardDef, DashboardNotFound, DashboardStore
|
||||
from fluksio.flow.events import event_bus
|
||||
from fluksio.flow.messages import MessageSpec, flow_of, qualify, validate_qualified
|
||||
from fluksio.flow.messages import (
|
||||
DType,
|
||||
MessageSpec,
|
||||
flow_of,
|
||||
qualify,
|
||||
validate_qualified,
|
||||
)
|
||||
from fluksio.flow.schemas import FlowDef, NodeDef, _validate_name
|
||||
from fluksio.flow.store import FlowStore, InvalidName, StaleVersion, checked
|
||||
|
||||
@@ -158,6 +164,61 @@ class DeviceState(BaseModel):
|
||||
_STATES = TypeAdapter(dict[str, DeviceState])
|
||||
|
||||
|
||||
class ReportDisplay(BaseModel):
|
||||
"""The device's screen, in pixels."""
|
||||
|
||||
w: int = Field(ge=0)
|
||||
h: int = Field(ge=0)
|
||||
shape: Literal["rect", "round"] = "rect"
|
||||
touch: bool = False
|
||||
|
||||
|
||||
class ReportPort(BaseModel):
|
||||
"""One port the device has, and how it may be wired."""
|
||||
|
||||
port: str = Field(pattern=PORT_PATTERN.pattern)
|
||||
#: Its default direction: ``in`` publishes, ``out`` follows.
|
||||
direction: Literal["in", "out"]
|
||||
dtype: DType
|
||||
#: A JSON schema of the ``config`` the device reads for this port.
|
||||
config: dict[str, Any] | None = None
|
||||
#: Sends only while somebody wants it, such as a camera.
|
||||
on_demand: bool | None = None
|
||||
#: The directions it may be wired in, when it may be either: a GPIO pin.
|
||||
modes: list[Literal["in", "out"]] | None = None
|
||||
|
||||
|
||||
class ReportCamera(BaseModel):
|
||||
"""The camera sensor the device found, and what it delivers."""
|
||||
|
||||
sensor: str = ""
|
||||
w: int = 0
|
||||
h: int = 0
|
||||
fps: int = 0
|
||||
|
||||
|
||||
class DeviceReport(BaseModel):
|
||||
"""What a device says it has, version 1.
|
||||
|
||||
Keys this version does not know are dropped, so a newer firmware still
|
||||
reports to an older instance. The whole report replaces the one before.
|
||||
"""
|
||||
|
||||
version: Literal[1] = Field(alias="schema")
|
||||
platform: str = ""
|
||||
board: str = ""
|
||||
firmware: str = ""
|
||||
display: ReportDisplay | None = None
|
||||
ports: list[ReportPort] = Field(default_factory=list)
|
||||
camera: ReportCamera | None = None
|
||||
#: The build's features, true when there: ``{"camera": true}``.
|
||||
features: dict[str, bool] = Field(default_factory=dict)
|
||||
|
||||
def stored(self) -> dict[str, Any]:
|
||||
"""As ``devices.json`` keeps it: the device's own keys, nothing unset."""
|
||||
return self.model_dump(mode="json", by_alias=True, exclude_none=True)
|
||||
|
||||
|
||||
class DeviceNotFound(KeyError):
|
||||
def __init__(self, device_id: str) -> None:
|
||||
super().__init__(device_id)
|
||||
@@ -372,6 +433,20 @@ class DeviceStore:
|
||||
self._write_states(states)
|
||||
return state.nonce
|
||||
|
||||
def report(self, device_id: str, report: DeviceReport) -> bool:
|
||||
"""Keep what the device says it has, in place of what it said before.
|
||||
|
||||
Returns whether it is the first report this device made.
|
||||
"""
|
||||
with self._state_lock:
|
||||
states = self._read_states()
|
||||
state = states.setdefault(device_id, DeviceState())
|
||||
first = state.report is None
|
||||
state.report = report.stored()
|
||||
state.reported_at = datetime.now(UTC)
|
||||
self._write_states(states)
|
||||
return first
|
||||
|
||||
# -- the panels file of an older instance -------------------------------
|
||||
|
||||
def migrate(self, panels_file: Path) -> None:
|
||||
|
||||
Reference in New Issue
Block a user