chore: cargo fmt

This commit is contained in:
2025-03-13 20:32:28 +01:00
parent 3cdaacabac
commit f340278236
5 changed files with 318 additions and 288 deletions

View File

@@ -1,7 +1,7 @@
use std::{collections::HashMap, sync::Mutex};
use serde::Serialize;
use std::{collections::HashMap, sync::Mutex};
use strum::{EnumIter, IntoEnumIterator};
use strum_macros::IntoStaticStr;
use strum_macros::IntoStaticStr;
use esp_idf_svc::systime::EspSystemTime;
use once_cell::sync::Lazy;
@@ -9,16 +9,17 @@ 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 TXT_SHORT_LENGTH: usize = 8;
const TXT_LONG_LENGTH: usize = 32;
const BUFFER_SIZE:usize = 220;
const BUFFER_SIZE: usize = 220;
#[link_section = ".rtc.data"]
static mut BUFFER:ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE> = ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>::new();
static mut BUFFER: ConstGenericRingBuffer<LogEntry, BUFFER_SIZE> =
ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>::new();
#[allow(static_mut_refs)]
static BUFFER_ACCESS: Lazy<Mutex<&mut ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>>> = Lazy::new(|| unsafe { Mutex::new(&mut BUFFER) });
static BUFFER_ACCESS: Lazy<Mutex<&mut ConstGenericRingBuffer<LogEntry, BUFFER_SIZE>>> =
Lazy::new(|| unsafe { Mutex::new(&mut BUFFER) });
#[derive(Serialize, Debug, Clone)]
pub struct LogEntry {
@@ -27,36 +28,36 @@ pub struct LogEntry {
pub a: u32,
pub b: u32,
pub txt_short: heapless::String<TXT_SHORT_LENGTH>,
pub txt_long: heapless::String<TXT_LONG_LENGTH>
pub txt_long: heapless::String<TXT_LONG_LENGTH>,
}
pub fn init(){
unsafe {
pub fn init() {
unsafe {
BUFFER = ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>::new();
};
let mut access = BUFFER_ACCESS.lock().unwrap();
access.drain().for_each(|_| {});
}
fn limit_length <const LIMIT:usize> (input: &str, target: &mut heapless::String<LIMIT>){
fn limit_length<const LIMIT: usize>(input: &str, target: &mut heapless::String<LIMIT>) {
for char in input.chars() {
match target.push(char) {
Ok(_) => {}, //continue adding chars
Ok(_) => {} //continue adding chars
Err(_) => {
//clear space for two asci chars
while target.len()+2 >= LIMIT {
while target.len() + 2 >= LIMIT {
target.pop().unwrap();
}
//add .. to shortened strings
target.push('.').unwrap();
target.push('.').unwrap();
return;
},
}
}
}
}
pub fn get_log() -> Vec<LogEntry>{
pub fn get_log() -> Vec<LogEntry> {
let buffer = BUFFER_ACCESS.lock().unwrap();
let mut read_copy = Vec::new();
for entry in buffer.iter() {
@@ -67,19 +68,17 @@ pub fn get_log() -> Vec<LogEntry>{
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();
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();
limit_length(txt_short, &mut txt_short_stack);
limit_length(txt_long, &mut txt_long_stack);
let time = EspSystemTime {}.now().as_millis() as u64;
let ordinal = message_key.ordinal() as u16;
let template_string:&str = message_key.into();
let template_string: &str = message_key.into();
let mut values: HashMap<&str, &str> = HashMap::new();
let number_a_str = number_a.to_string();
@@ -92,12 +91,10 @@ pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short:&str
let template = Template::from(template_string);
let serial_entry = template.fill_in(&values);
println!("{serial_entry}");
let entry = LogEntry{
let entry = LogEntry {
timestamp: time,
message_id: ordinal,
a: number_a,
@@ -106,14 +103,10 @@ pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short:&str
txt_long: txt_long_stack,
};
let mut buffer = BUFFER_ACCESS.lock().unwrap();
let mut buffer = BUFFER_ACCESS.lock().unwrap();
buffer.push(entry);
}
#[cfg(test)]
mod tests {
use super::*;
@@ -121,9 +114,9 @@ mod tests {
#[test]
fn within_limit() {
let test = "12345678";
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();
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();
limit_length(test, &mut txt_short_stack);
limit_length(test, &mut txt_long_stack);
@@ -132,39 +125,43 @@ mod tests {
}
}
#[derive(IntoStaticStr, EnumIter, Serialize, PartialEq, Eq, PartialOrd, Ord, Clone, UnitEnum)]
pub enum LogMessage {
#[strum(serialize = "Reset due to ${txt_long} requires rtc clear ${number_a} and force config mode ${number_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 ${number_a}")]
#[strum(serialize = "Current restart to conf mode ${number_a}")]
RestartToConfig,
#[strum(serialize = "Current low voltage detection is ${number_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 ${number_a} percent ${number_b}")]
#[strum(serialize = "Tank sensor raw ${number_a} percent ${number_b}")]
SensorTankRaw,
#[strum(serialize = "raw measure unscaled ${number_a} hz ${number_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:${number_a} b:${number_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 ${number_a} total ${number_b} use ${txt_short}")]
FilesystemMount,
#[strum(serialize = "Mounting Filesystem, this will format the first time and needs quite some time!")]
#[strum(
serialize = "Mounting Filesystem, this will format the first time and needs quite some time!"
)]
MountingFilesystem,
#[strum(serialize = "Year inplausible, force config mode")]
YearInplausibleForceConfig,
@@ -178,12 +175,16 @@ pub enum LogMessage {
ConfigModeMissingConfig,
#[strum(serialize = "startup state wifi ${number_a} sntp ${number_b} mqtt ${txt_short}")]
StartupInfo,
#[strum(serialize = "Trying to pump for ${number_b}s with pump ${number_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: ${number_a}")]
EnableMain,
#[strum(serialize = "Pumped multiple times, but plant is still to try attempt: ${number_a} limit :: ${number_b} plant: ${txt_short}")]
ConsecutivePumpCountLimit
#[strum(
serialize = "Pumped multiple times, but plant is still to try attempt: ${number_a} limit :: ${number_b} plant: ${txt_short}"
)]
ConsecutivePumpCountLimit,
}
#[derive(Serialize)]