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
@@ -30,6 +30,18 @@ struct CommandEnvelope {
Transport transport = Transport::WEBSOCKET;
// Who issued the command, as far as the transport knows. Set by each
// transport when it builds the envelope (MQTT/WebSocket/HTTP → APP, UART
// slave board → BUTTON). Handlers that record provenance (PlaybackHandler →
// status/playback "source") read this instead of guessing from `transport`.
enum class Origin {
UNKNOWN,
APP,
BUTTON,
SCHEDULE
};
Origin origin = Origin::UNKNOWN;
// WebSocket client ID — 0 for transports that don't have per-client identity
uint32_t clientId = 0;
@@ -649,19 +649,18 @@ void CommunicationRouter::publishInfo(const String& type, const JsonDocument& pa
LOG_DEBUG(TAG, "Info published: %s", type.c_str());
}
void CommunicationRouter::publishPlayback(const String& action, uint32_t timeElapsedSec, uint64_t projectedRunTimeMs) {
void CommunicationRouter::publishPlayback(const PlaybackSnapshot& snapshot) {
if (!_mqttClient.isConnected()) return;
if (!_configManager.getMqttTopicConfig().playback) return;
if (!_mqttClient.hasHeapForPublish()) return; // skip building the JsonDocument too, not just the publish
JsonDocument doc;
doc["action"] = action;
doc["time_elapsed"] = timeElapsedSec;
doc["projected_run_time"] = projectedRunTimeMs;
snapshot.writeTo(doc.to<JsonObject>());
String out;
serializeJson(doc, out);
_mqttClient.publishDirect(_mqttClient.getPlaybackTopic(), out, 1, true);
LOG_DEBUG(TAG, "Playback published: %s", action.c_str());
LOG_DEBUG(TAG, "Playback published: %s", snapshot.action);
}
void CommunicationRouter::publishReport(const String& type, const JsonDocument& payload) {
@@ -37,6 +37,7 @@
#include "../Transport/WebServerTransport/WebServerTransport.hpp"
#include "../../OTAManager/OTAManager.hpp" // needed for OTAManager::ErrorCode in otaErrorCodeToString()
#include "../../Player/PlaybackState.hpp" // PlaybackSnapshot for publishPlayback()
// Forward declarations — resolved in .cpp
class ConfigManager;
@@ -121,16 +122,15 @@ public:
const String& severity);
/**
* @brief Publish a playback state transition to vesper/{id}/status/playback (retained).
* @brief Publish the playback state to vesper/{id}/status/playback (retained, QoS 1).
* Called by Player::setStatus() on every transition. WS clients
* still receive the same event via broadcastStatus() separately —
* this is the MQTT-specific path.
* still receive the same fields via broadcastStatus() separately —
* this is the MQTT-specific path. Skipped (not queued) when MQTT is
* down, the "playback" switch is off, or heap is below the publish guard.
*
* @param action "playing" | "paused" | "idle"
* @param timeElapsedSec Seconds since playback started (0 for idle)
* @param projectedRunTimeMs Projected total run time in ms (0 if not applicable)
* @param snapshot Player::getPlaybackSnapshot() — see PlaybackState.hpp for fields
*/
void publishPlayback(const String& action, uint32_t timeElapsedSec, uint64_t projectedRunTimeMs);
void publishPlayback(const PlaybackSnapshot& snapshot);
/**
* @brief Publish a critical, time-sensitive board-initiated event to
@@ -16,12 +16,23 @@ CommandResult PlaybackHandler::execute(const CommandEnvelope& envelope) {
return CommandResult::err("playback", "Unknown playback command");
}
// Carry the command's origin through to the Player so status/playback can
// report what triggered playback (see PlaybackState.hpp).
static PlaybackSource sourceFromOrigin(CommandEnvelope::Origin origin) {
switch (origin) {
case CommandEnvelope::Origin::APP: return PlaybackSource::APP;
case CommandEnvelope::Origin::BUTTON: return PlaybackSource::BUTTON;
case CommandEnvelope::Origin::SCHEDULE: return PlaybackSource::SCHEDULE;
default: return PlaybackSource::UNKNOWN;
}
}
CommandResult PlaybackHandler::handlePlay(const CommandEnvelope& envelope) {
// Inject action so Player::command() takes the play branch regardless of
// whether it was set by the V1 adapter or arrived as a bare V2 play command.
JsonDocument& contents = const_cast<JsonDocument&>(envelope.contents);
contents["action"] = "play";
bool ok = _player.command(contents.as<JsonVariant>());
bool ok = _player.command(contents.as<JsonVariant>(), sourceFromOrigin(envelope.origin));
if (ok) return CommandResult::ok("playback", "Playback command executed");
return CommandResult::err("playback", "Playback command failed");
}
@@ -29,7 +40,7 @@ CommandResult PlaybackHandler::handlePlay(const CommandEnvelope& envelope) {
CommandResult PlaybackHandler::handleStop(const CommandEnvelope& envelope) {
JsonDocument& contents = const_cast<JsonDocument&>(envelope.contents);
contents["action"] = "stop";
bool ok = _player.command(contents.as<JsonVariant>());
bool ok = _player.command(contents.as<JsonVariant>(), sourceFromOrigin(envelope.origin));
if (ok) return CommandResult::ok("playback", "Playback command executed");
return CommandResult::err("playback", "Playback command failed");
}
@@ -85,6 +85,7 @@ void HTTPTransport::handleCommandRequest(AsyncWebServerRequest* request,
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::HTTP;
envelope.origin = CommandEnvelope::Origin::APP;
envelope.clientId = 0;
envelope.command = cmd;
envelope.version = doc["v"] | 2;
@@ -138,6 +139,7 @@ void HTTPTransport::handleStatusRequest(AsyncWebServerRequest* request) {
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::HTTP;
envelope.origin = CommandEnvelope::Origin::APP;
envelope.clientId = 0;
envelope.command = "system.status";
envelope.version = 2;
@@ -82,6 +82,7 @@ void MQTTTransport::onMessage(const String& topic, const String& payload) {
// Build the envelope
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::MQTT;
envelope.origin = CommandEnvelope::Origin::APP;
envelope.clientId = 0;
envelope.command = doc["cmd"].as<String>();
envelope.version = doc["v"] | 1;
@@ -76,6 +76,7 @@ void UARTTransport::onMessage(JsonDocument& doc) {
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::UART;
envelope.origin = CommandEnvelope::Origin::BUTTON; // UART peer is the LCD/button slave board
envelope.clientId = 0;
envelope.command = cmd;
envelope.version = doc["v"] | 1;
@@ -87,6 +87,7 @@ void WebServerTransport::handleSetMode(AsyncWebServerRequest* request) {
// Route through CommandBus: network.set_config with permanent_ap_mode field.
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::HTTP;
envelope.origin = CommandEnvelope::Origin::APP;
envelope.clientId = 0;
envelope.command = "network.set_config";
envelope.version = 2;
@@ -116,6 +117,7 @@ void WebServerTransport::handleReboot(AsyncWebServerRequest* request) {
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::HTTP;
envelope.origin = CommandEnvelope::Origin::APP;
envelope.clientId = 0;
envelope.command = "system.restart";
envelope.version = 2;
@@ -68,6 +68,7 @@ void WebSocketTransport::onMessage(uint32_t clientId, const JsonDocument& doc) {
CommandEnvelope envelope;
envelope.transport = CommandEnvelope::Transport::WEBSOCKET;
envelope.origin = CommandEnvelope::Origin::APP;
envelope.clientId = clientId;
envelope.command = cmd;
envelope.version = doc["v"] | 1;