feat(Player,MQTT): add pid/speed/duration/started_at/source/ts to status/playback (F-064)

The remote phone app treats status/playback as the source of truth for what
the board is doing, but the payload couldn't say what was playing, and a
client receiving a stale retained message couldn't work out real elapsed
time. Adds (old fields kept):

- pid, speed, duration (effective stop timer after mode/legacy resolution)
- started_at / ts — Unix epoch s (UTC); system clock once NTP-synced, else
  new Timekeeper::getUtcEpoch() (RTC local time minus configured offsets),
  else 0
- source — app | rf | button | schedule | unknown. Carried, not guessed:
  new CommandEnvelope::origin set by each transport (MQTT/WS/HTTP → APP,
  UART slave board → BUTTON), mapped by PlaybackHandler; RFRemote passes RF

New PlaybackState.hpp has one PlaybackSnapshot::writeTo() used for both the
WebSocket INFO broadcast (under "payload") and MQTT status/playback (flat),
so local and remote clients match. publishPlayback() now honours the
hasHeapForPublish() guard before building the document.

Bumps FW_VERSION to 2.3.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 20:09:16 +03:00
co-authored by Claude Opus 5.5
parent b80dbc9b89
commit 204b54fce1
19 changed files with 280 additions and 62 deletions
+46
View File
@@ -76,6 +76,7 @@ Check `git log -- src/Version.hpp` for the real bump history before tagging a ne
| 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 |
| 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 |
---
@@ -1147,3 +1148,48 @@ guard itself must stay silent by design.
Verified: `pio run -e vesper-std-v1` builds clean.
Subsystem: `src/Communication/Transport/MQTTTransport/MQTTTransport.cpp`
---
### F-064 — Richer `status/playback` payload
Built for the remote (MQTT) phone app, which treats `status/playback` as the source of truth for what
the board is doing (a `control/ack` SUCCESS only means "accepted"). The old payload
(`action`, `time_elapsed`, `projected_run_time`) couldn't say *what* was playing, and — because the topic
is retained — a client receiving an old message had no way to work out real elapsed time.
**New fields** (old ones kept unchanged for backward compatibility):
- `pid` — melody being played (`""` when idle)
- `speed` — ms per beat (0 when idle)
- `duration` — requested run-time cap in ms. This is `Player::total_duration` *after* `mode`/legacy
resolution (`timed` → `duration`, `interval` → `total_duration`, legacy → `segment_duration` or
`total_duration`), i.e. the value the stop timer actually uses. `0` = no cap.
- `started_at` — Unix epoch s (UTC) when playback started, derived at publish time as `ts − elapsed`
so it is right even for a snapshot built mid-playback. `0` when idle or no valid clock.
- `source` — `app` | `rf` | `button` | `schedule` | `unknown` (`""` when idle).
- `ts` — Unix epoch s (UTC) when the message was built, `0` if no valid clock.
**Clock.** `Player::currentEpoch()`: the system clock if it is past 2024-01-01 (it is UTC once NTP has
synced via `configTime(0, 0, …)`), else `Timekeeper::getUtcEpoch()` (new) on RTC builds, else `0`.
The RTC holds *local* time (NTP results and `clock.set_time` are written with GMT+DST applied), so
`getUtcEpoch()` subtracts the configured offsets back out; it returns 0 when the RTC isn't running and
never logs. RTC-less builds (agnus) report `0` until NTP syncs.
**Source is carried, not guessed.** `CommandEnvelope` gained an `origin` field
(`UNKNOWN`/`APP`/`BUTTON`/`SCHEDULE`) set by each transport when it builds the envelope: MQTT, WebSocket,
HTTP and the settings web server → `APP`; UART → `BUTTON` (its peer is the LCD/button slave board).
`PlaybackHandler` maps it to `PlaybackSource` and passes it to `Player::command()`; `RFRemote::triggerPlay()`
passes `PlaybackSource::RF` directly. `SCHEDULE` is reserved — `Timekeeper::triggerEvent()` is still a TODO
and never starts playback.
**One writer for both transports.** New `src/Player/PlaybackState.hpp` defines `PlaybackSnapshot` with a
single `writeTo(JsonObject)`. `Player::setStatus()` builds one snapshot (`Player::getPlaybackSnapshot()`,
public so the MQTT connect path can reuse it) and writes it both into the WebSocket INFO broadcast
(under `payload`, legacy shape) and to `CommunicationRouter::publishPlayback(snapshot)` (flat), so
local and remote clients see identical fields. `publishPlayback()` now checks
`MQTTDriver::hasHeapForPublish()` before building the document, same as the heartbeat. Payload is ~170
bytes.
Verified: `pio run -e vesper-std-v1` and `pio run -e agnus-v1` both build clean.
Subsystem: `src/Player/PlaybackState.hpp` (new), `src/Player/Player.hpp`, `src/Player/Player.cpp`, `src/TimeKeeper/TimeKeeper.hpp`, `src/TimeKeeper/TimeKeeper.cpp`, `src/Communication/CommandBus/CommandEnvelope.hpp`, `src/Communication/Handlers/PlaybackHandler/PlaybackHandler.cpp`, `src/Communication/CommunicationRouter/CommunicationRouter.hpp`, `src/Communication/CommunicationRouter/CommunicationRouter.cpp`, all five transports (origin), `src/RFRemote/RFRemote.cpp`, `src/Version.hpp` (→ 2.3.0)
+31 -3
View File
@@ -87,19 +87,47 @@ 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).
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.
### Payload
Flat — no envelope wrapper.
```json
{ "action": "playing", "time_elapsed": 12, "projected_run_time": 45000 }
{
"action": "playing",
"time_elapsed": 12,
"projected_run_time": 45000,
"pid": "westminster",
"speed": 400,
"duration": 120000,
"started_at": 1790000000,
"source": "app",
"ts": 1790000012
}
```
```json
{ "action": "idle", "time_elapsed": 0, "projected_run_time": 0, "pid": "", "speed": 0, "duration": 0, "started_at": 0, "source": "", "ts": 1790000130 }
```
| Field | Type | Description |
|-------|------|-------------|
| `action` | string | `playing` \| `paused` \| `idle` |
| `time_elapsed` | integer | Seconds since playback started (0 for `idle`) |
| `time_elapsed` | integer | Seconds since playback started, as of `ts` (0 for `idle`) |
| `projected_run_time` | integer | Projected total run time in ms (0 if not applicable) |
| `pid` | string | Melody being played (`""` when idle) |
| `speed` | integer | ms per beat (0 when idle) |
| `duration` | integer | Requested run-time cap in ms — the effective stop timer after `mode`/legacy field resolution (`timed` → `duration`, `interval` → `total_duration`, legacy → `segment_duration`/`total_duration`). `0` = no cap: a `single` play ends naturally, `infinite` runs until stopped. 0 when idle. |
| `started_at` | integer | Unix epoch seconds (UTC) when playback started. `0` when idle or when the board has no valid clock. |
| `source` | string | What started the playback: `app` (MQTT/WebSocket/HTTP command), `rf` (RF remote slot), `button` (UART LCD/button slave board), `schedule` (reserved — scheduled events don't start playback yet), `unknown`. `""` when idle. |
| `ts` | integer | Unix epoch seconds (UTC) when this message was built. `0` when the board has no valid clock. |
**Clock source for `started_at` / `ts`:** the NTP-synced system clock if valid, else the RTC (converted from its stored local time to UTC using the configured offsets), else `0`. RTC-less builds (agnus) therefore report `0` until NTP has synced. Always check for `0` before using either.
**Stale retained messages.** Because the topic is retained, a client may receive a message published long ago. Real elapsed time is `now − started_at` (when `started_at != 0`); `ts` tells you how old the snapshot is. `time_elapsed` is only correct as of `ts`.
The WebSocket transport receives the same fields on every transition, wrapped in the legacy broadcast envelope: `{ "status": "INFO", "type": "playback", "payload": { ...fields above... } }`.
---
+1 -1
View File
@@ -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`/`idle`, plus `time_elapsed`, `projected_run_time`. Published on transition, not polled. |
| `vesper/{device_id}/status/playback` | Playback state transitions only | 1 | **Yes** | `playback` | `action`: `playing`/`paused`/`idle`, 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}/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()`. |