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
+69
View File
@@ -56,6 +56,7 @@
#include <Arduino.h>
#include <esp_system.h>
#include <nvs.h>
#include <ArduinoJson.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "../Logging/Logging.hpp"
@@ -83,6 +84,34 @@ static constexpr uint16_t BOOT_LOG_MAX_ENTRIES = 200;
// Runtime flush interval in milliseconds (10 minutes)
static constexpr uint32_t RUNTIME_FLUSH_INTERVAL_MS = 10 * 60 * 1000UL;
// Diagnostics report — local sampling + batch-send intervals (see
// docs/features/feature-catalog.md for why these differ). Temperature is
// sampled locally every DIAGNOSTICS_SAMPLE_INTERVAL_MS and reduced to
// min/max/avg; the batch is sent every DIAGNOSTICS_REPORT_INTERVAL_MS by
// CommunicationRouter::reportDiagnosticsOnce()-equivalent periodic call.
static constexpr uint32_t DIAGNOSTICS_SAMPLE_INTERVAL_MS = 15 * 1000UL;
static constexpr uint32_t DIAGNOSTICS_REPORT_INTERVAL_MS = 5 * 60 * 1000UL;
// Running min/max/avg accumulator for a value sampled many times between
// reports, then reset. Avoids storing every raw sample — O(1) per sample.
struct RunningStats {
float sum = 0;
float min = 0;
float max = 0;
uint16_t count = 0;
void reset() { sum = 0; min = 0; max = 0; count = 0; }
void addSample(float value) {
if (count == 0) { min = value; max = value; }
else { if (value < min) min = value; if (value > max) max = value; }
sum += value;
count++;
}
float average() const { return count > 0 ? (sum / count) : 0; }
};
class Telemetry {
private:
// Bell tracking
@@ -125,6 +154,14 @@ private:
// Most recent crash's coredump summary, if any — read once at begin().
CrashReport crashReport;
// Diagnostics — CPU temperature sampled locally every 15s, reduced to
// min/max/avg between 5-minute batch reports. See RunningStats above for
// why only temperature needs local sampling (stack high-water and the
// WiFi reconnect count are both monotonic/cumulative — a single read at
// report time is exactly as informative as reading them continuously).
RunningStats temperatureStats;
uint32_t lastDiagnosticsSampleMs = 0;
void initBootTracking(); // opens NVS namespace, increments+persists lifetimeBootCount
void captureCrashReport(); // reads+erases the coredump summary, if present
@@ -201,6 +238,38 @@ public:
void logTemperature(float temperature);
void logVibration(float vibration);
// ─── Diagnostics report (temperature, stack high-water, etc.) ──────────
// See docs/features/feature-catalog.md and Telemetry/README.md "Diagnostics
// reporting" section for the full design rationale.
// Called from telemetryTask's existing 1s loop. Internally no-ops unless
// DIAGNOSTICS_SAMPLE_INTERVAL_MS has elapsed since the last sample.
void sampleDiagnosticsIfDue();
// Current accumulated min/max/avg since the last resetDiagnosticsStats().
const RunningStats& getTemperatureStats() const { return temperatureStats; }
void resetDiagnosticsStats() { temperatureStats.reset(); }
// Registers a task handle (with a short display name, e.g. "TelemetryTask")
// to be included in diagnostics_report's stack_high_water object. Call once
// per task, any time after the task is created — typically from that
// subsystem's begin(). A handle of nullptr is silently ignored (lets
// callers register unconditionally without an extra null check).
static void registerTaskForStackMonitoring(const char* name, TaskHandle_t handle);
// Writes {"TaskName": bytesFree, ...} for every registered task into the
// given JsonObject. Bytes, not words — uxTaskGetStackHighWaterMark()
// returns words remaining; multiplied by sizeof(StackType_t) here so the
// console doesn't need to know the platform's word size.
static void writeStackHighWaterMarks(JsonObject& out);
// Writes this instance's cpu_temp {avg,min,max,samples} object into the
// given JsonDocument, IF resetStats is false (peek without consuming the
// accumulator — used by the on-demand telemetry.get_diagnostics command,
// which shouldn't reset what the next scheduled 5-minute report will see).
// Omits the field entirely if no samples have been taken yet.
void writeTemperatureStats(JsonDocument& doc) const;
// Force stop callback (to be set by main application)
void setForceStopCallback(void (*callback)());