it's alive
This commit is contained in:
@@ -1,14 +1,13 @@
|
||||
use crate::vec;
|
||||
use alloc::string::ToString;
|
||||
use alloc::vec::Vec;
|
||||
use embassy_sync::blocking_mutex::raw::CriticalSectionRawMutex;
|
||||
use embassy_sync::lazy_lock::LazyLock;
|
||||
use embassy_sync::mutex::Mutex;
|
||||
use serde::Serialize;
|
||||
use std::{collections::HashMap, sync::Mutex};
|
||||
use strum::EnumIter;
|
||||
use strum_macros::IntoStaticStr;
|
||||
use strum_macros::{EnumIter, IntoStaticStr};
|
||||
|
||||
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;
|
||||
@@ -20,8 +19,8 @@ const BUFFER_SIZE: usize = 220;
|
||||
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: LazyLock<Mutex<CriticalSectionRawMutex,&mut ConstGenericRingBuffer<LogEntry, BUFFER_SIZE>>> =
|
||||
LazyLock::new(|| unsafe { Mutex::new(&mut BUFFER) });
|
||||
|
||||
#[derive(Serialize, Debug, Clone)]
|
||||
pub struct LogEntry {
|
||||
@@ -33,11 +32,11 @@ pub struct LogEntry {
|
||||
pub txt_long: heapless::String<TXT_LONG_LENGTH>,
|
||||
}
|
||||
|
||||
pub fn init() {
|
||||
pub async fn init() {
|
||||
unsafe {
|
||||
BUFFER = ConstGenericRingBuffer::<LogEntry, BUFFER_SIZE>::new();
|
||||
};
|
||||
let mut access = BUFFER_ACCESS.lock().unwrap();
|
||||
let mut access = BUFFER_ACCESS.get().lock().await;
|
||||
access.drain().for_each(|_| {});
|
||||
}
|
||||
|
||||
@@ -59,8 +58,8 @@ fn limit_length<const LIMIT: usize>(input: &str, target: &mut heapless::String<L
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_log() -> Vec<LogEntry> {
|
||||
let buffer = BUFFER_ACCESS.lock().unwrap();
|
||||
pub async fn get_log() -> Vec<LogEntry> {
|
||||
let buffer = BUFFER_ACCESS.get().lock().await;
|
||||
let mut read_copy = Vec::new();
|
||||
for entry in buffer.iter() {
|
||||
let copy = entry.clone();
|
||||
@@ -70,32 +69,35 @@ pub fn get_log() -> Vec<LogEntry> {
|
||||
read_copy
|
||||
}
|
||||
|
||||
pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short: &str, txt_long: &str) {
|
||||
pub async 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;
|
||||
//TODO
|
||||
let time = 0;
|
||||
|
||||
// let time = EspSystemTime {}.now().as_millis() as u64;
|
||||
//
|
||||
let ordinal = message_key.ordinal() as u16;
|
||||
let template_string: &str = message_key.into();
|
||||
|
||||
let mut values: HashMap<&str, &str> = HashMap::new();
|
||||
let number_a_str = number_a.to_string();
|
||||
let number_b_str = number_b.to_string();
|
||||
|
||||
values.insert("number_a", &number_a_str);
|
||||
values.insert("number_b", &number_b_str);
|
||||
values.insert("txt_short", txt_short);
|
||||
values.insert("txt_long", txt_long);
|
||||
|
||||
let template = Template::from(template_string);
|
||||
let serial_entry = template.fill_in(&values);
|
||||
|
||||
log::info!("{serial_entry}");
|
||||
//TODO push to mqtt?
|
||||
// let template_string: &str = message_key.into();
|
||||
//
|
||||
// let mut values: HashMap<&str, &str> = HashMap::new();
|
||||
// let number_a_str = number_a.to_string();
|
||||
// let number_b_str = number_b.to_string();
|
||||
//
|
||||
// values.insert("number_a", &number_a_str);
|
||||
// values.insert("number_b", &number_b_str);
|
||||
// values.insert("txt_short", txt_short);
|
||||
// values.insert("txt_long", txt_long);
|
||||
//
|
||||
// let template = Template::from(template_string);
|
||||
// let serial_entry = template.fill_in(&values);
|
||||
//
|
||||
// log::info!("{serial_entry}");
|
||||
// //TODO push to mqtt?
|
||||
|
||||
let entry = LogEntry {
|
||||
timestamp: time,
|
||||
@@ -106,29 +108,10 @@ pub fn log(message_key: LogMessage, number_a: u32, number_b: u32, txt_short: &st
|
||||
txt_long: txt_long_stack,
|
||||
};
|
||||
|
||||
let mut buffer = BUFFER_ACCESS.lock().unwrap();
|
||||
let mut buffer = BUFFER_ACCESS.get().lock().await;
|
||||
buffer.push(entry);
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[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();
|
||||
limit_length(test, &mut txt_short_stack);
|
||||
limit_length(test, &mut txt_long_stack);
|
||||
|
||||
assert_eq!(txt_short_stack.as_str(), test);
|
||||
assert_eq!(txt_long_stack.as_str(), test);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(IntoStaticStr, EnumIter, Serialize, PartialEq, Eq, PartialOrd, Ord, Clone, UnitEnum)]
|
||||
#[derive(IntoStaticStr, 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}"
|
||||
|
||||
Reference in New Issue
Block a user