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 2b83d99820
commit 43a0c3c274
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,
}))
}
}

View File

@@ -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;
@@ -791,20 +791,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,
avg_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,
avg_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;
}
}