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
+84
View File
@@ -74,6 +74,7 @@ Check `git log -- src/Version.hpp` for the real bump history before tagging a ne
| F-059 | Fix: partition table mismatch blocking safe OTA to pre-restructure field devices | v2.1.0 | Stable |
| F-060 | `system.set_identity` — sysadmin override of factory-set device identity (serial/hw_family/hw_revision) | v2.1.0 | Stable |
| F-061 | Fix: `LegacyResponseAdapter` — v1 tablets saw device as "unavailable" while playing, due to `system.status` reply schema mismatch | v2.1.1 | Stable (temporary migration scaffolding) |
| F-062 | MQTT topic spec v2 — restructured topics (`control/command`/`control/ack`/`control/reports`/`status/playback`/`system/*`), LWT, `req_id` correlation, per-topic ON/OFF switches (`mqtt.set_topics`) | v2.2.0 | Stable |
---
@@ -1036,3 +1037,86 @@ before/after schema table, and the exact removal steps are in
Verified: `pio run -e vesper-std-v1` builds clean (all four environments share the touched files).
Subsystem: `src/Communication/CommandBus/LegacyResponseAdapter.hpp` (new), `src/Communication/CommandBus/LegacyResponseAdapter.cpp` (new), `src/Communication/Transport/WebSocketTransport/WebSocketTransport.cpp`, `src/Communication/Transport/MQTTTransport/MQTTTransport.cpp`, `src/Communication/Transport/HTTPTransport/HTTPTransport.cpp`
---
### F-062 — MQTT topic spec v2
Full rebuild of the MQTT wire topics per `docs/reference/vesper_mqtt_topic_spec_v2.md`. Only the MQTT
transport path changed — WebSocket, HTTP, and UART are untouched.
**Old → new topic mapping:**
- `control` (in) / `data` (out, replies + broadcasts mixed) → `control/command` (in, read-only from
the board's perspective) / `control/ack` (out, **strictly** replies + bus-wide broadcasts — nothing
purpose-specific is ever published here)
- `status/alerts` → `system/alerts` (unchanged shape, now retained until cleared — previously published
with `retain=false`, which was a latent bug against its own stated "state-change events" intent)
- `status/info` → `system/info` (unchanged shape, now retained)
- New: `status/playback` — playback transitions only, split out of the old `data` topic. Published by
`CommunicationRouter::publishPlayback()`, called from `Player::setStatus()` alongside the existing
WebSocket `broadcastStatus()` call (WS shape/behaviour unchanged).
- New: `control/reports` — critical, unsolicited, board-initiated events that need to be read *now*
(currently: `bell_overload`, moved off the old `data` topic). Not retained — an event stream, not a
status snapshot. The naming mirrors `control/command`: where `command` is inbound-only from the
board's perspective, `reports` is outbound-only, unsolicited.
- New: `system/logs` — the log-publish topic, unchanged in shape/mechanism from the pre-existing
`Logging` MQTT channel, just renamed and moved under `system/`.
- New: `system/metrics` — the existing 5-minute diagnostics report (CPU temp, WiFi reconnects, OTA
state, stack high-water marks) moved here from `system/info`, since it's exactly the "structured
telemetry for fleet-health graphing" the spec describes for this topic. Also gained per-bell
strike/heat-load data (`Telemetry::getStrikeCount()`/`getBellLoad()`/`isCoolingActive()` for all 16
channels) — the spec explicitly calls out "bell heat ratings" for this topic and there was previously
no MQTT-side exposure of it at all.
- Reserved, not yet built: `system/ota` (OTA-over-MQTT status streaming — no OTA-over-MQTT capability
exists yet).
**LWT (Last Will and Testament) — new.** `MQTTDriver::begin()` registers a Will via
`AsyncMqttClient::setWill()` targeting `status/heartbeat` with `{"device_id","state":"offline","ok":false}`
(QoS 1, retained). The broker publishes this automatically on an *unclean* disconnect
(crash/power-loss/network-loss) — there was no LWT at all before this change, so an unclean disconnect
previously left a stale "last known good" heartbeat retained indefinitely with no way to distinguish
"device is fine, just hasn't sent one in a while" from "device dropped off the network an hour ago."
`AsyncMqttClient` resends the Will on every reconnect automatically — no manual re-registration needed.
For *intentional* disconnects (which don't trigger LWT), `MQTTDriver::disconnect(true)` explicitly
publishes the same offline payload first — wired into `mqtt.disable`.
**Heartbeat payload reshaped.** Flattened (no more `status`/`type`/`payload` envelope wrapper — the
topic itself is the discriminator now, matching every other v2 topic). New fields: `state`
(`idle`/`playing`/`paused`/`error`/`booting`) and `ok` (bool), both independently derived via a new
`MQTTDriver::setStateProvider()` callback — wired by `CommunicationRouter` from
`Player::isCurrentlyPlaying()`/`isCurrentlyPaused()` and `HealthMonitor::isFirmwareStable()`, per the
spec's explicit requirement that these NOT be mirrored from `status/playback` or `system/alerts`.
`timestamp`/`firmware_version` renamed to `uptime_human`/`fw_version` for consistency with the rest of
the v2 field naming.
**`req_id` correlation — new, bus-wide.** `CommandEnvelope` and `CommandResult` both gained an optional
`reqId` field (`CommandBus::dispatch()` copies it from envelope to result before calling `reply()`, so
individual handlers never need to know about it). Always optional — a request without `req_id` gets a
reply without one, so v1 clients and any existing caller are unaffected. Implemented bus-wide rather
than MQTT-only because the same correlation problem (multiple logical senders sharing one channel)
applies to any future multiplexed transport, not just MQTT's shared broker topics. MQTT is the first
(and currently only) transport that actually parses/echoes it, in `MQTTTransport::onMessage()`.
**Per-topic ON/OFF switches — new.** `ConfigManager::MqttTopicConfig` (8 bools: `command`, `heartbeat`,
`playback`, `reports`, `alerts`, `info`, `logs`, `metrics`) persisted alongside the existing
`GeneralConfig` (SD: nested `mqttTopics` object in `generalConfig.json`; NVS on no-SD boards: packed
into a single `u8` bitfield at key `gc_mtop`). All default to `true` (no behaviour change on upgrade).
These are **sub-switches under the existing `mqtt.enable`/`mqtt.disable` master switch** — turning the
master off still disconnects the broker entirely regardless of these flags; while connected, each flag
independently gates whether its topic publishes. `command` is the one switch that covers both
directions — turning it off unsubscribes `control/command` live (`MQTTDriver::applyCommandTopicSwitch()`,
no reconnect needed) and blocks `control/ack` replies. Read via `mqtt.get_topics`, changed via
`mqtt.set_topics` (partial update — only send the fields you want to change).
**Why this shape (design decisions made with the user):** `control/ack` is strictly reply-only — no
unsolicited event is ever published there, full stop; anything unsolicited-but-important got its own
topic (`control/reports`) instead of being folded into the reply channel. Diagnostics moved to
`system/metrics` rather than staying on `system/info` because it's quantitative telemetry, not a
discrete one-off event like `boot_report`. `req_id` went in bus-wide (not MQTT-scoped) on the reasoning
that multiple devices/senders sharing one transport is a general problem, not MQTT-specific — with no
access control for now (anyone can set any `req_id`), left as a deliberate future extension point.
Verified: `pio run -e vesper-std-v1` and `pio run -e agnus-v1` both build clean (SD and no-SD NVS config
paths both touched).
Subsystem: `src/Communication/Drivers/MQTTDriver/MQTTDriver.hpp`, `src/Communication/Drivers/MQTTDriver/MQTTDriver.cpp`, `src/Communication/Transport/MQTTTransport/MQTTTransport.hpp`, `src/Communication/Transport/MQTTTransport/MQTTTransport.cpp`, `src/Communication/Handlers/MQTTHandler/MQTTHandler.hpp`, `src/Communication/Handlers/MQTTHandler/MQTTHandler.cpp`, `src/Communication/CommandBus/CommandEnvelope.hpp`, `src/Communication/CommandBus/CommandResult.hpp`, `src/Communication/CommandBus/CommandBus.cpp`, `src/Communication/CommunicationRouter/CommunicationRouter.hpp`, `src/Communication/CommunicationRouter/CommunicationRouter.cpp`, `src/ConfigManager/ConfigManager.hpp`, `src/ConfigManager/ConfigManager.cpp`, `src/Player/Player.cpp`