mqtt_system_monitor/
status.rs

1use serde::Serialize;
2use std::collections::HashMap;
3use std::fmt;
4
5/// Message sent to the MQTT broker which later forwards it to Home Assistant
6///
7/// This contains the payload that Home Assistant uses to read the values.
8#[derive(Serialize, Debug, Default)]
9pub struct StatusMessage {
10    pub available: &'static str,
11
12    /// CPU usage in %
13    pub cpu_usage: Option<f32>,
14
15    /// CPU temperature in °C
16    pub cpu_temp: Option<f32>,
17
18    /// Memory usage in %
19    pub memory_usage: Option<f32>,
20
21    pub network: HashMap<String, NetworkStatus>,
22}
23
24/// Network status
25#[derive(Serialize, Debug, Default)]
26pub struct NetworkStatus {
27    /// Net TX rate in KiB/s
28    pub tx: Option<f64>,
29
30    /// Net RX rate in KiB/s
31    pub rx: Option<f64>,
32}
33
34impl fmt::Display for StatusMessage {
35    /// Formats the message to a JSON string
36    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37        let Ok(str) = serde_json::to_string(&self) else {
38            return Err(fmt::Error);
39        };
40        write!(f, "{str}")
41    }
42}