From b07e57293bbe0483ef3d8051e93b6150c93584b4 Mon Sep 17 00:00:00 2001 From: Dennis Luxen Date: Fri, 12 Jan 2024 10:46:11 +0100 Subject: [PATCH] Implement an optional cooperative mode that will also update S-Miles Cloud (#78) --- config.toml | 1 + src/bin/hms-mqtt-publish/main.rs | 29 ++++++++++++++++++++++++++--- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/config.toml b/config.toml index 037cb156..fcb6dc53 100644 --- a/config.toml +++ b/config.toml @@ -1,4 +1,5 @@ inverter_host = "192.168.4.182" +update_interval = 30500 [home_assistant] host = "192.168.178.250" diff --git a/src/bin/hms-mqtt-publish/main.rs b/src/bin/hms-mqtt-publish/main.rs index f89d3b86..3d8ca170 100644 --- a/src/bin/hms-mqtt-publish/main.rs +++ b/src/bin/hms-mqtt-publish/main.rs @@ -21,11 +21,12 @@ use log::{error, info}; #[derive(Debug, Deserialize)] struct Config { inverter_host: String, + update_interval: Option, home_assistant: Option, simple_mqtt: Option, } -static REQUEST_DELAY: u64 = 30_500; +static REQUEST_DELAY_DEFAULT: u64 = 30_500; fn main() { logging::init_logger(); @@ -53,6 +54,21 @@ fn main() { let contents = fs::read_to_string(path).expect("Could not read config.toml"); let config: Config = toml::from_str(&contents).expect("toml config unparsable"); + if config + .update_interval + .is_some_and(|value| value > REQUEST_DELAY_DEFAULT) + { + info!( + "using non-default update interval of {:.2}s", + (config.update_interval.unwrap() as f64 / 1000.) + ) + } else { + info!( + "using default update interval of {:.2}s", + (REQUEST_DELAY_DEFAULT as f64 / 1000.) + ) + } + info!("inverter host: {}", config.inverter_host); let mut inverter = Inverter::new(&config.inverter_host); @@ -74,7 +90,14 @@ fn main() { }) } - // TODO: this has to move into the Inverter struct in an async implementation - thread::sleep(Duration::from_millis(REQUEST_DELAY)); + // TODO: the sleep has to move into the Inverter struct in an async implementation + if config + .update_interval + .is_some_and(|value| value > REQUEST_DELAY_DEFAULT) + { + thread::sleep(Duration::from_millis(config.update_interval.unwrap())); + } else { + thread::sleep(Duration::from_millis(REQUEST_DELAY_DEFAULT)); + } } }