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:
@@ -0,0 +1,61 @@
|
||||
// Per-device settings of an app, rendered from its manifest (plan step 3.1a): the
|
||||
// fields come from GET /api/platform/modules → device_app.config, the texts from
|
||||
// modules:<app>.config.<key>.{label,hint,choices.<value>}. The backend validates them
|
||||
// (validation.device_config). Used by the pair and edit dialogs of DevicesPage.
|
||||
import { useEffect, useState } from 'react'
|
||||
import { useTranslation } from '@xenia/i18n'
|
||||
import { Field, Input, Select, Switch } from '@xenia/ui'
|
||||
import client from '../../api/client'
|
||||
import { configValue, refOptions } from './devicesLogic'
|
||||
|
||||
export default function DeviceConfigFields({ appKey, fields = [], value, onChange, devices = [], selfId = null }) {
|
||||
const { t } = useTranslation('manager')
|
||||
const needsZones = fields.some(f => f.choices_from === 'prep_zones')
|
||||
const [zones, setZones] = useState([])
|
||||
|
||||
useEffect(() => {
|
||||
if (!needsZones) return undefined
|
||||
let alive = true
|
||||
client.get('/api/prep-zones').then(r => { if (alive) setZones(r.data) }).catch(() => {})
|
||||
return () => { alive = false }
|
||||
}, [needsZones])
|
||||
|
||||
if (!fields.length) return null
|
||||
const base = key => `modules:${appKey}.config.${key}`
|
||||
const set = (f, raw) => onChange({ ...value, [f.key]: configValue(f, raw) })
|
||||
|
||||
return (
|
||||
<div className="grid gap-4" data-testid="device-config">
|
||||
{fields.map(f => {
|
||||
const label = t(`${base(f.key)}.label`, { defaultValue: f.key })
|
||||
const hint = t(`${base(f.key)}.hint`, { defaultValue: '' }) || undefined
|
||||
const v = value?.[f.key]
|
||||
if (f.type === 'bool') {
|
||||
return <Switch key={f.key} checked={!!v} onChange={on => set(f, on)} label={label} description={hint} data-config={f.key} />
|
||||
}
|
||||
if (f.choices?.length) {
|
||||
return (
|
||||
<Field key={f.key} label={label} hint={hint}>
|
||||
<Select value={v == null ? '' : String(v)} onChange={e => set(f, e.target.value)} data-config={f.key}
|
||||
options={f.choices.map(c => ({ value: String(c), label: t(`${base(f.key)}.choices.${c}`, { defaultValue: String(c) }) }))} />
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
if (f.type === 'ref') {
|
||||
return (
|
||||
<Field key={f.key} label={label} hint={hint}>
|
||||
<Select value={v == null ? '' : String(v)} onChange={e => set(f, e.target.value)} data-config={f.key}
|
||||
options={[{ value: '', label: t('devices.form.none') }, ...refOptions(f, { zones, devices, selfId })]} />
|
||||
</Field>
|
||||
)
|
||||
}
|
||||
return (
|
||||
<Field key={f.key} label={label} hint={hint}>
|
||||
<Input type={f.type === 'int' ? 'number' : 'text'} value={v ?? ''} min={f.min ?? undefined} max={f.max ?? undefined}
|
||||
onChange={e => set(f, e.target.value)} data-config={f.key} />
|
||||
</Field>
|
||||
)
|
||||
})}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
@@ -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>
|
||||
|
||||
@@ -53,3 +53,31 @@ export function hasRoom(usage, appKey, { moving = false } = {}) {
|
||||
export function linkTargets(devices, selfId = null) {
|
||||
return devices.filter(d => !d.revoked_at && d.id !== selfId)
|
||||
}
|
||||
|
||||
// ── per-device settings (plan step 3.1a) ────────────────────────────────────
|
||||
// `fields` = the app's device_app.config from GET /api/platform/modules:
|
||||
// [{ key, type: 'bool'|'int'|'str'|'enum'|'ref', default, choices, min, max, choices_from }]
|
||||
|
||||
/** The form's starting values: the device's config (already effective) or the defaults. */
|
||||
export function configInitial(fields = [], current = null) {
|
||||
return Object.fromEntries(fields.map(f => [f.key, current && f.key in current ? current[f.key] : f.default]))
|
||||
}
|
||||
|
||||
/** A form control's raw value → the value sent to the API (selects and inputs give strings). */
|
||||
export function configValue(field, raw) {
|
||||
if (field.type === 'bool') return !!raw
|
||||
if (field.type === 'int') return raw === '' || raw == null ? null : Number(raw)
|
||||
if (field.type === 'ref') return raw === '' || raw == null ? null : Number(raw)
|
||||
if (field.type === 'enum') return raw
|
||||
return raw == null ? '' : String(raw)
|
||||
}
|
||||
|
||||
/** Options of a `ref` field: prep zones, or live devices of one app ("devices:<app>"). */
|
||||
export function refOptions(field, { zones = [], devices = [], selfId = null } = {}) {
|
||||
if (field.choices_from === 'prep_zones') return zones.map(z => ({ value: String(z.id), label: z.name }))
|
||||
if (field.choices_from?.startsWith('devices:')) {
|
||||
const app = field.choices_from.slice('devices:'.length)
|
||||
return devices.filter(d => !d.revoked_at && d.app_key === app && d.id !== selfId).map(d => ({ value: String(d.id), label: d.name }))
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user