diff --git a/src/dsmr/logger.rs b/src/dsmr/logger.rs index 7fbcdc7..69d4964 100644 --- a/src/dsmr/logger.rs +++ b/src/dsmr/logger.rs @@ -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 } } diff --git a/src/dsmr/mod.rs b/src/dsmr/mod.rs index b173c26..97f1a80 100644 --- a/src/dsmr/mod.rs +++ b/src/dsmr/mod.rs @@ -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; } diff --git a/src/dsmr/reader.rs b/src/dsmr/reader.rs index bf5abc3..97abaa7 100644 --- a/src/dsmr/reader.rs +++ b/src/dsmr/reader.rs @@ -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 } } diff --git a/src/dsmr/sender.rs b/src/dsmr/sender.rs index 65b7334..7443c8d 100644 --- a/src/dsmr/sender.rs +++ b/src/dsmr/sender.rs @@ -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(""); @@ -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>, + logger: LoggingConsumer, } impl DelegatingConsumer { pub fn new(targets: Vec) -> Self { let mut delegates: Vec> = 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... } }