remove boardaccess arc and replace with static lazy

This commit is contained in:
Empire 2023-12-27 20:00:06 +01:00
parent 4c02b99ea7
commit 5724088780
5 changed files with 20 additions and 25 deletions

View File

@ -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 }

View File

@ -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]

View File

@ -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<AtomicBool>) -> !{
}
}
static BOARD_ACCESS: Lazy<PlantCtrlBoard> = Lazy::new(|| {
PlantHal::create()?;
pub static BOARD_ACCESS: Lazy<Mutex<PlantCtrlBoard>> = 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());
},
}

View File

@ -140,7 +140,7 @@ pub trait PlantCtrlBoardInteraction {
}
pub trait CreatePlantHal<'a> {
fn create() -> Result<Arc<Mutex<PlantCtrlBoard<'static>>>>;
fn create() -> Result<Mutex<PlantCtrlBoard<'static>>>;
}
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<config::WifiConfig> {

View File

@ -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<Mutex<PlantCtrlBoard<'static>>>, reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
pub fn httpd_initial(reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
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<Mutex<PlantCtrlBoard<'static>>>, 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<Mutex<PlantCtrlBoard<'static>>>, 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<Mutex<PlantCtrlBoard<'static>>>, 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<Mutex<PlantCtrlBoard<'static>>>, 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<Mutex<PlantCtrlBoard<'static>>>, reboot_no
return server
}
pub fn httpd(board_access:Arc<Mutex<PlantCtrlBoard<'static>>>, reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
pub fn httpd(reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
let mut server = shared();
server
@ -95,12 +93,11 @@ pub fn httpd(board_access:Arc<Mutex<PlantCtrlBoard<'static>>>, reboot_now: Arc<A
return Ok(())
}).unwrap();
let board_access_for_get_config= board_access.clone();
server
.fn_handler("/get_config",Method::Get, move |request| {
let mut response = request.into_ok_response()?;
let mut board = board_access_for_get_config.lock()?;
match (board.get_config()) {
let mut board = BOARD_ACCESS.lock()?;
match board.get_config() {
Ok(config) => {
let config_json = serde_json::to_string(&config)?;
response.write(config_json.as_bytes())?;
@ -113,7 +110,6 @@ pub fn httpd(board_access:Arc<Mutex<PlantCtrlBoard<'static>>>, reboot_now: Arc<A
return Ok(())
}).unwrap();
let board_access_for_set_config= board_access.clone();
server.fn_handler("/set_config",Method::Post, move |mut request| {
let mut buf = [0_u8;2048];
let read = request.read(&mut buf);
@ -133,7 +129,7 @@ pub fn httpd(board_access:Arc<Mutex<PlantCtrlBoard<'static>>>, reboot_now: Arc<A
request.into_status_response(500)?.write(error_text.as_bytes())?;
return Ok(());
}
let mut board = board_access_for_set_config.lock().unwrap();
let mut board = BOARD_ACCESS.lock().unwrap();
board.set_config(&config.unwrap())?;
let mut response = request.into_status_response(202)?;
response.write("saved".as_bytes())?;