Skip to content

Commit

Permalink
Make logging more accurate
Browse files Browse the repository at this point in the history
  • Loading branch information
mthmulders committed Nov 20, 2023
1 parent 9959ba6 commit ba99d9f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
9 changes: 5 additions & 4 deletions src/dsmr/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ impl LoggingConsumer {
}
}
impl super::TelegramConsumer for LoggingConsumer {
fn consume(&mut self, _telegram: &str) {
fn consume(&mut self, _telegram: &str) -> bool {
self.telegram_counter += 1;
// We expect a telegram every 10 seconds -> 6 per minute -> 360 per hour.
if self.telegram_counter == 360 {
log::info!("Uploaded 360 telegrams to {} host(s)", self.host_counter);
if self.telegram_counter == 10000 {
log::info!("Uploaded 10000 telegrams to {} host(s)", self.host_counter);
self.telegram_counter = 0;
}

true
}
}
2 changes: 1 addition & 1 deletion src/dsmr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ pub mod sender;
pub mod settings;

pub trait TelegramConsumer {
fn consume(&mut self, telegram: &str);
fn consume(&mut self, telegram: &str) -> bool;
}
3 changes: 2 additions & 1 deletion src/dsmr/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ mod tests {
}
}
impl super::super::TelegramConsumer for TestConsumer {
fn consume(&mut self, telegram: &str) -> () {
fn consume(&mut self, telegram: &str) -> bool {
self.invoked = true;
self.telegram = String::from(telegram);
true
}
}

Expand Down
27 changes: 18 additions & 9 deletions src/dsmr/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl UploadConsumer {
}
}
impl super::TelegramConsumer for UploadConsumer {
fn consume(&mut self, telegram: &str) {
fn consume(&mut self, telegram: &str) -> bool {
log::trace!("- uploading telegram to {}", self.host);
let url = [&self.host, "/api/v1/datalogger/dsmrreading"].join("");

Expand All @@ -35,35 +35,44 @@ impl super::TelegramConsumer for UploadConsumer {
.send();

match result {
Ok(response) => log::trace!("Got response with status {}", response.status()),
Err(msg) => log::warn!("Could not upload telegram due to {}", msg),
Ok(response) => {
log::trace!("Got response with status {}", response.status());
true
}
Err(msg) => {
log::warn!("Could not upload telegram due to {}", msg);
false
}
}
}
}

pub struct DelegatingConsumer {
delegates: Vec<Box<dyn TelegramConsumer>>,
logger: LoggingConsumer,
}
impl DelegatingConsumer {
pub fn new(targets: Vec<settings::Host>) -> Self {
let mut delegates: Vec<Box<dyn TelegramConsumer>> = Vec::with_capacity(targets.len() + 1);

let counter = LoggingConsumer::new(targets.len() as u32);
let logger: LoggingConsumer = LoggingConsumer::new(targets.len() as u32);

(0..targets.len())
.map(|index| UploadConsumer::new(&targets[index]))
.map(Box::new)
.for_each(|b| delegates.push(b));

delegates.push(Box::new(counter));

DelegatingConsumer { delegates }
DelegatingConsumer { delegates, logger }
}
}
impl super::TelegramConsumer for DelegatingConsumer {
fn consume(&mut self, telegram: &str) {
fn consume(&mut self, telegram: &str) -> bool {
for delegate in &mut self.delegates {
delegate.consume(telegram);
if delegate.consume(telegram) {
self.logger.consume(telegram);
}
}

true // ignored anyway...
}
}

0 comments on commit ba99d9f

Please sign in to comment.