From 92a0a8d5e89c49bf65b8360a9dc54dda23a7baf2 Mon Sep 17 00:00:00 2001 From: bonamin Date: Tue, 29 Sep 2026 20:11:20 +0300 Subject: [PATCH] fix(CommunicationRouter): republish status/playback on every MQTT (re)connect (F-066) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit status/playback is retained but was only published on a Player transition. After a power loss mid-melody the Player boots straight into STOPPED with no transition, so the broker kept serving the old "playing" forever. The MQTT on-connect callback now publishes Player::getPlaybackSnapshot() on every CONNECT (not just the first after boot) — "idle" after a reboot, the live state after a plain reconnect. Co-Authored-By: Claude Opus 5.5 --- docs/features/feature-catalog.md | 21 +++++++++++++++++++ docs/reference/mqtt-events.md | 2 +- docs/reference/vesper_mqtt_topic_spec_v2.md | 2 +- .../CommunicationRouter.cpp | 6 ++++++ 4 files changed, 29 insertions(+), 2 deletions(-) diff --git a/docs/features/feature-catalog.md b/docs/features/feature-catalog.md index da55d8f..add85ce 100644 --- a/docs/features/feature-catalog.md +++ b/docs/features/feature-catalog.md @@ -78,6 +78,7 @@ Check `git log -- src/Version.hpp` for the real bump history before tagging a ne | F-063 | Fix: malformed `control/command` input (bad JSON, missing `cmd`) now gets an `ERROR` reply on `control/ack` instead of being silently dropped | v2.2.1 | Stable | | F-064 | Richer `status/playback` — `pid`, `speed`, `duration`, `started_at`, `source`, `ts` (same fields on the WebSocket playback INFO event); command origin carried on `CommandEnvelope` | v2.3.0 | Stable | | F-065 | `stopping` playback state formally documented (duration reached, bells finishing the current loop) — kept on `status/playback`, not suppressed | v2.3.0 | Stable | +| F-066 | Fix: stale retained `status/playback` — republished with the Player's real state on every MQTT (re)connect | v2.3.0 | Stable | --- @@ -1218,3 +1219,23 @@ Documented semantics (mqtt-events.md → States): entered only from `playing` wh No firmware change. Subsystem: `docs/reference/mqtt-events.md`, `docs/reference/vesper_mqtt_topic_spec_v2.md`, `docs/reference/api-reference.md` + +--- + +### F-066 — Fix: stale retained `status/playback` after power loss + +`status/playback` is retained, but was only ever published by `Player::setStatus()` on a *transition*. +If the board lost power (or its link) mid-melody, the last retained message was `playing` — and after +the reboot the Player starts in `STOPPED` without a transition, so nothing ever overwrote it. The +broker kept serving "playing" to every new subscriber indefinitely. + +**Fix.** The `MQTTDriver` on-connect callback registered in `CommunicationRouter::begin()` (which already +ran `reportBootOnce()`) now also calls `publishPlayback(_player->getPlaybackSnapshot())`. It runs on +**every** CONNECT, not just the first after boot: after a reboot it publishes `idle`; after a plain +reconnect it publishes the live state (which also refreshes `ts`/`time_elapsed` for anyone who +subscribed while the board was offline). Same gates as any other playback publish: `playback` topic +switch, heap guard. + +Verified: `pio run -e vesper-std-v1` builds clean. + +Subsystem: `src/Communication/CommunicationRouter/CommunicationRouter.cpp` diff --git a/docs/reference/mqtt-events.md b/docs/reference/mqtt-events.md index 5f572bd..4790bf8 100644 --- a/docs/reference/mqtt-events.md +++ b/docs/reference/mqtt-events.md @@ -87,7 +87,7 @@ to `status/heartbeat` (QoS 1, retained), overwriting the last "online" heartbeat **QoS:** 1 — **Retained:** yes — **Switch:** `playback` — **Trigger:** playback state transition only -Published by `Player::setStatus()` on every transition (not polled). This topic — not the `control/ack` reply — is the source of truth for what the board is actually doing. Skipped (not queued) while free heap is below the publish guard. +Published by `Player::setStatus()` on every transition (not polled), **and on every MQTT connect/reconnect** with the Player's current real state — so a retained `playing` left behind by a power loss mid-melody is overwritten (normally with `idle`) as soon as the board is back. This topic — not the `control/ack` reply — is the source of truth for what the board is actually doing. Skipped (not queued) while free heap is below the publish guard. ### Payload diff --git a/docs/reference/vesper_mqtt_topic_spec_v2.md b/docs/reference/vesper_mqtt_topic_spec_v2.md index 887653e..cbb8708 100644 --- a/docs/reference/vesper_mqtt_topic_spec_v2.md +++ b/docs/reference/vesper_mqtt_topic_spec_v2.md @@ -12,7 +12,7 @@ and [MQTTTransport](../../src/Communication/Transport/MQTTTransport/README.md) ( | `vesper/{device_id}/control/ack` | Replies to commands, including `pong` | 1 | No | `command` | **Strictly replies only — every message here answers a specific request, and every reply goes here. Nothing unsolicited is ever published to this topic.** Echoes `req_id` from the originating command when one was sent. `status: SUCCESS/ERROR/INFO`, `type`, optional `message`/`data`. Malformed input (invalid JSON, missing `cmd`) also gets an `ERROR` reply here (`type: "parse_error"`) rather than being silently dropped — see [Malformed input](#malformed-input) below. | | `vesper/{device_id}/control/reports` | Critical, time-sensitive board-initiated events (e.g. bell overload) | 1 | No | `reports` | The opposite of `control/command` — unsolicited data the board sends because it needs to be read *now*. Not a reply to anything, no `req_id`. Not retained — an event stream, not a status snapshot. | | `vesper/{device_id}/status/heartbeat` | Periodic health/state summary, every 30s | 1 | **Yes** | `heartbeat` | Also carries LWT (see below). Fields: `device_id`, `state` (`idle`/`playing`/`paused`/`error`/`booting`), `ok` (bool), `fw_version`, `uptime_ms`, `uptime_human`, `ip_address`, `gateway`, `rssi`, `free_heap` — `state`/`ok` independently derived, never mirrored from `status/playback` or `system/alerts`. | -| `vesper/{device_id}/status/playback` | Playback state transitions only | 1 | **Yes** | `playback` | `action`: `playing`/`paused`/`stopping`/`idle` (`stopping` = run-time cap reached, bells finishing the current loop), plus `time_elapsed`, `projected_run_time`, `pid`, `speed`, `duration`, `started_at`, `source`, `ts` — see [mqtt-events.md](mqtt-events.md#vesperidstatusplayback). Published on transition, not polled. Source of truth for what the board is doing (a `control/ack` SUCCESS only means "accepted"). | +| `vesper/{device_id}/status/playback` | Playback state transitions only | 1 | **Yes** | `playback` | `action`: `playing`/`paused`/`stopping`/`idle` (`stopping` = run-time cap reached, bells finishing the current loop), plus `time_elapsed`, `projected_run_time`, `pid`, `speed`, `duration`, `started_at`, `source`, `ts` — see [mqtt-events.md](mqtt-events.md#vesperidstatusplayback). Published on transition, not polled — plus once on every (re)connect with the current real state, so a stale retained `playing` never outlives a reboot. Source of truth for what the board is doing (a `control/ack` SUCCESS only means "accepted"). | | `vesper/{device_id}/system/alerts` | Subsystem state changes (warnings/errors) | 1 | **Yes**, until cleared | `alerts` | Published on transition only (WARNING/CRITICAL/FAILED/CLEARED). Not consumed by phone app — webserver/fleet only. | | `vesper/{device_id}/system/info` | Discrete system events (e.g. `boot_report`) | 1 | **Yes** | `info` | Not consumed by phone app. Excludes errors (those go to `alerts`) and metrics (those go to `metrics`). | | `vesper/{device_id}/system/logs` | Full log stream, serial-log equivalent | 0 | No | `logs` | Not consumed by phone app. Respects device's configured log level (`log.set_mqtt`) **and** this on/off switch independently — the switch can silence the whole channel without touching the configured level. Should not compete with heap/buffer needed for command traffic — guard same as `hasHeapForPublish()`. | diff --git a/src/Communication/CommunicationRouter/CommunicationRouter.cpp b/src/Communication/CommunicationRouter/CommunicationRouter.cpp index 335c426..15eded0 100644 --- a/src/Communication/CommunicationRouter/CommunicationRouter.cpp +++ b/src/Communication/CommunicationRouter/CommunicationRouter.cpp @@ -135,6 +135,12 @@ void CommunicationRouter::begin() { _mqttClient.setOnConnectCallback([this](bool /*sessionPresent*/) { reportBootOnce(); + + // status/playback is retained: if the board lost power (or the link) + // mid-melody, the broker would keep serving "playing" indefinitely. + // Overwrite it with the Player's real state on EVERY connect — normally + // "idle" after a reboot, the live state after a plain reconnect. + if (_player) publishPlayback(_player->getPlaybackSnapshot()); }); // Heartbeat state/ok derivation — independently derived per the topic spec,