Skip to content

Commit

Permalink
Implement an optional cooperative mode that will also update S-Miles …
Browse files Browse the repository at this point in the history
…Cloud (#78)
  • Loading branch information
DennisOSRM authored Jan 12, 2024
1 parent 06de09d commit b07e572
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
inverter_host = "192.168.4.182"
update_interval = 30500

[home_assistant]
host = "192.168.178.250"
Expand Down
29 changes: 26 additions & 3 deletions src/bin/hms-mqtt-publish/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ use log::{error, info};
#[derive(Debug, Deserialize)]
struct Config {
inverter_host: String,
update_interval: Option<u64>,
home_assistant: Option<MqttConfig>,
simple_mqtt: Option<MqttConfig>,
}

static REQUEST_DELAY: u64 = 30_500;
static REQUEST_DELAY_DEFAULT: u64 = 30_500;

fn main() {
logging::init_logger();
Expand Down Expand Up @@ -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);

Expand All @@ -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));
}
}
}

0 comments on commit b07e572

Please sign in to comment.