adjust rust code to new config file, fix bq34z100 flasher

This commit is contained in:
2024-12-16 02:15:03 +01:00
parent c89a617d9d
commit 74f9c17461
4 changed files with 267 additions and 220 deletions

View File

@@ -22,7 +22,7 @@ use plant_hal::{PlantCtrlBoard, PlantHal, PLANT_COUNT};
use serde::{Deserialize, Serialize};
use crate::{
config::Config,
config::PlantControllerConfig,
espota::{mark_app_valid, rollback_and_reboot},
webserver::webserver::httpd,
};
@@ -250,7 +250,7 @@ fn safe_main() -> anyhow::Result<()> {
}
}
let config: Config;
let config: PlantControllerConfig;
match board.get_config() {
Ok(valid) => {
config = valid;
@@ -271,8 +271,12 @@ fn safe_main() -> anyhow::Result<()> {
let mut sntp = false;
println!("attempting to connect wifi");
let mut ip_address: Option<String> = None;
if config.ssid.is_some() {
match board.wifi(config.ssid.clone().unwrap(), config.password.clone(), 10000) {
if config.network.ssid.is_some() {
match board.wifi(
config.network.ssid.clone().unwrap(),
config.network.password.clone(),
10000,
) {
Ok(ip_info) => {
ip_address = Some(ip_info.ip.to_string());
wifi = true;
@@ -412,16 +416,16 @@ fn safe_main() -> anyhow::Result<()> {
let mut did_pump = false;
match plant_to_pump {
Some(plant) => {
let plant_config = &config.plants[plant];
let state = &mut plantstate[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 > config.max_consecutive_pump_count.into() {
if state.consecutive_pump_count > plant_config.max_consecutive_pump_count.into() {
state.not_effective = true;
board.fault(plant, true);
}
let plant_config = &config.plants[plant];
println!(
"Trying to pump for {}s with pump {} now",
plant_config.pump_time_s, plant
@@ -454,8 +458,8 @@ fn safe_main() -> anyhow::Result<()> {
light_state.is_day = is_day;
light_state.out_of_work_hour = !in_time_range(
&timezone_time,
config.night_lamp_hour_start,
config.night_lamp_hour_end,
config.night_lamp.night_lamp_hour_start,
config.night_lamp.night_lamp_hour_end,
);
let state_of_charge = board.state_charge_percent().unwrap_or(0);
@@ -467,7 +471,7 @@ fn safe_main() -> anyhow::Result<()> {
light_state.battery_low = board.low_voltage_in_cycle();
if !light_state.out_of_work_hour {
if config.night_lamp_only_when_dark {
if config.night_lamp.night_lamp_only_when_dark {
if !light_state.is_day {
if light_state.battery_low {
board.light(false).unwrap();
@@ -537,7 +541,7 @@ fn safe_main() -> anyhow::Result<()> {
fn publish_battery_state(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
config: &Config,
config: &PlantControllerConfig,
) {
let bat = BatteryState {
voltage_milli_volt: &to_string(&board.voltage_milli_volt()),
@@ -560,9 +564,9 @@ fn publish_battery_state(
fn determine_tank_state(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
config: &Config,
config: &PlantControllerConfig,
) -> TankState {
if config.tank_sensor_enabled {
if config.tank.tank_sensor_enabled {
let mut rv: TankState = TankState {
..Default::default()
};
@@ -572,30 +576,30 @@ fn determine_tank_state(
rv.raw = raw;
return map_range(
(
config.tank_empty_percent as f32,
config.tank_full_percent as f32,
config.tank.tank_empty_percent as f32,
config.tank.tank_full_percent as f32,
),
raw as f32,
);
})
.and_then(|percent| {
rv.left_ml = ((percent * config.tank_useable_ml as f32) / 100_f32) as u32;
rv.left_ml = ((percent * config.tank.tank_useable_ml as f32) / 100_f32) as u32;
println!(
"Tank sensor returned mv {} as {}% leaving {} ml useable",
rv.raw, percent as u8, rv.left_ml
);
if config.tank_warn_percent > percent as u8 {
if config.tank.tank_warn_percent > percent as u8 {
board.general_fault(true);
println!(
"Low water, current percent is {}, minimum warn level is {}",
percent as u8, config.tank_warn_percent
percent as u8, config.tank.tank_warn_percent
);
rv.warn_level = true;
}
if config.tank_empty_percent < percent as u8 {
if config.tank.tank_empty_percent < percent as u8 {
println!(
"Enough water, current percent is {}, minimum empty level is {}",
percent as u8, config.tank_empty_percent
percent as u8, config.tank.tank_empty_percent
);
rv.enough_water = true;
}
@@ -624,7 +628,7 @@ fn determine_state_target_moisture_for_plant(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
plant: usize,
state: &mut PlantState,
config: &Config,
config: &PlantControllerConfig,
tank_state: &TankState,
cur: DateTime<Tz>,
) {
@@ -671,7 +675,7 @@ fn determine_state_target_moisture_for_plant(
if a_low || b_low {
state.dry = true;
if tank_state.sensor_error && !config.tank_allow_pumping_if_sensor_error {
if tank_state.sensor_error && !config.tank.tank_allow_pumping_if_sensor_error {
//ignore is ok
} else if !tank_state.enough_water {
state.no_water = true;
@@ -714,7 +718,7 @@ fn determine_state_timer_only_for_plant(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
plant: usize,
state: &mut PlantState,
config: &Config,
config: &PlantControllerConfig,
tank_state: &TankState,
cur: DateTime<Tz>,
) {
@@ -730,7 +734,7 @@ fn determine_state_timer_only_for_plant(
state.next_pump = Some(europe_time);
state.cooldown = true;
} else {
if tank_state.sensor_error && !config.tank_allow_pumping_if_sensor_error {
if tank_state.sensor_error && !config.tank.tank_allow_pumping_if_sensor_error {
state.do_water = true;
} else if !tank_state.enough_water {
state.no_water = true;
@@ -752,7 +756,7 @@ fn determine_state_timer_and_deadzone_for_plant(
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
plant: usize,
state: &mut PlantState,
config: &Config,
config: &PlantControllerConfig,
tank_state: &TankState,
cur: DateTime<Tz>,
) {
@@ -776,7 +780,7 @@ fn determine_state_timer_and_deadzone_for_plant(
state.out_of_work_hour = true;
}
if !state.cooldown && !state.out_of_work_hour {
if tank_state.sensor_error && !config.tank_allow_pumping_if_sensor_error {
if tank_state.sensor_error && !config.tank.tank_allow_pumping_if_sensor_error {
state.do_water = true;
} else if !tank_state.enough_water {
state.no_water = true;
@@ -799,7 +803,7 @@ fn determine_next_plant(
cur: DateTime<Tz>,
tank_state: &TankState,
water_frozen: bool,
config: &Config,
config: &PlantControllerConfig,
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
) -> Option<usize> {
for plant in 0..PLANT_COUNT {
@@ -850,7 +854,7 @@ fn determine_next_plant(
fn update_plant_state(
plantstate: &mut [PlantState; PLANT_COUNT],
board: &mut std::sync::MutexGuard<'_, PlantCtrlBoard<'_>>,
config: &Config,
config: &PlantControllerConfig,
) {
for plant in 0..PLANT_COUNT {
let state = &plantstate[plant];