refactor: BatteryInfo structure (consistent layout)

- use tagged enum serialization for BatteryError
- flatten BatteryInfo telemetry with consistent field names and typed error
This commit is contained in:
2026-05-10 14:04:54 +02:00
parent 12d929d853
commit dc0bfcbbfd
2 changed files with 51 additions and 28 deletions

View File

@@ -27,20 +27,22 @@ pub trait BatteryInteraction {
#[derive(Debug, Serialize)]
pub struct BatteryInfo {
pub voltage_milli_volt: u16,
pub average_current_milli_ampere: i16,
pub cycle_count: u16,
pub design_milli_ampere_hour: u16,
pub remaining_milli_ampere_hour: u16,
pub state_of_charge: f32,
pub state_of_health: u16,
pub temperature: u16,
pub voltage_mv: Option<u16>,
pub avg_current_ma: Option<i16>,
pub soc_pct: Option<f32>,
pub soh_pct: Option<u16>,
pub temperature_c: Option<u16>,
pub cycle_count: Option<u16>,
pub remaining_mah: Option<u16>,
pub design_mah: Option<u16>,
pub error: Option<BatteryError>,
}
#[derive(Debug, Serialize)]
#[serde(tag = "kind")]
pub enum BatteryError {
NoBatteryMonitor,
CommunicationError(String),
CommunicationError { message: String },
}
#[derive(Debug, Serialize)]
@@ -180,14 +182,15 @@ impl BatteryInteraction for BQ34Z100G1 {
async fn get_battery_state(&mut self) -> FatResult<BatteryState> {
Ok(BatteryState::Info(BatteryInfo {
voltage_milli_volt: self.voltage_milli_volt().await?,
average_current_milli_ampere: self.average_current_milli_ampere().await?,
cycle_count: self.cycle_count().await?,
design_milli_ampere_hour: self.design_milli_ampere_hour().await?,
remaining_milli_ampere_hour: self.remaining_milli_ampere_hour().await?,
state_of_charge: self.state_charge_percent().await?,
state_of_health: self.state_health_percent().await?,
temperature: self.bat_temperature().await?,
voltage_mv: Some(self.voltage_milli_volt().await?),
avg_current_ma: Some(self.average_current_milli_ampere().await?),
soc_pct: Some(self.state_charge_percent().await?),
soh_pct: Some(self.state_health_percent().await?),
temperature_c: Some(self.bat_temperature().await?),
cycle_count: Some(self.cycle_count().await?),
remaining_mah: Some(self.remaining_milli_ampere_hour().await?),
design_mah: Some(self.design_milli_ampere_hour().await?),
error: None,
}))
}
}