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:
@@ -0,0 +1,96 @@
|
||||
#include "WebSocketTransport.hpp"
|
||||
#include "../../CommandBus/CommandBus.hpp"
|
||||
#include "../../CommandBus/CommandEnvelope.hpp"
|
||||
#include "../../CommandBus/CommandResult.hpp"
|
||||
|
||||
#define TAG "WebSocketTransport"
|
||||
#include "../../../Logging/Logging.hpp"
|
||||
|
||||
WebSocketTransport::WebSocketTransport(WebSocketServer& wsServer, CommandBus& bus)
|
||||
: _ws(wsServer)
|
||||
, _bus(bus) {
|
||||
}
|
||||
|
||||
void WebSocketTransport::begin() {
|
||||
_ws.setCallback([this](uint32_t clientId, const JsonDocument& doc) {
|
||||
onMessage(clientId, doc);
|
||||
});
|
||||
|
||||
_ws.begin();
|
||||
LOG_INFO(TAG, "WebSocketTransport initialised");
|
||||
}
|
||||
|
||||
void WebSocketTransport::loop() {
|
||||
// AsyncWebServer drives the event loop; nothing to poll here.
|
||||
}
|
||||
|
||||
bool WebSocketTransport::isHealthy() const {
|
||||
return true; // Always healthy — WebSocket server is always listening
|
||||
}
|
||||
|
||||
void WebSocketTransport::broadcast(const String& message) {
|
||||
_ws.broadcastToAll(message);
|
||||
}
|
||||
|
||||
void WebSocketTransport::sendToClient(uint32_t clientId, const String& message) {
|
||||
_ws.sendToClient(clientId, message);
|
||||
}
|
||||
|
||||
void WebSocketTransport::broadcastToMasters(const String& message) {
|
||||
_ws.broadcastToMaster(message);
|
||||
}
|
||||
|
||||
void WebSocketTransport::broadcastToSecondary(const String& message) {
|
||||
_ws.broadcastToSecondary(message);
|
||||
}
|
||||
|
||||
bool WebSocketTransport::hasClients() const {
|
||||
return _ws.hasClients();
|
||||
}
|
||||
|
||||
size_t WebSocketTransport::getClientCount() const {
|
||||
return _ws.getClientCount();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void WebSocketTransport::onMessage(uint32_t clientId, const JsonDocument& doc) {
|
||||
const char* cmd = doc["cmd"] | "";
|
||||
if (cmd[0] == '\0') {
|
||||
LOG_WARNING(TAG, "Client #%u: WebSocket message missing 'cmd' field — dropping", clientId);
|
||||
String err = R"({"status":"ERROR","type":"bad_request","message":"Missing 'cmd' field"})";
|
||||
_ws.sendToClient(clientId, err);
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG(TAG, "Client #%u CMD: %s", clientId, cmd);
|
||||
|
||||
CommandEnvelope envelope;
|
||||
envelope.transport = CommandEnvelope::Transport::WEBSOCKET;
|
||||
envelope.clientId = clientId;
|
||||
envelope.command = cmd;
|
||||
envelope.version = doc["v"] | 2;
|
||||
|
||||
if (doc.containsKey("contents")) {
|
||||
envelope.contents.set(doc["contents"]);
|
||||
}
|
||||
|
||||
// Reply routes back to the originating WebSocket client only
|
||||
envelope.reply = [this, clientId](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);
|
||||
_ws.sendToClient(clientId, out);
|
||||
};
|
||||
|
||||
_bus.dispatch(envelope);
|
||||
}
|
||||
Reference in New Issue
Block a user