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:
@@ -31,6 +31,12 @@ Telemetry tracks per-bell strike counts and thermal load to protect actuators fr
|
||||
| `String getResetReasonString() const` | Human-readable reset reason, e.g. `"PANIC"`, `"POWERON"`, `"TASK_WATCHDOG"`. |
|
||||
| `uint32_t getLifetimeBootCount() const` | NVS-backed count of every boot this device has ever done. The authoritative counter — see [Boot tracking](#boot-tracking) below for why this exists alongside two other, narrower counters. |
|
||||
| `const CrashReport& getCrashReport() const` | Coredump summary from the most recent crash, if one was found at `begin()`. `.available` is `false` when the last reset wasn't a crash, or no coredump was present for one that was. |
|
||||
| `void sampleDiagnosticsIfDue()` | Called from `TelemetryTask`'s existing 1s loop. Reads `temperatureRead()` every `DIAGNOSTICS_SAMPLE_INTERVAL_MS` (15s) and folds it into `temperatureStats`. See [Diagnostics reporting](#diagnostics-reporting) below. |
|
||||
| `const RunningStats& getTemperatureStats() const` | Current accumulated min/max/avg temperature since the last `resetDiagnosticsStats()`. |
|
||||
| `void writeTemperatureStats(JsonDocument&) const` | Writes the `cpu_temp` object into the given document without resetting the accumulator (a peek, not a consuming read) — used by `telemetry.get_diagnostics`. Omits the field entirely if no samples exist yet. |
|
||||
| `void resetDiagnosticsStats()` | Zeroes the temperature accumulator. Called after each periodic diagnostics report, not after an on-demand `telemetry.get_diagnostics` read. |
|
||||
| `static void registerTaskForStackMonitoring(const char*, TaskHandle_t)` | Registers a task for stack-headroom reporting in future diagnostics reports. Call once per task, any time after creation — a `nullptr` handle is silently ignored. Fixed 8-slot registry (this codebase's established no-dynamic-allocation style). |
|
||||
| `static void writeStackHighWaterMarks(JsonObject&)` | Writes `{"TaskName": bytesFree, ...}` for every registered task. Bytes, not words — converts `uxTaskGetStackHighWaterMark()`'s word count via `sizeof(StackType_t)` so callers don't need to know the platform word size. |
|
||||
|
||||
---
|
||||
|
||||
@@ -177,4 +183,24 @@ Example requests:
|
||||
{ "v": 2, "cmd": "telemetry.reset_strikes", "contents": {} }
|
||||
{ "v": 2, "cmd": "telemetry.get_boot_history", "contents": {} }
|
||||
{ "v": 2, "cmd": "telemetry.get_boot_history", "contents": { "limit": 20 } }
|
||||
{ "v": 2, "cmd": "telemetry.get_diagnostics", "contents": {} }
|
||||
```
|
||||
|
||||
### Diagnostics reporting
|
||||
|
||||
A second, lower-frequency reporting mechanism alongside boot tracking above — see F-056 in the [feature catalog](../../docs/features/feature-catalog.md) for full design rationale. Where boot tracking answers "when did this device restart and why," diagnostics reporting answers "how is this device doing right now" across four dimensions that don't belong on the 30s heartbeat (too heavy/low-value at that frequency) but also shouldn't wait indefinitely for an on-demand poll (a real problem could be missed between polls):
|
||||
|
||||
| Field | Source | Sampling behaviour |
|
||||
|---|---|---|
|
||||
| `cpu_temp` | `Telemetry` (`temperatureRead()`, ESP32 die sensor) | Sampled locally every 15s, reduced to min/max/avg via `RunningStats`, reset after each periodic report |
|
||||
| `wifi_reconnects` | `Networking` | Cumulative lifetime-this-boot counter + last disconnect reason/time — read once per report, monotonic so no samples are lost between reports |
|
||||
| `ota` | `OTAManager` | Current/available version, update-available flag, last check time, last error — read once per report, changes rarely |
|
||||
| `stack_high_water` | `Telemetry`'s static task registry | FreeRTOS's own low-water-mark tracking already does the "don't miss a dip" job — a single read at report time is exactly as informative as continuous polling |
|
||||
|
||||
**Why temperature alone needs local sampling, and the other three don't:** temperature genuinely fluctuates between reports — a short thermal spike (e.g. a burst of relay activity) could occur and fully subside within the 5-minute gap between two reports, and a device reporting only its *current* value at report time would simply never see it. The other three fields are all monotonic or self-tracking (a reconnect counter only grows; `uxTaskGetStackHighWaterMark()` is itself already a running minimum since task start; OTA state changes only around an actual update) — reading them more often than once per report cannot reveal information a less-frequent read would miss.
|
||||
|
||||
**Publish cadence.** `CommunicationRouter::reportDiagnosticsIfDue()` checks a `millis()`-diff every main-loop iteration (same lightweight pattern as this class's own `RUNTIME_FLUSH_INTERVAL_MS` check — no new FreeRTOS timer) and publishes to `vesper/{uid}/status/info` every `DIAGNOSTICS_REPORT_INTERVAL_MS` (5 minutes) once `_telemetry` is set. See [CommunicationRouter's README](../Communication/CommunicationRouter/README.md) for the full payload shape.
|
||||
|
||||
**On-demand vs. periodic — the accumulator reset distinction.** `telemetry.get_diagnostics` (`TelemetryHandler`) returns the identical shape but calls `writeTemperatureStats()` — a peek — instead of resetting the accumulator afterward. This means polling the command doesn't rob the next scheduled periodic report of samples it would otherwise have seen; only the periodic path's own `resetDiagnosticsStats()` call clears the window.
|
||||
|
||||
**Stack monitoring coverage.** Only tasks that keep a stored `TaskHandle_t` can be registered — several short-lived tasks in this firmware (`mqttRecon`, `mqttHB`, `mqttConnect`, `netRecon`, `PlayerCtrl`, `MelodyDL`, `ntpSync`) are created with `xTaskCreate(..., NULL)` and discard their handle immediately, so they cannot be monitored this way. Currently registered: `TelemetryTask` (registered in `Telemetry::begin()`) and `HealthMonitor`'s monitoring task (registered in `HealthMonitor::begin()`) — the two tasks F-051's stack-overflow incident named directly. Additional tasks can be added by calling `Telemetry::registerTaskForStackMonitoring()` from their own `begin()`, once a handle exists.
|
||||
|
||||
Reference in New Issue
Block a user