fix message templating

This commit is contained in:
Empire 2025-02-28 22:07:01 +01:00
parent 567b1b4540
commit 59d27ab0b8

View File

@ -7,11 +7,12 @@ use esp_idf_svc::systime::EspSystemTime;
use once_cell::sync::Lazy;
use ringbuffer::{ConstGenericRingBuffer, RingBuffer};
use text_template::Template;
use unit_enum::UnitEnum;
const TXT_SHORT_LENGTH:usize = 8;
const TXT_LONG_LENGTH:usize = 32;
const BUFFER_SIZE:usize = 210;
const BUFFER_SIZE:usize = 220;
#[link_section = ".rtc.data"]
static mut BUFFER:ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE> = ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>::new();
@ -22,7 +23,7 @@ static BUFFER_ACCESS: Lazy<Mutex<&mut ConstGenericRingBuffer::<LogEntry, BUFFER_
#[derive(Serialize, Debug, Clone)]
pub struct LogEntry {
pub timestamp: u64,
pub message_id: u32,
pub message_id: u16,
pub a: u32,
pub b: u32,
pub txt_short: heapless::String<TXT_SHORT_LENGTH>,
@ -75,6 +76,8 @@ pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short:&str
let time = EspSystemTime {}.now().as_millis() as u64;
let ordinal = message_key.ordinal() as u16;
let template_string:&str = message_key.into();
@ -91,11 +94,12 @@ pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short:&str
let serial_entry = template.fill_in(&values);
println!("{serial_entry}");
let entry = LogEntry{
timestamp: time,
message_id: 1,
message_id: ordinal,
a: number_a,
b: number_b,
txt_short: txt_short_stack,
@ -130,35 +134,35 @@ mod tests {
#[derive(IntoStaticStr, EnumIter, Serialize, PartialEq, Eq, PartialOrd, Ord, Clone)]
#[derive(IntoStaticStr, EnumIter, Serialize, PartialEq, Eq, PartialOrd, Ord, Clone, UnitEnum)]
pub enum LogMessage {
#[strum(serialize = "Reset due to {{txt_long}} requires rtc clear {{a}} and force config mode {{b}}")]
#[strum(serialize = "Reset due to ${txt_long} requires rtc clear ${number_a} and force config mode ${number_b}")]
ResetReason,
#[strum(serialize = "Current restart to conf mode {{a}}")]
#[strum(serialize = "Current restart to conf mode ${number_a}")]
RestartToConfig,
#[strum(serialize = "Current low voltage detection is {{a}}")]
#[strum(serialize = "Current low voltage detection is ${number_a}")]
LowVoltage,
#[strum(serialize = "Error communicating with battery!! {{txt_long}}")]
#[strum(serialize = "Error communicating with battery!! ${txt_long}")]
BatteryCommunicationError,
#[strum(serialize = "Tank sensor raw {{a}} percent {{b}}")]
#[strum(serialize = "Tank sensor raw ${number_a} percent ${number_b}")]
SensorTankRaw,
#[strum(serialize = "raw measure unscaled {{a}} hz {{b}}, plant {{txt_short}} sensor {{txt_long}}")]
#[strum(serialize = "raw measure unscaled ${number_a} hz ${number_b}, plant ${txt_short} sensor ${txt_long}")]
RawMeasure,
#[strum(serialize = "IP info: {{txt_long}}")]
#[strum(serialize = "IP info: ${txt_long}")]
WifiInfo,
#[strum(serialize = "Plant:{{txt_short}} a:{{a}} b:{{b}}")]
#[strum(serialize = "Plant:${txt_short} a:${number_a} b:${number_b}")]
TestSensor,
#[strum(serialize = "Stay alive topic is {{txt_long}}")]
#[strum(serialize = "Stay alive topic is ${txt_long}")]
StayAlive,
#[strum(serialize = "Connecting mqtt {{txt_short}} with id {{txt_long}}")]
#[strum(serialize = "Connecting mqtt ${txt_short} with id ${txt_long}")]
MqttInfo,
#[strum(serialize = "Received stay alive with value {{txt_short}}")]
#[strum(serialize = "Received stay alive with value ${txt_short}")]
MqttStayAliveRec,
#[strum(serialize = "Unknown topic recieved {{txt_long}}")]
#[strum(serialize = "Unknown topic recieved ${txt_long}")]
UnknownTopic,
#[strum(serialize = "Partition state is {{txt_long}}")]
#[strum(serialize = "Partition state is ${txt_long}")]
PartitionState,
#[strum(serialize = "Mounted Filesystem free {{a}} total {{b}} use {{txt_short}}")]
#[strum(serialize = "Mounted Filesystem free ${number_a} total ${number_b} use ${txt_short}")]
FilesystemMount,
#[strum(serialize = "Mounting Filesystem, this will format the first time and needs quite some time!")]
MountingFilesystem,
@ -170,15 +174,15 @@ pub enum LogMessage {
ConfigModeButtonOverride,
#[strum(serialize = "Going to normal mode")]
NormalRun,
#[strum(serialize = "Missing normal config, entering config mode {{txt_long}}")]
#[strum(serialize = "Missing normal config, entering config mode ${txt_long}")]
ConfigModeMissingConfig,
#[strum(serialize = "startup state wifi {{a}} sntp {{b}} mqtt {{txt_short}}")]
#[strum(serialize = "startup state wifi ${number_a} sntp ${number_b} mqtt ${txt_short}")]
StartupInfo,
#[strum(serialize = "Trying to pump for {{b}}s with pump {{a}} now dryrun: {{txt_short}}")]
#[strum(serialize = "Trying to pump for ${number_b}s with pump ${number_a} now dryrun: ${txt_short}")]
PumpPlant,
#[strum(serialize = "Enable main power dryrun: {{a}}")]
#[strum(serialize = "Enable main power dryrun: ${number_a}")]
EnableMain,
#[strum(serialize = "Pumped multiple times, but plant is still to try attempt: {{a}} limit :: {{b}} plant: {{txt_short}}")]
#[strum(serialize = "Pumped multiple times, but plant is still to try attempt: ${number_a} limit :: ${number_b} plant: ${txt_short}")]
ConsecutivePumpCountLimit
}