Skip to main content

rustcv_core/
telemetry.rs

1use std::fmt;
2
3/// 设备健康状况与遥测数据
4///
5/// 这些数据通常不随每一帧变化,而是作为 DeviceControls 的一部分定期查询,
6/// 或者作为 FrameMetadata 的扩展。
7#[derive(Clone, Default, PartialEq)]
8pub struct DeviceTelemetry {
9    /// 芯片核心温度 (摄氏度)
10    /// 如果过高,意味着可能发生了热节流 (Thermal Throttling)
11    pub temperature_c: Option<f32>,
12
13    /// 当前 USB/网络链路的实际吞吐量 (Mbps)
14    pub link_throughput_mbps: Option<u32>,
15
16    /// 传输层丢包/误码计数
17    /// 比如 USB 的 Isochronous Packet Errors 或 GigE 的 Packet Resend
18    pub transmission_errors: u64,
19
20    /// 丢帧计数器 (因缓冲区溢出或 CPU 处理慢)
21    pub dropped_frames: u64,
22
23    /// 损坏帧计数器 (因 Payload 校验失败)
24    pub corrupted_frames: u64,
25
26    /// 当前功耗估算 (毫瓦/mW)
27    pub power_consumption_mw: Option<u32>,
28}
29
30impl fmt::Debug for DeviceTelemetry {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        f.debug_struct("DeviceTelemetry")
33            .field("temp_c", &self.temperature_c.unwrap_or(-1.0))
34            .field("link_mbps", &self.link_throughput_mbps.unwrap_or(0))
35            .field("errors", &self.transmission_errors)
36            .field("dropped", &self.dropped_frames)
37            .finish()
38    }
39}
40
41/// 简单的状态指示灯
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum DeviceHealthStatus {
44    Healthy,
45    Warning(HealthIssue),
46    Critical(HealthIssue),
47}
48
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum HealthIssue {
51    Overheating,
52    BandwidthSaturation,
53    HighPacketLoss,
54    SensorError,
55}
56
57impl DeviceTelemetry {
58    /// 基于遥测数据简单的健康评估
59    pub fn assess_health(&self) -> DeviceHealthStatus {
60        if let Some(t) = self.temperature_c {
61            if t > 85.0 {
62                return DeviceHealthStatus::Critical(HealthIssue::Overheating);
63            } else if t > 75.0 {
64                return DeviceHealthStatus::Warning(HealthIssue::Overheating);
65            }
66        }
67
68        if self.transmission_errors > 100 {
69            return DeviceHealthStatus::Warning(HealthIssue::HighPacketLoss);
70        }
71
72        DeviceHealthStatus::Healthy
73    }
74}