new ota logic
This commit is contained in:
@@ -29,8 +29,8 @@ use ::log::{error, info, warn};
|
||||
use alloc::borrow::ToOwned;
|
||||
use alloc::string::{String, ToString};
|
||||
use alloc::sync::Arc;
|
||||
use alloc::{format, vec};
|
||||
use alloc::vec::Vec;
|
||||
use alloc::{format, vec};
|
||||
use chrono::{DateTime, Datelike, Timelike, Utc};
|
||||
use chrono_tz::Tz::{self, UTC};
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
@@ -42,6 +42,7 @@ use embassy_sync::once_lock::OnceLock;
|
||||
use embassy_time::{Duration, Timer, WithTimeout};
|
||||
use esp_hal::rom::ets_delay_us;
|
||||
use esp_hal::system::software_reset;
|
||||
use esp_hal_ota::OtaImgState;
|
||||
use esp_println::{logger, println};
|
||||
use hal::battery::BatteryState;
|
||||
use log::LogMessage;
|
||||
@@ -501,16 +502,17 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
|
||||
info!("state of charg");
|
||||
let is_day = board.board_hal.is_day();
|
||||
let battery_state = board.board_hal.get_battery_monitor().get_state().await.unwrap_or(BatteryState::Unknown);
|
||||
let battery_state = board
|
||||
.board_hal
|
||||
.get_battery_monitor()
|
||||
.get_state()
|
||||
.await
|
||||
.unwrap_or(BatteryState::Unknown);
|
||||
info!("Battery state is {battery_state:?}");
|
||||
|
||||
let state_of_charge = match &battery_state {
|
||||
BatteryState::Unknown => {
|
||||
0
|
||||
}
|
||||
BatteryState::Info(data) => {
|
||||
data.state_of_charge
|
||||
}
|
||||
BatteryState::Unknown => 0,
|
||||
BatteryState::Info(data) => data.state_of_charge,
|
||||
};
|
||||
|
||||
let mut light_state = LightState {
|
||||
@@ -529,22 +531,10 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
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 {
|
||||
board.board_hal.get_esp().set_low_voltage_in_cycle();
|
||||
info!("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 {
|
||||
board.board_hal.get_esp().clear_low_voltage_in_cycle();
|
||||
info!("Clear low voltage in cycle");
|
||||
}
|
||||
@@ -758,7 +748,7 @@ pub async fn do_secure_pump(
|
||||
|
||||
async fn update_charge_indicator(
|
||||
board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>,
|
||||
) -> FatResult<()>{
|
||||
) -> FatResult<()> {
|
||||
//FIXME add config and code to allow power supply mode, in this case this is a nop
|
||||
//we have mppt controller, ask it for charging current
|
||||
let current = board.board_hal.get_mptt_current().await?;
|
||||
@@ -766,10 +756,9 @@ async fn update_charge_indicator(
|
||||
.board_hal
|
||||
.set_charge_indicator(current.as_milliamperes() > 20_f64)
|
||||
.await?;
|
||||
Ok(())
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
async fn publish_tank_state(
|
||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||
tank_state: &TankState,
|
||||
@@ -949,11 +938,7 @@ async fn publish_mppt_state(
|
||||
async fn publish_battery_state(
|
||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'static>>,
|
||||
) -> () {
|
||||
let state = board
|
||||
.board_hal
|
||||
.get_battery_monitor()
|
||||
.get_state()
|
||||
.await;
|
||||
let state = board.board_hal.get_battery_monitor().get_state().await;
|
||||
let value = match state {
|
||||
Ok(state) => {
|
||||
let json = serde_json::to_string(&state).unwrap().to_owned();
|
||||
@@ -986,7 +971,7 @@ async fn wait_infinity(
|
||||
loop {
|
||||
{
|
||||
let mut board = BOARD_ACCESS.get().await.lock().await;
|
||||
match update_charge_indicator(&mut board).await{
|
||||
match update_charge_indicator(&mut board).await {
|
||||
Ok(_) => {}
|
||||
Err(error) => {
|
||||
if !suppress_further_mppt_error {
|
||||
@@ -1069,35 +1054,38 @@ async fn wait_infinity(
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_serial_config(board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'_>>, serial_config_receive: &AtomicBool, reboot_now: &AtomicBool) -> FatResult<()> {
|
||||
async fn handle_serial_config(
|
||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'_>>,
|
||||
serial_config_receive: &AtomicBool,
|
||||
reboot_now: &AtomicBool,
|
||||
) -> FatResult<()> {
|
||||
match board.board_hal.get_esp().read_serial_line().await {
|
||||
Ok(serial_line) => {
|
||||
match serial_line {
|
||||
None => {
|
||||
Ok(serial_line) => match serial_line {
|
||||
None => Ok(()),
|
||||
Some(line) => {
|
||||
if serial_config_receive.load(Ordering::Relaxed) {
|
||||
let ll = line.as_str();
|
||||
let config: PlantControllerConfig = serde_json::from_str(ll)?;
|
||||
board
|
||||
.board_hal
|
||||
.get_esp()
|
||||
.save_config(Vec::from(ll.as_bytes()))
|
||||
.await?;
|
||||
board.board_hal.set_config(config);
|
||||
serial_config_receive.store(false, Ordering::Relaxed);
|
||||
info!("Config received, rebooting");
|
||||
board.board_hal.get_esp().set_restart_to_conf(false);
|
||||
reboot_now.store(true, Ordering::Relaxed);
|
||||
Ok(())
|
||||
} else {
|
||||
if line == "automation:streamconfig" {
|
||||
serial_config_receive.store(true, Ordering::Relaxed);
|
||||
info!("streamconfig:recieving");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Some(line) => {
|
||||
if serial_config_receive.load(Ordering::Relaxed) {
|
||||
let ll = line.as_str();
|
||||
let config: PlantControllerConfig = serde_json::from_str(ll)?;
|
||||
board.board_hal.get_esp().save_config(Vec::from(ll.as_bytes())).await?;
|
||||
board.board_hal.set_config(config);
|
||||
serial_config_receive.store(false, Ordering::Relaxed);
|
||||
info!("Config received, rebooting");
|
||||
board.board_hal.get_esp().set_restart_to_conf(false);
|
||||
reboot_now.store(true, Ordering::Relaxed);
|
||||
Ok(())
|
||||
} else {
|
||||
if line == "automation:streamconfig" {
|
||||
serial_config_receive.store(true, Ordering::Relaxed);
|
||||
info!("streamconfig:recieving");
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
error!("Error reading serial line");
|
||||
Ok(())
|
||||
@@ -1153,13 +1141,35 @@ async fn get_version(
|
||||
let branch = env!("VERGEN_GIT_BRANCH").to_owned();
|
||||
let hash = &env!("VERGEN_GIT_SHA")[0..8];
|
||||
|
||||
let board = board.board_hal.get_esp();
|
||||
let esp = board.board_hal.get_esp();
|
||||
let current_partition: String = match esp.ota.get_currently_booted_partition() {
|
||||
Some(partition) => match partition {
|
||||
0 => "OTA0".to_string(),
|
||||
1 => "OTA1".to_string(),
|
||||
_ => format!("Pentry: {:?}", partition),
|
||||
},
|
||||
None => "Missing otainfo".to_string(),
|
||||
};
|
||||
let state = esp.ota.get_ota_image_state();
|
||||
let current_state: String = match state {
|
||||
Ok(state) => match state {
|
||||
OtaImgState::EspOtaImgNew => "New".to_string(),
|
||||
OtaImgState::EspOtaImgPendingVerify => "PendingVerify".to_string(),
|
||||
OtaImgState::EspOtaImgValid => "Valid".to_string(),
|
||||
OtaImgState::EspOtaImgInvalid => "Invalid".to_string(),
|
||||
OtaImgState::EspOtaImgAborted => "Aborted".to_string(),
|
||||
OtaImgState::EspOtaImgUndefined => "Undefined".to_string(),
|
||||
},
|
||||
Err(err) => {
|
||||
format!("Error: {:?}", err)
|
||||
}
|
||||
};
|
||||
|
||||
VersionInfo {
|
||||
git_hash: branch + "@" + hash,
|
||||
build_time: env!("VERGEN_BUILD_TIMESTAMP").to_owned(),
|
||||
current: format!("{:?}", board.current),
|
||||
slot0_state: format!("{:?}", board.slot0_state),
|
||||
slot1_state: format!("{:?}", board.slot1_state),
|
||||
current: format!("{:?}", current_partition),
|
||||
state: format!("{:?}", current_state),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1168,6 +1178,5 @@ struct VersionInfo {
|
||||
git_hash: String,
|
||||
build_time: String,
|
||||
current: String,
|
||||
slot0_state: String,
|
||||
slot1_state: String,
|
||||
state: String,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user