feat: semver versioning, FastAPI OTA integration, health alerts via MQTT
- Version.hpp: extract FW_VERSION from main.cpp into shared header
- OTAManager: replace uint16_t version with SemVer struct; update API URLs
to new FastAPI backend (/api/firmware/{hw_type}/{channel}/latest);
add postDownloadTelemetry() and postFlashTelemetry() for fleet visibility
- ConfigManager: remove fwVersion from DeviceConfig (always read from
FW_VERSION constant); saveDeviceConfig/loadDeviceConfig are now no-ops;
fallbackServerUrl replaced with fallbackServerUrls[2] array
- HealthMonitor: add AlertCallback + setAlertCallback() firing on state
transitions (including CLEARED on recovery)
- CommunicationRouter: wire HealthMonitor alert callback on begin();
add publishAlert() and publishInfo() for MQTT status topics
- MQTTDriver: add _alertsTopic + _infoTopic; add publishDirect() for
absolute topic publishing; migrate heartbeat to JsonDocument
- ResponseBuilder, ClientManager, BellEngine: StaticJsonDocument /
DynamicJsonDocument → JsonDocument; remove dead notifyBellsFired()
- docs: reorganise into architecture/, dev/, features/, reference/
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -129,6 +129,16 @@ void CommunicationRouter::begin() {
|
||||
// ── Register all domain handlers ─────────────────────────────────────────
|
||||
registerHandlers();
|
||||
|
||||
// ── Wire HealthMonitor alert callback ────────────────────────────────────
|
||||
if (_healthMonitor) {
|
||||
_healthMonitor->setAlertCallback(
|
||||
[this](const String& subsystem, const String& newState, const String& message) {
|
||||
publishAlert(subsystem, newState, message);
|
||||
}
|
||||
);
|
||||
LOG_INFO(TAG, "HealthMonitor alert callback registered");
|
||||
}
|
||||
|
||||
LOG_INFO(TAG, "Communication Router ready — CommandBus active");
|
||||
}
|
||||
|
||||
@@ -263,7 +273,7 @@ void CommunicationRouter::setupUdpDiscovery() {
|
||||
if (msg.indexOf("discover") >= 0) {
|
||||
shouldReply = true;
|
||||
} else {
|
||||
StaticJsonDocument<128> req;
|
||||
JsonDocument req;
|
||||
if (!deserializeJson(req, msg)) {
|
||||
shouldReply = (req["op"] == "discover");
|
||||
}
|
||||
@@ -271,7 +281,7 @@ void CommunicationRouter::setupUdpDiscovery() {
|
||||
|
||||
if (!shouldReply) return;
|
||||
|
||||
StaticJsonDocument<256> doc;
|
||||
JsonDocument doc;
|
||||
doc["op"] = "discover_reply";
|
||||
doc["svc"] = "vesper";
|
||||
doc["ver"] = 1;
|
||||
@@ -381,7 +391,7 @@ void CommunicationRouter::sendBellOverloadNotification(
|
||||
const std::vector<uint16_t>& bellLoads,
|
||||
const String& severity) {
|
||||
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc;
|
||||
doc["status"] = "INFO";
|
||||
doc["type"] = "bell_overload";
|
||||
|
||||
@@ -399,6 +409,41 @@ void CommunicationRouter::sendBellOverloadNotification(
|
||||
bellNumbers.size(), severity.c_str());
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Alert / Info publishing
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
void CommunicationRouter::publishAlert(const String& subsystem, const String& newState, const String& message) {
|
||||
if (!_mqttClient.isConnected()) return;
|
||||
|
||||
JsonDocument doc;
|
||||
doc["subsystem"] = subsystem;
|
||||
doc["state"] = newState;
|
||||
if (!message.isEmpty()) {
|
||||
doc["msg"] = message;
|
||||
}
|
||||
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
_mqttClient.publishDirect(_mqttClient.getAlertsTopic(), out, 1, false);
|
||||
LOG_DEBUG(TAG, "Alert published: %s → %s", subsystem.c_str(), newState.c_str());
|
||||
}
|
||||
|
||||
void CommunicationRouter::publishInfo(const String& type, const JsonDocument& payload) {
|
||||
if (!_mqttClient.isConnected()) return;
|
||||
|
||||
JsonDocument doc;
|
||||
doc["type"] = type;
|
||||
if (!payload.isNull()) {
|
||||
doc["payload"] = payload;
|
||||
}
|
||||
|
||||
String out;
|
||||
serializeJson(doc, out);
|
||||
_mqttClient.publishDirect(_mqttClient.getInfoTopic(), out, 0, false);
|
||||
LOG_DEBUG(TAG, "Info published: %s", type.c_str());
|
||||
}
|
||||
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
// Network state callbacks
|
||||
// ─────────────────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -97,6 +97,26 @@ public:
|
||||
const std::vector<uint16_t>& bellLoads,
|
||||
const String& severity);
|
||||
|
||||
/**
|
||||
* @brief Publish a subsystem alert to vesper/{id}/status/alerts.
|
||||
* Called by HealthMonitor when a subsystem state changes.
|
||||
* Only publishes on actual state change; no-ops if MQTT not connected.
|
||||
*
|
||||
* @param subsystem Subsystem name e.g. "BellEngine"
|
||||
* @param newState New state string: "HEALTHY" | "WARNING" | "CRITICAL" | "FAILED"
|
||||
* @param message Human-readable description (empty string on CLEARED)
|
||||
*/
|
||||
void publishAlert(const String& subsystem, const String& newState, const String& message = "");
|
||||
|
||||
/**
|
||||
* @brief Publish a general device info event to vesper/{id}/status/info.
|
||||
* Used for state changes such as playback start/stop, reboot reason, etc.
|
||||
*
|
||||
* @param type Event type string e.g. "playback_started"
|
||||
* @param payload JSON object with event-specific fields (pass an empty doc for no payload)
|
||||
*/
|
||||
void publishInfo(const String& type, const JsonDocument& payload);
|
||||
|
||||
// Network state callbacks (called by Networking)
|
||||
void onNetworkConnected();
|
||||
void onNetworkDisconnected();
|
||||
|
||||
@@ -76,10 +76,14 @@ void MQTTDriver::begin() {
|
||||
// Build topic strings (cache for performance)
|
||||
String deviceUID = _configManager.getDeviceUID();
|
||||
_controlTopic = "vesper/" + deviceUID + "/control";
|
||||
_dataTopic = "vesper/" + deviceUID + "/data";
|
||||
_clientId = "vesper-" + deviceUID;
|
||||
|
||||
LOG_INFO(TAG, "MQTT Topics: control=%s, data=%s", _controlTopic.c_str(), _dataTopic.c_str());
|
||||
_dataTopic = "vesper/" + deviceUID + "/data";
|
||||
_alertsTopic = "vesper/" + deviceUID + "/status/alerts";
|
||||
_infoTopic = "vesper/" + deviceUID + "/status/info";
|
||||
_clientId = "vesper-" + deviceUID;
|
||||
|
||||
LOG_INFO(TAG, "MQTT Topics: control=%s, data=%s, alerts=%s, info=%s",
|
||||
_controlTopic.c_str(), _dataTopic.c_str(),
|
||||
_alertsTopic.c_str(), _infoTopic.c_str());
|
||||
|
||||
// Setup event handlers
|
||||
_mqttClient.onConnect([this](bool sessionPresent) {
|
||||
@@ -165,6 +169,17 @@ uint16_t MQTTDriver::publish(const String& topic, const String& payload, int qos
|
||||
return packetId;
|
||||
}
|
||||
|
||||
uint16_t MQTTDriver::publishDirect(const String& fullTopic, const String& payload, int qos, bool retain) {
|
||||
if (!_mqttClient.connected()) {
|
||||
return 0;
|
||||
}
|
||||
uint16_t packetId = _mqttClient.publish(fullTopic.c_str(), qos, retain, payload.c_str());
|
||||
if (packetId > 0) {
|
||||
LOG_DEBUG(TAG, "Published to %s (packetId=%d)", fullTopic.c_str(), packetId);
|
||||
}
|
||||
return packetId;
|
||||
}
|
||||
|
||||
void MQTTDriver::setCallback(MessageCallback callback) {
|
||||
_messageCallback = callback;
|
||||
}
|
||||
@@ -361,11 +376,11 @@ void MQTTDriver::publishHeartbeat() {
|
||||
}
|
||||
|
||||
// Build heartbeat JSON message
|
||||
StaticJsonDocument<512> doc;
|
||||
JsonDocument doc;
|
||||
doc["status"] = "INFO";
|
||||
doc["type"] = "heartbeat";
|
||||
|
||||
JsonObject payload = doc.createNestedObject("payload");
|
||||
|
||||
JsonObject payload = doc["payload"].to<JsonObject>();
|
||||
|
||||
// Device ID from NVS
|
||||
payload["device_id"] = _configManager.getDeviceUID();
|
||||
|
||||
@@ -49,6 +49,7 @@ public:
|
||||
/**
|
||||
* @brief Publish message to MQTT
|
||||
* @param topic Topic to publish to (relative, will prepend "vesper/{deviceID}/")
|
||||
* If empty string — publishes to the data topic.
|
||||
* @param payload Message payload
|
||||
* @param qos QoS level (0, 1, or 2)
|
||||
* @param retain Retain flag
|
||||
@@ -56,6 +57,16 @@ public:
|
||||
*/
|
||||
uint16_t publish(const String& topic, const String& payload, int qos = 0, bool retain = false);
|
||||
|
||||
/**
|
||||
* @brief Publish to an absolute, fully-qualified topic (no prefix applied).
|
||||
* @param fullTopic Complete topic string
|
||||
* @param payload Message payload
|
||||
* @param qos QoS level (0, 1, or 2)
|
||||
* @param retain Retain flag
|
||||
* @return Packet ID (0 if failed)
|
||||
*/
|
||||
uint16_t publishDirect(const String& fullTopic, const String& payload, int qos = 0, bool retain = false);
|
||||
|
||||
/**
|
||||
* @brief Set message received callback
|
||||
*/
|
||||
@@ -66,6 +77,10 @@ public:
|
||||
*/
|
||||
bool isConnected() const;
|
||||
|
||||
/** @brief Full topic strings (read-only, available after begin()) */
|
||||
const String& getAlertsTopic() const { return _alertsTopic; }
|
||||
const String& getInfoTopic() const { return _infoTopic; }
|
||||
|
||||
/**
|
||||
* @brief Handle network connection callback
|
||||
*/
|
||||
@@ -86,7 +101,9 @@ private:
|
||||
// Device topic strings (cached for performance)
|
||||
String _controlTopic;
|
||||
String _dataTopic;
|
||||
String _clientId; // Store client ID to keep it alive
|
||||
String _alertsTopic; // vesper/{id}/status/alerts
|
||||
String _infoTopic; // vesper/{id}/status/info
|
||||
String _clientId; // Store client ID to keep it alive
|
||||
|
||||
/**
|
||||
* @brief Subscribe to control topic
|
||||
|
||||
@@ -98,7 +98,7 @@ void UARTDriver::processLine(const char* line) {
|
||||
if (strlen(line) == 0) return;
|
||||
|
||||
// Parse JSON
|
||||
StaticJsonDocument<1024> doc;
|
||||
JsonDocument doc;
|
||||
DeserializationError error = deserializeJson(doc, line);
|
||||
|
||||
if (error) {
|
||||
@@ -106,7 +106,7 @@ void UARTDriver::processLine(const char* line) {
|
||||
_errorCount++;
|
||||
|
||||
// Send error response back
|
||||
StaticJsonDocument<256> errorDoc;
|
||||
JsonDocument errorDoc;
|
||||
errorDoc["status"] = "ERROR";
|
||||
errorDoc["type"] = "parse_error";
|
||||
errorDoc["payload"] = error.c_str();
|
||||
|
||||
@@ -132,7 +132,7 @@ void WebSocketDriver::onData(AsyncWebSocketClient* client, void* arg, uint8_t* d
|
||||
LOG_DEBUG(TAG, "WebSocket client #%u sent: %s", client->id(), payload);
|
||||
|
||||
// Parse JSON
|
||||
StaticJsonDocument<2048> doc;
|
||||
JsonDocument doc;
|
||||
DeserializationError error = deserializeJson(doc, payload);
|
||||
|
||||
if (error) {
|
||||
|
||||
@@ -103,7 +103,7 @@ CommandResult FirmwareHandler::handleOtaCustom(const CommandEnvelope& e) {
|
||||
String url = e.contents["firmware_url"].as<String>();
|
||||
String checksum = e.contents["checksum"] | String("");
|
||||
size_t fileSize = e.contents["file_size"] | 0;
|
||||
uint16_t version = e.contents["version"] | 0;
|
||||
String version = e.contents["version"].as<String>();
|
||||
|
||||
LOG_WARNING(TAG, "Custom OTA update: %s", url.c_str());
|
||||
_otaManager.performCustomUpdate(url, checksum, fileSize, version);
|
||||
|
||||
@@ -21,7 +21,7 @@ String ResponseBuilder::fromResult(const CommandResult& result) {
|
||||
}
|
||||
|
||||
// Static member initialization
|
||||
StaticJsonDocument<512> ResponseBuilder::_responseDoc;
|
||||
JsonDocument ResponseBuilder::_responseDoc;
|
||||
|
||||
String ResponseBuilder::success(const String& type, const String& payload) {
|
||||
return buildResponse(Status::SUCCESS, type, payload);
|
||||
@@ -52,13 +52,13 @@ String ResponseBuilder::pong() {
|
||||
}
|
||||
|
||||
String ResponseBuilder::deviceStatus(PlayerStatus playerStatus, uint32_t timeElapsed, uint64_t projectedRunTime, const uint32_t strikeCounters[16]) {
|
||||
DynamicJsonDocument statusDoc(1024); // Increased size for strikeCounters array
|
||||
JsonDocument statusDoc;
|
||||
|
||||
statusDoc["status"] = "SUCCESS";
|
||||
statusDoc["type"] = "current_status";
|
||||
|
||||
// Create payload object with the exact format expected by Flutter
|
||||
JsonObject payload = statusDoc.createNestedObject("payload");
|
||||
JsonObject payload = statusDoc["payload"].to<JsonObject>();
|
||||
|
||||
// Convert PlayerStatus to string
|
||||
const char* statusStr;
|
||||
@@ -83,7 +83,7 @@ String ResponseBuilder::deviceStatus(PlayerStatus playerStatus, uint32_t timeEla
|
||||
payload["projected_run_time"] = projectedRunTime; // NEW: total projected duration
|
||||
|
||||
// Add strike counters array
|
||||
JsonArray strikeCountersArray = payload.createNestedArray("strike_counters");
|
||||
JsonArray strikeCountersArray = payload["strike_counters"].to<JsonArray>();
|
||||
for (uint8_t i = 0; i < 16; i++) {
|
||||
strikeCountersArray.add(strikeCounters[i]);
|
||||
}
|
||||
|
||||
@@ -90,5 +90,5 @@ public:
|
||||
private:
|
||||
// Internal helper methods
|
||||
static const char* statusToString(Status status);
|
||||
static StaticJsonDocument<512> _responseDoc; // Reusable document for efficiency
|
||||
static JsonDocument _responseDoc; // Reusable document for efficiency
|
||||
};
|
||||
|
||||
@@ -88,7 +88,7 @@ void HTTPTransport::handleCommandRequest(AsyncWebServerRequest* request,
|
||||
envelope.command = cmd;
|
||||
envelope.version = doc["v"] | 2;
|
||||
|
||||
if (doc.containsKey("contents")) {
|
||||
if (doc["contents"].is<JsonVariant>()) {
|
||||
envelope.contents.set(doc["contents"]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user