2026-05-30 20:04:04 +08:00
|
|
|
|
/*
|
2026-04-27 00:42:26 +08:00
|
|
|
|
* health_monitor.h - 系统健康监控头文件
|
2026-05-31 03:20:26 +08:00
|
|
|
|
*
|
|
|
|
|
|
* HealthMonitor 类封装健康状态管理、温度监控和 WiFi RSSI 监控。
|
2026-04-27 00:42:26 +08:00
|
|
|
|
*/
|
2026-04-27 02:34:47 +08:00
|
|
|
|
#pragma once
|
2026-04-27 00:42:26 +08:00
|
|
|
|
|
|
|
|
|
|
#include <stdint.h>
|
2026-05-31 03:20:26 +08:00
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
|
|
#include "driver/temperature_sensor.h"
|
2026-04-27 00:42:26 +08:00
|
|
|
|
#include "app_config.h"
|
|
|
|
|
|
|
|
|
|
|
|
// T9.3: 健康状态等级
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
|
SYS_NORMAL = 0, // 正常
|
|
|
|
|
|
SYS_WARNING = 1, // 警告
|
|
|
|
|
|
SYS_CRITICAL = 2, // 危险
|
|
|
|
|
|
SYS_SAFE_SHUTDOWN = 3 // 安全关机
|
|
|
|
|
|
} health_state_t;
|
|
|
|
|
|
|
|
|
|
|
|
typedef enum {
|
|
|
|
|
|
HEALTH_MON_OK = 0,
|
|
|
|
|
|
HEALTH_MON_WARNING = 1,
|
|
|
|
|
|
HEALTH_MON_CRITICAL = 2,
|
|
|
|
|
|
HEALTH_MON_SHUTDOWN = 3
|
|
|
|
|
|
} health_status_t;
|
|
|
|
|
|
|
|
|
|
|
|
// WiFi RSSI 阈值(来自 app_config.h)
|
|
|
|
|
|
#define WIFI_RSSI_NORMAL WIFI_RSSI_WARNING
|
|
|
|
|
|
#define WIFI_RSSI_WEAK WIFI_RSSI_CRITICAL
|
|
|
|
|
|
|
2026-05-05 04:48:31 +08:00
|
|
|
|
/**
|
2026-05-31 03:20:26 +08:00
|
|
|
|
* @brief 系统健康监控类(单例)
|
2026-05-30 20:04:04 +08:00
|
|
|
|
*
|
2026-05-31 03:20:26 +08:00
|
|
|
|
* 封装健康状态管理、温度定时监控和 WiFi RSSI 监控。
|
2026-05-30 20:04:04 +08:00
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
class HealthMonitor {
|
|
|
|
|
|
public:
|
2026-06-15 01:14:29 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 获取 HealthMonitor 单例实例。
|
|
|
|
|
|
* @return HealthMonitor 引用。
|
|
|
|
|
|
*/
|
2026-05-31 03:20:26 +08:00
|
|
|
|
static HealthMonitor& instance();
|
2026-05-30 20:04:04 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 初始化健康监控模块,创建内部互斥锁与基础状态。 */
|
|
|
|
|
|
void init();
|
2026-05-05 04:48:31 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 健康监控 FreeRTOS 任务入口。 */
|
|
|
|
|
|
static void taskEntry(void *pvParameters);
|
2026-05-05 04:48:31 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief OTA 传输前执行健康检查。 */
|
|
|
|
|
|
health_status_t checkBeforeOta();
|
2026-05-05 04:48:31 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief OTA 中继转发阶段执行健康检查(跳过 WiFi 检查)。 */
|
|
|
|
|
|
health_status_t checkBeforeOtaRelay();
|
2026-05-05 04:48:31 +08:00
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
/** @brief 记录一条健康事件日志。 */
|
|
|
|
|
|
void logEvent(const char *event, int value);
|
|
|
|
|
|
|
2026-06-14 17:10:24 +08:00
|
|
|
|
/** @brief 获取当前系统健康状态(线程安全)。 */
|
2026-05-31 03:20:26 +08:00
|
|
|
|
health_state_t getCurrent();
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 根据 WiFi RSSI 更新健康状态。 */
|
|
|
|
|
|
health_status_t updateFromWifiRssi(int8_t rssi);
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 获取最近一次芯片温度。 */
|
|
|
|
|
|
float getLastTemperature();
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 获取最近一次 WiFi RSSI 及其年龄。 */
|
|
|
|
|
|
bool getLastWifiRssi(int8_t *out_rssi, uint32_t *out_age_ms);
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
2026-06-14 17:10:24 +08:00
|
|
|
|
/** @brief 加锁读取健康状态。 */
|
|
|
|
|
|
health_state_t getState();
|
|
|
|
|
|
|
|
|
|
|
|
/** @brief 加锁设置健康状态。 */
|
|
|
|
|
|
void setState(health_state_t newState);
|
|
|
|
|
|
|
2026-05-31 03:20:26 +08:00
|
|
|
|
HealthMonitor() = default;
|
|
|
|
|
|
~HealthMonitor() = default;
|
|
|
|
|
|
HealthMonitor(const HealthMonitor&) = delete;
|
|
|
|
|
|
HealthMonitor& operator=(const HealthMonitor&) = delete;
|
|
|
|
|
|
|
|
|
|
|
|
SemaphoreHandle_t m_mutex = NULL;
|
|
|
|
|
|
temperature_sensor_handle_t m_tempHandle = NULL;
|
2026-06-14 17:10:24 +08:00
|
|
|
|
health_state_t m_healthState = SYS_NORMAL;
|
2026-05-31 03:20:26 +08:00
|
|
|
|
float m_lastTemperature = 0.0f;
|
|
|
|
|
|
int8_t m_lastWifiRssi = -127;
|
|
|
|
|
|
int64_t m_lastWifiRssiMs = 0;
|
|
|
|
|
|
|
|
|
|
|
|
static void tempMonitorCallback(void *arg);
|
|
|
|
|
|
};
|