Panel I/O: PanelDef.io ports, scoped publish and upload, I/O section in the Panels dialog
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user