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

Cooperative Mode for S-Miles cloud with parameter to enable / disable it. Will pause polling the inverter at appropriate times #110

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
inverter_host = "192.168.4.182"
update_interval = 30500
smiles_cooperation = false

[home_assistant]
host = "192.168.178.250"
Expand Down
26 changes: 25 additions & 1 deletion src/bin/hms-mqtt-publish/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use rumqttc_wrapper::RumqttcWrapper;
use serde_derive::Deserialize;
use std::fs;
use std::thread;
use std::time::Duration;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use log::{error, info};

#[derive(Debug, Deserialize)]
struct Config {
inverter_host: String,
update_interval: Option<u64>,
smiles_cooperation: bool,
home_assistant: Option<MqttConfig>,
simple_mqtt: Option<MqttConfig>,
}
Expand Down Expand Up @@ -83,7 +84,30 @@ fn main() {
output_channels.push(Box::new(SimpleMqtt::<RumqttcWrapper>::new(&config)));
}

if config.smiles_cooperation {
gendelo3 marked this conversation as resolved.
Show resolved Hide resolved
info!("S-Miles cloud cooperative mode enabled");
}

loop {
// Do not query the inverter when the S-Miles cloud is about to update
if config.smiles_cooperation {
let now = SystemTime::now();
let duration_since_epoch = now.duration_since(UNIX_EPOCH).unwrap();
let seconds_since_epoch = duration_since_epoch.as_secs();

let seconds_in_current_minute = seconds_since_epoch % 60;
let minutes_since_epoch = seconds_since_epoch / 60;
let minutes_in_current_hour = minutes_since_epoch % 60;

// This is the time at which the S-Miles update seems to take place
// Adding some extra time before and after, in which we dont publish
if minutes_in_current_hour % 15 == 14 {
thread::sleep(Duration::from_millis(
(15 + 60 - seconds_in_current_minute) * 1000,
DennisOSRM marked this conversation as resolved.
Show resolved Hide resolved
));
}
}

if let Some(r) = inverter.update_state() {
output_channels.iter_mut().for_each(|channel| {
channel.publish(&r);
Expand Down
Loading