feat(F-040,F-041,F-042): relay.set_config, output numbering fix, clock batch commands

Enforce 1-based output numbering throughout the entire stack. Stored values,
MQTT wire format, and console UI are all 1-based; the single 0-based conversion
happens only in OutputManager::getPhysicalOutput(). Fixed createDefaultBellConfig
(was setting identity 0-based map instead of 0=disabled), updateBellDurations and
updateBellOutputs (support both v2 0/1 and v1 b1/b2 keys), all call
sites that bypassed getPhysicalOutput, and fireClockOutput conversion.

New commands:
- relay.set_config: combined durations + outputs in one round trip (F-040)
- clock.set_config: enabled/c1/c2/pulse_duration/pause_duration in one call (F-042)
- clock.set_alerts_config: alert type, bells, and silence windows in one call (F-042)

Feature catalog updated with F-040, F-041, F-042 detail sections.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-07-12 18:44:52 +03:00
co-authored by Claude Sonnet 4.6
parent a5395b0de7
commit 0ac63e82ed
8 changed files with 241 additions and 68 deletions
@@ -14,6 +14,7 @@ std::vector<String> ClockHandler::commands() const {
return {
"clock.set_outputs", "clock.set_timings", "clock.set_alerts",
"clock.set_backlight", "clock.set_silence",
"clock.set_config", "clock.set_alerts_config",
"clock.set_time", "clock.set_face",
"clock.set_timezone", "clock.get_timezone", "clock.sync_ntp",
"clock.enable", "clock.disable",
@@ -24,9 +25,11 @@ std::vector<String> ClockHandler::commands() const {
}
CommandResult ClockHandler::execute(const CommandEnvelope& envelope) {
if (envelope.command == "clock.set_outputs") return handleSetOutputs(envelope);
if (envelope.command == "clock.set_timings") return handleSetTimings(envelope);
if (envelope.command == "clock.set_alerts") return handleSetAlerts(envelope);
if (envelope.command == "clock.set_outputs") return handleSetOutputs(envelope);
if (envelope.command == "clock.set_timings") return handleSetTimings(envelope);
if (envelope.command == "clock.set_alerts") return handleSetAlerts(envelope);
if (envelope.command == "clock.set_config") return handleSetConfig(envelope);
if (envelope.command == "clock.set_alerts_config") return handleSetAlertsConfig(envelope);
if (envelope.command == "clock.set_backlight") return handleSetBacklight(envelope);
if (envelope.command == "clock.set_silence") return handleSetSilence(envelope);
if (envelope.command == "clock.set_time") return handleSetTime(envelope);
@@ -277,4 +280,92 @@ CommandResult ClockHandler::handleTestC2(const CommandEnvelope& e) {
return CommandResult::ok("clock.test_c2", doc);
}
CommandResult ClockHandler::handleSetConfig(const CommandEnvelope& e) {
JsonVariantConst c = e.contents.as<JsonVariantConst>();
bool hasEnabled = c["enabled"].is<bool>();
bool hasC1 = c["c1"].is<int>();
bool hasC2 = c["c2"].is<int>();
bool hasPulseDuration = c["pulse_duration"].is<int>();
bool hasPauseDuration = c["pause_duration"].is<int>();
if (!hasEnabled && !hasC1 && !hasC2 && !hasPulseDuration && !hasPauseDuration) {
return CommandResult::err("clock.set_config",
"Provide at least one field: enabled, c1, c2, pulse_duration, pause_duration");
}
if (hasEnabled) _configManager.setClockEnabled(c["enabled"].as<bool>());
if (hasC1 || hasC2) {
JsonDocument tmp;
if (hasC1) tmp["c1output"] = c["c1"].as<int>();
if (hasC2) tmp["c2output"] = c["c2"].as<int>();
_configManager.updateClockOutputs(tmp.as<JsonVariant>());
}
if (hasPulseDuration) _configManager.setClockPulseDuration(c["pulse_duration"].as<uint16_t>());
if (hasPauseDuration) _configManager.setClockPauseDuration(c["pause_duration"].as<uint16_t>());
if (!_configManager.saveClockConfig()) {
return CommandResult::err("clock.set_config", "Settings applied but failed to save to SD");
}
LOG_INFO(TAG, "clock.set_config applied");
return CommandResult::ok("clock.set_config", "Clock configuration updated and saved");
}
CommandResult ClockHandler::handleSetAlertsConfig(const CommandEnvelope& e) {
JsonVariantConst c = e.contents.as<JsonVariantConst>();
bool hasAlerts = c["alert_type"].is<JsonVariantConst>() ||
c["alert_interval"].is<JsonVariantConst>() ||
c["hour_bell"].is<JsonVariantConst>() ||
c["half_bell"].is<JsonVariantConst>() ||
c["quarter_bell"].is<JsonVariantConst>();
bool hasSilence = c["daytime_silence"].is<JsonVariantConst>() ||
c["daytime_on"].is<JsonVariantConst>() ||
c["daytime_off"].is<JsonVariantConst>() ||
c["night_silence"].is<JsonVariantConst>() ||
c["night_on"].is<JsonVariantConst>() ||
c["night_off"].is<JsonVariantConst>();
if (!hasAlerts && !hasSilence) {
return CommandResult::err("clock.set_alerts_config",
"Provide at least one of: alert_type, alert_interval, hour_bell, half_bell, quarter_bell, "
"daytime_silence, daytime_on, daytime_off, night_silence, night_on, night_off");
}
if (hasAlerts) {
// Map wire-format snake_case keys to what updateClockAlerts expects (camelCase)
JsonDocument alertsDoc;
if (c["alert_type"].is<JsonVariantConst>()) alertsDoc["alertType"] = c["alert_type"];
if (c["alert_interval"].is<JsonVariantConst>()) alertsDoc["alertRingInterval"] = c["alert_interval"];
if (c["hour_bell"].is<JsonVariantConst>()) alertsDoc["hourBell"] = c["hour_bell"];
if (c["half_bell"].is<JsonVariantConst>()) alertsDoc["halfBell"] = c["half_bell"];
if (c["quarter_bell"].is<JsonVariantConst>()) alertsDoc["quarterBell"] = c["quarter_bell"];
_configManager.updateClockAlerts(alertsDoc.as<JsonVariant>());
}
if (hasSilence) {
// Map wire-format flat keys to nested structure that updateClockSilence expects
JsonDocument silenceDoc;
if (c["daytime_silence"].is<JsonVariantConst>() || c["daytime_on"].is<JsonVariantConst>() || c["daytime_off"].is<JsonVariantConst>()) {
JsonObject dt = silenceDoc["daytime"].to<JsonObject>();
if (c["daytime_silence"].is<JsonVariantConst>()) dt["enabled"] = c["daytime_silence"];
if (c["daytime_on"].is<JsonVariantConst>()) dt["onTime"] = c["daytime_on"];
if (c["daytime_off"].is<JsonVariantConst>()) dt["offTime"] = c["daytime_off"];
}
if (c["night_silence"].is<JsonVariantConst>() || c["night_on"].is<JsonVariantConst>() || c["night_off"].is<JsonVariantConst>()) {
JsonObject nt = silenceDoc["nighttime"].to<JsonObject>();
if (c["night_silence"].is<JsonVariantConst>()) nt["enabled"] = c["night_silence"];
if (c["night_on"].is<JsonVariantConst>()) nt["onTime"] = c["night_on"];
if (c["night_off"].is<JsonVariantConst>()) nt["offTime"] = c["night_off"];
}
_configManager.updateClockSilence(silenceDoc.as<JsonVariant>());
}
if (!_configManager.saveClockConfig()) {
return CommandResult::err("clock.set_alerts_config", "Settings applied but failed to save to SD");
}
LOG_INFO(TAG, "clock.set_alerts_config applied");
return CommandResult::ok("clock.set_alerts_config", "Clock alerts and silence configuration updated and saved");
}
#endif // HAS_RTC
@@ -39,6 +39,8 @@ private:
CommandResult handleGetConfig(const CommandEnvelope& e);
CommandResult handleTestC1(const CommandEnvelope& e);
CommandResult handleTestC2(const CommandEnvelope& e);
CommandResult handleSetConfig(const CommandEnvelope& e);
CommandResult handleSetAlertsConfig(const CommandEnvelope& e);
};
#endif // HAS_RTC
@@ -11,7 +11,7 @@ RelayHandler::RelayHandler(ConfigManager& configManager, OutputManager& outputMa
std::vector<String> RelayHandler::commands() const {
return {
"relay.set_durations", "relay.set_outputs", "relay.get_config",
"relay.set_durations", "relay.set_outputs", "relay.set_config", "relay.get_config",
"relay.test_output", "relay.test_bell", "relay.test_batch"
};
}
@@ -19,6 +19,7 @@ std::vector<String> RelayHandler::commands() const {
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.set_config") return handleSetConfig(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);
@@ -56,6 +57,34 @@ CommandResult RelayHandler::handleSetOutputs(const CommandEnvelope& e) {
return CommandResult::ok("relay.set_outputs", "Bell outputs updated and saved");
}
CommandResult RelayHandler::handleSetConfig(const CommandEnvelope& e) {
JsonVariant contents = const_cast<JsonDocument&>(e.contents).as<JsonVariant>();
bool hasDurations = contents["durations"].is<JsonObject>();
bool hasOutputs = contents["outputs"].is<JsonObject>();
if (!hasDurations && !hasOutputs) {
return CommandResult::err("relay.set_config", "Provide 'durations' and/or 'outputs' objects");
}
if (hasDurations) {
_configManager.updateBellDurations(contents["durations"].as<JsonVariant>());
if (!_configManager.saveBellDurations()) {
return CommandResult::err("relay.set_config", "Durations applied but failed to save to SD");
}
}
if (hasOutputs) {
_configManager.updateBellOutputs(contents["outputs"].as<JsonVariant>());
if (!_configManager.saveBellOutputs()) {
return CommandResult::err("relay.set_config", "Outputs applied but failed to save to SD");
}
}
LOG_INFO(TAG, "relay.config applied (durations=%d outputs=%d)", hasDurations, hasOutputs);
return CommandResult::ok("relay.set_config", "Bell configuration updated and saved");
}
CommandResult RelayHandler::handleGetConfig(const CommandEnvelope& e) {
JsonDocument doc;
JsonObject durations = doc["durations"].to<JsonObject>();
@@ -104,15 +133,19 @@ CommandResult RelayHandler::handleTestBell(const CommandEnvelope& e) {
return CommandResult::err("relay.test_bell", "bell must be 0-15");
}
uint8_t output = (uint8_t)_configManager.getBellOutput(bell);
uint8_t output = _outputManager.getPhysicalOutput((uint8_t)bell);
uint16_t durationMs = _configManager.getBellDuration(bell);
if (output == 255) {
return CommandResult::err("relay.test_bell", "Bell " + String(bell) + " is disabled or unconfigured");
}
_outputManager.fireOutputForDuration(output, durationMs);
LOG_INFO(TAG, "Test bell %d → output %d for %d ms", bell, output, durationMs);
LOG_INFO(TAG, "Test bell %d → chip pin %d for %d ms", bell, output, durationMs);
JsonDocument doc;
doc["bell"] = bell;
doc["output"] = output;
doc["output"] = output + 1; // 1-based for human readability
doc["duration_ms"] = durationMs;
return CommandResult::ok("relay.test_bell", doc);
}
@@ -134,8 +167,9 @@ CommandResult RelayHandler::handleTestBatch(const CommandEnvelope& e) {
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);
uint8_t output = _outputManager.getPhysicalOutput((uint8_t)bell);
uint16_t durationMs = _configManager.getBellDuration((uint8_t)bell);
if (output == 255) continue; // disabled/unconfigured — skip silently
_outputManager.fireOutputForDuration(output, durationMs);
fired++;
}
@@ -21,6 +21,7 @@ private:
CommandResult handleSetDurations(const CommandEnvelope& e);
CommandResult handleSetOutputs(const CommandEnvelope& e);
CommandResult handleSetConfig(const CommandEnvelope& e);
CommandResult handleGetConfig(const CommandEnvelope& e);
CommandResult handleTestOutput(const CommandEnvelope& e);
CommandResult handleTestBell(const CommandEnvelope& e);