Panel I/O: PanelDef.io ports, scoped publish and upload, I/O section in the Panels dialog

This commit is contained in:
Melvin Strobl
2026-10-13 21:10:54 +02:00
parent 80e595c02e
commit 659799d1e5
9 changed files with 443 additions and 14 deletions
+13 -3
View File
@@ -112,10 +112,15 @@ def _panel_writable(panel_id: str, request: Request) -> set[str]:
panel displays and another controls stays writable, because the unlocked
one is what entitles the screen to it. Reads are untouched — read-only is
not blind, and a locked dashboard has to keep drawing live data.
The panel's own ``in`` ports are added outside the lock: it covers the
controls on a dashboard, not the sensors on the device. Its ``out`` ports
are read, not written, unless a widget targets them too.
"""
store: DashboardStore | None = getattr(request.app.state, "dashboard_store", None)
if store is None:
return set()
writable = panels.io_in(panel_id)
defns = panels.dashboards_for(panel_id, store)
bound = sorted(
{s.message for d in defns if (s := d.settings.get("locked")) and s.message}
@@ -124,7 +129,6 @@ def _panel_writable(panel_id: str, request: Request) -> set[str]:
request.app.state, "flow_controller", None
)
live = controller.state.get_present(bound) if bound and controller else {}
writable: set[str] = set()
for defn in defns:
writable |= (
panels.requests_of(defn)
@@ -162,8 +166,10 @@ def _panel_may(payload: dict[str, Any], request: Request) -> None:
A panel credential names the account that approved the pairing, so without
this it would be that person's session hanging on a wall. What a panel
genuinely needs is small and worth writing out: the dashboards it was
assigned, its own definition, the messages its own widgets bind to, and the
bytes those messages currently point at, for a tile drawing a camera frame.
assigned, its own definition, the messages its own widgets and ports bind
to, and the bytes those messages currently point at, for a tile drawing a
camera frame — plus an upload named after one of its ``in`` ports, for a
microphone whose recording is the value it publishes.
Publishing is in the list because a panel cannot be strictly read-only — a
control on a panel is the point of putting one there, and a querying chart
@@ -223,6 +229,10 @@ def _panel_may(payload: dict[str, Any], request: Request) -> None:
# off an old run because it happens to know the digest.
digest = unquote(path[len(f"{api}/artifacts/") :])
allowed = "/" not in digest and digest in _panel_digests(panel.id, request)
elif method == "PUT" and path == f"{api}/artifacts":
# What the device's own sensor recorded — a microphone's utterance —
# uploaded under the ``in`` message it is about to publish it to.
allowed = request.query_params.get("name", "") in panels.io_in(panel.id)
if not allowed:
raise HTTPException(
+58 -5
View File
@@ -12,8 +12,10 @@ which screen hangs where is the deployment's concern, not any one dashboard's.
from __future__ import annotations
import re
import threading
from pathlib import Path
from typing import Any, Literal
from pydantic import BaseModel, Field, field_validator
@@ -21,6 +23,43 @@ from fluksio.core.config import settings
from fluksio.flow.dashboards import DashboardDef, DashboardNotFound, DashboardStore
from fluksio.flow.schemas import _validate_name
#: Free text the device interprets: ``gpio.4``, ``backlight``, ``mic``.
PORT_PATTERN = re.compile(r"^[a-z0-9_.]+$")
class PanelPort(BaseModel):
"""One piece of the device's own hardware, wired to a message.
A wall panel is a node as well as a screen: a button on its GPIO, a
microphone, a speaker. Which ports exist is the device's business, so the
name is free text and ``config`` is passed through to it unread.
"""
port: str
#: Qualified, ``flow.name`` — a flow still has to declare it, as for any
#: widget, before a publish to it lands.
message: str
#: ``in`` is the device publishing into fluksio; ``out`` is the device
#: following the message.
direction: Literal["in", "out"]
config: dict[str, Any] = Field(default_factory=dict)
@field_validator("port")
@classmethod
def _check_port(cls, value: str) -> str:
if not PORT_PATTERN.fullmatch(value):
raise ValueError("Use lowercase letters, digits, underscores and dots")
return value
@field_validator("message")
@classmethod
def _check_message(cls, value: str) -> str:
flow, _, name = value.partition(".")
if not name:
raise ValueError("Name the message with its flow, as flow.name")
_validate_name(flow)
return value
class PanelDef(BaseModel):
"""One device, and what it shows."""
@@ -36,6 +75,10 @@ class PanelDef(BaseModel):
#: any dashboard: the same dashboard may hang on a hallway tablet and on a
#: desk browser, and only one of them has fingers on it.
touch: bool = False
#: The device's own inputs and outputs. An ``in`` port may publish whether
#: or not a dashboard is locked — a lock covers the controls on a
#: dashboard, not the sensors on the wall — and an ``out`` port only reads.
io: list[PanelPort] = Field(default_factory=list)
#: Which generation of credential this panel honours. A token names the
#: nonce it was minted at, so bumping this refuses the screen currently
#: hanging here and leaves the panel, its dashboards and their arrangement
@@ -151,12 +194,22 @@ def requests_of(defn: DashboardDef) -> set[str]:
return {w.target for w in defn.widgets if w.type == "chart" and w.target}
def messages_for(panel_id: str, store: DashboardStore) -> set[str]:
"""Every message this panel's dashboards read or write.
def io_in(panel_id: str) -> set[str]:
"""The messages this panel's own hardware publishes to."""
panel = find(panel_id)
if panel is None:
return set()
return {port.message for port in panel.io if port.direction == "in"}
What a screen is entitled to see, as its own dashboards define it, and
empty for a panel that is gone — which is the same answer as "nothing".
def messages_for(panel_id: str, store: DashboardStore) -> set[str]:
"""Every message this panel's dashboards and its own I/O read or write.
What a screen is entitled to see, as its dashboards and ports define it,
and empty for a panel that is gone — which is the same answer as "nothing".
"""
return {
panel = find(panel_id)
own = {port.message for port in panel.io} if panel else set()
return own | {
name for defn in dashboards_for(panel_id, store) for name in messages_of(defn)
}