chore: cargo fmt

This commit is contained in:
2025-03-13 20:32:28 +01:00
parent 3cdaacabac
commit f340278236
5 changed files with 318 additions and 288 deletions

View File

@@ -10,17 +10,11 @@ use chrono_tz::{Europe::Berlin, Tz};
use config::Mode;
use esp_idf_hal::delay::Delay;
use esp_idf_sys::{
esp_ota_get_app_partition_count,
esp_ota_get_running_partition,
esp_ota_get_state_partition,
esp_ota_img_states_t,
esp_ota_img_states_t_ESP_OTA_IMG_ABORTED,
esp_ota_img_states_t_ESP_OTA_IMG_INVALID,
esp_ota_img_states_t_ESP_OTA_IMG_NEW,
esp_ota_img_states_t_ESP_OTA_IMG_PENDING_VERIFY,
esp_ota_img_states_t_ESP_OTA_IMG_UNDEFINED,
esp_ota_img_states_t_ESP_OTA_IMG_VALID,
vTaskDelay
esp_ota_get_app_partition_count, esp_ota_get_running_partition, esp_ota_get_state_partition,
esp_ota_img_states_t, esp_ota_img_states_t_ESP_OTA_IMG_ABORTED,
esp_ota_img_states_t_ESP_OTA_IMG_INVALID, esp_ota_img_states_t_ESP_OTA_IMG_NEW,
esp_ota_img_states_t_ESP_OTA_IMG_PENDING_VERIFY, esp_ota_img_states_t_ESP_OTA_IMG_UNDEFINED,
esp_ota_img_states_t_ESP_OTA_IMG_VALID, vTaskDelay,
};
use esp_ota::{mark_app_valid, rollback_and_reboot};
use log::log;
@@ -28,15 +22,11 @@ use once_cell::sync::Lazy;
use plant_hal::{PlantCtrlBoard, PlantHal, PLANT_COUNT};
use serde::{Deserialize, Serialize};
use crate::{
config::PlantControllerConfig,
webserver::webserver::httpd,
};
mod log;
use crate::{config::PlantControllerConfig, webserver::webserver::httpd};
mod config;
mod log;
pub mod plant_hal;
const TIME_ZONE: Tz = Berlin;
const MOIST_SENSOR_MAX_FREQUENCY: u32 = 5000; // 60kHz (500Hz margin)
@@ -72,7 +62,6 @@ impl WaitType {
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Default)]
/// Light State tracking data for mqtt
struct LightState {
@@ -182,8 +171,6 @@ struct PlantStateMQTT<'a> {
next_pump: &'a str,
}
fn safe_main() -> anyhow::Result<()> {
// It is necessary to call this function once. Otherwise some patches to the runtime
// implemented by esp-idf-sys might not link properly. See https://github.com/esp-rs/esp-idf-template/issues/71
@@ -234,38 +221,45 @@ fn safe_main() -> anyhow::Result<()> {
&format!("unknown {ota_state}")
}
};
log(log::LogMessage::PartitionState, 0,0, "", ota_state_string);
log(log::LogMessage::PartitionState, 0, 0, "", ota_state_string);
let mut board: std::sync::MutexGuard<'_, PlantCtrlBoard<'_>> = BOARD_ACCESS.lock().unwrap();
board.general_fault(false);
log(log::LogMessage::MountingFilesystem, 0,0,"","");
log(log::LogMessage::MountingFilesystem, 0, 0, "", "");
board.mount_file_system()?;
let free_space = board.file_system_size()?;
log(log::LogMessage::FilesystemMount, free_space.free_size as u32,
free_space.total_size as u32, &free_space.used_size.to_string(), "");
log(
log::LogMessage::FilesystemMount,
free_space.free_size as u32,
free_space.total_size as u32,
&free_space.used_size.to_string(),
"",
);
let mut cur = board.get_rtc_time().or_else(|err| {
println!("rtc module error: {:?}", err);
board.general_fault(true);
board.time()
}).map_err(|err| -> Result<(), _>{
bail!("time error {}", err);
}).unwrap();
let mut cur = board
.get_rtc_time()
.or_else(|err| {
println!("rtc module error: {:?}", err);
board.general_fault(true);
board.time()
})
.map_err(|err| -> Result<(), _> {
bail!("time error {}", err);
})
.unwrap();
//check if we know the time current > 2020 (plausibility check, this code is newer than 2020)
if cur.year() < 2020 {
to_config = true;
log(log::LogMessage::YearInplausibleForceConfig, 0,0,"","");
log(log::LogMessage::YearInplausibleForceConfig, 0, 0, "", "");
}
println!("cur is {}", cur);
board.update_charge_indicator();
if board.get_restart_to_conf() {
log(log::LogMessage::ConfigModeSoftwareOverride, 0,0,"","");
log(log::LogMessage::ConfigModeSoftwareOverride, 0, 0, "", "");
for _i in 0..2 {
board.general_fault(true);
Delay::new_default().delay_ms(100);
@@ -277,7 +271,7 @@ fn safe_main() -> anyhow::Result<()> {
board.set_restart_to_conf(false);
} else if board.is_mode_override() {
board.general_fault(true);
log(log::LogMessage::ConfigModeButtonOverride, 0,0,"","");
log(log::LogMessage::ConfigModeButtonOverride, 0, 0, "", "");
for _i in 0..5 {
board.general_fault(true);
Delay::new_default().delay_ms(100);
@@ -291,7 +285,7 @@ fn safe_main() -> anyhow::Result<()> {
} else {
board.general_fault(false);
}
}
}
let config: PlantControllerConfig;
match board.get_config() {
@@ -299,7 +293,13 @@ fn safe_main() -> anyhow::Result<()> {
config = valid;
}
Err(err) => {
log(log::LogMessage::ConfigModeMissingConfig, 0,0,"",&err.to_string());
log(
log::LogMessage::ConfigModeMissingConfig,
0,
0,
"",
&err.to_string(),
);
//config upload will trigger reboot!
let _ = board.wifi_ap(Option::None);
drop(board);
@@ -403,7 +403,13 @@ fn safe_main() -> anyhow::Result<()> {
publish_battery_state(&mut board, &config);
}
log(log::LogMessage::StartupInfo, wifi as u32, sntp as u32,&mqtt.to_string(),"");
log(
log::LogMessage::StartupInfo,
wifi as u32,
sntp as u32,
&mqtt.to_string(),
"",
);
if to_config {
//check if client or ap mode and init wifi
@@ -414,10 +420,9 @@ fn safe_main() -> anyhow::Result<()> {
let _webserver = httpd(reboot_now.clone());
wait_infinity(WaitType::ConfigButton, reboot_now.clone());
} else {
log(log::LogMessage::NormalRun, 0,0,"","");
log(log::LogMessage::NormalRun, 0, 0, "", "");
}
let dry_run = false;
let tank_state = determine_tank_state(&mut board, &config);
@@ -464,8 +469,6 @@ fn safe_main() -> anyhow::Result<()> {
}
};
let mut plantstate: [PlantState; PLANT_COUNT] = core::array::from_fn(|_| PlantState {
..Default::default()
});
@@ -477,26 +480,36 @@ fn safe_main() -> anyhow::Result<()> {
&mut board,
);
let pump_required = plantstate.iter().any(|it| it.do_water) && !water_frozen;
if pump_required {
log(log::LogMessage::EnableMain, dry_run as u32,0,"","");
if !dry_run{
board.any_pump(true)?;
}
log(log::LogMessage::EnableMain, dry_run as u32, 0, "", "");
if !dry_run {
board.any_pump(true)?;
}
for plant in 0..PLANT_COUNT {
let state = &mut plantstate[plant];
if state.do_water {
let plant_config = &config.plants[plant];
state.consecutive_pump_count = board.consecutive_pump_count(plant) + 1;
board.store_consecutive_pump_count(plant, state.consecutive_pump_count);
if state.consecutive_pump_count > plant_config.max_consecutive_pump_count as u32 {
log(log::LogMessage::ConsecutivePumpCountLimit, state.consecutive_pump_count as u32,plant_config.max_consecutive_pump_count as u32,&plant.to_string(),"");
log(
log::LogMessage::ConsecutivePumpCountLimit,
state.consecutive_pump_count as u32,
plant_config.max_consecutive_pump_count as u32,
&plant.to_string(),
"",
);
state.not_effective = true;
board.fault(plant, true);
}
log(log::LogMessage::PumpPlant, (plant + 1) as u32,plant_config.pump_time_s as u32,&dry_run.to_string(),"");
log(
log::LogMessage::PumpPlant,
(plant + 1) as u32,
plant_config.pump_time_s as u32,
&dry_run.to_string(),
"",
);
board.store_last_pump_time(plant, cur);
board.last_pump_time(plant);
state.active = true;
@@ -519,21 +532,21 @@ fn safe_main() -> anyhow::Result<()> {
enabled: config.night_lamp.enabled,
..Default::default()
};
if light_state.enabled {
if light_state.enabled {
light_state.is_day = is_day;
light_state.out_of_work_hour = !in_time_range(
&timezone_time,
config.night_lamp.night_lamp_hour_start,
config.night_lamp.night_lamp_hour_end,
);
if state_of_charge < config.night_lamp.low_soc_cutoff {
board.set_low_voltage_in_cycle();
} else if state_of_charge > config.night_lamp.low_soc_restore {
board.clear_low_voltage_in_cycle();
}
light_state.battery_low = board.low_voltage_in_cycle();
if !light_state.out_of_work_hour {
if config.night_lamp.night_lamp_only_when_dark {
if !light_state.is_day {
@@ -556,7 +569,7 @@ fn safe_main() -> anyhow::Result<()> {
light_state.active = false;
board.light(false).unwrap();
}
println!("Lightstate is {:?}", light_state);
}
@@ -587,13 +600,11 @@ fn safe_main() -> anyhow::Result<()> {
//is deep sleep
mark_app_valid();
let stay_alive_mqtt = STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed);
let stay_alive = stay_alive_mqtt;
println!("Check stay alive, current state is {}", stay_alive);
if stay_alive {
println!("Go to stay alive move");
drop(board);
@@ -972,7 +983,7 @@ fn wait_infinity(wait_type: WaitType, reboot_now: Arc<AtomicBool>) -> ! {
if reboot_now.load(std::sync::atomic::Ordering::Relaxed) {
//ensure clean http answer
Delay::new_default().delay_ms(500);
BOARD_ACCESS.lock().unwrap().deep_sleep( 1);
BOARD_ACCESS.lock().unwrap().deep_sleep(1);
}
}
}
@@ -1095,7 +1106,7 @@ fn get_version() -> VersionInfo {
return VersionInfo {
git_hash: (branch + "@" + hash),
build_time: env!("VERGEN_BUILD_TIMESTAMP").to_owned(),
partition: partition.to_owned() + &address.to_string()
partition: partition.to_owned() + &address.to_string(),
};
}