Device node: a device's ports are wired in flows
- flow/nodes/device.py: `device` node with params `device` and `config`;
provides the device's `in` ports, requires its `out` ports without
waking anything, never run (Node.passive), down "Offline" from presence
- DeviceDef.io and DevicePort go; GET /devices/{id} computes `io` and
`bindings` from the device nodes of published flows, in a fixed order,
cached by FlowStore.revision; the credential's scope comes from them
- issues `unknown_device` and `device_port` (wired twice; against a report
the wrong direction or dtype, or unlisted as advisory), rechecked when a
device is created or deleted; ValidationIssue.advisory is a field
- a device publishing on a bound port is credited to its node by the
server; the panel endpoint kind is gone from `_endpoints` and the canvas
- a rebuild that moves a device's bindings sends `dashboard_changed` with
an empty name, which the firmware refetches on
- startup moves each device's `io` rows into device nodes in the flows
declaring their messages, draft and published, one commit; rows no
flow declares are logged and left out
- frontend: Device node settings (device, Publishes, Follows, a port
picker from a report), palette shortcuts per reported port, DeviceDetail
lists bindings with "Use in flow" in place of the I/O editor
- docs: devices, flow editor, node types, issues, API
This commit is contained in:
@@ -87,9 +87,16 @@ def _device_messages(device: DeviceDef, request: Request) -> set[str]:
|
||||
the answer to give when the answer cannot be worked out.
|
||||
"""
|
||||
store: DashboardStore | None = getattr(request.app.state, "dashboard_store", None)
|
||||
if store is None:
|
||||
owned: DeviceStore | None = getattr(request.app.state, "device_store", None)
|
||||
if store is None or owned is None:
|
||||
return set()
|
||||
return devices.messages_for(device, store)
|
||||
return devices.messages_for(device, store, owned)
|
||||
|
||||
|
||||
def _device_in(device: DeviceDef, request: Request) -> set[str]:
|
||||
"""The messages this device's own ``in`` ports are wired to publish."""
|
||||
owned: DeviceStore | None = getattr(request.app.state, "device_store", None)
|
||||
return devices.io_in(device, owned) if owned is not None else set()
|
||||
|
||||
|
||||
def _locked(defn: DashboardDef, live: dict[str, Any]) -> bool:
|
||||
@@ -128,14 +135,15 @@ def _device_writable(device: DeviceDef, request: Request) -> set[str]:
|
||||
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.
|
||||
The device's own ``in`` ports, as flows wire them, 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 = devices.io_in(device)
|
||||
writable = _device_in(device, request)
|
||||
defns = devices.dashboards_for(device, store)
|
||||
bound = sorted(
|
||||
{s.message for d in defns if (s := d.settings.get("locked")) and s.message}
|
||||
@@ -197,11 +205,18 @@ def _device_may(payload: dict[str, Any], request: Request) -> None:
|
||||
a panel to read every message it names and to publish to none of them but
|
||||
the requests its own charts ask by.
|
||||
|
||||
Its own ports are the device nodes the published flows hold for it, so
|
||||
wiring a port in a flow is what lets the device publish on it.
|
||||
|
||||
The device is left on ``request.state`` for the route, which credits a
|
||||
value published on one of its ports to the node holding that port.
|
||||
|
||||
# ponytail: the device file, its nonce and the published dashboards are
|
||||
# re-read per request. Cache them behind the store's revision if this
|
||||
# shows up in a profile.
|
||||
"""
|
||||
device = _device_for(payload, request.app)
|
||||
request.state.device = device
|
||||
|
||||
api = settings.API_V1_STR
|
||||
path = request.url.path
|
||||
@@ -252,7 +267,7 @@ def _device_may(payload: dict[str, Any], request: Request) -> None:
|
||||
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 devices.io_in(device)
|
||||
allowed = request.query_params.get("name", "") in _device_in(device, request)
|
||||
|
||||
if not allowed:
|
||||
raise HTTPException(
|
||||
|
||||
@@ -13,7 +13,8 @@ comes. Anyone on the internet can ask the portal for a code, so a request that
|
||||
came that way is adopted by its code only.
|
||||
|
||||
The credential is scoped: ``fluksio.api.deps`` lets it reach the dashboards
|
||||
that device was assigned, the messages their widgets bind to, and nothing else.
|
||||
that device was assigned, the messages their widgets bind to, the messages the
|
||||
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.
|
||||
|
||||
@@ -25,6 +26,7 @@ from __future__ import annotations
|
||||
|
||||
import secrets
|
||||
import time
|
||||
from contextlib import suppress
|
||||
from datetime import datetime, timedelta
|
||||
from typing import Any, cast
|
||||
|
||||
@@ -43,9 +45,12 @@ from fluksio.api.deps import (
|
||||
from fluksio.cloud import config as cloud_config
|
||||
from fluksio.core import security
|
||||
from fluksio.core.config import settings
|
||||
from fluksio.flow.controller import RebuildBusy
|
||||
from fluksio.flow.devices import (
|
||||
DeviceBinding,
|
||||
DeviceDef,
|
||||
DeviceExists,
|
||||
DeviceIo,
|
||||
DeviceNotFound,
|
||||
DeviceStore,
|
||||
is_lan,
|
||||
@@ -288,12 +293,27 @@ class DeviceCreate(BaseModel):
|
||||
title: str = ""
|
||||
|
||||
|
||||
class DevicePublic(DeviceDef):
|
||||
class DeviceDocument(DeviceDef):
|
||||
"""A device as it reads itself: what it shows, and what its ports carry.
|
||||
|
||||
``io`` is worked out from the device nodes in published flows, in a fixed
|
||||
order, so an unchanged state reads back as the same bytes.
|
||||
"""
|
||||
|
||||
#: Each port the flows wire, and the message it publishes or follows.
|
||||
io: list[DeviceIo] = Field(default_factory=list)
|
||||
#: The same ports by the flow and node holding them.
|
||||
bindings: list[DeviceBinding] = Field(default_factory=list)
|
||||
|
||||
|
||||
class DevicePublic(DeviceDocument):
|
||||
"""A device as the Devices tab lists it, with whether it is there."""
|
||||
|
||||
online: bool = False
|
||||
#: The last request it made, since this instance started.
|
||||
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
|
||||
|
||||
|
||||
class DevicesPublic(BaseModel):
|
||||
@@ -364,6 +384,26 @@ def _not_found(device_id: str) -> HTTPException:
|
||||
return HTTPException(status_code=404, detail=f"No device named {device_id!r}")
|
||||
|
||||
|
||||
def _document(device: DeviceDef, store: DeviceStore) -> DeviceDocument:
|
||||
return DeviceDocument(
|
||||
**device.model_dump(),
|
||||
io=store.io(device.id),
|
||||
bindings=store.bindings(device.id),
|
||||
)
|
||||
|
||||
|
||||
async def _recheck(request: Request) -> None:
|
||||
"""Validate the flows again: a device node may name this device.
|
||||
|
||||
The device is saved either way, so a busy engine is not this request's
|
||||
failure: the next rebuild validates again.
|
||||
"""
|
||||
controller = getattr(request.app.state, "flow_controller", None)
|
||||
if controller is not None:
|
||||
with suppress(RebuildBusy):
|
||||
await controller.recheck()
|
||||
|
||||
|
||||
def _changed() -> None:
|
||||
"""Tell every device socket that what it shows may have moved.
|
||||
|
||||
@@ -457,15 +497,19 @@ panels_router.add_api_route(
|
||||
@router.get("/", response_model=DevicesPublic, dependencies=[Depends(get_current_user)])
|
||||
async def read_devices(store: DeviceStoreDep) -> Any:
|
||||
"""Every device, what each one shows, and whether it is connected."""
|
||||
devices = await run_in_threadpool(store.list)
|
||||
data = [
|
||||
DevicePublic(
|
||||
**device.model_dump(),
|
||||
online=presence.online(device.id),
|
||||
last_seen=presence.last_seen(device.id),
|
||||
)
|
||||
for device in devices
|
||||
]
|
||||
|
||||
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,
|
||||
)
|
||||
for device in store.list()
|
||||
]
|
||||
|
||||
data = await run_in_threadpool(gather)
|
||||
return DevicesPublic(
|
||||
data=data,
|
||||
count=len(data),
|
||||
@@ -503,7 +547,10 @@ def pending_device(code: str) -> Any:
|
||||
dependencies=[Depends(get_current_active_superuser)],
|
||||
)
|
||||
async def create_device(
|
||||
device_id: str, store: DeviceStoreDep, body: DeviceCreate | None = None
|
||||
device_id: str,
|
||||
store: DeviceStoreDep,
|
||||
request: Request,
|
||||
body: DeviceCreate | None = None,
|
||||
) -> Any:
|
||||
"""Add a device. It shows nothing until it is given dashboards."""
|
||||
if device_id in RESERVED:
|
||||
@@ -513,35 +560,38 @@ async def create_device(
|
||||
except ValueError as exc:
|
||||
raise HTTPException(status_code=422, detail=str(exc))
|
||||
try:
|
||||
return await run_in_threadpool(store.create, device)
|
||||
created = await run_in_threadpool(store.create, device)
|
||||
except DeviceExists:
|
||||
raise HTTPException(status_code=409, detail=f"{device_id!r} already exists")
|
||||
await _recheck(request)
|
||||
return created
|
||||
|
||||
|
||||
@router.get(
|
||||
"/{device_id}",
|
||||
response_model=DeviceDef,
|
||||
response_model=DeviceDocument,
|
||||
dependencies=[Depends(get_current_user)],
|
||||
)
|
||||
async def read_device(device_id: str, store: DeviceStoreDep) -> Any:
|
||||
"""One device, read by a person or by the device holding its credential.
|
||||
|
||||
The definition alone: a device compares what it reads with what it is
|
||||
showing, and a field that changes on every request would rebuild its
|
||||
screen each time. Declared after the pairing routes and ``/pending``, so
|
||||
neither is mistaken for a device of that name.
|
||||
The definition and what the flows wire its ports to, nothing about this
|
||||
moment: a device compares what it reads with what it is showing, and a
|
||||
field that changes on every request would rebuild its screen each time.
|
||||
Declared after the pairing routes and ``/pending``, so neither is mistaken
|
||||
for a device of that name.
|
||||
"""
|
||||
device = await run_in_threadpool(store.find, device_id)
|
||||
if device is None:
|
||||
raise _not_found(device_id)
|
||||
return device
|
||||
return await run_in_threadpool(_document, device, store)
|
||||
|
||||
|
||||
panels_router.add_api_route(
|
||||
"/{device_id}",
|
||||
read_device,
|
||||
methods=["GET"],
|
||||
response_model=DeviceDef,
|
||||
response_model=DeviceDocument,
|
||||
dependencies=[Depends(get_current_user)],
|
||||
)
|
||||
|
||||
@@ -580,12 +630,13 @@ async def save_device(device_id: str, body: DeviceDef, store: DeviceStoreDep) ->
|
||||
response_model=Message,
|
||||
dependencies=[Depends(get_current_active_superuser)],
|
||||
)
|
||||
async def delete_device(device_id: str, store: DeviceStoreDep) -> Any:
|
||||
async def delete_device(device_id: str, store: DeviceStoreDep, request: Request) -> Any:
|
||||
"""Remove the device and refuse its credential, wherever it was minted here."""
|
||||
try:
|
||||
await run_in_threadpool(store.delete, device_id)
|
||||
except DeviceNotFound:
|
||||
raise _not_found(device_id)
|
||||
await _recheck(request)
|
||||
_changed()
|
||||
return Message(message=f"Deleted {device_id}")
|
||||
|
||||
|
||||
@@ -79,19 +79,18 @@ ws_router = APIRouter(prefix="/flows", tags=["flows"])
|
||||
class Endpoint(BaseModel):
|
||||
"""Something wired into this flow that is not a node in it.
|
||||
|
||||
A dashboard control setting one of its messages, a tile showing one, a
|
||||
port on a panel's own hardware, or a node in another flow on the far side
|
||||
of a dotted name. The canvas draws these so a value never appears to come
|
||||
A dashboard control setting one of its messages, a tile showing one, or a
|
||||
node in another flow on the far side of a dotted name. The canvas draws these so a value never appears to come
|
||||
from nowhere — or worse, appears to come from whichever node happens to be
|
||||
drawn as a producer.
|
||||
"""
|
||||
|
||||
#: dashboard, panel or flow.
|
||||
#: dashboard or flow.
|
||||
kind: str
|
||||
#: Stable within its kind, and used as the canvas node id.
|
||||
id: str
|
||||
label: str
|
||||
#: What sort of widget, the panel, or the node type in the other flow.
|
||||
#: What sort of widget, or the node type in the other flow.
|
||||
detail: str = ""
|
||||
#: Messages of this flow it publishes, and ones it reads.
|
||||
provides: list[str] = []
|
||||
@@ -157,29 +156,6 @@ def _endpoints(controller: FlowController, definition: FlowDef) -> list[Endpoint
|
||||
requires=binding["requires"],
|
||||
)
|
||||
|
||||
# A device's own hardware: an ``in`` port publishes like a control, an
|
||||
# ``out`` port reads like a tile. The id is the source id the device
|
||||
# publishes under, so its edges pulse for its values.
|
||||
prefix = f"{flow}."
|
||||
devices: DeviceStore | None = getattr(controller, "devices", None)
|
||||
for device in devices.list() if devices is not None else []:
|
||||
for port in device.io:
|
||||
if not port.message.startswith(prefix):
|
||||
continue
|
||||
key = f"panel:{device.id}:{port.port}"
|
||||
endpoint = found.setdefault(
|
||||
key,
|
||||
Endpoint(
|
||||
kind="panel",
|
||||
id=key,
|
||||
label=port.port,
|
||||
detail=device.title or device.id,
|
||||
),
|
||||
)
|
||||
side = endpoint.provides if port.direction == "in" else endpoint.requires
|
||||
if port.message not in side:
|
||||
side.append(port.message)
|
||||
|
||||
def _flow_endpoint(key: str, detail: str) -> Endpoint:
|
||||
"""One label per node on the far side, however many names reach it."""
|
||||
return found.setdefault(
|
||||
@@ -1102,7 +1078,7 @@ def panel_scope(token: str, app: Any) -> set[str] | None:
|
||||
devices: DeviceStore | None = getattr(app.state, "device_store", None)
|
||||
if store is None or devices is None:
|
||||
return set()
|
||||
return messages_for(devices.find(device_id), store)
|
||||
return messages_for(devices.find(device_id), store, devices)
|
||||
|
||||
|
||||
def event_for_panel(event: dict[str, Any], only: set[str]) -> bool:
|
||||
|
||||
@@ -8,11 +8,12 @@ into it without owning a node.
|
||||
|
||||
from typing import Any
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from fastapi.concurrency import run_in_threadpool
|
||||
from pydantic import BaseModel
|
||||
|
||||
from fluksio.api.deps import FlowControllerDep, get_current_user
|
||||
from fluksio.flow.devices import DeviceDef, DeviceStore
|
||||
from fluksio.flow.messages import flow_of
|
||||
from fluksio.flow.pipeline import ValueSource
|
||||
from fluksio.flow.state import as_number
|
||||
@@ -71,16 +72,38 @@ async def read_messages(controller: FlowControllerDep) -> Any:
|
||||
return MessagesPublic(data=infos, count=len(infos))
|
||||
|
||||
|
||||
def _port_source(request: Request, name: str) -> ValueSource | None:
|
||||
"""The device node a device's own publish on a bound ``in`` port is from.
|
||||
|
||||
Said by the server rather than by the device: the node is what the canvas
|
||||
draws the value leaving from, and the device does not know which node in
|
||||
which flow holds its port.
|
||||
"""
|
||||
device: DeviceDef | None = getattr(request.state, "device", None)
|
||||
devices: DeviceStore | None = getattr(request.app.state, "device_store", None)
|
||||
if device is None or devices is None:
|
||||
return None
|
||||
for bound in devices.bound(device.id):
|
||||
if bound.direction == "in" and bound.message == name:
|
||||
return ValueSource(
|
||||
kind="node",
|
||||
id=f"{bound.flow}.{bound.node}",
|
||||
label=device.title or device.id,
|
||||
detail=bound.port,
|
||||
)
|
||||
return None
|
||||
|
||||
|
||||
@router.post("/{name}", response_model=MessageValue)
|
||||
async def publish_message(
|
||||
name: str, body: PublishRequest, controller: FlowControllerDep
|
||||
name: str, body: PublishRequest, controller: FlowControllerDep, request: Request
|
||||
) -> Any:
|
||||
"""Put a value into the graph, as a dashboard control does.
|
||||
|
||||
Only a message some flow declares can be published to: flows own the
|
||||
namespace, and a dashboard is a client of it rather than a second author.
|
||||
"""
|
||||
source = ValueSource(
|
||||
source = _port_source(request, name) or ValueSource(
|
||||
kind=body.source_kind,
|
||||
id=body.source_id,
|
||||
label=body.source_label or body.source_id or "API",
|
||||
|
||||
@@ -28,7 +28,6 @@ from fluksio.api.deps import (
|
||||
from fluksio.api.routes.runs import elapsed_ms
|
||||
from fluksio.core.config import settings
|
||||
from fluksio.flow.controller import NodeStatus
|
||||
from fluksio.flow.pipeline import ADVISORY_ISSUES
|
||||
from fluksio.models import EngineEvent, FlowRun, Message, MetricBucket
|
||||
|
||||
router = APIRouter(
|
||||
@@ -236,9 +235,7 @@ async def read_summary(request: Request, controller: FlowControllerDep) -> Any:
|
||||
{
|
||||
issue.flow
|
||||
for issue in controller.issues
|
||||
if issue.flow
|
||||
and issue.code != "node_error"
|
||||
and issue.code not in ADVISORY_ISSUES
|
||||
if issue.flow and issue.code != "node_error" and not issue.advisory
|
||||
}
|
||||
)
|
||||
if invalid:
|
||||
|
||||
Reference in New Issue
Block a user