feat(Player,PlaybackHandler): specific ACK messages for playback.play/stop (F-067)

Every rejection used to reach the caller as "Playback command failed".
Player::command() now returns PlayerCommandResult { ok, message } and
PlaybackHandler forwards the message verbatim:

- busy, missing pid, invalid speed / note_assignments / durations / mode,
  timed|interval without duration, bells disabled, bell overload lockout,
  melody failed to load (incl. 0-byte file), not found without url,
  needs download but no SD card
- success: "Playback started", "Melody download started — …",
  "Playback stopped", and "Already stopped" (stop while idle stays SUCCESS —
  idempotent, goal state reached)

Validation runs before setMelodyAttributes(), so a rejected play no longer
half-applies its fields. Also fixes: url was sticky (a later play of another
pid could download the old URL under the new name); bells.disable let a play
"succeed" and report playing with silent bells; a non-string name/pid
assigned nullptr to std::string.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-09-29 20:17:47 +03:00
co-authored by Claude Opus 5.5
parent 92a0a8d5e8
commit 46a1a2567f
9 changed files with 331 additions and 117 deletions
+53
View File
@@ -79,6 +79,7 @@ Check `git log -- src/Version.hpp` for the real bump history before tagging a ne
| 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 |
| F-067 | Specific `playback.play` / `playback.stop` ACK messages (busy, invalid params, not found, load failed, bells disabled, overload…) instead of a generic "Playback command failed"; `url` no longer sticky | v2.3.0 | Stable |
---
@@ -1239,3 +1240,55 @@ switch, heap guard.
Verified: `pio run -e vesper-std-v1` builds clean.
Subsystem: `src/Communication/CommunicationRouter/CommunicationRouter.cpp`
---
### F-067 — Specific playback ACK messages
`Player::command()` returned a bare `bool`, so every rejection reached the caller as
`"Playback command failed"` and every success as `"Playback command executed"` — the phone app couldn't
tell "already playing" from "melody missing" from "bells switched off".
**Now** `Player::command()` returns `PlayerCommandResult { ok, message }` and `PlaybackHandler` sends
`message` verbatim. All strings are constants at the top of `Player.cpp` (plus the per-field
`Invalid <field>: …` validation messages) and are listed exhaustively, in check order, in
api-reference.md → `playback.play` and the console page. Reply `type` stays `"playback"`.
Success: `Playback started`, `Melody download started — playback begins when it completes`,
`Playback stopped`, `Already stopped`.
**Decision — stop when already idle is SUCCESS** (`"Already stopped"`), not an error: stop is idempotent,
the caller's goal state is reached, and an app's stop button shouldn't have to special-case an error.
The distinct message still lets a client tell the two apart.
**Behaviour changes that come with it:**
- Up-front validation (`Player::validatePlayParams()`) runs **before** `setMelodyAttributes()`, so a
rejected play no longer half-applies its fields to the sticky state. New rejections that used to be
silently accepted: `speed` outside 0–65535 (used to wrap), `note_assignments` that isn't an array of
≤16 ints in 0–16 (extras used to be ignored), negative/non-integer durations (used to wrap to huge
unsigned values), unknown `mode` (used to fall back to `single`), `timed`/`interval` with
`duration` 0 (`interval` with 0 re-paused on every timer tick).
- **Blocked gates** (`Player::playbackBlockedReason()`): `bells.disable` in effect (previously the
command succeeded and status said `playing` while `BellEngine::start()` silently refused — new
`BellEngine::areBellsEnabled()`), and any bell over its max load while the guard is on (the same
condition `Telemetry::checkBellLoads()` force-stops on, so a start would be cut off within a second).
- `loadMelodyInRAM()` returns the error string instead of `bool`, and now treats a **0-byte** SD file as
`Melody failed to load` (previously it "loaded" zero steps, `play()` bailed silently and the command
still returned success).
- Needs-a-download on a build with no FileManager (agnus) is now rejected up front instead of
queueing a download that could only fail.
- **`url` is no longer sticky.** A URL left over from an earlier play was reused for a later play of a
*different* `pid` that wasn't on SD, downloading the old melody's file and saving it under the new
pid's name. Omitted `url` now means "no download for this play".
- `name`/`pid`/`uid` are read with `| ""` — a non-string value used to assign `nullptr` to a
`std::string` (undefined behaviour / crash).
Download failures still can't be ACK errors (the download runs after the reply, on `MelodyDL`) — see
F-068 for how they are reported. RF-triggered plays log the rejection reason (`RF slot N play
rejected: …`).
Verified: `pio run -e vesper-std-v1` and `pio run -e agnus-v1` build clean; ArduinoJson 7 type-check
semantics used by the validator (`is<uint32_t>` on negatives/floats/2^32, `is<int>`, `| ""` on
non-strings/null) confirmed with a host-side test.
Subsystem: `src/Player/Player.hpp`, `src/Player/Player.cpp`, `src/BellEngine/BellEngine.hpp`, `src/BellEngine/BellEngine.cpp`, `src/Communication/Handlers/PlaybackHandler/PlaybackHandler.hpp`, `src/Communication/Handlers/PlaybackHandler/PlaybackHandler.cpp`, `src/RFRemote/RFRemote.cpp`
+33 -15
View File
@@ -165,22 +165,22 @@ Both commands reply with **`type: "playback"`** (not the command name).
| | |
|---|---|
| Transports | MQTT, WebSocket, HTTP (UART: v1 form only) |
| Returns | `{ "status": "SUCCESS", "type": "playback", "message": "Playback command executed" }` |
| Returns | `{ "status": "SUCCESS", "type": "playback", "message": "Playback started" }` — or `"message": "Melody download started — playback begins when it completes"` when the melody must be downloaded first |
The SUCCESS reply means the board **accepted** the command, not that bells are ringing. `status/playback` (MQTT) is the source of truth for what the board is actually doing — `playing` / `paused` / `stopping` / `idle`; see [mqtt-events.md → States](mqtt-events.md#states).
**Fields are sticky.** Every field except `mode`/`continuous_loop` keeps the value from the *previous* `playback.play` (or its boot default) when omitted — including `pid`, `url`, `speed` and the tail of `note_assignments`. Send every field you care about on every play.
**Fields are sticky.** Every field except `url`, `mode` and `continuous_loop` keeps the value from the *previous* `playback.play` (or its boot default) when omitted — including `pid`, `speed` and the tail of `note_assignments`. Send every field you care about on every play. A rejected play never changes the stored values (all checks run first).
| Field | Type | Required | Notes |
|---|---|---|---|
| `pid` | string | yes* | Melody ID. Looked up in the built-in library, then `/melodies/{pid}` on SD, then downloaded from `url`. *If omitted, the previous `pid` is reused; the command fails if that is empty. |
| `uid` | string | no | Alias for `pid`, read only when `pid` is absent. |
| `name` | string | no | Display name. Informational only. |
| `url` | string | no | Download URL, used only when the melody is not built-in and not on SD. **The field is `url` — `download_url` is not read by `playback.play`** (that name belongs to `files.download`). Sticky: a URL from an earlier play is reused if omitted. |
| `speed` | int | no | ms per beat. Boot default 500. `0` is coerced to 300. |
| `note_assignments` | int[] | no | Up to 16 entries. Index = melody note (0-based), value = **1-based bell channel** to ring for that note, `0` = silent. Boot default `[1,2,…,16]`. Entries past 16 are ignored; a shorter array leaves the remaining entries at their previous values. |
| `mode` | string | no | `"single"` \| `"timed"` \| `"interval"` \| `"infinite"`. Unrecognised values behave as `"single"`. Selects the timing model — see below. |
| `duration` | int | no | ms. **Read only when `mode` is present.** |
| `url` | string | no | Download URL, used only when the melody is not built-in and not on SD. **The field is `url` — `download_url` is not read by `playback.play`** (that name belongs to `files.download`). Not sticky: omitted = no download possible for this play. |
| `speed` | int | no | ms per beat, 0–65535. Boot default 500. `0` is coerced to 300. |
| `note_assignments` | int[] | no | Up to 16 integers, each 0–16. Index = melody note (0-based), value = **1-based bell channel** to ring for that note, `0` = silent. Boot default `[1,2,…,16]`. A shorter array leaves the remaining entries at their previous values. |
| `mode` | string | no | `"single"` \| `"timed"` \| `"interval"` \| `"infinite"` — anything else is rejected. Selects the timing model — see below. |
| `duration` | int | no | ms, ≥ 0. **Read only when `mode` is present**; must be > 0 for `timed`/`interval`. |
| `pause_duration` | int | no | ms between segments. Read when `mode` is `"interval"`, or when `mode` is absent. |
| `total_duration` | int | no | ms. Read when `mode` is `"interval"`, or (with `continuous_loop: true`) when `mode` is absent. |
| `segment_duration` | int | no | ms. Legacy — read only when `mode` is absent. |
@@ -191,8 +191,8 @@ The SUCCESS reply means the board **accepted** the command, not that bells are r
| `mode` | Behaviour | Fields read |
|---|---|---|
| `"single"` | Play the melody once, then stop. | — (`duration` ignored) |
| `"timed"` | Loop the melody; when `duration` ms have elapsed, finish the current loop and stop (status goes `stopping` → `idle`). `duration: 0` behaves like `single`. | `duration` |
| `"interval"` | Play for `duration` ms, pause `pause_duration` ms, repeat; stop after `total_duration` ms overall (`0` = forever). | `duration`, `pause_duration`, `total_duration` |
| `"timed"` | Loop the melody; when `duration` ms have elapsed, finish the current loop and stop (status goes `stopping` → `idle`). | `duration` (> 0) |
| `"interval"` | Play for `duration` ms, pause `pause_duration` ms, repeat; stop after `total_duration` ms overall (`0` = forever). | `duration` (> 0), `pause_duration`, `total_duration` |
| `"infinite"` | Loop until `playback.stop`. | — |
**Legacy timing** (`mode` absent):
@@ -202,11 +202,27 @@ The SUCCESS reply means the board **accepted** the command, not that bells are r
| `false` / absent | Run for `segment_duration` ms (sticky, boot default 15000), finish the loop, stop. **`total_duration` is ignored** — it is overwritten with `segment_duration`. `segment_duration: 0` plays the melody once. |
| `true` | Segments of `segment_duration` ms separated by `pause_duration` ms. Stops after `total_duration` ms if given in this command, else after `segment_duration` if that was given, else the previous `total_duration`. Total `0` = forever. |
| Error | Condition |
|---|---|
| `Playback command failed` | Any rejection: playback already active (playing / paused / stopping — send `playback.stop` first); `pid` empty or `"-"`; melody file on SD could not be loaded; melody not built-in/on SD and no `url` |
Errors are checked in this order; the first that applies is returned. Every one leaves the player (and its stored fields) untouched.
Known gaps in current firmware (addressed separately — see the feature catalog): when a download is needed the reply is SUCCESS and a failed download is only logged; with `bells.disable` in effect the command succeeds and status reports `playing` but no bell rings.
| Error `message` | Condition |
|---|---|
| `Playback already active — send playback.stop first` | Player is not idle (`playing` / `paused` / `stopping`) |
| `Missing required field: pid` | No `pid`/`uid` in this command and none stored from a previous play; or it is empty, `"-"`, or not a string |
| `Invalid speed: must be an integer from 0 to 65535 (ms per beat)` | `speed` present but not an integer in range |
| `Invalid note_assignments: must be an array of up to 16 integers, each 0-16` | Not an array, more than 16 entries, or an entry outside 0–16 |
| `Invalid duration: must be a non-negative integer (ms)` | `duration` negative / non-integer / > 4294967295 |
| `Invalid segment_duration: must be a non-negative integer (ms)` | same, for `segment_duration` |
| `Invalid pause_duration: must be a non-negative integer (ms)` | same, for `pause_duration` |
| `Invalid total_duration: must be a non-negative integer (ms)` | same, for `total_duration` |
| `Invalid mode: must be single, timed, interval or infinite` | Unrecognised `mode` |
| `Invalid duration: mode timed and interval require duration > 0` | `mode` is `timed`/`interval` and `duration` is missing or 0 |
| `Playback blocked: bell mechanism is disabled (bells.enable)` | Bells master switch off (`bells.disable`) |
| `Playback blocked: bell overload protection active — wait for the bells to cool` | Load guard enabled and any bell is over its max load (the guard would force-stop immediately) |
| `Melody failed to load: file is empty or corrupt` | `/melodies/{pid}` exists on SD but is empty or has an odd byte count |
| `Melody not found: not built-in, not on SD card, and no url given` | Needs a download but no `url` was sent |
| `Melody not found locally and this device has no SD card to download it to` | Needs a download but the build has no SD card / FileManager (agnus) |
A download itself happens **after** the SUCCESS reply (it would otherwise block the network task for seconds), so a failed download can't be an ACK error — the board logs `Melody download failed` (or `Melody failed to load: …` for a bad downloaded file) and playback simply doesn't start.
```json
{ "v": 2, "req_id": "a1", "cmd": "playback.play", "contents": { "pid": "westminster", "speed": 400, "mode": "single" } }
@@ -221,9 +237,11 @@ Known gaps in current firmware (addressed separately — see the feature catalog
|---|---|
| Transports | MQTT, WebSocket, HTTP (UART: v1 form only) |
| Contents | *(none)* |
| Returns | `{ "status": "SUCCESS", "type": "playback", "message": "Playback command executed" }` |
| Returns | `{ "status": "SUCCESS", "type": "playback", "message": "Playback stopped" }` |
Stops immediately (bells are cut mid-loop, status goes straight to `idle`). Sending it while already idle also returns SUCCESS with the same message.
Stops immediately (bells are cut mid-loop, status goes straight to `idle`).
**Already idle → SUCCESS**, `"message": "Already stopped"` (no status/playback publish, since nothing changed). Stop is idempotent: the caller's goal state is reached, so it is not an error — the distinct message lets a client tell the two cases apart if it cares. `playback.stop` has no error replies.
```json
{ "v": 2, "cmd": "playback.stop" }