Files
project-vesper/src/Communication/Transport/WebSocketTransport/WebSocketTransport.cpp
T
bonaminandClaude Sonnet 4.6 7e8d91673f fix: builtin aliases, cmd version default, relay compat, NTP UTC, player guards
BuiltInMelodies (F-018):
- 24 builtin_* UID aliases added for backward compat with old device PIDs
- Two new PROGMEM arrays: builtin_catehetical, builtin_mournfull_toll_alternative
- isBuiltInMelody() helper for lightweight PID existence check
- FileManager download-skip now uses isBuiltInMelody() instead of findMelodyByPID()

Command envelope version default (F-020):
- MQTT, UART, WebSocket transports: missing "v" field defaults to 1 (was 2)
- Fixes legacy adapter rejecting commands from app and Node-RED

RelayHandler v1/v2 compat (F-021):
- set_durations and set_outputs accept both nested {"bells":{...}} (v2)
  and flat {...} (v1) payload shapes

NTP UTC fix (F-019):
- configTime(0,0,ntpServer) fetches raw UTC; stored gmtOffset+dstOffset
  applied manually to epoch before writing RTC
- Eliminates double-offset bug from SDK internal timezone handling

Player guards (F-022):
- speed==0 defaults to 300ms instead of producing silent/broken playback
- calculateProjectedRunTime() guards against singleLoopDuration==0

Telemetry (F-023):
- bellMaxLoad default raised 200→500 to eliminate false overload warnings

Version bump: 2.0.1 → 2.0.3

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-30 09:52:39 +03:00

97 lines
2.9 KiB
C++

#include "WebSocketTransport.hpp"
#include "../../CommandBus/CommandBus.hpp"
#include "../../CommandBus/CommandEnvelope.hpp"
#include "../../CommandBus/CommandResult.hpp"
#define TAG "WebSocketTransport"
#include "../../../Logging/Logging.hpp"
WebSocketTransport::WebSocketTransport(WebSocketDriver& 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"] | 1;
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);
}