Merge pull request 'implement log localization config generation' (#1) from feature/log-config-json into develop

Reviewed-on: #1
This commit is contained in:
EmpirePhoenix 2025-02-26 19:35:28 +01:00
commit bd61359666
2 changed files with 24 additions and 4 deletions

View File

@ -1,5 +1,6 @@
use std::{collections::HashMap, sync::Mutex};
use std::{collections::{BTreeMap, HashMap}, sync::Mutex};
use serde::Serialize;
use strum::{EnumIter, IntoEnumIterator};
use strum_macros::IntoStaticStr;
use esp_idf_svc::systime::EspSystemTime;
@ -129,7 +130,7 @@ mod tests {
#[derive(IntoStaticStr)]
#[derive(IntoStaticStr, EnumIter, Serialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub enum LogMessage {
#[strum(serialize = "Reset due to {{txt_long}} requires rtc clear {{a}} and force config mode {{b}}")]
reset_reason,
@ -179,5 +180,15 @@ pub enum LogMessage {
enable_main,
#[strum(serialize = "Pumped multiple times, but plant is still to try attempt: {{a}} limit :: {{b}} plant: {{txt_short}}")]
consecutive_pump_count_limit
}
}
impl LogMessage {
pub fn to_log_localisation_config() -> BTreeMap<LogMessage,&'static str> {
let mut data = BTreeMap::new();
for msg_type in LogMessage::iter() {
let s: &'static str = msg_type.clone().into();
data.insert(msg_type, s);
}
data
}
}

View File

@ -5,7 +5,7 @@ use std::{
sync::{atomic::AtomicBool, Arc},
};
use crate::{
espota::OtaUpdate, get_version, map_range_moisture, plant_hal::{FileInfo, PLANT_COUNT}, BOARD_ACCESS,
espota::OtaUpdate, get_version, log::LogMessage, map_range_moisture, plant_hal::{FileInfo, PLANT_COUNT}, BOARD_ACCESS
};
use anyhow::bail;
use chrono::DateTime;
@ -214,6 +214,10 @@ fn get_log(
anyhow::Ok(Some(serde_json::to_string(&output)?))
}
fn get_log_localization_config() -> Result<std::string::String, anyhow::Error> {
anyhow::Ok(serde_json::to_string(&LogMessage::to_log_localisation_config())?)
}
fn get_version_web(
_request: &mut Request<&mut EspHttpConnection>,
) -> Result<Option<std::string::String>, anyhow::Error> {
@ -366,6 +370,11 @@ pub fn httpd(reboot_now: Arc<AtomicBool>) -> Box<EspHttpServer<'static>> {
handle_error_to500(request, get_log)
})
.unwrap();
server.fn_handler("/log_localization", Method::Get, |request| {
cors_response(request, 200, &get_log_localization_config().unwrap())
})
.unwrap();
server
.fn_handler("/battery", Method::Get, |request| {
handle_error_to500(request, get_battery_state)