diff --git a/rust/src/hal/battery.rs b/rust/src/hal/battery.rs index cf12f06..d0deb91 100644 --- a/rust/src/hal/battery.rs +++ b/rust/src/hal/battery.rs @@ -27,14 +27,15 @@ 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, + pub current_ma: Option, + pub soc_pct: Option, + pub soh_pct: Option, + pub temperature_c: Option, + pub cycle_count: Option, + pub remaining_mah: Option, + pub design_mah: Option, + pub error: Option, } #[derive(Debug, Serialize)] @@ -181,14 +182,15 @@ impl BatteryInteraction for BQ34Z100G1 { async fn get_battery_state(&mut self) -> FatResult { 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?), + 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, })) } } diff --git a/rust/src/main.rs b/rust/src/main.rs index 605ce29..4408738 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -43,7 +43,7 @@ use embassy_time::{Duration, Instant, Timer}; use esp_hal::rom::ets_delay_us; use esp_hal::system::software_reset; use esp_println::{logger, println}; -use hal::battery::BatteryState; +use hal::battery::{BatteryError, BatteryInfo, BatteryState}; use log::LogMessage; use option_lock::OptionLock; use plant_state::PlantState; @@ -857,20 +857,40 @@ async fn publish_mppt_state( async fn publish_battery_state( board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>, ) -> () { - let state = board + let telemetry = match board .board_hal .get_battery_monitor() .get_battery_state() - .await; - let value = match state { - Ok(state) => { - let json = serde_json::to_string(&state).unwrap().to_owned(); - json.to_owned() - } - Err(_) => "error".to_owned(), - }; + .await { - let _ = mqtt::publish("/battery", &*value).await; + Ok(BatteryState::Info(info)) => info, + Ok(BatteryState::Unknown) => BatteryInfo { + voltage_mv: None, + current_ma: None, + soc_pct: None, + soh_pct: None, + temperature_c: None, + cycle_count: None, + remaining_mah: None, + design_mah: None, + error: Some(BatteryError::NoBatteryMonitor), + }, + Err(e) => BatteryInfo { + voltage_mv: None, + current_ma: None, + soc_pct: None, + soh_pct: None, + temperature_c: None, + cycle_count: None, + remaining_mah: None, + design_mah: None, + error: Some(BatteryError::CommunicationError { + message: alloc::format!("{:?}", e), + }), + }, + }; + if let Ok(json) = serde_json::to_string(&telemetry) { + let _ = mqtt::publish("/battery", &json).await; } }