feat(Telemetry,Networking,OTAManager,CommunicationRouter): diagnostics report — CPU temp, WiFi reconnects, OTA state, stack high-water mark

Console requested four more Health-tab metrics beyond F-054's free_heap/ping
work. None needed 30s heartbeat freshness, so this adds a separate low-frequency
report instead of growing the heartbeat further: published every 5 min to
status/info (diagnostics_report) and queryable on-demand via a new
telemetry.get_diagnostics command.

Only CPU temperature needs local sampling (15s, via a min/max/avg RunningStats
accumulator on Telemetry) — a thermal spike could occur and subside entirely
within a 5-minute gap. The other three fields are monotonic/self-tracking
(WiFi reconnect counter, FreeRTOS's own stack high-water mark, rarely-changing
OTA state) so a single read at report time loses no information.

Verified: pio run on all four board variants.
This commit is contained in:
2026-07-17 13:09:16 +03:00
parent 9cd6b142ef
commit 959cb77d81
14 changed files with 388 additions and 9 deletions
@@ -185,6 +185,7 @@ void CommunicationRouter::loop() {
#if HAS_UART
_uartTransport.loop();
#endif
reportDiagnosticsIfDue();
}
// ─────────────────────────────────────────────────────────────────────────────
@@ -297,7 +298,7 @@ void CommunicationRouter::registerHandlers() {
// Telemetry
if (_telemetry) {
_bus.registerHandler(std::make_unique<TelemetryHandler>(*_telemetry));
_bus.registerHandler(std::make_unique<TelemetryHandler>(*_telemetry, _otaManager, _networking));
}
// MQTT control (enable / disable / get_config)
@@ -539,6 +540,57 @@ void CommunicationRouter::reportBootOnce() {
crash.available ? " (crash detail included)" : "");
}
// Diagnostics report — batched every DIAGNOSTICS_REPORT_INTERVAL_MS (5 min).
// Deliberately separate from the 30s heartbeat: temperature/stack/WiFi-
// reconnect/OTA state don't need 30s freshness, and the heartbeat already
// fires forever, per device, at fleet scale — adding heavier/rarer-need
// fields there is a permanent cost paid whether or not anyone's watching.
// Temperature is locally sampled every 15s by Telemetry::sampleDiagnosticsIfDue()
// (called from TelemetryTask's own loop) and reduced to min/max/avg here, so a
// short-lived spike between two 5-minute reports still gets caught — the other
// three fields (stack high-water, WiFi reconnects, OTA state) are monotonic/
// cumulative counters where a single read at report time is exactly as
// informative as continuous sampling would be. See docs/features/feature-catalog.md.
void CommunicationRouter::reportDiagnosticsIfDue() {
if (!_telemetry) return;
uint32_t now = millis();
if (now - _lastDiagnosticsReportMs < DIAGNOSTICS_REPORT_INTERVAL_MS) return;
_lastDiagnosticsReportMs = now;
JsonDocument payload;
// CPU temperature — min/max/avg since the last report. Captured before
// reset() so this report gets the full accumulated window.
uint16_t tempSampleCount = _telemetry->getTemperatureStats().count;
_telemetry->writeTemperatureStats(payload);
_telemetry->resetDiagnosticsStats();
// WiFi reconnects — lifetime this boot, plus last disconnect reason/time.
JsonObject wifiObj = payload["wifi_reconnects"].to<JsonObject>();
wifiObj["lifetime_count"] = _networking.getReconnectCount();
wifiObj["last_reason"] = _networking.getLastDisconnectReason();
wifiObj["last_at_uptime_ms"] = _networking.getLastDisconnectUptimeMs();
// OTA — current/available version, update-available flag, last check, last error.
JsonObject otaObj = payload["ota"].to<JsonObject>();
otaObj["current_version"] = _otaManager.getCurrentVersion().toString();
otaObj["update_available"] = _otaManager.isUpdateAvailable();
if (_otaManager.isUpdateAvailable()) {
otaObj["available_version"] = _otaManager.getAvailableVersion().toString();
}
otaObj["last_check_uptime_ms"] = _otaManager.getLastCheckUptimeMs();
otaObj["last_error"] = OTAManager::errorCodeToString(_otaManager.getLastError());
// Stack high-water mark — bytes free, per registered task (TelemetryTask,
// HealthMonitor, etc. — see Telemetry::registerTaskForStackMonitoring()).
JsonObject stackObj = payload["stack_high_water"].to<JsonObject>();
Telemetry::writeStackHighWaterMarks(stackObj);
publishInfo("diagnostics_report", payload);
LOG_INFO(TAG, "Diagnostics report published (temp samples=%u, wifi_reconnects=%lu)",
tempSampleCount, _networking.getReconnectCount());
}
void CommunicationRouter::publishInfo(const String& type, const JsonDocument& payload) {
if (!_mqttClient.isConnected()) return;