fix(Communication): add LegacyResponseAdapter — v1 tablets saw device as unavailable while playing (F-061)
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>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#include "LegacyResponseAdapter.hpp"
|
||||
#include "CommandResult.hpp"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Known v2 → v1 reply divergences.
|
||||
//
|
||||
// Each entry here corresponds to a v2 CommandResult "type" that a v1 client
|
||||
// polls for and parses with hardcoded v1 field names/shapes. Add a new case
|
||||
// only when a real v1 caller is confirmed to depend on the old shape — see
|
||||
// the file header for why this stays a narrow allowlist rather than a
|
||||
// generic mapper.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// system.status → current_status
|
||||
// v1 app: docs/reference/dart-command-handler.dart SystemInfo.getDeviceStatus()
|
||||
// matches on type=="current_status", reads payload.player_status (lowercase),
|
||||
// payload.time_elapsed, payload.strike_counters as an array.
|
||||
static bool translateSystemStatus(const CommandResult& result, JsonDocument& out) {
|
||||
JsonObjectConst data = result.data.as<JsonObjectConst>();
|
||||
|
||||
out["status"] = "SUCCESS";
|
||||
out["type"] = "current_status";
|
||||
JsonObject payload = out["payload"].to<JsonObject>();
|
||||
|
||||
// v2 sends UPPERCASE enum names (PLAYING/PAUSED/STOPPING/STOPPED);
|
||||
// v1 expects lowercase state words, with STOPPED spelled "idle".
|
||||
const char* v2Status = data["player_status"] | "STOPPED";
|
||||
const char* v1Status = "idle";
|
||||
if (strcmp(v2Status, "PLAYING") == 0) v1Status = "playing";
|
||||
else if (strcmp(v2Status, "PAUSED") == 0) v1Status = "paused";
|
||||
else if (strcmp(v2Status, "STOPPING") == 0) v1Status = "stopping";
|
||||
|
||||
payload["player_status"] = v1Status;
|
||||
payload["time_elapsed"] = data["time_elapsed_ms"] | 0; // same unit (ms), renamed key
|
||||
payload["projected_run_time"] = data["projected_run_time"] | 0;
|
||||
|
||||
// v2 sends strike_counters as an object keyed "0".."15"; v1 expects an array.
|
||||
JsonArray strikes = payload["strike_counters"].to<JsonArray>();
|
||||
JsonObjectConst v2Strikes = data["strike_counters"].as<JsonObjectConst>();
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
strikes.add(v2Strikes[String(i)] | 0);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool translateLegacyResponse(const CommandResult& result, JsonDocument& out) {
|
||||
if (result.type == "system.status" && result.hasData) {
|
||||
return translateSystemStatus(result, out);
|
||||
}
|
||||
|
||||
// Not a known divergence — caller falls back to standard v2 serialisation.
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// LegacyResponseAdapter — v2 → v1 reply translation
|
||||
//
|
||||
// Companion to LegacyAdapter (which translates v1 *requests* into v2 before
|
||||
// dispatch). This adapter runs the other direction: it takes the v2
|
||||
// CommandResult a handler produced and rewrites it into the wire shape a v1
|
||||
// client expects, but ONLY for replies going back to a client whose original
|
||||
// request came in as v1 (no "v" field, or "v":1).
|
||||
//
|
||||
// Why this exists:
|
||||
// V1 tablets (old Flutter app, pre-refactor) send commands without a "v"
|
||||
// field. LegacyAdapter maps those onto v2 internal commands so the same
|
||||
// handler code services both old and new clients. But v2 handlers reply
|
||||
// using the v2 envelope/field conventions (e.g. "system.status" instead of
|
||||
// "current_status", data nested under "data" instead of "payload",
|
||||
// UPPERCASE status enum strings, "strike_counters" as an object keyed by
|
||||
// index instead of an array). A v1-only app doesn't recognise any of that,
|
||||
// so from its point of view the device never replies — its own
|
||||
// request-timeout logic then marks the device "unavailable" even though it
|
||||
// answered correctly and promptly. See docs/architecture/legacy-response-adapter.md
|
||||
// for the full incident writeup.
|
||||
//
|
||||
// Translation is intentionally narrow: it only rewrites the handful of
|
||||
// fields/types that are actually known to diverge (see the .cpp). It is NOT
|
||||
// a generic v1/v2 schema mapper — anything not explicitly listed here is
|
||||
// passed through unchanged. If a future v2 handler introduces another
|
||||
// diverging reply shape that v1 clients rely on, add a case here rather than
|
||||
// generalising the mechanism.
|
||||
//
|
||||
// This is temporary migration scaffolding, not permanent architecture. Once
|
||||
// fleet telemetry (or "identify" logs) show zero v1 requests for a safe
|
||||
// observation period, delete this adapter, LegacyAdapter, and the call sites
|
||||
// in the transports together in one PR — see
|
||||
// docs/architecture/legacy-response-adapter.md for the exact removal steps.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
|
||||
struct CommandResult;
|
||||
|
||||
// Rewrites `result` into a v1-shaped reply JsonDocument if (and only if) its
|
||||
// `type` is one this adapter knows how to translate. Returns true and fills
|
||||
// `out` if a translation was applied; returns false (leaving `out`
|
||||
// untouched) if the result should be serialised normally — the caller must
|
||||
// fall back to the standard v2 serialisation in that case.
|
||||
bool translateLegacyResponse(const CommandResult& result, JsonDocument& out);
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../../CommandBus/CommandBus.hpp"
|
||||
#include "../../CommandBus/CommandEnvelope.hpp"
|
||||
#include "../../CommandBus/CommandResult.hpp"
|
||||
#include "../../CommandBus/LegacyResponseAdapter.hpp"
|
||||
#include "../../../ConfigManager/ConfigManager.hpp"
|
||||
|
||||
#define TAG "HTTPTransport"
|
||||
@@ -92,18 +93,26 @@ void HTTPTransport::handleCommandRequest(AsyncWebServerRequest* request,
|
||||
envelope.contents.set(doc["contents"]);
|
||||
}
|
||||
|
||||
// Capture the ORIGINAL version before CommandBus::dispatch() rewrites
|
||||
// envelope.version to 2 post-translation (see LegacyAdapter.hpp). HTTP
|
||||
// defaults unversioned requests to v2 (unlike WS/MQTT/UART, which default
|
||||
// to v1) — this only fires for a caller that explicitly sends "v":1.
|
||||
bool isLegacyRequest = (envelope.version == 1);
|
||||
|
||||
// Capture request pointer. The reply lambda must call request->send() exactly once.
|
||||
// AsyncWebServer keeps the request alive until we call send() or the connection closes.
|
||||
envelope.reply = [this, request](const CommandResult& result) {
|
||||
envelope.reply = [this, request, isLegacyRequest](const CommandResult& result) {
|
||||
JsonDocument resp;
|
||||
resp["status"] = result.isOk() ? "SUCCESS"
|
||||
: (result.status == CommandResult::Status::INFO ? "INFO" : "ERROR");
|
||||
resp["type"] = result.type;
|
||||
if (!result.payload.isEmpty()) {
|
||||
resp["message"] = result.payload;
|
||||
}
|
||||
if (result.hasData) {
|
||||
resp["data"] = result.data;
|
||||
if (!isLegacyRequest || !translateLegacyResponse(result, resp)) {
|
||||
resp["status"] = result.isOk() ? "SUCCESS"
|
||||
: (result.status == CommandResult::Status::INFO ? "INFO" : "ERROR");
|
||||
resp["type"] = result.type;
|
||||
if (!result.payload.isEmpty()) {
|
||||
resp["message"] = result.payload;
|
||||
}
|
||||
if (result.hasData) {
|
||||
resp["data"] = result.data;
|
||||
}
|
||||
}
|
||||
String out;
|
||||
serializeJson(resp, out);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../../CommandBus/CommandBus.hpp"
|
||||
#include "../../CommandBus/CommandEnvelope.hpp"
|
||||
#include "../../CommandBus/CommandResult.hpp"
|
||||
#include "../../CommandBus/LegacyResponseAdapter.hpp"
|
||||
|
||||
#define TAG "MQTTTransport"
|
||||
#include "../../../Logging/Logging.hpp"
|
||||
@@ -77,17 +78,23 @@ void MQTTTransport::onMessage(const String& topic, const String& payload) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Capture the ORIGINAL version before CommandBus::dispatch() rewrites
|
||||
// envelope.version to 2 post-translation (see LegacyAdapter.hpp).
|
||||
bool isLegacyRequest = (envelope.version == 1);
|
||||
|
||||
// Reply lambda — publishes the response back to the MQTT data topic
|
||||
envelope.reply = [this](const CommandResult& result) {
|
||||
envelope.reply = [this, isLegacyRequest](const CommandResult& result) {
|
||||
JsonDocument resp;
|
||||
resp["status"] = result.isOk() ? "SUCCESS"
|
||||
: (result.status == CommandResult::Status::INFO ? "INFO" : "ERROR");
|
||||
resp["type"] = result.type;
|
||||
if (!result.payload.isEmpty()) {
|
||||
resp["message"] = result.payload;
|
||||
}
|
||||
if (result.hasData) {
|
||||
resp["data"] = result.data;
|
||||
if (!isLegacyRequest || !translateLegacyResponse(result, resp)) {
|
||||
resp["status"] = result.isOk() ? "SUCCESS"
|
||||
: (result.status == CommandResult::Status::INFO ? "INFO" : "ERROR");
|
||||
resp["type"] = result.type;
|
||||
if (!result.payload.isEmpty()) {
|
||||
resp["message"] = result.payload;
|
||||
}
|
||||
if (result.hasData) {
|
||||
resp["data"] = result.data;
|
||||
}
|
||||
}
|
||||
String out;
|
||||
serializeJson(resp, out);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#include "../../CommandBus/CommandBus.hpp"
|
||||
#include "../../CommandBus/CommandEnvelope.hpp"
|
||||
#include "../../CommandBus/CommandResult.hpp"
|
||||
#include "../../CommandBus/LegacyResponseAdapter.hpp"
|
||||
|
||||
#define TAG "WebSocketTransport"
|
||||
#include "../../../Logging/Logging.hpp"
|
||||
@@ -75,17 +76,23 @@ void WebSocketTransport::onMessage(uint32_t clientId, const JsonDocument& doc) {
|
||||
envelope.contents.set(doc["contents"]);
|
||||
}
|
||||
|
||||
// Capture the ORIGINAL version before CommandBus::dispatch() rewrites
|
||||
// envelope.version to 2 post-translation (see LegacyAdapter.hpp).
|
||||
bool isLegacyRequest = (envelope.version == 1);
|
||||
|
||||
// Reply routes back to the originating WebSocket client only
|
||||
envelope.reply = [this, clientId](const CommandResult& result) {
|
||||
envelope.reply = [this, clientId, isLegacyRequest](const CommandResult& result) {
|
||||
JsonDocument resp;
|
||||
resp["status"] = result.isOk() ? "SUCCESS"
|
||||
: (result.status == CommandResult::Status::INFO ? "INFO" : "ERROR");
|
||||
resp["type"] = result.type;
|
||||
if (!result.payload.isEmpty()) {
|
||||
resp["message"] = result.payload;
|
||||
}
|
||||
if (result.hasData) {
|
||||
resp["data"] = result.data;
|
||||
if (!isLegacyRequest || !translateLegacyResponse(result, resp)) {
|
||||
resp["status"] = result.isOk() ? "SUCCESS"
|
||||
: (result.status == CommandResult::Status::INFO ? "INFO" : "ERROR");
|
||||
resp["type"] = result.type;
|
||||
if (!result.payload.isEmpty()) {
|
||||
resp["message"] = result.payload;
|
||||
}
|
||||
if (result.hasData) {
|
||||
resp["data"] = result.data;
|
||||
}
|
||||
}
|
||||
String out;
|
||||
serializeJson(resp, out);
|
||||
|
||||
Reference in New Issue
Block a user