fix mppt to not self destruct, log output

This commit is contained in:
2025-02-19 22:01:26 +01:00
parent aad1dbd458
commit 936c52e0e7
4 changed files with 218 additions and 241 deletions

View File

@@ -1,4 +1,5 @@
use std::{collections::HashMap, sync::Mutex};
use serde::Serialize;
use strum_macros::IntoStaticStr;
use esp_idf_svc::systime::EspSystemTime;
@@ -17,6 +18,7 @@ static mut BUFFER:ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE> = ConstGeneric
static BUFFER_ACCESS: Lazy<Mutex<&mut ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>>> = Lazy::new(|| unsafe { Mutex::new(&mut BUFFER) });
#[derive(Serialize, Debug, Clone)]
pub struct LogEntry {
pub timestamp: u64,
pub message_id: u32,
@@ -52,6 +54,17 @@ fn limit_length <const LIMIT:usize> (input: &str, target: &mut heapless::String<
}
}
pub fn get_log() -> Vec<LogEntry>{
let buffer = BUFFER_ACCESS.lock().unwrap();
let mut read_copy = Vec::new();
for entry in buffer.iter() {
let copy = entry.clone();
read_copy.push(copy);
}
drop(buffer);
return read_copy;
}
pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short:&str, txt_long:&str){
let mut txt_short_stack:heapless::String<TXT_SHORT_LENGTH> = heapless::String::new();
let mut txt_long_stack:heapless::String<TXT_LONG_LENGTH> = heapless::String::new();
@@ -94,6 +107,8 @@ pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short:&str
}
#[cfg(test)]
mod tests {
use super::*;

View File

@@ -207,6 +207,13 @@ fn get_battery_state(
anyhow::Ok(Some(battery_json))
}
fn get_log(
_request: &mut Request<&mut EspHttpConnection>,
) -> Result<Option<std::string::String>, anyhow::Error> {
let output = crate::log::get_log();
anyhow::Ok(Some(serde_json::to_string(&output)?))
}
fn get_version_web(
_request: &mut Request<&mut EspHttpConnection>,
) -> Result<Option<std::string::String>, anyhow::Error> {
@@ -354,6 +361,11 @@ pub fn httpd(reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
handle_error_to500(request, get_version_web)
})
.unwrap();
server
.fn_handler("/log", Method::Get, |request| {
handle_error_to500(request, get_log)
})
.unwrap();
server
.fn_handler("/battery", Method::Get, |request| {
handle_error_to500(request, get_battery_state)