Files
project-vesper/src/Communication/Handlers/RelayHandler/RelayHandler.cpp
T
bonaminandClaude Sonnet 4.6 07d5b2fb81 feat(F-038,F-039): bell and clock test fire commands
relay.test_bell  — fire bell N using configured output + duration, non-blocking
relay.test_batch — fire a list of bells simultaneously (single melody step)
clock.test_c1    — fire C1 (ODD) clock output using pulse_duration
clock.test_c2    — fire C2 (EVEN) clock output using pulse_duration

All four bypass bellsEnabled guard intentionally — test commands always fire.
ClockHandler now accepts an optional OutputManager* for the test commands.
CommunicationRouter passes _outputManager to ClockHandler.
Feature catalog updated: F-038 and F-039 added.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-07-10 19:53:54 +03:00

152 lines
6.6 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", "relay.test_bell", "relay.test_batch"
};
}
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);
if (envelope.command == "relay.test_bell") return handleTestBell(envelope);
if (envelope.command == "relay.test_batch") return handleTestBatch(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");
}
CommandResult RelayHandler::handleTestBell(const CommandEnvelope& e) {
if (!e.contents["bell"].is<int>()) {
return CommandResult::err("relay.test_bell", "Missing required field: bell (0-based bell index)");
}
int bell = e.contents["bell"].as<int>();
if (bell < 0 || bell > 15) {
return CommandResult::err("relay.test_bell", "bell must be 0-15");
}
uint8_t output = (uint8_t)_configManager.getBellOutput(bell);
uint16_t durationMs = _configManager.getBellDuration(bell);
_outputManager.fireOutputForDuration(output, durationMs);
LOG_INFO(TAG, "Test bell %d → output %d for %d ms", bell, output, durationMs);
JsonDocument doc;
doc["bell"] = bell;
doc["output"] = output;
doc["duration_ms"] = durationMs;
return CommandResult::ok("relay.test_bell", doc);
}
CommandResult RelayHandler::handleTestBatch(const CommandEnvelope& e) {
if (!e.contents["bells"].is<JsonArrayConst>()) {
return CommandResult::err("relay.test_batch", "Missing required field: bells (array of 0-based bell indices)");
}
JsonArrayConst bellArr = e.contents["bells"].as<JsonArrayConst>();
if (bellArr.size() == 0) {
return CommandResult::err("relay.test_batch", "bells array must not be empty");
}
// Fire each bell for its configured duration — non-blocking (OutputManager handles timers)
// Bypasses bellsEnabled intentionally: test commands always fire
int fired = 0;
for (JsonVariantConst v : bellArr) {
if (!v.is<int>()) continue;
int bell = v.as<int>();
if (bell < 0 || bell > 15) continue;
uint8_t output = (uint8_t)_configManager.getBellOutput((uint8_t)bell);
uint16_t durationMs = _configManager.getBellDuration((uint8_t)bell);
_outputManager.fireOutputForDuration(output, durationMs);
fired++;
}
if (fired == 0) {
return CommandResult::err("relay.test_batch", "No valid bell indices in array");
}
LOG_INFO(TAG, "Test batch: %d bells fired", fired);
JsonDocument doc;
doc["fired"] = fired;
return CommandResult::ok("relay.test_batch", doc);
}