get log to work, make time accessible

This commit is contained in:
2025-09-20 11:31:51 +02:00
parent 584d6df2d0
commit c94f5bdb45
6 changed files with 334 additions and 212 deletions

View File

@@ -3,29 +3,30 @@
use crate::config::PlantControllerConfig;
use crate::{get_version, log::LogMessage, BOARD_ACCESS};
use alloc::borrow::ToOwned;
use alloc::fmt::format;
use alloc::format;
use alloc::string::{String, ToString};
use alloc::sync::Arc;
use alloc::vec::Vec;
use anyhow::bail;
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 chrono::DateTime;
use edge_http::io::server::{Connection, Handler, Server};
use edge_http::io::Error;
use edge_http::Method;
use edge_nal::{TcpBind, TcpSplit};
use edge_nal::{TcpBind};
use edge_nal_embassy::{Tcp, TcpBuffers};
use embassy_net::Stack;
use embassy_time::Instant;
use embedded_io_async::{Read, Write};
use esp_println::println;
use littlefs2_core::Path;
use log::info;
use serde::{Deserialize, Serialize};
use crate::hal::{esp_set_time, esp_time};
use crate::log::{LOG_ACCESS};
#[derive(Serialize, Debug)]
struct SSIDList {
@@ -72,25 +73,6 @@ pub struct NightLampCommand {
active: bool,
}
//
// fn write_time(
// request: &mut Request<&mut EspHttpConnection>,
// ) -> Result<Option<std::string::String>, anyhow::Error> {
// let actual_data = read_up_to_bytes_from_request(request, None)?;
// let time: SetTime = serde_json::from_slice(&actual_data)?;
// let parsed = DateTime::parse_from_rfc3339(time.time).map_err(|err| anyhow::anyhow!(err))?;
// let mut board = BOARD_ACCESS.lock().expect("board access");
//
// let now = timeval {
// tv_sec: parsed.to_utc().timestamp(),
// tv_usec: 0,
// };
// unsafe { settimeofday(&now, core::ptr::null_mut()) };
// board
// .board_hal
// .get_rtc_module()
// .set_rtc_time(&parsed.to_utc())?;
// anyhow::Ok(None)
// }
//
//
@@ -430,12 +412,10 @@ impl Handler for HttpHandler {
conn.write_all(include_bytes!("bundle.js")).await?;
Some(200)
}
"/reboot" => {
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.get_esp().set_restart_to_conf(true);
self.reboot_now.store(true, Ordering::Relaxed);
"/log" => {
let buf = get_log(conn).await;
Some(200)
}
},
&_ => {
let json = match path {
"/version" => Some(get_version_web(conn).await),
@@ -445,7 +425,6 @@ impl Handler for HttpHandler {
"/get_config" => Some(get_config(conn).await),
"/files" => Some(list_files(conn).await),
"/log_localization" => Some(get_log_localization_config(conn).await),
"/log" => Some(get_log(conn).await),
"/wifiscan" => Some(wifi_scan(conn).await),
_ => None,
};
@@ -459,6 +438,13 @@ impl Handler for HttpHandler {
let json = match path {
"/wifiscan" => Some(wifi_scan(conn).await),
"/set_config" => Some(set_config(conn).await),
"/time" => Some(write_time(conn).await),
"/reboot" => {
let mut board = BOARD_ACCESS.get().await.lock().await;
board.board_hal.get_esp().set_restart_to_conf(true);
self.reboot_now.store(true, Ordering::Relaxed);
Some(Ok(None))
}
_ => None,
};
match json {
@@ -568,6 +554,20 @@ impl Handler for HttpHandler {
// })
// .unwrap();
async fn write_time<T, const N: usize>(
request: &mut Connection<'_, T, N>,
) -> Result<Option<String>, anyhow::Error>
where
T: Read + Write
{
let actual_data = read_up_to_bytes_from_request(request, None).await?;
let time: SetTime = serde_json::from_slice(&actual_data)?;
let parsed = DateTime::parse_from_rfc3339(time.time).unwrap();
esp_set_time(parsed).await;
anyhow::Ok(None)
}
async fn set_config<T, const N: usize>(
request: &mut Connection<'_, T, N>,
) -> Result<Option<String>, anyhow::Error>
@@ -633,10 +633,30 @@ async fn wifi_scan<T, const N: usize>(
}
async fn get_log<T, const N: usize>(
_request: &mut Connection<'_, T, N>,
) -> Result<Option<String>, anyhow::Error> {
let output = crate::log::get_log().await;
anyhow::Ok(Some(serde_json::to_string(&output)?))
conn: &mut Connection<'_, T, N>,
) -> anyhow::Result<()>
where
T: Read + Write,{
let log = LOG_ACCESS.lock().await.get();
conn.initiate_response(
200,
Some("OK"),
&[("Content-Type", "text/javascript")],
)
.await
.unwrap();
conn.write_all("[".as_bytes()).await.unwrap();
let mut append = false;
for entry in log {
if append {
conn.write_all(",".as_bytes()).await.unwrap();
}
append = true;
let json = serde_json::to_string(&entry)?;
conn.write_all(json.as_bytes()).await.unwrap();
}
conn.write_all("]".as_bytes()).await.unwrap();
Ok(())
}
async fn get_log_localization_config<T, const N: usize>(
@@ -698,16 +718,17 @@ async fn get_time<T, const N: usize>(
) -> Result<Option<String>, anyhow::Error> {
let mut board = BOARD_ACCESS.get().await.lock().await;
//TODO do not fail if rtc module is missing
let native = board.board_hal.get_esp().time().to_rfc3339();
let rtc = board
.board_hal
.get_rtc_module()
.get_rtc_time()
.await?
.to_rfc3339();
let native = esp_time().await.to_rfc3339();
let rtc = "todo";
// board
// .board_hal
// .get_rtc_module()
// .get_rtc_time()
// .await?
// .to_rfc3339();
let data = LoadData {
rtc: rtc.as_str(),
rtc,
native: native.as_str(),
};
let json = serde_json::to_string(&data)?;