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,170 @@
|
||||
#include "HTTPTransport.hpp"
|
||||
#include "../../CommandBus/CommandBus.hpp"
|
||||
#include "../../CommandBus/CommandEnvelope.hpp"
|
||||
#include "../../CommandBus/CommandResult.hpp"
|
||||
#include "../../../ConfigManager/ConfigManager.hpp"
|
||||
|
||||
#define TAG "HTTPTransport"
|
||||
#include "../../../Logging/Logging.hpp"
|
||||
|
||||
HTTPTransport::HTTPTransport(AsyncWebServer& server, CommandBus& bus, ConfigManager& configManager)
|
||||
: _server(server)
|
||||
, _bus(bus)
|
||||
, _configManager(configManager) {
|
||||
}
|
||||
|
||||
void HTTPTransport::begin() {
|
||||
// POST /api/command — execute any command
|
||||
_server.on("/api/command", HTTP_POST,
|
||||
// Called when body-less POST completes (shouldn't happen, but guard it)
|
||||
[](AsyncWebServerRequest* request) {
|
||||
request->send(400, "application/json",
|
||||
R"({"status":"ERROR","type":"bad_request","message":"Body required"})");
|
||||
},
|
||||
nullptr, // no upload handler
|
||||
[this](AsyncWebServerRequest* request, uint8_t* data, size_t len,
|
||||
size_t index, size_t total) {
|
||||
// Wait for full body before processing
|
||||
if (index + len == total) {
|
||||
handleCommandRequest(request, data, len);
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// GET /api/ping — lightweight health check
|
||||
_server.on("/api/ping", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) {
|
||||
handlePingRequest(request);
|
||||
}
|
||||
);
|
||||
|
||||
// GET /api/status — convenience shortcut for system.status
|
||||
_server.on("/api/status", HTTP_GET,
|
||||
[this](AsyncWebServerRequest* request) {
|
||||
handleStatusRequest(request);
|
||||
}
|
||||
);
|
||||
|
||||
// CORS headers for browser clients
|
||||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Origin", "*");
|
||||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
|
||||
DefaultHeaders::Instance().addHeader("Access-Control-Allow-Headers", "Content-Type");
|
||||
|
||||
_initialised = true;
|
||||
LOG_INFO(TAG, "HTTPTransport initialised — POST /api/command, GET /api/ping, GET /api/status");
|
||||
}
|
||||
|
||||
void HTTPTransport::loop() {
|
||||
// AsyncWebServer is fully event-driven; nothing to poll.
|
||||
}
|
||||
|
||||
bool HTTPTransport::isHealthy() const {
|
||||
return _initialised;
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void HTTPTransport::handleCommandRequest(AsyncWebServerRequest* request,
|
||||
uint8_t* data, size_t len) {
|
||||
JsonDocument doc;
|
||||
DeserializationError err = deserializeJson(doc, data, len);
|
||||
if (err) {
|
||||
LOG_WARNING(TAG, "JSON parse error: %s", err.c_str());
|
||||
sendErrorJson(request, 400, "Invalid JSON");
|
||||
return;
|
||||
}
|
||||
|
||||
const char* cmd = doc["cmd"] | "";
|
||||
if (cmd[0] == '\0') {
|
||||
sendErrorJson(request, 400, "Missing 'cmd' field");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_DEBUG(TAG, "HTTP CMD: %s", cmd);
|
||||
|
||||
CommandEnvelope envelope;
|
||||
envelope.transport = CommandEnvelope::Transport::HTTP;
|
||||
envelope.clientId = 0;
|
||||
envelope.command = cmd;
|
||||
envelope.version = doc["v"] | 2;
|
||||
|
||||
if (doc.containsKey("contents")) {
|
||||
envelope.contents.set(doc["contents"]);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
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);
|
||||
sendJsonResponse(request, 200, out);
|
||||
};
|
||||
|
||||
_bus.dispatch(envelope);
|
||||
}
|
||||
|
||||
void HTTPTransport::handlePingRequest(AsyncWebServerRequest* request) {
|
||||
LOG_DEBUG(TAG, "Ping request");
|
||||
JsonDocument doc;
|
||||
doc["status"] = "SUCCESS";
|
||||
doc["type"] = "pong";
|
||||
doc["uptime"] = millis();
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
sendJsonResponse(request, 200, out);
|
||||
}
|
||||
|
||||
void HTTPTransport::handleStatusRequest(AsyncWebServerRequest* request) {
|
||||
LOG_DEBUG(TAG, "Status shortcut — dispatching system.status");
|
||||
|
||||
CommandEnvelope envelope;
|
||||
envelope.transport = CommandEnvelope::Transport::HTTP;
|
||||
envelope.clientId = 0;
|
||||
envelope.command = "system.status";
|
||||
envelope.version = 2;
|
||||
|
||||
envelope.reply = [this, request](const CommandResult& result) {
|
||||
JsonDocument resp;
|
||||
resp["status"] = result.isOk() ? "SUCCESS" : "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);
|
||||
sendJsonResponse(request, 200, out);
|
||||
};
|
||||
|
||||
_bus.dispatch(envelope);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
void HTTPTransport::sendJsonResponse(AsyncWebServerRequest* request,
|
||||
int code, const String& json) {
|
||||
request->send(code, "application/json", json);
|
||||
}
|
||||
|
||||
void HTTPTransport::sendErrorJson(AsyncWebServerRequest* request,
|
||||
int code, const String& message) {
|
||||
JsonDocument doc;
|
||||
doc["status"] = "ERROR";
|
||||
doc["type"] = "bad_request";
|
||||
doc["message"] = message;
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
sendJsonResponse(request, code, out);
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <ESPAsyncWebServer.h>
|
||||
#include "../ITransport.hpp"
|
||||
|
||||
class CommandBus;
|
||||
class ConfigManager;
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// HTTPTransport
|
||||
//
|
||||
// ITransport implementation for HTTP — replaces HTTPRequestHandler.
|
||||
//
|
||||
// HTTP is now a proper first-class transport with its own identity.
|
||||
// No more 0xFFFFFFFF fake WebSocket client ID.
|
||||
//
|
||||
// Endpoints registered:
|
||||
// POST /api/command — execute any command via CommandBus
|
||||
// GET /api/ping — health check (no bus involvement)
|
||||
// GET /api/status — shortcut: dispatches system.status to the bus
|
||||
//
|
||||
// broadcast() is a no-op (HTTP is stateless; no persistent connection to push to).
|
||||
//
|
||||
// The reply lambda captures the AsyncWebServerRequest* and sends the HTTP
|
||||
// response directly — the bus and handlers never see the HTTP layer.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
class HTTPTransport : public ITransport {
|
||||
public:
|
||||
HTTPTransport(AsyncWebServer& server, CommandBus& bus, ConfigManager& configManager);
|
||||
|
||||
void begin() override;
|
||||
void loop() override; // no-op — AsyncWebServer is event-driven
|
||||
bool isHealthy() const override;
|
||||
String name() const override { return "HTTP"; }
|
||||
|
||||
// No persistent connections — broadcast is a no-op.
|
||||
void broadcast(const String& message) override {}
|
||||
|
||||
private:
|
||||
AsyncWebServer& _server;
|
||||
CommandBus& _bus;
|
||||
ConfigManager& _configManager;
|
||||
bool _initialised = false;
|
||||
|
||||
// Endpoint handlers
|
||||
void handleCommandRequest(AsyncWebServerRequest* request, uint8_t* data, size_t len);
|
||||
void handlePingRequest(AsyncWebServerRequest* request);
|
||||
void handleStatusRequest(AsyncWebServerRequest* request);
|
||||
|
||||
void sendJsonResponse(AsyncWebServerRequest* request, int code, const String& json);
|
||||
void sendErrorJson(AsyncWebServerRequest* request, int code, const String& message);
|
||||
};
|
||||
Reference in New Issue
Block a user