Files
OpenSquawk/server/api/bridge/command-result.post.ts
itsrubberduck 3fe0ea5f8a feat(sim-control): wire frequency-sim-control command channel end-to-end
Implements the open items from docs/plans/2026-07-14-frequency-sim-control-design.md
§4/§"Offen für die Implementierungsphase": a per-bridge-token in-memory command
queue piggybacked on the existing telemetry channel, plus the client-side gate
and TTS confirmations.

- server/utils/simControlQueue.ts: TTL-based queue keyed by bridge token
  (enqueue → drainPending → resolve → drainResultsForClient).
- server/api/bridge/data.post.ts: response gains a `commands` field the
  bridge drains on its next telemetry POST.
- server/api/bridge/command.post.ts (new): client enqueues a parsed command,
  re-validated server-side via isValidSimControlCommand.
- server/api/bridge/command-result.post.ts (new): bridge reports ok/failed.
- server/api/bridge/live.get.ts: response gains `commandResults` so the
  client can announce outcomes.
- shared/utils/simControl.ts: wire types, isValidSimControlCommand, and
  simControlRejectionSpeech/simControlResultSpeech TTS phrasing.
- useLiveAtcSession.ts: parseSimControl() gated on bridgeConnected, wired in
  right after the local special cases and before the frequency check —
  matched commands never reach radioBackend.transmit().
- useSimBridgeSync.ts / live-atc.vue: bridgeToken threaded through, command
  results forwarded from the telemetry poll to TTS.

43 new tests (shared parser/validation/speech + server queue lifecycle/TTL).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-15 09:31:17 +02:00

56 lines
1.9 KiB
TypeScript

import { createError, readBody } from 'h3'
import { BridgeToken } from '../../models/BridgeToken'
import { getBridgeTokenFromHeader } from '../../utils/bridge'
import { logBridgeEvent } from '../../utils/bridgeLog'
import { simControlQueue } from '../../utils/simControlQueue'
interface CommandResultBody {
id?: string
status?: 'ok' | 'failed'
reason?: string
}
/**
* The bridge reports the outcome of a previously delivered frequency-sim-
* control command (design doc §4).
*
* POST /api/bridge/command-result
* x-bridge-token: <bridge-token>
* body: { id: string, status: 'ok' | 'failed', reason?: string }
*/
export default defineEventHandler(async (event) => {
const token = getBridgeTokenFromHeader(event)
if (!token) {
throw createError({ statusCode: 401, statusMessage: 'x-bridge-token header fehlt oder ist ungültig.' })
}
const exists = await BridgeToken.exists({ token })
if (!exists) {
throw createError({ statusCode: 404, statusMessage: 'Bridge-Token ist nicht verknüpft.' })
}
const body = await readBody<CommandResultBody>(event)
const id = typeof body?.id === 'string' ? body.id : ''
const status = body?.status
if (!id || (status !== 'ok' && status !== 'failed')) {
throw createError({ statusCode: 400, statusMessage: "id und status ('ok'|'failed') sind erforderlich." })
}
const resolved = simControlQueue.resolve(token, id, status, body?.reason)
console.info(
`\x1b[32m[bridge:command-result]\x1b[0m token=\x1b[96m${token.slice(0, 6)}...\x1b[0m id=\x1b[96m${id.slice(0, 8)}\x1b[0m status=\x1b[93m${status}\x1b[0m resolved=\x1b[93m${resolved}\x1b[0m`,
)
logBridgeEvent(token, {
endpoint: '/api/bridge/command-result',
method: 'POST',
statusCode: 200,
color: status === 'ok' ? '#22c55e' : '#ef4444',
summary: `${id.slice(0, 8)}${status}${body?.reason ? `: ${body.reason}` : ''}`,
data: { id, status, reason: body?.reason ?? null, resolved },
})
return { ok: resolved }
})