feat(comms): Stages 3–6 — transport layer, router simplification, legacy adapter, WebServerTransport
## Stage 3 — Transport Layer
- MQTTTransport: wraps MQTTAsyncClient, implements ITransport
- WebSocketTransport: wraps WebSocketServer + ClientManager; broadcastToMasters/Secondary
- HTTPTransport: first-class HTTP identity (no more 0xFFFFFFFF fake WS client); registers
POST /api/command, GET /api/ping, GET /api/status; reply lambda captures AsyncWebServerRequest*
- UARTTransport: wraps UARTCommandHandler; UART whitelist enforced before dispatch
## Stage 4 — CommunicationRouter simplification
- Router now owns CommandBus + 4 transports; registerHandlers() wires all 10 domain handlers
- Old CommandHandler marked DEPRECATED on disk, no longer called
- HTTPRequestHandler no longer instantiated (replaced by HTTPTransport)
- ResponseBuilder::fromResult() factory method added
- main.cpp: all setXxxReference() calls moved before begin(); HealthMonitor + OutputManager wired
## Stage 5 — Legacy Adapter
- LegacyAdapter.hpp/cpp: 35 v1→v2 command mappings (simple 1:1, action-based, boolean splits)
- CommandBus::dispatch() applies translation when envelope.version == 1
## Stage 6 — WebServerTransport (4th transport, absorbs SettingsWebServer)
- WebServerTransport implements ITransport; registered in CommunicationRouter::begin()
- GET /settings: HTML settings page (PROGMEM template unchanged)
- POST /api/set-mode: routes network.set_config {permanent_ap_mode} through CommandBus
- POST /api/reboot: routes system.restart through CommandBus
- NetworkHandler::handleSetConfig: handles permanent_ap_mode field independently
- SettingsWebServer member removed from CommunicationRouter; src/SettingsWebServer/ is dead
code, deletable in a follow-up PR once SettingsPage.h is moved
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "CommandBus.hpp"
|
||||
#include "LegacyAdapter.hpp"
|
||||
#include "../../Logging/Logging.hpp"
|
||||
|
||||
#define TAG "CommandBus"
|
||||
@@ -32,6 +33,14 @@ void CommandBus::addTransport(ITransport* transport) {
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void CommandBus::dispatch(CommandEnvelope& envelope) {
|
||||
// Translate legacy v1 envelopes before registry lookup.
|
||||
// No-op for v2 — the check is a single integer compare.
|
||||
if (envelope.version == 1) {
|
||||
LOG_DEBUG(TAG, "v1 envelope received — translating '%s'", envelope.command.c_str());
|
||||
translateLegacyEnvelope(envelope);
|
||||
LOG_DEBUG(TAG, "v1 translation result: '%s'", envelope.command.c_str());
|
||||
}
|
||||
|
||||
auto it = _registry.find(envelope.command);
|
||||
|
||||
if (it == _registry.end()) {
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
#include "LegacyAdapter.hpp"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Translation tables
|
||||
//
|
||||
// SIMPLE_MAP: v1 cmd (no action) → v2 cmd
|
||||
// ACTION_MAP: v1 "cmd:action" compound key → v2 cmd
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// Compound key helper — "cmd\0action" stored as two concatenated const char* pairs
|
||||
struct ActionEntry {
|
||||
const char* cmd;
|
||||
const char* action;
|
||||
const char* v2;
|
||||
};
|
||||
|
||||
static const ActionEntry ACTION_MAP[] = {
|
||||
// file_manager
|
||||
{ "file_manager", "list_melodies", "files.list" },
|
||||
{ "file_manager", "download_melody", "files.download" },
|
||||
{ "file_manager", "delete_melody", "files.delete" },
|
||||
|
||||
// relay_setup
|
||||
{ "relay_setup", "set_timings", "relay.set_durations" },
|
||||
{ "relay_setup", "set_outputs", "relay.set_outputs" },
|
||||
|
||||
// clock_setup — generic actions (enabled split handled separately below)
|
||||
{ "clock_setup", "set_outputs", "clock.set_outputs" },
|
||||
{ "clock_setup", "set_timings", "clock.set_timings" },
|
||||
{ "clock_setup", "set_alerts", "clock.set_alerts" },
|
||||
{ "clock_setup", "set_backlight", "clock.set_backlight" },
|
||||
{ "clock_setup", "set_silence", "clock.set_silence" },
|
||||
{ "clock_setup", "set_rtc_time", "clock.set_time" },
|
||||
{ "clock_setup", "set_physical_clock_time", "clock.set_face" },
|
||||
{ "clock_setup", "pause_clock_updates", "clock.pause" },
|
||||
{ "clock_setup", "resume_clock_updates", "clock.resume" },
|
||||
|
||||
// system_info
|
||||
{ "system_info", "report_status", "system.status" },
|
||||
{ "system_info", "get_device_time", "system.get_time" },
|
||||
{ "system_info", "get_clock_time", "clock.get_face" },
|
||||
{ "system_info", "get_firmware_status", "firmware.status" },
|
||||
{ "system_info", "network_info", "network.info" },
|
||||
{ "system_info", "get_full_settings", "system.get_settings" },
|
||||
// sync_time_to_lcd retired — LCD now uses request/response model
|
||||
{ "system_info", "sync_time_to_lcd", "system.get_time" },
|
||||
|
||||
// system
|
||||
{ "system", "status", "system.status" },
|
||||
{ "system", "reset_defaults", "system.factory_reset" },
|
||||
{ "system", "commit_firmware", "firmware.commit" },
|
||||
{ "system", "rollback_firmware", "firmware.rollback" },
|
||||
{ "system", "get_firmware_status", "firmware.status" },
|
||||
{ "system", "set_network_config", "network.set_config" },
|
||||
{ "system", "set_serial_log_level", "log.set_serial" },
|
||||
{ "system", "set_sd_log_level", "log.set_sd" },
|
||||
{ "system", "set_mqtt_log_level", "log.set_mqtt" },
|
||||
{ "system", "restart", "system.restart" },
|
||||
{ "system", "reboot", "system.restart" },
|
||||
{ "system", "force_update", "ota.update" },
|
||||
{ "system", "custom_update", "ota.custom" },
|
||||
};
|
||||
|
||||
static constexpr size_t ACTION_MAP_SIZE = sizeof(ACTION_MAP) / sizeof(ACTION_MAP[0]);
|
||||
|
||||
// v1 top-level commands with no "action" field that map 1-to-1
|
||||
struct SimpleEntry {
|
||||
const char* v1;
|
||||
const char* v2;
|
||||
};
|
||||
|
||||
static const SimpleEntry SIMPLE_MAP[] = {
|
||||
{ "ping", "ping" },
|
||||
{ "identify", "identify" },
|
||||
{ "playback", "playback" },
|
||||
};
|
||||
|
||||
static constexpr size_t SIMPLE_MAP_SIZE = sizeof(SIMPLE_MAP) / sizeof(SIMPLE_MAP[0]);
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void translateLegacyEnvelope(CommandEnvelope& envelope) {
|
||||
const String& cmd = envelope.command;
|
||||
|
||||
// ── 1. Simple 1-to-1 commands (no action needed) ───────────────────────
|
||||
for (size_t i = 0; i < SIMPLE_MAP_SIZE; ++i) {
|
||||
if (cmd == SIMPLE_MAP[i].v1) {
|
||||
envelope.command = SIMPLE_MAP[i].v2;
|
||||
envelope.version = 2;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// ── 2. Commands that require an "action" field ──────────────────────────
|
||||
// Extract action from contents, strip it, then look up in ACTION_MAP.
|
||||
const char* action = envelope.contents["action"] | "";
|
||||
|
||||
// ── 2a. Special case: clock_setup → set_enabled (boolean split) ─────────
|
||||
if (cmd == "clock_setup" && strcmp(action, "set_enabled") == 0) {
|
||||
bool enabled = envelope.contents["enabled"] | false;
|
||||
envelope.command = enabled ? "clock.enable" : "clock.disable";
|
||||
envelope.contents.remove("action");
|
||||
envelope.contents.remove("enabled");
|
||||
envelope.version = 2;
|
||||
return;
|
||||
}
|
||||
|
||||
// ── 2b. Special case: system → set_mqtt_enabled (boolean split) ─────────
|
||||
if (cmd == "system" && strcmp(action, "set_mqtt_enabled") == 0) {
|
||||
bool enabled = envelope.contents["enabled"] | false;
|
||||
envelope.command = enabled ? "mqtt.enable" : "mqtt.disable";
|
||||
envelope.contents.remove("action");
|
||||
envelope.contents.remove("enabled");
|
||||
envelope.version = 2;
|
||||
return;
|
||||
}
|
||||
|
||||
// ── 2c. General action-based lookup ─────────────────────────────────────
|
||||
if (action[0] != '\0') {
|
||||
for (size_t i = 0; i < ACTION_MAP_SIZE; ++i) {
|
||||
if (cmd == ACTION_MAP[i].cmd && strcmp(action, ACTION_MAP[i].action) == 0) {
|
||||
envelope.command = ACTION_MAP[i].v2;
|
||||
envelope.contents.remove("action");
|
||||
envelope.version = 2;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 3. Unknown / unmapped — leave command unchanged so the bus returns ──
|
||||
// its normal "Unknown command: ..." error. Mark as v2 so we don't loop.
|
||||
envelope.version = 2;
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// LegacyAdapter — v1 → v2 command translation
|
||||
//
|
||||
// When a client sends { "v": 1 } (or omits "v"), the envelope arrives here
|
||||
// before reaching the CommandBus registry. The adapter maps the old flat/
|
||||
// umbrella command shape to the new namespaced v2 command.
|
||||
//
|
||||
// Translation logic:
|
||||
// 1. If the v1 message has an "action" field inside contents, combine
|
||||
// cmd + action → v2 flat command name, then strip "action" from contents.
|
||||
// 2. If no action, map the top-level cmd name directly (e.g. "ping" → "ping").
|
||||
// 3. Special cases:
|
||||
// - clock_setup → set_enabled uses contents["enabled"] bool to pick
|
||||
// clock.enable vs clock.disable
|
||||
// - system → set_mqtt_enabled uses contents["enabled"] bool to pick
|
||||
// mqtt.enable vs mqtt.disable
|
||||
// - system_info → sync_time_to_lcd is remapped to system.get_time
|
||||
// (LCD now uses request/response; old push model retired)
|
||||
//
|
||||
// To delete legacy support in one PR: remove the #include and the single
|
||||
// call in CommandBus::dispatch(), then delete this file.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
#include "CommandEnvelope.hpp"
|
||||
|
||||
// Translate an envelope whose version == 1 into its v2 equivalent.
|
||||
// Mutates envelope.command and (where needed) envelope.contents in place.
|
||||
// Sets envelope.version = 2 on return.
|
||||
// No-op if the command is already valid v2 (unknown v1 commands are left
|
||||
// unchanged so the bus returns a proper "Unknown command" error).
|
||||
void translateLegacyEnvelope(CommandEnvelope& envelope);
|
||||
Reference in New Issue
Block a user