Files
project-vesper/src/Communication/Handlers/RelayHandler/RelayHandler.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

92 lines
4.2 KiB
C++

#include "RelayHandler.hpp"
#include "../../../ConfigManager/ConfigManager.hpp"
#include "../../../OutputManager/OutputManager.hpp"
#include "../../../Player/Player.hpp"
#include "../../../Logging/Logging.hpp"
#define TAG "RelayHandler"
RelayHandler::RelayHandler(ConfigManager& configManager, OutputManager& outputManager, Player& player)
: _configManager(configManager), _outputManager(outputManager), _player(player) {}
std::vector<String> RelayHandler::commands() const {
return { "relay.set_durations", "relay.set_outputs", "relay.get_config", "relay.test_output" };
}
CommandResult RelayHandler::execute(const CommandEnvelope& envelope) {
if (envelope.command == "relay.set_durations") return handleSetDurations(envelope);
if (envelope.command == "relay.set_outputs") return handleSetOutputs(envelope);
if (envelope.command == "relay.get_config") return handleGetConfig(envelope);
if (envelope.command == "relay.test_output") return handleTestOutput(envelope);
return CommandResult::err(envelope.command, "Unknown relay command");
}
CommandResult RelayHandler::handleSetDurations(const CommandEnvelope& e) {
JsonVariant contents = const_cast<JsonDocument&>(e.contents).as<JsonVariant>();
// Accept v2 nested {"bells":{"b1":...}} or v1 flat {"b1":...}
JsonVariant bells = contents["bells"].is<JsonObject>() ? contents["bells"].as<JsonVariant>() : contents;
if (bells.isNull()) {
return CommandResult::err("relay.set_durations", "Missing or invalid 'bells' object");
}
_configManager.updateBellDurations(bells);
if (!_configManager.saveBellDurations()) {
return CommandResult::err("relay.set_durations", "Durations updated but failed to save to SD");
}
LOG_INFO(TAG, "Bell durations updated and saved");
return CommandResult::ok("relay.set_durations", "Bell durations updated and saved");
}
CommandResult RelayHandler::handleSetOutputs(const CommandEnvelope& e) {
JsonVariant contents = const_cast<JsonDocument&>(e.contents).as<JsonVariant>();
// Accept v2 nested {"bells":{"b1":...}} or v1 flat {"b1":...}
JsonVariant bells = contents["bells"].is<JsonObject>() ? contents["bells"].as<JsonVariant>() : contents;
if (bells.isNull()) {
return CommandResult::err("relay.set_outputs", "Missing or invalid 'bells' object");
}
_configManager.updateBellOutputs(bells);
if (!_configManager.saveBellOutputs()) {
return CommandResult::err("relay.set_outputs", "Outputs updated but failed to save to SD");
}
LOG_INFO(TAG, "Bell outputs updated and saved");
return CommandResult::ok("relay.set_outputs", "Bell outputs updated and saved");
}
CommandResult RelayHandler::handleGetConfig(const CommandEnvelope& e) {
JsonDocument doc;
JsonObject durations = doc["durations"].to<JsonObject>();
JsonObject outputs = doc["outputs"].to<JsonObject>();
for (uint8_t i = 0; i < 16; i++) {
durations[String(i)] = _configManager.getBellDuration(i);
outputs[String(i)] = _configManager.getBellOutput(i);
}
return CommandResult::ok("relay.get_config", doc);
}
CommandResult RelayHandler::handleTestOutput(const CommandEnvelope& e) {
if (!e.contents["output"].is<int>()) {
return CommandResult::err("relay.test_output", "Missing required field: output");
}
if (!e.contents["duration_ms"].is<int>()) {
return CommandResult::err("relay.test_output", "Missing required field: duration_ms");
}
if (!_player.isCurrentlyStopped()) {
return CommandResult::err("relay.test_output", "Player must be stopped before testing outputs");
}
int output = e.contents["output"].as<int>();
int durationMs = e.contents["duration_ms"].as<int>();
if (output < 0 || output > 31) {
return CommandResult::err("relay.test_output", "output must be 0-31");
}
if (durationMs < 1 || durationMs > 5000) {
return CommandResult::err("relay.test_output", "duration_ms must be 1-5000");
}
_outputManager.fireOutputForDuration((uint8_t)output, (uint16_t)durationMs);
LOG_INFO(TAG, "Test output %d fired for %d ms", output, durationMs);
return CommandResult::ok("relay.test_output",
"Output " + String(output) + " fired for " + String(durationMs) + " ms");
}