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
@@ -2,7 +2,9 @@
## Summary
`MQTTTransport` wraps `MQTTDriver` and connects it to the `CommandBus`. It subscribes to the device control topic, parses incoming JSON messages into `CommandEnvelope` objects, and dispatches them. Command replies are published back to the device data topic. Unsolicited events (telemetry, bell strikes) reach clients via `broadcast()`, which also publishes to the data topic. The underlying `MQTTDriver` manages the broker connection lifecycle, reconnect backoff, and heartbeat independently; `MQTTTransport` only owns the message-routing glue.
`MQTTTransport` wraps `MQTTDriver` and connects it to the `CommandBus`. It subscribes to `control/command`, parses incoming JSON messages into `CommandEnvelope` objects (including the optional `req_id`), and dispatches them. Command replies are published back to `control/ack`. Bus-wide unsolicited broadcasts also go through `broadcast()` to `control/ack` — but purpose-specific events (playback transitions, alerts, reports, metrics) are published directly by `CommunicationRouter` to their own dedicated topics instead, so `control/ack` stays strictly request/response. The underlying `MQTTDriver` manages the broker connection lifecycle, reconnect backoff, LWT, and heartbeat independently; `MQTTTransport` only owns the message-routing glue.
See [docs/reference/vesper_mqtt_topic_spec_v2.md](../../../../docs/reference/vesper_mqtt_topic_spec_v2.md) for the full topic table.
---
@@ -15,7 +17,7 @@
| `void loop()` | No-op. AsyncMqttClient is fully event-driven. |
| `bool isHealthy() const` | Returns `MQTTDriver::isConnected()`. |
| `String name() const` | Returns `"MQTT"`. |
| `void broadcast(const String& msg)` | Publishes `msg` to the device data topic (`vesper/{uid}/data`). |
| `void broadcast(const String& msg)` | Publishes `msg` to `control/ack` (bus-wide broadcasts only — see summary above). |
| `void onNetworkConnected()` | Forwards to `MQTTDriver::onNetworkConnected()`, which schedules the broker connection after a 2-second stabilisation delay. |
| `void onNetworkDisconnected()` | Forwards to `MQTTDriver::onNetworkDisconnected()`. |
@@ -29,19 +31,21 @@
## Message Format
Incoming control messages must be JSON with at least a `"cmd"` field:
Incoming control messages (on `control/command`) must be JSON with at least a `"cmd"` field:
```json
{ "v": 2, "cmd": "relay.set_durations", "contents": { "bells": { "0": 500 } } }
{ "v": 2, "cmd": "relay.set_durations", "req_id": "abc123", "contents": { "bells": { "0": 500 } } }
```
`"v"` defaults to `2` if omitted. `"v": 1` triggers legacy translation before dispatch.
`"v"` defaults to `2` if omitted. `"v": 1` triggers legacy translation before dispatch. `"req_id"` is optional — see `CommandEnvelope.hpp` / `docs/reference/vesper_mqtt_topic_spec_v2.md` for the correlation contract.
Responses are published to the data topic:
Responses are published to `control/ack`:
```json
{ "status": "SUCCESS", "type": "relay.set_durations", "message": "Bell durations updated and saved" }
{ "status": "SUCCESS", "type": "relay.set_durations", "req_id": "abc123", "message": "Bell durations updated and saved" }
```
`req_id` is only present in the reply if the request included one.
---
## Dependencies
@@ -54,9 +58,9 @@ Responses are published to the data topic:
## Gotchas / Notes
- **Both reply and broadcast publish to the same data topic.** The broker does not distinguish between command replies and unsolicited events — clients must inspect `"type"` and `"status"` to differentiate.
- **`control/ack` is strictly request/response plus bus-wide broadcasts — never purpose-specific events.** The broker does not distinguish a command reply from a `CommandBus::broadcast()` fan-out; clients must inspect `"type"` and `"status"` to differentiate. But playback transitions, alerts, reports, and metrics never appear here — those go to their own topics via `CommunicationRouter` (`publishPlayback()`, `publishAlert()`, `publishReport()`, `publishMetrics()`), bypassing this transport entirely.
- **Broker connection is deferred.** `begin()` does not connect. The connection is triggered by `onNetworkConnected()`, which starts a 2-second non-blocking stabilisation timer in `MQTTDriver`.
- **MQTT must be enabled in config.** `MQTTDriver::connect()` is a no-op if `mqttConfig.enabled` is false. Toggle with `mqtt.enable` / `mqtt.disable` commands.
- **No per-message QoS.** Command replies and broadcasts are published at QoS 0. Heartbeats use QoS 1 with `retain = true`.
- **MQTT must be enabled in config, and the `command` topic switch must be on.** `MQTTDriver::connect()` is a no-op if `mqttConfig.enabled` is false. Toggle the master switch with `mqtt.enable` / `mqtt.disable`. Independently, the `command` topic switch (via `mqtt.set_topics`) gates the `control/command` subscription and `control/ack` replies — turning it off while MQTT stays connected silently stops all command traffic.
- **No per-message QoS.** Command replies and broadcasts are published at QoS 1 (matching the topic table). Heartbeats use QoS 1 with `retain = true`.
- **JSON parse errors drop the message silently** (with a `LOG_WARNING`). The client receives no error response.
- **Incoming commands are dropped under critical heap pressure.** `onMessage()` checks `MQTTDriver::hasHeapForPublish()` (free heap ≥ 20KB) before even constructing the `JsonDocument` used to deserialize the incoming payload. Below that threshold, the message is dropped with no reply and no error response — indistinguishable from a lost network message to the caller. This exists because a rapid burst of commands (each involving deserialize + dispatch + reply, all allocation-heavy, all running inline on AsyncTCP's own task) previously fragmented the heap enough to crash the device with an unrecoverable `abort()` — see `docs/features/feature-catalog.md` F-055. A full fix (queueing incoming commands to a dedicated task, so this work never runs on AsyncTCP's task at all) is a scoped future improvement, not yet implemented.