From 692a24e959a302e7d198294c46d4769e1619a99c Mon Sep 17 00:00:00 2001 From: Cosmin Constantin Lazar Date: Fri, 22 Nov 2024 23:06:02 +0100 Subject: [PATCH 1/2] Update on loop --- config/default.toml | 3 ++- src/main.rs | 30 +++++++++++++++++++++--------- src/settings.rs | 1 + tests/full_flow_insta.rs | 1 + 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/config/default.toml b/config/default.toml index 1ffb8d6..38f8135 100644 --- a/config/default.toml +++ b/config/default.toml @@ -1,3 +1,5 @@ +update_interval_minutes = 60 + [mqtt] client_id = "affaldvarme" host = "10.0.1.3" @@ -5,7 +7,6 @@ port = 1883 username = "" password = "" - [affaldvarme] base_url = "https://portal-api.kredslob.dk" diff --git a/src/main.rs b/src/main.rs index deca50c..54492e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,16 +3,28 @@ use ha_mitaffald::sync_data; #[tokio::main] async fn main() { - println!("Starting data synchronization"); + loop { + println!("Starting data synchronization"); - let settings = Settings::new().expect("Failed to read settings"); - let report = sync_data(settings).await; + let settings = Settings::new().expect("Failed to read settings"); + let update_interval = + tokio::time::Duration::from_secs(settings.update_interval_minutes * 60); - match report { - Ok(_) => println!("Data synchronization completed"), - Err(x) => eprintln!( - "Data synchronization failed (some entities may have been updated), error: {}", - x - ), + let report = sync_data(settings).await; + + match report { + Ok(_) => println!("Data synchronization completed"), + Err(x) => eprintln!( + "Data synchronization failed (some entities may have been updated), error: {}", + x + ), + } + + println!( + "Next synchronization scheduled at {}", + (chrono::Local::now() + update_interval).format("%Y-%m-%d %H:%M:%S") + ); + + tokio::time::sleep(update_interval).await; } } diff --git a/src/settings.rs b/src/settings.rs index 6a565a5..b246b7d 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -7,6 +7,7 @@ use serde::Deserialize; pub struct Settings { pub mqtt: MQTTConfig, pub affaldvarme: AffaldVarmeConfig, + pub update_interval_minutes: u64, } impl Settings { diff --git a/tests/full_flow_insta.rs b/tests/full_flow_insta.rs index 76288c6..2eec1c0 100644 --- a/tests/full_flow_insta.rs +++ b/tests/full_flow_insta.rs @@ -46,6 +46,7 @@ async fn smoke_test_insta() { .await; let settings = Settings { + update_interval_minutes: 60, affaldvarme: AffaldVarmeConfig { address: Address::Id(AddressId { id: address_id }), base_url: mit_affald_server_url, From 119c224a8e74f62936fe64b147565dc65c04372a Mon Sep 17 00:00:00 2001 From: Cosmin Constantin Lazar Date: Sat, 23 Nov 2024 07:18:43 +0100 Subject: [PATCH 2/2] Welcome to the 21st century println -> tracing --- Cargo.toml | 2 ++ src/main.rs | 15 +++++++++++---- src/mitaffald/mod.rs | 3 ++- tests/mqtt/mod.rs | 7 ++++--- 4 files changed, 19 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 6b2e295..07cb771 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,8 @@ rumqttc = "0.24.0" serde = { version = "1.0.215", features = ["derive"] } serde_json = "1.0.133" tokio = { version = "1.41.1", features = ["full"] } +tracing = "0.1.40" +tracing-subscriber = "0.3.18" url = { version = "2.5.4", features = ["serde"] } [dev-dependencies] diff --git a/src/main.rs b/src/main.rs index 54492e3..2d85035 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,17 @@ use ha_mitaffald::settings::Settings; use ha_mitaffald::sync_data; +use tracing::{error, info, Level}; +use tracing_subscriber::FmtSubscriber; #[tokio::main] async fn main() { + let subscriber = FmtSubscriber::builder() + .with_max_level(Level::INFO) + .finish(); + + tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); loop { - println!("Starting data synchronization"); + info!("Starting data synchronization"); let settings = Settings::new().expect("Failed to read settings"); let update_interval = @@ -13,14 +20,14 @@ async fn main() { let report = sync_data(settings).await; match report { - Ok(_) => println!("Data synchronization completed"), - Err(x) => eprintln!( + Ok(_) => info!("Data synchronization completed"), + Err(x) => error!( "Data synchronization failed (some entities may have been updated), error: {}", x ), } - println!( + info!( "Next synchronization scheduled at {}", (chrono::Local::now() + update_interval).format("%Y-%m-%d %H:%M:%S") ); diff --git a/src/mitaffald/mod.rs b/src/mitaffald/mod.rs index 5c51a27..9525677 100644 --- a/src/mitaffald/mod.rs +++ b/src/mitaffald/mod.rs @@ -3,6 +3,7 @@ pub mod settings; use chrono::{DateTime, Utc}; use serde::Deserialize; use settings::{Address, AffaldVarmeConfig}; +use tracing::info; use url::Url; use self::settings::{AddressId, TraditionalAddress}; @@ -25,7 +26,7 @@ pub async fn get_containers(config: AffaldVarmeConfig) -> Result, .next() .ok_or_else(|| "No data found".to_string()) .map(|response| { - println!("Received information for stand: {}", response.stand_name); + info!("Received information for stand: {}", response.stand_name); response.into() }) }) diff --git a/tests/mqtt/mod.rs b/tests/mqtt/mod.rs index b36a7f6..442e86b 100644 --- a/tests/mqtt/mod.rs +++ b/tests/mqtt/mod.rs @@ -6,6 +6,7 @@ use std::{ }; use rumqttc::{Client, Event, Packet, Publish, QoS}; +use tracing::info; pub struct CollectingClient { received_messages: std::sync::Arc>>, @@ -37,7 +38,7 @@ impl CollectingClient { loop { let message = connection.recv_timeout(Duration::from_secs(1)); - println!("Received message: {:?}", &message); + info!("Received message: {:?}", &message); match message { Ok(Ok(Event::Incoming(Packet::SubAck(_)))) => { tx.send(()).expect("Cannot report ready to main thread") @@ -49,7 +50,7 @@ impl CollectingClient { } if stopping_flag.load(std::sync::atomic::Ordering::Relaxed) { - println!("Thread is terminating"); + info!("Thread is terminating"); break; } } @@ -83,7 +84,7 @@ impl CollectingClient { std::thread::sleep(std::time::Duration::from_millis(500)); } - println!("Joining worker thread..."); + info!("Joining worker thread..."); if let Some(handle) = self.join_handle { handle.join().unwrap(); }