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:
@@ -42,8 +42,8 @@ use embassy_sync::once_lock::OnceLock;
|
||||
use embassy_time::{Duration, Instant, Timer};
|
||||
use esp_hal::rom::ets_delay_us;
|
||||
use esp_hal::system::software_reset;
|
||||
use esp_println::println;
|
||||
use hal::battery::BatteryState;
|
||||
use esp_println::{logger, println};
|
||||
use hal::battery::{BatteryError, BatteryInfo, BatteryState};
|
||||
use log::LogMessage;
|
||||
use option_lock::OptionLock;
|
||||
use plant_state::PlantState;
|
||||
@@ -581,16 +581,16 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
board.board_hal.get_config().night_lamp.night_lamp_hour_end,
|
||||
);
|
||||
|
||||
match battery_state {
|
||||
match &battery_state {
|
||||
BatteryState::Unknown => {
|
||||
light_state.battery_low = false;
|
||||
}
|
||||
BatteryState::Info(data) => {
|
||||
if data.state_of_charge < board.board_hal.get_config().night_lamp.low_soc_cutoff {
|
||||
if data.soc_pct.is_some_and(|soc| soc < board.board_hal.get_config().night_lamp.low_soc_cutoff as f32) {
|
||||
board.board_hal.get_esp().set_low_voltage_in_cycle();
|
||||
info!("Set low voltage in cycle");
|
||||
}
|
||||
if data.state_of_charge > board.board_hal.get_config().night_lamp.low_soc_restore {
|
||||
if data.soc_pct.is_some_and(|soc| soc > board.board_hal.get_config().night_lamp.low_soc_restore as f32) {
|
||||
board.board_hal.get_esp().clear_low_voltage_in_cycle();
|
||||
info!("Clear low voltage in cycle");
|
||||
}
|
||||
@@ -639,7 +639,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
let deep_sleep_duration_minutes: u32 =
|
||||
// if battery soc is unknown assume battery has enough change
|
||||
if matches!(battery_state, BatteryState::Info(data) if data.state_of_charge < 10) {
|
||||
if matches!(battery_state, BatteryState::Info(data) if data.soc_pct.is_some_and(|soc| soc < 10.)) {
|
||||
let _ = mqtt::publish("/deepsleep", "low Volt 12h").await;
|
||||
12 * 60
|
||||
} else if is_day {
|
||||
@@ -984,16 +984,38 @@ async fn publish_mppt_state(
|
||||
async fn publish_battery_state(
|
||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||
) -> FatResult<()> {
|
||||
let state = board.board_hal.get_battery_monitor().get_state().await;
|
||||
let value = match state {
|
||||
Ok(state) => {
|
||||
let json = serde_json::to_string(&state)?.to_owned();
|
||||
json.to_owned()
|
||||
}
|
||||
Err(_) => "error".to_owned(),
|
||||
};
|
||||
let telemetry = match board
|
||||
.board_hal
|
||||
.get_battery_monitor()
|
||||
.get_state()
|
||||
.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,
|
||||
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,
|
||||
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;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user