feat(comms): Stage 2 — all 10 domain handlers implemented
Adds the complete set of v2 command handlers. Old CommandHandler is
untouched — handlers are standalone and not yet wired to anything.
Handlers added:
PlaybackHandler — playback (1 dependency)
LoggingHandler — log.set_serial/sd/mqtt
FileHandler — files.list/list_builtin/download/delete
TelemetryHandler — telemetry.get_strikes/reset_strikes/get_loads
RelayHandler — relay.set_durations/set_outputs/get_config/test_output
FirmwareHandler — firmware.status/commit/rollback + ota.update/custom
NetworkHandler — network.info/status/set_config
MQTTHandler — mqtt.enable/disable
SystemHandler — ping/identify + system.* commands (8 total)
ClockHandler — 16 clock.* commands
Notable fixes vs. original CommandHandler:
- clock.set_timings now sets pulseDuration/pauseDuration correctly
(original called updateClockOutputs — was a bug)
- system.restart uses a deferred FreeRTOS task (no blocking delay)
- relay.test_output safety-gated: player must be fully stopped
- Timekeeper class name (lowercase k) corrected throughout
Two compile-time fixes applied during build:
- JsonVariantConst → const_cast<JsonDocument&>().as<JsonVariant>()
(ConfigManager methods take non-const JsonVariant)
- class TimeKeeper → class Timekeeper (actual class name in header)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#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>();
|
||||
if (contents["bells"].isNull()) {
|
||||
return CommandResult::err("relay.set_durations", "Missing or invalid 'bells' object");
|
||||
}
|
||||
_configManager.updateBellDurations(contents);
|
||||
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>();
|
||||
if (contents["bells"].isNull()) {
|
||||
return CommandResult::err("relay.set_outputs", "Missing or invalid 'bells' object");
|
||||
}
|
||||
_configManager.updateBellOutputs(contents);
|
||||
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");
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include "../ICommandHandler.hpp"
|
||||
|
||||
class ConfigManager;
|
||||
class OutputManager;
|
||||
class Player;
|
||||
|
||||
class RelayHandler : public ICommandHandler {
|
||||
public:
|
||||
RelayHandler(ConfigManager& configManager, OutputManager& outputManager, Player& player);
|
||||
|
||||
std::vector<String> commands() const override;
|
||||
CommandResult execute(const CommandEnvelope& envelope) override;
|
||||
|
||||
private:
|
||||
ConfigManager& _configManager;
|
||||
OutputManager& _outputManager;
|
||||
Player& _player;
|
||||
|
||||
CommandResult handleSetDurations(const CommandEnvelope& e);
|
||||
CommandResult handleSetOutputs(const CommandEnvelope& e);
|
||||
CommandResult handleGetConfig(const CommandEnvelope& e);
|
||||
CommandResult handleTestOutput(const CommandEnvelope& e);
|
||||
};
|
||||
Reference in New Issue
Block a user