clippy happier
This commit is contained in:
105
rust/src/main.rs
105
rust/src/main.rs
@@ -65,7 +65,7 @@ struct LightState {
|
||||
active: bool,
|
||||
/// led should not be on at this time of day
|
||||
out_of_work_hour: bool,
|
||||
/// battery is low so do not use led
|
||||
/// the battery is low so do not use led
|
||||
battery_low: bool,
|
||||
/// the sun is up
|
||||
is_day: bool,
|
||||
@@ -79,8 +79,8 @@ enum SensorError {
|
||||
OpenCircuit { hz: f32, min: f32 },
|
||||
}
|
||||
|
||||
fn safe_main() -> anyhow::Result<()> {
|
||||
// It is necessary to call this function once. Otherwise some patches to the runtime
|
||||
fn safe_main() -> 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
|
||||
esp_idf_svc::sys::link_patches();
|
||||
|
||||
@@ -100,7 +100,7 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
|
||||
let version = get_version();
|
||||
println!(
|
||||
"Version useing git has {} build on {}",
|
||||
"Version using git has {} build on {}",
|
||||
version.git_hash, version.build_time
|
||||
);
|
||||
|
||||
@@ -129,16 +129,16 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
&format!("unknown {ota_state}")
|
||||
}
|
||||
};
|
||||
log(log::LogMessage::PartitionState, 0, 0, "", ota_state_string);
|
||||
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(LogMessage::MountingFilesystem, 0, 0, "", "");
|
||||
board.mount_file_system()?;
|
||||
let free_space = board.file_system_size()?;
|
||||
log(
|
||||
log::LogMessage::FilesystemMount,
|
||||
LogMessage::FilesystemMount,
|
||||
free_space.free_size as u32,
|
||||
free_space.total_size as u32,
|
||||
&free_space.used_size.to_string(),
|
||||
@@ -157,17 +157,17 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
})
|
||||
.unwrap();
|
||||
|
||||
//check if we know the time current > 2020 (plausibility check, this code is newer than 2020)
|
||||
//check if we know the time current > 2020 (plausibility checks, this code is newer than 2020)
|
||||
if cur.year() < 2020 {
|
||||
to_config = true;
|
||||
log(log::LogMessage::YearInplausibleForceConfig, 0, 0, "", "");
|
||||
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(LogMessage::ConfigModeSoftwareOverride, 0, 0, "", "");
|
||||
for _i in 0..2 {
|
||||
board.general_fault(true);
|
||||
Delay::new_default().delay_ms(100);
|
||||
@@ -177,9 +177,9 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
to_config = true;
|
||||
board.general_fault(true);
|
||||
board.set_restart_to_conf(false);
|
||||
} else if board.is_mode_override() {
|
||||
} else if board.mode_override_pressed() {
|
||||
board.general_fault(true);
|
||||
log(log::LogMessage::ConfigModeButtonOverride, 0, 0, "", "");
|
||||
log(LogMessage::ConfigModeButtonOverride, 0, 0, "", "");
|
||||
for _i in 0..5 {
|
||||
board.general_fault(true);
|
||||
Delay::new_default().delay_ms(100);
|
||||
@@ -187,7 +187,7 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
Delay::new_default().delay_ms(100);
|
||||
}
|
||||
|
||||
if board.is_mode_override() {
|
||||
if board.mode_override_pressed() {
|
||||
board.general_fault(true);
|
||||
to_config = true;
|
||||
} else {
|
||||
@@ -195,27 +195,26 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
let config: PlantControllerConfig;
|
||||
match board.get_config() {
|
||||
let config: PlantControllerConfig = match board.get_config() {
|
||||
Ok(valid) => {
|
||||
config = valid;
|
||||
valid
|
||||
}
|
||||
Err(err) => {
|
||||
log(
|
||||
log::LogMessage::ConfigModeMissingConfig,
|
||||
LogMessage::ConfigModeMissingConfig,
|
||||
0,
|
||||
0,
|
||||
"",
|
||||
&err.to_string(),
|
||||
);
|
||||
//config upload will trigger reboot!
|
||||
let _ = board.wifi_ap(Option::None);
|
||||
let _ = board.wifi_ap(None);
|
||||
drop(board);
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
let _webserver = httpd(reboot_now.clone());
|
||||
wait_infinity(WaitType::MissingConfig, reboot_now.clone());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut wifi = false;
|
||||
let mut mqtt = false;
|
||||
@@ -273,7 +272,7 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
}
|
||||
Err(err) => println!(
|
||||
"Could not start config override ap mode due to {}",
|
||||
err.to_string()
|
||||
err
|
||||
),
|
||||
}
|
||||
}
|
||||
@@ -321,7 +320,7 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
}
|
||||
|
||||
log(
|
||||
log::LogMessage::StartupInfo,
|
||||
LogMessage::StartupInfo,
|
||||
wifi as u32,
|
||||
sntp as u32,
|
||||
&mqtt.to_string(),
|
||||
@@ -329,7 +328,7 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
);
|
||||
|
||||
if to_config {
|
||||
//check if client or ap mode and init wifi
|
||||
//check if client or ap mode and init Wi-Fi
|
||||
println!("executing config mode override");
|
||||
//config upload will trigger reboot!
|
||||
drop(board);
|
||||
@@ -337,7 +336,7 @@ 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(LogMessage::NormalRun, 0, 0, "", "");
|
||||
}
|
||||
|
||||
let dry_run = false;
|
||||
@@ -367,10 +366,10 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
0,
|
||||
0,
|
||||
"",
|
||||
&format!("{}", &err.to_string()),
|
||||
&err.to_string()
|
||||
),
|
||||
}
|
||||
// disabled can not trigger this because of wrapping if is_enabled
|
||||
// disabled cannot trigger this because of wrapping if is_enabled
|
||||
board.general_fault(true);
|
||||
} else if tank_state.warn_level(&config.tank).is_ok_and(|warn| warn) {
|
||||
log(LogMessage::TankWaterLevelLow, 0, 0, "", "");
|
||||
@@ -432,15 +431,15 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
let pump_required = plantstate
|
||||
.iter()
|
||||
.zip(&config.plants)
|
||||
.any(|(it, conf)| it.needs_to_be_watered(&conf, &timezone_time))
|
||||
.any(|(it, conf)| it.needs_to_be_watered(conf, &timezone_time))
|
||||
&& !water_frozen;
|
||||
if pump_required {
|
||||
log(log::LogMessage::EnableMain, dry_run as u32, 0, "", "");
|
||||
log(LogMessage::EnableMain, dry_run as u32, 0, "", "");
|
||||
if !dry_run {
|
||||
board.any_pump(true)?; // what does this do? Does it need to be reset?
|
||||
}
|
||||
for (plant_id, (state, plant_config)) in plantstate.iter().zip(&config.plants).enumerate() {
|
||||
if state.needs_to_be_watered(&plant_config, &timezone_time) {
|
||||
if state.needs_to_be_watered(plant_config, &timezone_time) {
|
||||
let pump_count = board.consecutive_pump_count(plant_id) + 1;
|
||||
board.store_consecutive_pump_count(plant_id, pump_count);
|
||||
//TODO(judge) where to put this?
|
||||
@@ -456,11 +455,11 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
// board.fault(plant, true);
|
||||
//}
|
||||
log(
|
||||
log::LogMessage::PumpPlant,
|
||||
LogMessage::PumpPlant,
|
||||
(plant_id + 1) as u32,
|
||||
plant_config.pump_time_s as u32,
|
||||
&dry_run.to_string(),
|
||||
"",
|
||||
"",
|
||||
);
|
||||
board.store_last_pump_time(plant_id, cur);
|
||||
board.last_pump_time(plant_id);
|
||||
@@ -470,7 +469,7 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
Delay::new_default().delay_ms(1000 * plant_config.pump_time_s as u32);
|
||||
board.pump(plant_id, false)?;
|
||||
}
|
||||
} else if !state.pump_in_timeout(&plant_config, &timezone_time) {
|
||||
} else if !state.pump_in_timeout(plant_config, &timezone_time) {
|
||||
// plant does not need to be watered and is not in timeout
|
||||
// -> reset consecutive pump count
|
||||
board.store_consecutive_pump_count(plant_id, 0);
|
||||
@@ -504,23 +503,21 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
if config.night_lamp.night_lamp_only_when_dark {
|
||||
if !light_state.is_day {
|
||||
if light_state.battery_low {
|
||||
board.light(false).unwrap();
|
||||
board.light(false)?;
|
||||
} else {
|
||||
light_state.active = true;
|
||||
board.light(true).unwrap();
|
||||
board.light(true)?;
|
||||
}
|
||||
}
|
||||
} else if light_state.battery_low {
|
||||
board.light(false)?;
|
||||
} else {
|
||||
if light_state.battery_low {
|
||||
board.light(false).unwrap();
|
||||
} else {
|
||||
light_state.active = true;
|
||||
board.light(true).unwrap();
|
||||
}
|
||||
light_state.active = true;
|
||||
board.light(true)?;
|
||||
}
|
||||
} else {
|
||||
light_state.active = false;
|
||||
board.light(false).unwrap();
|
||||
board.light(false)?;
|
||||
}
|
||||
|
||||
println!("Lightstate is {:?}", light_state);
|
||||
@@ -547,10 +544,6 @@ fn safe_main() -> anyhow::Result<()> {
|
||||
};
|
||||
let _ = board.mqtt_publish(&config, "/state", "sleep".as_bytes());
|
||||
|
||||
//determine next event
|
||||
//is light out of work trigger soon?
|
||||
//is battery low ??
|
||||
//is deep sleep
|
||||
mark_app_valid();
|
||||
|
||||
let stay_alive_mqtt = STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed);
|
||||
@@ -576,7 +569,7 @@ fn publish_battery_state(
|
||||
let bat = board.get_battery_state();
|
||||
match serde_json::to_string(&bat) {
|
||||
Ok(state) => {
|
||||
let _ = board.mqtt_publish(&config, "/battery", state.as_bytes());
|
||||
let _ = board.mqtt_publish(config, "/battery", state.as_bytes());
|
||||
}
|
||||
Err(err) => {
|
||||
println!("Error publishing battery_state {}", err);
|
||||
@@ -631,10 +624,8 @@ fn wait_infinity(wait_type: WaitType, reboot_now: Arc<AtomicBool>) -> ! {
|
||||
drop(lock);
|
||||
vTaskDelay(delay);
|
||||
|
||||
if wait_type == WaitType::MqttConfig {
|
||||
if !STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
reboot_now.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
if wait_type == WaitType::MqttConfig && !STAY_ALIVE.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
reboot_now.store(true, std::sync::atomic::Ordering::Relaxed);
|
||||
}
|
||||
if reboot_now.load(std::sync::atomic::Ordering::Relaxed) {
|
||||
//ensure clean http answer
|
||||
@@ -655,7 +646,7 @@ fn main() {
|
||||
BOARD_ACCESS.lock().unwrap().set_restart_to_conf(false);
|
||||
BOARD_ACCESS.lock().unwrap().deep_sleep(1);
|
||||
}
|
||||
// if safe_main exists with error, rollback to known good ota version
|
||||
// if safe_main exists with an error, rollback to a known good ota version
|
||||
Err(err) => {
|
||||
println!("Failed main {}", err);
|
||||
let _rollback_successful = rollback_and_reboot();
|
||||
@@ -665,22 +656,22 @@ fn main() {
|
||||
}
|
||||
|
||||
fn to_string<T: Display>(value: Result<T>) -> String {
|
||||
return match value {
|
||||
match value {
|
||||
Ok(v) => v.to_string(),
|
||||
Err(err) => {
|
||||
format!("{:?}", err)
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn in_time_range(cur: &DateTime<Tz>, start: u8, end: u8) -> bool {
|
||||
let curhour = cur.hour() as u8;
|
||||
//eg 10-14
|
||||
if start < end {
|
||||
return curhour > start && curhour < end;
|
||||
curhour > start && curhour < end
|
||||
} else {
|
||||
//eg 20-05
|
||||
return curhour > start || curhour < end;
|
||||
curhour > start || curhour < end
|
||||
}
|
||||
}
|
||||
|
||||
@@ -695,11 +686,11 @@ fn get_version() -> VersionInfo {
|
||||
} else {
|
||||
"ota_0 @ "
|
||||
};
|
||||
return VersionInfo {
|
||||
git_hash: (branch + "@" + hash),
|
||||
VersionInfo {
|
||||
git_hash: branch + "@" + hash,
|
||||
build_time: env!("VERGEN_BUILD_TIMESTAMP").to_owned(),
|
||||
partition: partition.to_owned() + &address.to_string(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Debug)]
|
||||
|
||||
Reference in New Issue
Block a user