fix: flatten BatteryInfo telemetry with consistent field names and typed error
This commit is contained in:
@@ -27,14 +27,15 @@ pub trait BatteryInteraction {
|
|||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
pub struct BatteryInfo {
|
pub struct BatteryInfo {
|
||||||
pub voltage_milli_volt: u16,
|
pub voltage_mv: Option<u16>,
|
||||||
pub average_current_milli_ampere: i16,
|
pub current_ma: Option<i16>,
|
||||||
pub cycle_count: u16,
|
pub soc_pct: Option<f32>,
|
||||||
pub design_milli_ampere_hour: u16,
|
pub soh_pct: Option<u16>,
|
||||||
pub remaining_milli_ampere_hour: u16,
|
pub temperature_c: Option<u16>,
|
||||||
pub state_of_charge: f32,
|
pub cycle_count: Option<u16>,
|
||||||
pub state_of_health: u16,
|
pub remaining_mah: Option<u16>,
|
||||||
pub temperature: u16,
|
pub design_mah: Option<u16>,
|
||||||
|
pub error: Option<BatteryError>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize)]
|
#[derive(Debug, Serialize)]
|
||||||
@@ -181,14 +182,15 @@ impl BatteryInteraction for BQ34Z100G1 {
|
|||||||
|
|
||||||
async fn get_battery_state(&mut self) -> FatResult<BatteryState> {
|
async fn get_battery_state(&mut self) -> FatResult<BatteryState> {
|
||||||
Ok(BatteryState::Info(BatteryInfo {
|
Ok(BatteryState::Info(BatteryInfo {
|
||||||
voltage_milli_volt: self.voltage_milli_volt().await?,
|
voltage_mv: Some(self.voltage_milli_volt().await?),
|
||||||
average_current_milli_ampere: self.average_current_milli_ampere().await?,
|
current_ma: Some(self.average_current_milli_ampere().await?),
|
||||||
cycle_count: self.cycle_count().await?,
|
soc_pct: Some(self.state_charge_percent().await?),
|
||||||
design_milli_ampere_hour: self.design_milli_ampere_hour().await?,
|
soh_pct: Some(self.state_health_percent().await?),
|
||||||
remaining_milli_ampere_hour: self.remaining_milli_ampere_hour().await?,
|
temperature_c: Some(self.bat_temperature().await?),
|
||||||
state_of_charge: self.state_charge_percent().await?,
|
cycle_count: Some(self.cycle_count().await?),
|
||||||
state_of_health: self.state_health_percent().await?,
|
remaining_mah: Some(self.remaining_milli_ampere_hour().await?),
|
||||||
temperature: self.bat_temperature().await?,
|
design_mah: Some(self.design_milli_ampere_hour().await?),
|
||||||
|
error: None,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ use embassy_time::{Duration, Instant, Timer};
|
|||||||
use esp_hal::rom::ets_delay_us;
|
use esp_hal::rom::ets_delay_us;
|
||||||
use esp_hal::system::software_reset;
|
use esp_hal::system::software_reset;
|
||||||
use esp_println::{logger, println};
|
use esp_println::{logger, println};
|
||||||
use hal::battery::BatteryState;
|
use hal::battery::{BatteryError, BatteryInfo, BatteryState};
|
||||||
use log::LogMessage;
|
use log::LogMessage;
|
||||||
use option_lock::OptionLock;
|
use option_lock::OptionLock;
|
||||||
use plant_state::PlantState;
|
use plant_state::PlantState;
|
||||||
@@ -857,20 +857,40 @@ async fn publish_mppt_state(
|
|||||||
async fn publish_battery_state(
|
async fn publish_battery_state(
|
||||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||||
) -> () {
|
) -> () {
|
||||||
let state = board
|
let telemetry = match board
|
||||||
.board_hal
|
.board_hal
|
||||||
.get_battery_monitor()
|
.get_battery_monitor()
|
||||||
.get_battery_state()
|
.get_battery_state()
|
||||||
.await;
|
.await
|
||||||
let value = match state {
|
|
||||||
Ok(state) => {
|
|
||||||
let json = serde_json::to_string(&state).unwrap().to_owned();
|
|
||||||
json.to_owned()
|
|
||||||
}
|
|
||||||
Err(_) => "error".to_owned(),
|
|
||||||
};
|
|
||||||
{
|
{
|
||||||
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user