LegacyAdapter already translates incoming v1 requests to v2 internal commands, but v2 handler replies used v2-only conventions (namespaced type strings, data nested under "data", UPPERCASE status enum, object-keyed strike_counters). A v1-only Flutter app polling system.status never recognised the reply, hit its own 5s request timeout, and its catch block explicitly set the UI to "unavailable" — even though the device was playing correctly and had already replied in time. Not a connectivity bug; a pure reply-schema mismatch. Adds LegacyResponseAdapter, the reply-side mirror of LegacyAdapter: a narrow, allowlisted translator that rewrites a CommandResult back into v1 shape, but only for replies to requests whose original envelope version was 1. Wired into WebSocket, MQTT, and HTTP's general command path by capturing envelope.version before CommandBus::dispatch() overwrites it to 2. v2 clients and all other reply types are untouched, byte-for-byte. This is deliberately temporary migration scaffolding for the v1→v2 tablet rollout window, not new permanent architecture — designed to be deleted alongside LegacyAdapter in one PR once fleet telemetry shows zero v1 requests. Full rationale and removal steps in docs/architecture/legacy-response-adapter.md. Verified: pio run across all four environments (vesper-std-v1, agnus-v1, agnus-mini-v1, bespoke-rs485-v1). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
133 lines
8.0 KiB
Markdown
133 lines
8.0 KiB
Markdown
# LegacyResponseAdapter — v2 → v1 reply translation
|
|
|
|
> Written: 2026-09-17
|
|
> Status: **Active** — temporary migration scaffolding, scoped to a known removal condition
|
|
> Related: [communication-refactor-plan.md](communication-refactor-plan.md), [reference/api-reference.md](../reference/api-reference.md), [reference/api-reference-v1-legacy.md](../reference/api-reference-v1-legacy.md)
|
|
|
|
---
|
|
|
|
## Why this exists
|
|
|
|
The firmware's `LegacyAdapter` (`src/Communication/CommandBus/LegacyAdapter.cpp`) already translates
|
|
incoming v1 **requests** (no `"v"` field, or `"v":1` — sent by tablets still running the pre-refactor
|
|
Flutter app) into v2 internal commands, so old and new clients can share the same handler code. That
|
|
half of the migration bridge works correctly.
|
|
|
|
The other half was missing: v2 handlers reply using v2 conventions (namespaced `type` strings, data
|
|
nested under `"data"` instead of `"payload"`, UPPERCASE status-enum strings, object-keyed
|
|
`strike_counters` instead of an array). A v1-only app doesn't recognise any of that. Concretely, for
|
|
`system.status` (the v1 app's `system_info/report_status` poll):
|
|
|
|
| Aspect | v1 app expects | v2 firmware sent (before this fix) |
|
|
|---|---|---|
|
|
| Reply `type` | `"current_status"` | `"system.status"` |
|
|
| Structured field container | `payload` | `data` |
|
|
| `player_status` value | lowercase (`"playing"`, `"idle"`, ...) | UPPERCASE (`"PLAYING"`, `"STOPPED"`, ...) |
|
|
| Elapsed-time field | `time_elapsed` | `time_elapsed_ms` |
|
|
| `strike_counters` | JSON array | JSON object keyed `"0".."15"` |
|
|
|
|
Because the reply never matched what the app's `sendAndWaitForResponse` matcher was waiting for, the
|
|
app's own 5-second request timeout fired, hit its `catch` block, and explicitly set
|
|
`playbackStatus: PlaybackStatus.unavailable` — **while the device was actually playing correctly and
|
|
had already replied.** This looked like an intermittent connectivity/availability bug but was a pure
|
|
reply-schema mismatch. `ResponseBuilder::deviceStatus()` (still present in
|
|
`src/Communication/ResponseBuilder/ResponseBuilder.cpp`) already builds the exact correct v1 shape —
|
|
it's dead code, unwired since the v2 refactor, which is the strongest evidence this was a regression
|
|
rather than an intentional break.
|
|
|
|
The async `playback` INFO push (`Player::setStatus()`, `src/Player/Player.cpp:519-565`) was checked
|
|
too and does **not** have this problem — it already uses `type/payload`, `action` (not
|
|
`player_status`), and lowercase status words, matching what the v1 app expects. No changes were made
|
|
there.
|
|
|
|
## What this migration is (and isn't)
|
|
|
|
The plan going forward is to update **all** devices (firmware + tablets) to the v2 API. Until every
|
|
tablet in the field is confirmed on v2+, some boards running v2 firmware will still be talking to
|
|
tablets that only understand v1. `LegacyResponseAdapter` exists purely to keep that mixed-fleet window
|
|
functional — it is not a permanent second protocol.
|
|
|
|
**Exit condition:** once fleet/console telemetry confirms zero v1-originated requests (no envelope
|
|
arriving with `"v"` missing or `"v":1`) for a safe observation period, delete this adapter and
|
|
`LegacyAdapter` together — see "How to remove" below.
|
|
|
|
## What was built
|
|
|
|
### `LegacyResponseAdapter` (`src/Communication/CommandBus/LegacyResponseAdapter.{hpp,cpp}`)
|
|
|
|
A narrow, allowlisted translator — the reply-side mirror of `LegacyAdapter`. It does **not** attempt a
|
|
generic v1/v2 schema mapping; it only rewrites the specific `CommandResult` types known to diverge.
|
|
Today that's just `system.status`. If a future v2 handler introduces another reply shape a v1 client
|
|
depends on, add a case to `LegacyResponseAdapter.cpp` rather than generalising the mechanism — keeping
|
|
it a short, explicit list makes it easy to audit and easy to delete in full later.
|
|
|
|
```cpp
|
|
// Returns true and fills `out` if a translation was applied.
|
|
// Returns false (out untouched) if the result isn't one this adapter
|
|
// knows how to translate — caller falls back to normal v2 serialisation.
|
|
bool translateLegacyResponse(const CommandResult& result, JsonDocument& out);
|
|
```
|
|
|
|
### Wiring: capture the *original* request version before dispatch mutates it
|
|
|
|
`CommandBus::dispatch()` overwrites `envelope.version = 2` after `LegacyAdapter` translates a v1
|
|
envelope (so the bus doesn't re-translate on a hypothetical second pass). That happens *before* the
|
|
reply lambda runs, so the reply lambda can't just read `envelope.version` at reply time — it has to
|
|
capture the original value by copy, at the point the lambda is constructed (which happens after
|
|
`envelope.version` is set from the incoming `"v"` field, but before `_bus.dispatch(envelope)` runs).
|
|
|
|
Each transport's reply lambda now does this:
|
|
|
|
```cpp
|
|
envelope.version = doc["v"] | 1; // (unchanged — existing per-transport default)
|
|
|
|
// Capture the ORIGINAL version before CommandBus::dispatch() rewrites
|
|
// envelope.version to 2 post-translation (see LegacyAdapter.hpp).
|
|
bool isLegacyRequest = (envelope.version == 1);
|
|
|
|
envelope.reply = [this, /* ... */, isLegacyRequest](const CommandResult& result) {
|
|
JsonDocument resp;
|
|
if (!isLegacyRequest || !translateLegacyResponse(result, resp)) {
|
|
// ... existing v2 serialisation, unchanged ...
|
|
}
|
|
// ... serialise `resp` and send, unchanged ...
|
|
};
|
|
```
|
|
|
|
If `isLegacyRequest` is false, or the result's `type` isn't one `LegacyResponseAdapter` knows about,
|
|
behaviour is byte-for-byte identical to before this change — v2 clients and unaffected reply types are
|
|
untouched.
|
|
|
|
### Scope — which transports were changed
|
|
|
|
| Transport | Changed? | Reason |
|
|
|---|---|---|
|
|
| WebSocket (`WebSocketTransport.cpp`) | ✅ | Primary tablet transport — this is the live bug's path |
|
|
| MQTT (`MQTTTransport.cpp`) | ✅ | Same request/reply shape, same v1 default; console/backend or any MQTT-side v1 caller gets the same fix |
|
|
| HTTP (`HTTPTransport.cpp`, `handleCommandRequest`) | ✅ | Defaults unversioned requests to v2 already (`doc["v"] \| 2`, unlike the other transports' `\| 1`), so this only fires if a caller explicitly sends `"v":1` — added for symmetry/completeness, not a known live bug |
|
|
| HTTP (`HTTPTransport.cpp`, `handleStatusRequest`) | ❌ | Hardcodes `envelope.version = 2` — internal shortcut route, never receives a v1-tagged request |
|
|
| UART (`UARTTransport.cpp`) | ❌ | Used for board-to-board/internal links per `UARTTransport::WHITELIST`, not tablets. No evidence any UART peer speaks v1. Left alone to avoid unnecessary risk — revisit if that assumption changes |
|
|
| `CommandBus::broadcast()` | ❌ | Separate mechanism (internal subsystem → client push events, e.g. `_bus.broadcast(CommandResult::info(...))`), unrelated to per-request replies or to `Player`'s playback push. Not exercised by the bug. |
|
|
|
|
## How to remove (when the migration is complete)
|
|
|
|
This was designed to be deleted as a single, contained change:
|
|
|
|
1. Delete `src/Communication/CommandBus/LegacyResponseAdapter.hpp` and `.cpp`.
|
|
2. In each of `WebSocketTransport.cpp`, `MQTTTransport.cpp`, `HTTPTransport.cpp`:
|
|
- Remove the `#include ".../LegacyResponseAdapter.hpp"` line.
|
|
- Remove the `bool isLegacyRequest = ...` line.
|
|
- Remove the `if (!isLegacyRequest || !translateLegacyResponse(result, resp))` wrapper, keeping the
|
|
v2 serialisation body unconditional (drop the `if`, keep the `{ ... }` contents).
|
|
3. Do the same removal for `LegacyAdapter` (see its own header comment in
|
|
`src/Communication/CommandBus/LegacyAdapter.hpp`) in the same PR — the two are a matched pair; there
|
|
is no reason to keep one without the other.
|
|
4. Delete `ResponseBuilder::deviceStatus()` (dead code, superseded by this adapter) if it's still
|
|
unused at that point — confirm with a repo-wide grep first.
|
|
5. Update `docs/README.md`'s reference table and this file's own status line to reflect removal, and
|
|
move `docs/reference/api-reference-v1-legacy.md` content into a clearly historical/archived note if
|
|
it isn't already treated that way.
|
|
|
|
No data migration, no NVS changes, no wire-format changes for v2-only clients — this is a pure code
|
|
deletion once the exit condition is met.
|