set read config initial somewhat ready

This commit is contained in:
2025-09-17 03:50:21 +02:00
parent 4c54edbcea
commit cd63e76469
9 changed files with 164 additions and 156 deletions

View File

@@ -46,6 +46,8 @@ static mut LOW_VOLTAGE_DETECTED: bool = false;
#[link_section = ".rtc.data"]
static mut RESTART_TO_CONF: bool = false;
static CONFIG_FILE: &str = "config.json";
#[derive(Serialize, Debug)]
pub struct FileInfo {
filename: String,
@@ -87,6 +89,14 @@ pub struct Esp<'a> {
pub ota_next: &'static mut FlashRegion<'static, FlashStorage>,
}
// SAFETY: On this target we never move Esp across OS threads; the firmware runs single-core
// cooperative tasks with Embassy. All interior mutability of non-Send peripherals is gated
// behind &mut self or embassy_sync Mutex with CriticalSectionRawMutex, which does not rely on
// thread scheduling. Therefore it is sound to mark Esp as Send to satisfy trait object bounds
// (e.g., Box<dyn BoardInteraction + Send>). If you add fields that are accessed from multiple
// CPU cores/threads, reconsider this.
unsafe impl Send for Esp<'_> {}
pub struct IpInfo {
pub(crate) ip: IpAddr,
netmask: IpAddr,
@@ -224,7 +234,7 @@ impl Esp<'_> {
}
pub(crate) async fn wifi_ap(&mut self) -> anyhow::Result<Stack<'static>> {
let ssid = match self.load_config() {
let ssid = match self.load_config().await {
Ok(config) => config.network.ap_ssid.as_str().to_string(),
Err(_) => "PlantCtrl Emergency Mode".to_string(),
};
@@ -370,50 +380,29 @@ impl Esp<'_> {
// log(LogMessage::WifiInfo, 0, 0, "", &format!("{address:?}"));
// anyhow::Ok(address)
}
pub(crate) fn load_config(&mut self) -> anyhow::Result<PlantControllerConfig> {
bail!("todo");
// let cfg = File::open(Self::CONFIG_FILE)?;
// let config: PlantControllerConfig = serde_json::from_reader(cfg)?;
// anyhow::Ok(config)
pub(crate) async fn load_config(&mut self) -> anyhow::Result<PlantControllerConfig> {
let cfg = PathBuf::try_from(CONFIG_FILE).unwrap();
match self.fs.lock().await.read::<4096>(&cfg) {
Ok(data) => {
let config: PlantControllerConfig = serde_json::from_slice(&data)?;
anyhow::Ok(config)
}
Err(err) => {
bail!(format!("{err:?}"))
}
}
}
pub(crate) async fn save_config(
&mut self,
_config: &PlantControllerConfig,
) -> anyhow::Result<()> {
bail!("todo");
// let mut cfg = File::create(Self::CONFIG_FILE)?;
// serde_json::to_writer(&mut cfg, &config)?;
// log::info!("Wrote config config {:?}", config);
// anyhow::Ok(())
}
pub(crate) fn mount_file_system(&mut self) -> anyhow::Result<()> {
bail!("fail");
// log(LogMessage::MountingFilesystem, 0, 0, "", "");
// let base_path = String::try_from("/spiffs")?;
// let storage = String::try_from(Self::SPIFFS_PARTITION_NAME)?;
//let conf = todo!();
pub(crate) async fn save_config(&mut self, config: Vec<u8>) -> anyhow::Result<()> {
let filesystem = self.fs.lock().await;
let cfg = PathBuf::try_from(CONFIG_FILE).unwrap();
//let conf = esp_idf_sys::esp_vfs_spiffs_conf_t {
//base_path: base_path.as_ptr(),
//partition_label: storage.as_ptr(),
//max_files: 5,
//format_if_mount_failed: true,
//};
//TODO
//unsafe {
//esp_idf_sys::esp!(esp_idf_sys::esp_vfs_spiffs_register(&conf))?;
//}
// let free_space = self.file_system_size()?;
// log(
// LogMessage::FilesystemMount,
// free_space.free_size as u32,
// free_space.total_size as u32,
// &free_space.used_size.to_string(),
// "",
// );
// anyhow::Ok(())
match filesystem.write(&cfg, &*config) {
Ok(_) => {}
Err(err) => {
bail!(format!("{err:?}"))
}
}
anyhow::Ok(())
}
async fn file_system_size(&mut self) -> anyhow::Result<FileSystemSizeInfo> {
bail!("fail");