bring selftest online, bring tz online, fix set_config/get_config race
This commit is contained in:
223
rust/src/main.rs
223
rust/src/main.rs
@@ -17,7 +17,7 @@ use crate::hal::{esp_time, TIME_ACCESS};
|
||||
use crate::log::LOG_ACCESS;
|
||||
use crate::tank::{determine_tank_state, TankError, WATER_FROZEN_THRESH};
|
||||
use crate::webserver::httpd;
|
||||
use crate::FatError::FatResult;
|
||||
use crate::fat_error::FatResult;
|
||||
use crate::{
|
||||
config::BoardVersion::INITIAL,
|
||||
hal::{PlantHal, HAL, PLANT_COUNT},
|
||||
@@ -28,9 +28,10 @@ use alloc::string::{String, ToString};
|
||||
use alloc::sync::Arc;
|
||||
use alloc::{format, vec};
|
||||
use chrono::{DateTime, Datelike, Timelike, Utc};
|
||||
use chrono_tz::Tz::{self};
|
||||
use chrono_tz::Tz::{self, UTC};
|
||||
use core::sync::atomic::{AtomicBool, Ordering};
|
||||
use embassy_executor::Spawner;
|
||||
use embassy_net::Stack;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::mutex::{Mutex, MutexGuard};
|
||||
use embassy_sync::once_lock::OnceLock;
|
||||
@@ -57,7 +58,7 @@ extern "C" fn custom_halt() -> ! {
|
||||
}
|
||||
|
||||
//use tank::*;
|
||||
mod FatError;
|
||||
mod fat_error;
|
||||
mod config;
|
||||
mod hal;
|
||||
mod log;
|
||||
@@ -185,10 +186,8 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
.log(LogMessage::YearInplausibleForceConfig, 0, 0, "", "")
|
||||
.await;
|
||||
}
|
||||
|
||||
info!("cur is {}", cur);
|
||||
update_charge_indicator(&mut board).await;
|
||||
println!("faul led3");
|
||||
if board.board_hal.get_esp().get_restart_to_conf() {
|
||||
LOG_ACCESS
|
||||
.lock()
|
||||
@@ -228,10 +227,10 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
info!("no mode override");
|
||||
}
|
||||
|
||||
//TODO hack
|
||||
if true
|
||||
|| (board.board_hal.get_config().hardware.board == INITIAL
|
||||
&& board.board_hal.get_config().network.ssid.is_none())
|
||||
|
||||
|
||||
if (board.board_hal.get_config().hardware.board == INITIAL
|
||||
&& board.board_hal.get_config().network.ssid.is_none())
|
||||
{
|
||||
info!("No wifi configured, starting initial config mode");
|
||||
|
||||
@@ -241,13 +240,14 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
println!("starting webserver");
|
||||
|
||||
spawner.spawn(httpd(reboot_now.clone(), stack))?;
|
||||
|
||||
wait_infinity(board, WaitType::MissingConfig, reboot_now.clone()).await;
|
||||
}
|
||||
|
||||
info!("attempting to connect wifi");
|
||||
info!("attempting to connect wifi ");
|
||||
|
||||
let mut stack = Option::None;
|
||||
let network_mode = if board.board_hal.get_config().network.ssid.is_some() {
|
||||
try_connect_wifi_sntp_mqtt().await
|
||||
try_connect_wifi_sntp_mqtt(&mut board, *&mut stack).await
|
||||
} else {
|
||||
info!("No wifi configured");
|
||||
//the current sensors require this amount to stabilize, in case of wifi this is already handles for sure;
|
||||
@@ -255,34 +255,38 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
NetworkMode::OFFLINE
|
||||
};
|
||||
|
||||
|
||||
if matches!(network_mode, NetworkMode::OFFLINE) && to_config {
|
||||
info!("Could not connect to station and config mode forced, switching to ap mode!");
|
||||
|
||||
let res = {
|
||||
let esp = board.board_hal.get_esp();
|
||||
esp.wifi_ap().await
|
||||
|
||||
};
|
||||
match res {
|
||||
Ok(_) => {
|
||||
Ok(ap_stack) => {
|
||||
stack = Some(ap_stack);
|
||||
info!("Started ap, continuing")
|
||||
}
|
||||
Err(err) => info!("Could not start config override ap mode due to {}", err),
|
||||
}
|
||||
}
|
||||
|
||||
// let timezone = match &board.board_hal.get_config().timezone {
|
||||
// Some(tz_str) => tz_str.parse::<Tz>().unwrap_or_else(|_| {
|
||||
// info!("Invalid timezone '{}', falling back to UTC", tz_str);
|
||||
// UTC
|
||||
// }),
|
||||
// None => UTC, // Fallback to UTC if no timezone is set
|
||||
// };
|
||||
let tz = & board.board_hal.get_config().timezone;
|
||||
let timezone = match tz {
|
||||
Some(tz_str) => tz_str.parse::<Tz>().unwrap_or_else(|_| {
|
||||
info!("Invalid timezone '{}', falling back to UTC", tz_str);
|
||||
UTC
|
||||
}),
|
||||
None => UTC, // Fallback to UTC if no timezone is set
|
||||
};
|
||||
let _timezone = Tz::UTC;
|
||||
|
||||
let timezone_time = cur; //TODO.with_timezone(&timezone);
|
||||
let timezone_time = cur.with_timezone(&timezone);
|
||||
info!(
|
||||
"Running logic at utc {} and {} {}",
|
||||
cur, "todo timezone.name()", timezone_time
|
||||
cur, timezone.name(), timezone_time
|
||||
);
|
||||
|
||||
if let NetworkMode::WIFI { ref ip_address, .. } = network_mode {
|
||||
@@ -316,8 +320,7 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
info!("executing config mode override");
|
||||
//config upload will trigger reboot!
|
||||
let reboot_now = Arc::new(AtomicBool::new(false));
|
||||
//spawner.spawn(httpd(reboot_now.clone(), stack))?;
|
||||
let board = BOARD_ACCESS.get().await.lock().await;
|
||||
spawner.spawn(httpd(reboot_now.clone(), stack.unwrap()))?;
|
||||
wait_infinity(board, WaitType::ConfigButton, reboot_now.clone()).await;
|
||||
} else {
|
||||
LOG_ACCESS
|
||||
@@ -496,71 +499,71 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
.await
|
||||
.unwrap_or(BatteryState::Unknown);
|
||||
|
||||
let light_state = LightState {
|
||||
let mut light_state = LightState {
|
||||
enabled: board.board_hal.get_config().night_lamp.enabled,
|
||||
..Default::default()
|
||||
};
|
||||
// if light_state.enabled {
|
||||
// light_state.is_day = is_day;
|
||||
// light_state.out_of_work_hour = !in_time_range(
|
||||
// &timezone_time,
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_config()
|
||||
// .night_lamp
|
||||
// .night_lamp_hour_start,
|
||||
// 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
|
||||
// .into()
|
||||
// {
|
||||
// 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
|
||||
// .into()
|
||||
// {
|
||||
// board.board_hal.get_esp().clear_low_voltage_in_cycle();
|
||||
// }
|
||||
// light_state.battery_low = board.board_hal.get_esp().low_voltage_in_cycle();
|
||||
//
|
||||
// if !light_state.out_of_work_hour {
|
||||
// if board
|
||||
// .board_hal
|
||||
// .get_config()
|
||||
// .night_lamp
|
||||
// .night_lamp_only_when_dark
|
||||
// {
|
||||
// if !light_state.is_day {
|
||||
// if light_state.battery_low {
|
||||
// board.board_hal.light(false)?;
|
||||
// } else {
|
||||
// light_state.active = true;
|
||||
// board.board_hal.light(true)?;
|
||||
// }
|
||||
// }
|
||||
// } else if light_state.battery_low {
|
||||
// board.board_hal.light(false)?;
|
||||
// } else {
|
||||
// light_state.active = true;
|
||||
// board.board_hal.light(true)?;
|
||||
// }
|
||||
// } else {
|
||||
// light_state.active = false;
|
||||
// board.board_hal.light(false)?;
|
||||
// }
|
||||
//
|
||||
// info!("Lightstate is {:?}", light_state);
|
||||
// }
|
||||
if light_state.enabled {
|
||||
light_state.is_day = is_day;
|
||||
light_state.out_of_work_hour = !in_time_range(
|
||||
&timezone_time,
|
||||
board
|
||||
.board_hal
|
||||
.get_config()
|
||||
.night_lamp
|
||||
.night_lamp_hour_start,
|
||||
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
|
||||
.into()
|
||||
{
|
||||
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
|
||||
.into()
|
||||
{
|
||||
board.board_hal.get_esp().clear_low_voltage_in_cycle();
|
||||
}
|
||||
light_state.battery_low = board.board_hal.get_esp().low_voltage_in_cycle();
|
||||
|
||||
if !light_state.out_of_work_hour {
|
||||
if board
|
||||
.board_hal
|
||||
.get_config()
|
||||
.night_lamp
|
||||
.night_lamp_only_when_dark
|
||||
{
|
||||
if !light_state.is_day {
|
||||
if light_state.battery_low {
|
||||
board.board_hal.light(false).await?;
|
||||
} else {
|
||||
light_state.active = true;
|
||||
board.board_hal.light(true).await?;
|
||||
}
|
||||
}
|
||||
} else if light_state.battery_low {
|
||||
board.board_hal.light(false).await?;
|
||||
} else {
|
||||
light_state.active = true;
|
||||
board.board_hal.light(true).await?;
|
||||
}
|
||||
} else {
|
||||
light_state.active = false;
|
||||
board.board_hal.light(false).await?;
|
||||
}
|
||||
|
||||
info!("Lightstate is {:?}", light_state);
|
||||
}
|
||||
|
||||
match serde_json::to_string(&light_state) {
|
||||
Ok(state) => {
|
||||
@@ -629,7 +632,9 @@ async fn safe_main(spawner: Spawner) -> FatResult<()> {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub async fn do_secure_pump(
|
||||
board: &mut MutexGuard<'_, CriticalSectionRawMutex, HAL<'_>>,
|
||||
plant_id: usize,
|
||||
plant_config: &PlantConfig,
|
||||
dry_run: bool,
|
||||
@@ -639,26 +644,22 @@ pub async fn do_secure_pump(
|
||||
let mut error = false;
|
||||
let mut first_error = true;
|
||||
let mut pump_time_s = 0;
|
||||
let board = &mut BOARD_ACCESS.get().await.lock().await;
|
||||
if !dry_run {
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_tank_sensor()
|
||||
// .unwrap()
|
||||
// .reset_flow_meter();
|
||||
// board
|
||||
// .board_hal
|
||||
// .get_tank_sensor()
|
||||
// .unwrap()
|
||||
// .start_flow_meter();
|
||||
board
|
||||
.board_hal
|
||||
.get_tank_sensor()?
|
||||
.reset_flow_meter();
|
||||
board
|
||||
.board_hal
|
||||
.get_tank_sensor()?
|
||||
.start_flow_meter();
|
||||
board.board_hal.pump(plant_id, true).await?;
|
||||
Timer::after_millis(10).await;
|
||||
for step in 0..plant_config.pump_time_s as usize {
|
||||
// let flow_value = board
|
||||
// .board_hal
|
||||
// .get_tank_sensor()
|
||||
// .unwrap()
|
||||
// .get_flow_meter_value();
|
||||
let flow_value = board
|
||||
.board_hal
|
||||
.get_tank_sensor()?
|
||||
.get_flow_meter_value();
|
||||
let flow_value = 1;
|
||||
flow_collector[step] = flow_value;
|
||||
let flow_value_ml = flow_value as f32 * board.board_hal.get_config().tank.ml_per_pulse;
|
||||
@@ -749,12 +750,11 @@ pub async fn do_secure_pump(
|
||||
pump_time_s += 1;
|
||||
}
|
||||
}
|
||||
// board.board_hal.get_tank_sensor().unwrap().stop_flow_meter();
|
||||
// let final_flow_value = board
|
||||
// .board_hal
|
||||
// .get_tank_sensor()
|
||||
// .unwrap()
|
||||
// .get_flow_meter_value();
|
||||
board.board_hal.get_tank_sensor().unwrap().stop_flow_meter();
|
||||
let final_flow_value = board
|
||||
.board_hal
|
||||
.get_tank_sensor()?
|
||||
.get_flow_meter_value();
|
||||
let final_flow_value = 12;
|
||||
let flow_value_ml = final_flow_value as f32 * board.board_hal.get_config().tank.ml_per_pulse;
|
||||
info!(
|
||||
@@ -898,8 +898,7 @@ async fn publish_firmware_info(
|
||||
let _ = esp.mqtt_publish("/state", "online".as_bytes()).await;
|
||||
}
|
||||
|
||||
async fn try_connect_wifi_sntp_mqtt() -> NetworkMode {
|
||||
let board = &mut BOARD_ACCESS.get().await.lock().await;
|
||||
async fn try_connect_wifi_sntp_mqtt(board: &mut MutexGuard<'static, CriticalSectionRawMutex, HAL<'static>>, stack_store:Option<Stack<'_>>) -> NetworkMode {
|
||||
let nw_conf = &board.board_hal.get_config().network.clone();
|
||||
match board.board_hal.get_esp().wifi(nw_conf).await {
|
||||
Ok(ip_info) => {
|
||||
@@ -936,8 +935,8 @@ async fn try_connect_wifi_sntp_mqtt() -> NetworkMode {
|
||||
ip_address: ip_info.ip.to_string(),
|
||||
}
|
||||
}
|
||||
Err(_) => {
|
||||
info!("Offline mode");
|
||||
Err(err) => {
|
||||
info!("Offline mode due to {}", err);
|
||||
board.board_hal.general_fault(true).await;
|
||||
NetworkMode::OFFLINE
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user