Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make logging counter more accurate #82

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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...
}
}
Loading