Skip to content

Commit

Permalink
Reimplement to also fix #73
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisOSRM committed Jan 11, 2024
1 parent 1aa2344 commit 57ccf22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
inverter_host = "192.168.4.182"
coop_mode = false
update_interval = 30500

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

static REQUEST_DELAY_DEFAULT: u64 = 30_500;
static REQUEST_DELAY_COOP_MODE: u64 = 60_500;

fn main() {
logging::init_logger();
Expand Down Expand Up @@ -55,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 @@ -75,12 +89,13 @@ fn main() {
channel.publish(&r);
})
}

// TODO: the sleep has to move into the Inverter struct in an async implementation
if config.coop_mode.is_some_and(|value| value) {
// In coop mode, the inverter is updated approximately once a minute. This is sparse
// enough for the cloud to get updated, too.
thread::sleep(Duration::from_millis(REQUEST_DELAY_COOP_MODE));
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));
}
Expand Down

0 comments on commit 57ccf22

Please sign in to comment.