integrate battery state update into full codebase
This commit is contained in:
parent
506f0c36b5
commit
71973f8fe9
@ -32,7 +32,7 @@ pub(crate) fn create_initial_board(
|
|||||||
general_fault,
|
general_fault,
|
||||||
config,
|
config,
|
||||||
esp,
|
esp,
|
||||||
battery: Box::new(NoBatteryMonitor{}),
|
battery: Box::new(NoBatteryMonitor {}),
|
||||||
};
|
};
|
||||||
Ok(Box::new(v))
|
Ok(Box::new(v))
|
||||||
}
|
}
|
||||||
|
@ -281,34 +281,35 @@ impl PlantHal {
|
|||||||
|
|
||||||
let hal = match config {
|
let hal = match config {
|
||||||
Result::Ok(config) => {
|
Result::Ok(config) => {
|
||||||
let battery_interaction: Box<dyn BatteryInteraction + Send> = match config.hardware.battery {
|
let battery_interaction: Box<dyn BatteryInteraction + Send> =
|
||||||
BatteryBoardVersion::Disabled => Box::new(NoBatteryMonitor{}),
|
match config.hardware.battery {
|
||||||
BatteryBoardVersion::BQ34Z100G1 => {
|
BatteryBoardVersion::Disabled => Box::new(NoBatteryMonitor {}),
|
||||||
let mut battery_driver = Bq34z100g1Driver {
|
BatteryBoardVersion::BQ34Z100G1 => {
|
||||||
i2c: MutexDevice::new(&I2C_DRIVER),
|
let mut battery_driver = Bq34z100g1Driver {
|
||||||
delay: Delay::new(0),
|
i2c: MutexDevice::new(&I2C_DRIVER),
|
||||||
flash_block_data: [0; 32],
|
delay: Delay::new(0),
|
||||||
};
|
flash_block_data: [0; 32],
|
||||||
let status = print_battery_bq34z100(&mut battery_driver);
|
};
|
||||||
match status {
|
let status = print_battery_bq34z100(&mut battery_driver);
|
||||||
OkStd(_) => {}
|
match status {
|
||||||
Err(err) => {
|
OkStd(_) => {}
|
||||||
log(
|
Err(err) => {
|
||||||
LogMessage::BatteryCommunicationError,
|
log(
|
||||||
0u32,
|
LogMessage::BatteryCommunicationError,
|
||||||
0,
|
0u32,
|
||||||
"",
|
0,
|
||||||
&format!("{err:?})"),
|
"",
|
||||||
);
|
&format!("{err:?})"),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Box::new(BQ34Z100G1 { battery_driver })
|
||||||
}
|
}
|
||||||
Box::new(BQ34Z100G1 { battery_driver })
|
BatteryBoardVersion::WchI2cSlave => {
|
||||||
}
|
// TODO use correct implementation once availible
|
||||||
BatteryBoardVersion::WchI2cSlave => {
|
Box::new(NoBatteryMonitor {})
|
||||||
// TODO use correct implementation once availible
|
}
|
||||||
Box::new(NoBatteryMonitor{})
|
};
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
let board_hal: Box<dyn BoardInteraction + Send> = match config.hardware.board {
|
let board_hal: Box<dyn BoardInteraction + Send> = match config.hardware.board {
|
||||||
BoardVersion::INITIAL => {
|
BoardVersion::INITIAL => {
|
||||||
|
@ -12,6 +12,7 @@ use esp_idf_sys::{
|
|||||||
esp_ota_img_states_t_ESP_OTA_IMG_VALID, vTaskDelay,
|
esp_ota_img_states_t_ESP_OTA_IMG_VALID, vTaskDelay,
|
||||||
};
|
};
|
||||||
use esp_ota::{mark_app_valid, rollback_and_reboot};
|
use esp_ota::{mark_app_valid, rollback_and_reboot};
|
||||||
|
use hal::battery::BatteryState;
|
||||||
use log::{log, LogMessage};
|
use log::{log, LogMessage};
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
@ -421,7 +422,14 @@ fn safe_main() -> anyhow::Result<()> {
|
|||||||
.board_hal
|
.board_hal
|
||||||
.get_battery_monitor()
|
.get_battery_monitor()
|
||||||
.state_charge_percent()
|
.state_charge_percent()
|
||||||
.unwrap_or(0);
|
.unwrap_or(0.);
|
||||||
|
|
||||||
|
/// try to load full battery state if failed the battery state is unknown
|
||||||
|
let battery_state = board
|
||||||
|
.board_hal
|
||||||
|
.get_battery_monitor()
|
||||||
|
.get_battery_state()
|
||||||
|
.unwrap_or(hal::battery::BatteryState::Unknown);
|
||||||
|
|
||||||
let mut light_state = LightState {
|
let mut light_state = LightState {
|
||||||
enabled: board.board_hal.get_config().night_lamp.enabled,
|
enabled: board.board_hal.get_config().night_lamp.enabled,
|
||||||
@ -439,9 +447,23 @@ fn safe_main() -> anyhow::Result<()> {
|
|||||||
board.board_hal.get_config().night_lamp.night_lamp_hour_end,
|
board.board_hal.get_config().night_lamp.night_lamp_hour_end,
|
||||||
);
|
);
|
||||||
|
|
||||||
if state_of_charge < board.board_hal.get_config().night_lamp.low_soc_cutoff {
|
if state_of_charge
|
||||||
|
< board
|
||||||
|
.board_hal
|
||||||
|
.get_config()
|
||||||
|
.night_lamp
|
||||||
|
.low_soc_cutoff
|
||||||
|
.into()
|
||||||
|
{
|
||||||
board.board_hal.get_esp().set_low_voltage_in_cycle();
|
board.board_hal.get_esp().set_low_voltage_in_cycle();
|
||||||
} else if state_of_charge > board.board_hal.get_config().night_lamp.low_soc_restore {
|
} else if state_of_charge
|
||||||
|
> board
|
||||||
|
.board_hal
|
||||||
|
.get_config()
|
||||||
|
.night_lamp
|
||||||
|
.low_soc_restore
|
||||||
|
.into()
|
||||||
|
{
|
||||||
board.board_hal.get_esp().clear_low_voltage_in_cycle();
|
board.board_hal.get_esp().clear_low_voltage_in_cycle();
|
||||||
}
|
}
|
||||||
light_state.battery_low = board.board_hal.get_esp().low_voltage_in_cycle();
|
light_state.battery_low = board.board_hal.get_esp().low_voltage_in_cycle();
|
||||||
@ -487,25 +509,27 @@ fn safe_main() -> anyhow::Result<()> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let deep_sleep_duration_minutes: u32 = if state_of_charge < 10 {
|
let deep_sleep_duration_minutes: u32 =
|
||||||
let _ = board
|
// if battery soc is unknown assume battery has enough change
|
||||||
.board_hal
|
if state_of_charge < 10.0 && !matches!(battery_state, BatteryState::Unknown) {
|
||||||
.get_esp()
|
let _ = board
|
||||||
.mqtt_publish("/deepsleep", "low Volt 12h".as_bytes());
|
.board_hal
|
||||||
12 * 60
|
.get_esp()
|
||||||
} else if is_day {
|
.mqtt_publish("/deepsleep", "low Volt 12h".as_bytes());
|
||||||
let _ = board
|
12 * 60
|
||||||
.board_hal
|
} else if is_day {
|
||||||
.get_esp()
|
let _ = board
|
||||||
.mqtt_publish("/deepsleep", "normal 20m".as_bytes());
|
.board_hal
|
||||||
20
|
.get_esp()
|
||||||
} else {
|
.mqtt_publish("/deepsleep", "normal 20m".as_bytes());
|
||||||
let _ = board
|
20
|
||||||
.board_hal
|
} else {
|
||||||
.get_esp()
|
let _ = board
|
||||||
.mqtt_publish("/deepsleep", "night 1h".as_bytes());
|
.board_hal
|
||||||
60
|
.get_esp()
|
||||||
};
|
.mqtt_publish("/deepsleep", "night 1h".as_bytes());
|
||||||
|
60
|
||||||
|
};
|
||||||
let _ = board
|
let _ = board
|
||||||
.board_hal
|
.board_hal
|
||||||
.get_esp()
|
.get_esp()
|
||||||
@ -715,10 +739,13 @@ fn pump_info(
|
|||||||
|
|
||||||
fn publish_battery_state(board: &mut MutexGuard<'_, HAL<'_>>) {
|
fn publish_battery_state(board: &mut MutexGuard<'_, HAL<'_>>) {
|
||||||
let state = board.board_hal.get_battery_monitor().get_battery_state();
|
let state = board.board_hal.get_battery_monitor().get_battery_state();
|
||||||
let _ = board
|
if let Ok(serialized_battery_state_bytes) = serde_json::to_string(&state).map(|s| s.into_bytes())
|
||||||
.board_hal
|
{
|
||||||
.get_esp()
|
let _ = board
|
||||||
.mqtt_publish("/battery", state.as_bytes());
|
.board_hal
|
||||||
|
.get_esp()
|
||||||
|
.mqtt_publish("/battery", &serialized_battery_state_bytes);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn wait_infinity(wait_type: WaitType, reboot_now: Arc<AtomicBool>) -> ! {
|
fn wait_infinity(wait_type: WaitType, reboot_now: Arc<AtomicBool>) -> ! {
|
||||||
|
@ -223,7 +223,7 @@ fn get_battery_state(
|
|||||||
) -> Result<Option<std::string::String>, anyhow::Error> {
|
) -> Result<Option<std::string::String>, anyhow::Error> {
|
||||||
let mut board = BOARD_ACCESS.lock().expect("board access");
|
let mut board = BOARD_ACCESS.lock().expect("board access");
|
||||||
let battery_state = board.board_hal.get_battery_monitor().get_battery_state();
|
let battery_state = board.board_hal.get_battery_monitor().get_battery_state();
|
||||||
anyhow::Ok(Some(battery_state))
|
anyhow::Ok(Some(serde_json::to_string(&battery_state)?))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_log(
|
fn get_log(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user