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
+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" }