feat(MQTT): rebuild topic spec to v2 (F-062)

Full MQTT topic rebuild per docs/reference/vesper_mqtt_topic_spec_v2.md.
Only the MQTT transport path changed — WebSocket, HTTP, and UART are untouched.

- control/command (in) + control/ack (out, strictly replies) replace the old
  control/data pair; new control/reports for critical unsolicited events
  (bell overload) that don't belong on the strictly-reply ack channel
- status/playback split out for playback transitions only
- status/alerts + status/info renamed to system/alerts + system/info, both
  now correctly retained; new system/logs and system/metrics (diagnostics
  report moved here from system/info, gained bell strike/heat data)
- LWT registered for the first time — status/heartbeat gets
  {"state":"offline","ok":false} on an unclean disconnect; graceful
  disconnect (mqtt.disable) announces offline explicitly since LWT doesn't
  fire on a clean disconnect
- heartbeat payload flattened, gains independently-derived state/ok fields
  (via a new MQTTDriver::setStateProvider() wired from Player+HealthMonitor)
- optional req_id correlation added bus-wide (CommandEnvelope/CommandResult),
  not MQTT-only, since any transport with multiple senders sharing one
  channel has the same problem. Always optional — v1 clients unaffected.
- per-topic ON/OFF switches (ConfigManager::MqttTopicConfig, 8 flags) as
  sub-switches under the existing mqtt.enable/disable master switch, so a
  device can breathe by disabling topics it doesn't need. New
  mqtt.get_topics/mqtt.set_topics commands; command switch applies live.

Bumped to v2.2.0. Verified: pio run -e vesper-std-v1 -e agnus-v1 both build clean.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
2026-09-21 10:48:36 +03:00
co-authored by Claude Sonnet 5
parent 20576f12ff
commit 391ecb63fc
26 changed files with 1053 additions and 330 deletions
@@ -9,11 +9,14 @@ MQTTHandler::MQTTHandler(ConfigManager& configManager, MQTTDriver& mqttClient)
: _configManager(configManager), _mqttClient(mqttClient) {}
std::vector<String> MQTTHandler::commands() const {
return { "mqtt.enable", "mqtt.disable", "mqtt.get_config" };
return { "mqtt.enable", "mqtt.disable", "mqtt.get_config",
"mqtt.get_topics", "mqtt.set_topics" };
}
CommandResult MQTTHandler::execute(const CommandEnvelope& envelope) {
if (envelope.command == "mqtt.get_config") return handleGetConfig(envelope);
if (envelope.command == "mqtt.get_topics") return handleGetTopics(envelope);
if (envelope.command == "mqtt.set_topics") return handleSetTopics(envelope);
bool enable = (envelope.command == "mqtt.enable");
@@ -26,7 +29,10 @@ CommandResult MQTTHandler::execute(const CommandEnvelope& envelope) {
if (enable) {
_mqttClient.connect();
} else {
_mqttClient.disconnect();
// Graceful: announce offline on status/heartbeat before disconnecting —
// LWT only fires on an unclean drop, so an intentional disable would
// otherwise leave a stale "online" retained heartbeat behind.
_mqttClient.disconnect(true);
}
LOG_INFO(TAG, "MQTT %s by user command", enable ? "enabled" : "disabled");
@@ -44,3 +50,56 @@ CommandResult MQTTHandler::handleGetConfig(const CommandEnvelope& e) {
doc["use_ssl"] = mqttCfg.useSSL;
return CommandResult::ok("mqtt.get_config", doc);
}
CommandResult MQTTHandler::handleGetTopics(const CommandEnvelope& e) {
const auto& t = _configManager.getMqttTopicConfig();
JsonDocument doc;
doc["command"] = t.command;
doc["heartbeat"] = t.heartbeat;
doc["playback"] = t.playback;
doc["reports"] = t.reports;
doc["alerts"] = t.alerts;
doc["info"] = t.info;
doc["logs"] = t.logs;
doc["metrics"] = t.metrics;
return CommandResult::ok("mqtt.get_topics", doc);
}
CommandResult MQTTHandler::handleSetTopics(const CommandEnvelope& e) {
// Partial update — only fields present in contents are changed, matching
// the "at least one field, partial OK" handler convention used elsewhere
// (relay.set_durations, network.set_config, etc.). These switches only
// take effect while the mqtt.enable master switch is on.
ConfigManager::MqttTopicConfig t = _configManager.getMqttTopicConfig();
bool any = false;
if (e.contents["command"].is<bool>()) { t.command = e.contents["command"].as<bool>(); any = true; }
if (e.contents["heartbeat"].is<bool>()) { t.heartbeat = e.contents["heartbeat"].as<bool>(); any = true; }
if (e.contents["playback"].is<bool>()) { t.playback = e.contents["playback"].as<bool>(); any = true; }
if (e.contents["reports"].is<bool>()) { t.reports = e.contents["reports"].as<bool>(); any = true; }
if (e.contents["alerts"].is<bool>()) { t.alerts = e.contents["alerts"].as<bool>(); any = true; }
if (e.contents["info"].is<bool>()) { t.info = e.contents["info"].as<bool>(); any = true; }
if (e.contents["logs"].is<bool>()) { t.logs = e.contents["logs"].as<bool>(); any = true; }
if (e.contents["metrics"].is<bool>()) { t.metrics = e.contents["metrics"].as<bool>(); any = true; }
if (!any) {
return CommandResult::err("mqtt.set_topics",
"No recognised topic fields in contents (command, heartbeat, playback, reports, alerts, info, logs, metrics)");
}
bool wasCommandOn = _configManager.getMqttTopicConfig().command;
_configManager.setMqttTopicConfig(t);
if (!_configManager.saveGeneralConfig()) {
return CommandResult::err("mqtt.set_topics", "Topic switches changed but failed to save to SD");
}
// command topic switch controls the control/command subscription — flip
// the live subscription immediately rather than waiting for a reconnect.
if (wasCommandOn != t.command) {
_mqttClient.applyCommandTopicSwitch(t.command);
}
LOG_INFO(TAG, "MQTT topic switches updated");
return CommandResult::ok("mqtt.set_topics", "MQTT topic switches updated and saved");
}
@@ -20,4 +20,6 @@ private:
MQTTDriver& _mqttClient;
CommandResult handleGetConfig(const CommandEnvelope& e);
CommandResult handleGetTopics(const CommandEnvelope& e);
CommandResult handleSetTopics(const CommandEnvelope& e);
};