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
@@ -6,7 +6,8 @@
// (license.limit_reached).
// - Edit (name, sign-in mode, link) → PATCH; change app → POST /reassign (signs out whoever
// is on it); remove → POST /revoke (the device goes back to its pairing screen).
// Per-app settings (e.g. a KDS's prep zone) arrive with the apps themselves in phase 3.
// - Per-app settings (3.1a): each app's device_app.config fields are rendered by
// DeviceConfigFields in the pair and edit dialogs and sent as `config`.
import { useCallback, useEffect, useMemo, useState } from 'react'
import toast from 'react-hot-toast'
import { MonitorSmartphone, Plus } from 'lucide-react'
@@ -14,7 +15,8 @@ import { errorMessage, parseError } from '@xenia/api'
import { useTranslation, useFormat } from '@xenia/i18n'
import { Button, Dialog, EmptyState, Field, Input, Select, Spinner } from '@xenia/ui'
import client from '../../api/client'
import { deviceStatus, deviceUsage, hasRoom, linkTargets, normalizeCode } from './devicesLogic'
import DeviceConfigFields from './DeviceConfigFields'
import { configInitial, deviceStatus, deviceUsage, hasRoom, linkTargets, normalizeCode } from './devicesLogic'
const STATUS_TONE = {
online: 'bg-success-soft text-success-ink',
@@ -206,8 +208,10 @@ function PairDialog({ apps, usage, devices, appName, limitLabel, onClose, onPair
const [name, setName] = useState('')
const [mode, setMode] = useState('')
const [link, setLink] = useState('')
const { busy, error, run } = useSubmit(onPair)
const app = apps.find(a => a.key === appKey)
const fields = app?.device_app?.config || []
const [config, setConfig] = useState(() => configInitial(fields))
const { busy, error, run } = useSubmit(onPair)
const full = appKey && !hasRoom(usage, appKey)
const modes = loginModeOptions(app, t)
const ready = code.length === 6 && appKey && name.trim() && !full
@@ -215,7 +219,8 @@ function PairDialog({ apps, usage, devices, appName, limitLabel, onClose, onPair
const submit = e => {
e.preventDefault()
if (!ready || busy) return
run({ code, app_key: appKey, name: name.trim(), login_mode: mode || null, linked_device_id: link ? Number(link) : null })
run({ code, app_key: appKey, name: name.trim(), login_mode: mode || null, linked_device_id: link ? Number(link) : null,
config: fields.length ? config : null })
}
return (
@@ -230,7 +235,11 @@ function PairDialog({ apps, usage, devices, appName, limitLabel, onClose, onPair
className="font-mono tracking-[0.3em] text-center uppercase" autoFocus />
</Field>
<Field label={t('devices.form.app')} error={full ? t('devices.form.full', { label: limitLabel(appKey), max: usage[appKey].max }) : undefined}>
<Select value={appKey} onChange={e => { setAppKey(e.target.value); setMode('') }}
<Select value={appKey} onChange={e => {
const next = e.target.value
setAppKey(next); setMode('')
setConfig(configInitial(apps.find(a => a.key === next)?.device_app?.config))
}}
options={usable.map(a => ({ value: a.key, label: appName(a.key) }))} />
</Field>
<Field label={t('devices.form.name')}>
@@ -247,6 +256,7 @@ function PairDialog({ apps, usage, devices, appName, limitLabel, onClose, onPair
options={[{ value: '', label: t('devices.form.noLink') }, ...linkTargets(devices).map(d => ({ value: String(d.id), label: d.name }))]} />
</Field>
)}
<DeviceConfigFields appKey={appKey} fields={fields} value={config} onChange={setConfig} devices={devices} />
{error && <p className="text-label text-danger-ink" role="alert" data-testid="pair-error">{error}</p>}
<button type="submit" hidden aria-hidden="true" tabIndex={-1} />
</form>
@@ -260,14 +270,18 @@ function EditDialog({ device, apps, devices, onClose, onSave }) {
const [name, setName] = useState(device.name)
const [mode, setMode] = useState(device.login_mode || '')
const [link, setLink] = useState(device.linked_device_id ? String(device.linked_device_id) : '')
const app = apps.find(a => a.key === device.app_key)
const fields = app?.device_app?.config || []
const [config, setConfig] = useState(() => configInitial(fields, device.config))
const { busy, error, run } = useSubmit(onSave)
const modes = loginModeOptions(apps.find(a => a.key === device.app_key), t)
const modes = loginModeOptions(app, t)
const targets = linkTargets(devices, device.id)
const submit = e => {
e.preventDefault()
if (!name.trim() || busy) return
run({ name: name.trim(), login_mode: mode || null, linked_device_id: link ? Number(link) : null })
run({ name: name.trim(), login_mode: mode || null, linked_device_id: link ? Number(link) : null,
...(fields.length ? { config } : {}) })
}
return (
@@ -288,6 +302,7 @@ function EditDialog({ device, apps, devices, onClose, onSave }) {
options={[{ value: '', label: t('devices.form.noLink') }, ...targets.map(d => ({ value: String(d.id), label: d.name }))]} />
</Field>
)}
<DeviceConfigFields appKey={device.app_key} fields={fields} value={config} onChange={setConfig} devices={devices} selfId={device.id} />
{error && <p className="text-label text-danger-ink" role="alert">{error}</p>}
<button type="submit" hidden aria-hidden="true" tabIndex={-1} />
</form>