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:
2026-02-28 22:54:43 +02:00
co-authored by Claude Sonnet 4.6
parent f6107e2cfe
commit 682c604abe
21 changed files with 1505 additions and 365 deletions
@@ -0,0 +1,105 @@
#include "UARTTransport.hpp"
#include "../../CommandBus/CommandBus.hpp"
#include "../../CommandBus/CommandEnvelope.hpp"
#include "../../CommandBus/CommandResult.hpp"
#define TAG "UARTTransport"
#include "../../../Logging/Logging.hpp"
// ---------------------------------------------------------------------------
// Whitelist
// ---------------------------------------------------------------------------
const char* const UARTTransport::WHITELIST[] = {
"ping",
"playback",
"clock.pause",
"clock.resume",
"system.get_time",
};
const size_t UARTTransport::WHITELIST_SIZE =
sizeof(UARTTransport::WHITELIST) / sizeof(UARTTransport::WHITELIST[0]);
// ---------------------------------------------------------------------------
UARTTransport::UARTTransport(UARTCommandHandler& uart, CommandBus& bus)
: _uart(uart)
, _bus(bus) {
}
void UARTTransport::begin() {
_uart.setCallback([this](JsonDocument& doc) {
onMessage(doc);
});
_uart.begin();
LOG_INFO(TAG, "UARTTransport initialised (whitelist: %u commands)", WHITELIST_SIZE);
}
void UARTTransport::loop() {
_uart.loop();
}
bool UARTTransport::isHealthy() const {
return _uart.isReady();
}
void UARTTransport::broadcast(const String& message) {
_uart.send(message);
}
// ---------------------------------------------------------------------------
bool UARTTransport::isWhitelisted(const String& command) const {
for (size_t i = 0; i < WHITELIST_SIZE; ++i) {
if (command == WHITELIST[i]) {
return true;
}
}
return false;
}
void UARTTransport::onMessage(JsonDocument& doc) {
const char* cmd = doc["cmd"] | "";
if (cmd[0] == '\0') {
LOG_WARNING(TAG, "UART message missing 'cmd' field — dropping");
return;
}
if (!isWhitelisted(cmd)) {
LOG_WARNING(TAG, "UART command '%s' not whitelisted — silently dropped", cmd);
return;
}
LOG_DEBUG(TAG, "UART CMD (whitelisted): %s", cmd);
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::UART;
envelope.clientId = 0;
envelope.command = cmd;
envelope.version = doc["v"] | 2;
if (doc.containsKey("contents")) {
envelope.contents.set(doc["contents"]);
}
// Reply routes back over UART to the slave
envelope.reply = [this](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;
}
String out;
serializeJson(resp, out);
_uart.send(out);
};
_bus.dispatch(envelope);
}
@@ -0,0 +1,56 @@
#pragma once
#include <Arduino.h>
#include "../ITransport.hpp"
#include "../../UARTCommandHandler/UARTCommandHandler.hpp"
class CommandBus;
// ---------------------------------------------------------------------------
// UARTTransport
//
// ITransport wrapper around the existing UARTCommandHandler.
//
// The UART port talks to a slave LCD/button board. Only a small whitelist of
// commands are accepted from this transport to prevent feedback loops and
// stop a compromised slave from issuing sensitive commands.
//
// Whitelist (v2 — see Communication-Refactor-Plan.md):
// ping — connection check
// playback — play/stop from physical buttons
// clock.pause — button-triggered pause
// clock.resume — button-triggered resume
// system.get_time — LCD requests current time (replaces old push model)
//
// All other commands are silently dropped before they reach the bus.
//
// broadcast() sends to the UART slave (unsolicited events / telemetry).
// For connection-less context this is optional; the slave may ignore messages
// it doesn't request.
// ---------------------------------------------------------------------------
class UARTTransport : public ITransport {
public:
UARTTransport(UARTCommandHandler& uart, CommandBus& bus);
void begin() override;
void loop() override; // polls UARTCommandHandler::loop()
bool isHealthy() const override;
String name() const override { return "UART"; }
// Send a serialised JSON string to the UART slave (newline appended by handler).
void broadcast(const String& message) override;
private:
UARTCommandHandler& _uart;
CommandBus& _bus;
// Commands the UART slave is allowed to issue
static const char* const WHITELIST[];
static const size_t WHITELIST_SIZE;
bool isWhitelisted(const String& command) const;
// Invoked by UARTCommandHandler's message callback
void onMessage(JsonDocument& doc);
};