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

@@ -1,17 +1,21 @@
//offer ota and config mode
use crate::config::PlantControllerConfig;
use crate::{get_version, log::LogMessage, BOARD_ACCESS};
use alloc::borrow::ToOwned;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use anyhow::bail;
use core::fmt::{Debug, Display};
use core::net::{IpAddr, Ipv4Addr, SocketAddr};
use core::result::Result::Ok;
use core::str::from_utf8;
use core::sync::atomic::{AtomicBool, Ordering};
use edge_http::io::server::{Connection, Handler, Server};
use edge_http::io::Error;
use edge_http::Method;
use edge_nal::TcpBind;
use edge_nal::{TcpBind, TcpSplit};
use edge_nal_embassy::{Tcp, TcpBuffers};
use embassy_net::Stack;
use embassy_time::Instant;
@@ -185,16 +189,7 @@ pub struct NightLampCommand {
// anyhow::Ok(Some(json))
// }
//
// fn set_config(
// request: &mut Request<&mut EspHttpConnection>,
// ) -> Result<Option<std::string::String>, anyhow::Error> {
// let all = read_up_to_bytes_from_request(request, Some(4096))?;
// let config: PlantControllerConfig = serde_json::from_slice(&all)?;
//
// let mut board = BOARD_ACCESS.lock().expect("board access");
// board.board_hal.set_config(config)?;
// anyhow::Ok(Some("saved".to_owned()))
// }
//
//
@@ -314,11 +309,7 @@ struct HttpHandler {
}
impl Handler for HttpHandler {
type Error<E>
= Error<E>
where
E: Debug;
type Error<E: core::fmt::Debug> = Error<E>;
async fn handle<'a, T, const N: usize>(
&self,
_task_id: impl Display + Copy,
@@ -381,6 +372,7 @@ impl Handler for HttpHandler {
Method::Post => {
let json = match path {
"/wifiscan" => Some(wifi_scan(conn).await),
"/set_config" => Some(set_config(conn).await),
_ => None,
};
match json {
@@ -398,7 +390,6 @@ impl Handler for HttpHandler {
}
Some(code) => code,
};
conn.complete().await?;
let response_time = Instant::now().duration_since(start).as_millis();
@@ -407,28 +398,65 @@ impl Handler for HttpHandler {
}
}
// .fn_handler("/reboot", Method::Post, move |_| {
// BOARD_ACCESS
// .lock()
// .unwrap()
// .board_hal
// .get_esp()
// .set_restart_to_conf(true);
// reboot_now_for_reboot.store(true, std::sync::atomic::Ordering::Relaxed);
// anyhow::Ok(())
// fn wifi_scan(
// _request: &mut Request<&mut EspHttpConnection>,
// fn set_config(
// request: &mut Request<&mut EspHttpConnection>,
// ) -> Result<Option<std::string::String>, anyhow::Error> {
// let mut board = BOARD_ACCESS.lock().unwrap();
// let scan_result = board.board_hal.get_esp().wifi_scan()?;
// let mut ssids: Vec<&String<32>> = Vec::new();
// scan_result.iter().for_each(|s| ssids.push(&s.ssid));
// let ssid_json = serde_json::to_string(&SSIDList { ssids })?;
// log::info!("Sending ssid list {}", &ssid_json);
// anyhow::Ok(Some(ssid_json))
// let all = read_up_to_bytes_from_request(request, Some(4096))?;
// let config: PlantControllerConfig = serde_json::from_slice(&all)?;
//
// let mut board = BOARD_ACCESS.lock().expect("board access");
// board.board_hal.set_config(config)?;
// anyhow::Ok(Some("saved".to_owned()))
// }
async fn set_config<T, const N: usize>(
request: &mut Connection<'_, T, N>,
) -> Result<Option<String>, anyhow::Error>
where
T: Read + Write,
{
let all = read_up_to_bytes_from_request(request, Some(4096)).await?;
let length = all.len();
let config: PlantControllerConfig = serde_json::from_slice(&all)?;
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.get_esp().save_config(all).await?;
log::info!("Wrote config config {:?} with size {}", config, length);
board.board_hal.set_config(config);
anyhow::Ok(Some("saved".to_string()))
}
async fn read_up_to_bytes_from_request<T, const N: usize>(
request: &mut Connection<'_, T, N>,
limit: Option<usize>,
) -> Result<Vec<u8>, anyhow::Error>
where
T: Read + Write,
{
let max_read = limit.unwrap_or(1024);
let mut data_store = Vec::new();
let mut total_read = 0;
loop {
let mut buf = [0_u8; 64];
let read = match request.read(&mut buf).await {
Ok(read) => read,
Err(e) => bail!("Error reading request {:?}", e),
};
if read == 0 {
break;
}
let actual_data = &buf[0..read];
total_read += read;
if total_read > max_read {
bail!("Request too large {total_read} > {max_read}");
}
data_store.push(actual_data.to_owned());
}
let allvec = data_store.concat();
log::info!("Raw data {}", from_utf8(&allvec)?);
Ok(allvec)
}
async fn wifi_scan<T, const N: usize>(
_request: &mut Connection<'_, T, N>,
) -> Result<Option<String>, anyhow::Error> {
@@ -463,7 +491,7 @@ async fn list_files<T, const N: usize>(
_request: &mut Connection<'_, T, N>,
) -> Result<Option<String>, anyhow::Error> {
let mut board = BOARD_ACCESS.get().await.lock().await;
let result = board.board_hal.get_esp().list_files().await;
let result = board.board_hal.get_esp().list_files().await?;
let file_list_json = serde_json::to_string(&result)?;
anyhow::Ok(Some(file_list_json))
}
@@ -537,7 +565,7 @@ pub async fn httpd(reboot_now: Arc<AtomicBool>, stack: Stack<'static>) {
.await
.unwrap();
let mut server: Server<2, 512, 10> = Server::new();
let mut server: Server<2, 512, 15> = Server::new();
server
.run(Some(5000), acceptor, HttpHandler { reboot_now })
.await
@@ -637,11 +665,6 @@ pub async fn httpd(reboot_now: Arc<AtomicBool>, stack: Stack<'static>) {
// .unwrap();
//
// server
// .fn_handler("/set_config", Method::Post, move |request| {
// handle_error_to500(request, set_config)
// })
// .unwrap();
// server
// .fn_handler("/backup_config", Method::Post, move |request| {
// handle_error_to500(request, backup_config)
// })
@@ -817,30 +840,6 @@ pub async fn httpd(reboot_now: Arc<AtomicBool>, stack: Stack<'static>) {
//server
}
//
// fn read_up_to_bytes_from_request(
// request: &mut Request<&mut EspHttpConnection<'_>>,
// limit: Option<usize>,
// ) -> Result<Vec<u8>, anyhow::Error> {
// let max_read = limit.unwrap_or(1024);
// let mut data_store = Vec::new();
// let mut total_read = 0;
// loop {
// let mut buf = [0_u8; 64];
// let read = request.read(&mut buf)?;
// if read == 0 {
// break;
// }
// let actual_data = &buf[0..read];
// total_read += read;
// if total_read > max_read {
// bail!("Request too large {total_read} > {max_read}");
// }
// data_store.push(actual_data.to_owned());
// }
// let allvec = data_store.concat();
// log::info!("Raw data {}", from_utf8(&allvec)?);
// Ok(allvec)
// }
async fn handle_json<'a, T, const N: usize>(
conn: &mut Connection<'a, T, N>,