feat(backend): live connection and per-device settings for paired screens (platform-v2 3.1a)

- WebSocket: credentials may come in the first frame ({cursor, token?, device?}).
  A device token alone connects a paired screen (login-none apps get every event, a
  sign-in app only events addressed to it); a terminal session's JWT needs its own
  device, as over HTTP. Refusals send {type: refused, data: {code}} + close 4001/4003.
- ws_bus: registry of open sockets by user, session and device. Ending a session
  closes its sockets (session.ended); revoke/reassign close the device's sockets
  (device.revoked / device.updated); edits and module on/off send device.updated.
  Events can target devices (device_ids=..., sync_events.target_device_ids, rev 0014).
  Pushes go through each socket's own loop (call_soon_threadsafe).
- Device config: DeviceApp.config = typed ConfigFields, validated on pair/edit/
  reassign (validation.device_config), effective values (defaults filled) in both
  device views and the module catalogue. First field: auto_sign_out_minutes (KDS, POS).
- Manager: DeviceConfigFields renders an app's fields in the pair and edit dialogs.
- @xenia/api createLiveClient: `credentials` and `onRefused` options.
- Tests: tests/test_live.py (11), module texts for config fields, live client,
  devices logic, manager.mjs device setting + live socket checks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-14 16:31:59 +02:00
co-authored by Claude Opus 5.5
parent 034426f870
commit bda5cde92b
31 changed files with 974 additions and 145 deletions
+25 -5
View File
@@ -19,6 +19,11 @@ Agent notes:
sessions ended). Rows are never deleted.
- Login on a device: POST /api/auth/login {client: "terminal", ...} with the device header
(routers/auth.py). The login mode is effective_login_mode(); tap/none exist only here.
- `config` is validated against the app's ConfigFields (core/modules/device_config.py,
3.1a); both views return the effective config (every field, defaults filled).
- Live (3.1a): an edit sends `device.updated` to the device's open sockets; reassign sends
`device.updated` and closes them; revoke sends `device.revoked` and closes them
(services/ws_bus.py). The screen re-reads GET /me or goes back to pairing.
"""
from __future__ import annotations
@@ -36,12 +41,14 @@ from sqlalchemy.orm import Session
from core import sessions
from core.access.context import hash_token
from core.modules import device_config
from core.access.deps import public, require, require_device
from core.errors import XeniaError
from core.modules.registry import registry
from core.timeutil import iso_z, utcnow
from database import get_db
from models.device import Device, DevicePairRequest
from services import ws_bus
router = APIRouter()
@@ -143,6 +150,14 @@ def effective_login_mode(db, device) -> str:
return mode if mode in modes else modes[0]
def _config(app_key: str, config, db) -> dict | None:
return device_config.validate(device_config.for_app(app_key), config, db)
def _effective_config(d: Device, db) -> dict:
return device_config.effective(device_config.for_app(d.app_key), d.config, db)
def _get(db, device_id: int) -> Device:
d = db.get(Device, device_id)
if d is None:
@@ -153,7 +168,8 @@ def _get(db, device_id: int) -> Device:
def _out(d: Device, db) -> dict:
"""Manager view of a device."""
return {
"id": d.id, "uuid": d.uuid, "name": d.name, "app_key": d.app_key, "config": d.config,
"id": d.id, "uuid": d.uuid, "name": d.name, "app_key": d.app_key,
"config": _effective_config(d, db) if d.app_key in registry() else {},
"login_mode": d.login_mode,
"effective_login_mode": effective_login_mode(db, d) if d.app_key in registry() else None,
"linked_device_id": d.linked_device_id, "user_agent": d.user_agent,
@@ -169,7 +185,7 @@ def _self_out(d: Device, db) -> dict:
linked = db.get(Device, d.linked_device_id) if d.linked_device_id else None
status = "ok" if is_effective(d.app_key, db) else blocked_error(d.app_key, db).code
return {
"id": d.id, "uuid": d.uuid, "name": d.name, "app_key": d.app_key, "config": d.config,
"id": d.id, "uuid": d.uuid, "name": d.name, "app_key": d.app_key, "config": _effective_config(d, db),
"login_mode": effective_login_mode(db, d),
"linked_device": ({"id": linked.id, "uuid": linked.uuid, "name": linked.name, "app_key": linked.app_key}
if linked is not None and linked.revoked_at is None else None),
@@ -262,7 +278,8 @@ def approve_device(body: PairApprove, db: Session = Depends(get_db), user=Depend
_app(body.app_key)
_require_effective(body.app_key, db)
_check_limit(db, body.app_key)
d = Device(uuid=uuidlib.uuid4().hex, name=body.name.strip(), app_key=body.app_key, config=body.config,
d = Device(uuid=uuidlib.uuid4().hex, name=body.name.strip(), app_key=body.app_key,
config=_config(body.app_key, body.config, db),
login_mode=_check_mode(body.app_key, body.login_mode),
linked_device_id=_check_link(db, None, body.linked_device_id),
user_agent=row.user_agent, paired_at=now, paired_by=getattr(user, "id", None) or None)
@@ -308,12 +325,13 @@ def update_device(device_id: int, body: DevicePatch, db: Session = Depends(get_d
if "name" in sent and body.name:
d.name = body.name.strip()
if "config" in sent:
d.config = body.config
d.config = _config(d.app_key, body.config, db)
if "login_mode" in sent:
d.login_mode = _check_mode(d.app_key, body.login_mode)
if "linked_device_id" in sent:
d.linked_device_id = _check_link(db, d.id, body.linked_device_id)
db.commit()
ws_bus.notify_device([d.id])
return _out(d, db)
@@ -328,11 +346,12 @@ def reassign_device(device_id: int, body: DeviceReassign, db: Session = Depends(
_require_effective(body.app_key, db)
if body.app_key != d.app_key:
_check_limit(db, body.app_key, exclude_id=d.id)
d.config = _config(body.app_key, body.config, db)
d.app_key = body.app_key
d.config = body.config
if d.login_mode and d.login_mode not in _app(body.app_key).device_app.login_modes:
d.login_mode = None
db.commit()
ws_bus.end_device(d.id, "device.updated", {"app_key": d.app_key}) # before the session's own close
sessions.end_for_device(db, d.id, "device_reassigned")
return _out(d, db)
@@ -347,5 +366,6 @@ def revoke_device(device_id: int, db: Session = Depends(get_db), user=Depends(re
db.query(Device).filter(Device.linked_device_id == d.id).update({Device.linked_device_id: None})
db.query(DevicePairRequest).filter(DevicePairRequest.device_id == d.id).delete()
db.commit()
ws_bus.end_device(d.id, "device.revoked")
sessions.end_for_device(db, d.id, "device_revoked")
return _out(d, db)