diff --git a/rust/.cargo/config.toml b/rust/.cargo/config.toml index 6a35545..bfe791a 100644 --- a/rust/.cargo/config.toml +++ b/rust/.cargo/config.toml @@ -15,3 +15,4 @@ MCU="esp32" # Note: this variable is not used by the pio builder (`cargo build --features pio`) ESP_IDF_VERSION = "v5.1.1" CHRONO_TZ_TIMEZONE_FILTER="UTC|Europe/Berlin" +CARGO_WORKSPACE_DIR = { value = "", relative = true } \ No newline at end of file diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 934993a..be479b7 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -76,6 +76,7 @@ schemars = "0.8.16" heapless = { version = "0.7", features = ["serde"] } serde_json = "1.0.108" strum = { version = "0.25.0", features = ["derive"] } +once_cell = "1.19.0" #?bq34z100 required [build-dependencies] diff --git a/rust/src/main.rs b/rust/src/main.rs index 6aaa15f..2297ef5 100644 --- a/rust/src/main.rs +++ b/rust/src/main.rs @@ -1,14 +1,13 @@ use std::{sync::{Arc, Mutex, atomic::AtomicBool}, env}; use chrono::{Datelike, NaiveDateTime, Timelike}; - +use once_cell::sync::Lazy; use anyhow::Result; use chrono_tz::Europe::Berlin; use esp_idf_hal::delay::Delay; use esp_idf_sys::{esp_restart, vTaskDelay}; use plant_hal::{CreatePlantHal, PlantCtrlBoard, PlantCtrlBoardInteraction, PlantHal, PLANT_COUNT}; - use crate::{config::{Config, WifiConfig}, webserver::webserver::{httpd_initial, httpd}}; mod config; pub mod plant_hal; @@ -64,8 +63,8 @@ fn wait_infinity(wait_type:WaitType, reboot_now:Arc) -> !{ } } -static BOARD_ACCESS: Lazy = Lazy::new(|| { - PlantHal::create()?; +pub static BOARD_ACCESS: Lazy> = Lazy::new(|| { + PlantHal::create().unwrap() }); fn main() -> Result<()> { @@ -143,7 +142,7 @@ fn main() -> Result<()> { //config upload will trigger reboot! drop(board); let reboot_now = Arc::new(AtomicBool::new(false)); - let _webserver = httpd_initial(BOARD_ACCESS.clone(), reboot_now.clone()); + let _webserver = httpd_initial(reboot_now.clone()); wait_infinity(WaitType::InitialConfig, reboot_now.clone()); }, }; @@ -224,8 +223,8 @@ fn main() -> Result<()> { //config upload will trigger reboot! drop(board); let reboot_now = Arc::new(AtomicBool::new(false)); - let _webserver = httpd(BOARD_ACCESS.clone(),reboot_now.clone()); - wait_infinity(BOARD_ACCESS.clone(), WaitType::NormalConfig, reboot_now.clone()); + let _webserver = httpd(reboot_now.clone()); + wait_infinity(WaitType::NormalConfig, reboot_now.clone()); }, } diff --git a/rust/src/plant_hal.rs b/rust/src/plant_hal.rs index a6d84d7..25001d5 100644 --- a/rust/src/plant_hal.rs +++ b/rust/src/plant_hal.rs @@ -140,7 +140,7 @@ pub trait PlantCtrlBoardInteraction { } pub trait CreatePlantHal<'a> { - fn create() -> Result>>>; + fn create() -> Result>>; } pub struct PlantHal {} @@ -557,12 +557,10 @@ impl PlantCtrlBoardInteraction for PlantCtrlBoard<'_> { if wifi_config.exists() { println!("Removing wifi config"); std::fs::remove_file(wifi_config)?; - Ok(ClearConfigType::WifiConfig); + return Ok(ClearConfigType::WifiConfig); } - Ok((ClearConfigType::None)); - - + return Ok((ClearConfigType::None)); } fn get_wifi(&mut self) -> Result { diff --git a/rust/src/webserver/webserver.rs b/rust/src/webserver/webserver.rs index 5690ca0..93fbe57 100644 --- a/rust/src/webserver/webserver.rs +++ b/rust/src/webserver/webserver.rs @@ -7,6 +7,7 @@ use esp_idf_svc::http::server::EspHttpServer; use esp_ota::OtaUpdate; use heapless::String; use serde::Serialize; +use crate::BOARD_ACCESS; use crate::{plant_hal::{PlantCtrlBoard, PlantCtrlBoardInteraction, PLANT_COUNT}, config::{WifiConfig, Config, Plant}}; @@ -16,7 +17,7 @@ struct SSIDList<'a> { ssids: Vec<&'a String<32>> } -pub fn httpd_initial(board_access:Arc>>, reboot_now: Arc) -> Box> { +pub fn httpd_initial(reboot_now: Arc) -> Box> { let mut server = shared(); server.fn_handler("/",Method::Get, move |request| { let mut response = request.into_ok_response()?; @@ -24,10 +25,9 @@ pub fn httpd_initial(board_access:Arc>>, reboot_no return Ok(()) }).unwrap(); - let board_access_for_scan = board_access.clone(); server.fn_handler("/wifiscan",Method::Post, move |request| { let mut response = request.into_ok_response()?; - let mut board = board_access_for_scan.lock().unwrap(); + let mut board = BOARD_ACCESS.lock().unwrap(); match board.wifi_scan() { Err(error) => { response.write(format!("Error scanning wifi: {}", error).as_bytes())?; @@ -46,7 +46,6 @@ pub fn httpd_initial(board_access:Arc>>, reboot_no }).unwrap(); - let board_access_for_save = board_access.clone(); server.fn_handler("/wifisave",Method::Post, move |mut request| { let mut buf = [0_u8;2048]; @@ -67,7 +66,7 @@ pub fn httpd_initial(board_access:Arc>>, reboot_no request.into_status_response(500)?.write(error_text.as_bytes())?; return Ok(()); } - let mut board = board_access_for_save.lock().unwrap(); + let mut board = BOARD_ACCESS.lock().unwrap(); board.set_wifi(&wifi_config.unwrap())?; let mut response = request.into_status_response(202)?; response.write("saved".as_bytes())?; @@ -75,9 +74,8 @@ pub fn httpd_initial(board_access:Arc>>, reboot_no return Ok(()) }).unwrap(); - let board_access_for_test= board_access.clone(); server.fn_handler("/boardtest",Method::Post, move |request| { - let mut board = board_access_for_test.lock().unwrap(); + let mut board = BOARD_ACCESS.lock().unwrap(); board.test(); return Ok(()) }).unwrap(); @@ -85,7 +83,7 @@ pub fn httpd_initial(board_access:Arc>>, reboot_no return server } -pub fn httpd(board_access:Arc>>, reboot_now: Arc) -> Box> { +pub fn httpd(reboot_now: Arc) -> Box> { let mut server = shared(); server @@ -95,12 +93,11 @@ pub fn httpd(board_access:Arc>>, reboot_now: Arc { let config_json = serde_json::to_string(&config)?; response.write(config_json.as_bytes())?; @@ -113,7 +110,6 @@ pub fn httpd(board_access:Arc>>, reboot_now: Arc>>, reboot_now: Arc