Skip to content

Commit

Permalink
Added auto reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
daft-panda committed Sep 23, 2023
1 parent ad9e18f commit fb2731f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
9 changes: 6 additions & 3 deletions ebusd-thermostat/src/ebusd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ impl Ebusd {
let arg = mode.into_cmd_arg();
debug!("Setting mode {}", arg);
let cmd = format!("w -c bai SetModeOverride {}\n", arg);
self.connection
.write_all(cmd.as_bytes())
.await?;
self.connection.write_all(cmd.as_bytes()).await?;

let mut buffer = [0; 1024];
let bytes_read = self.connection.read(&mut buffer).await?;
Expand All @@ -54,4 +52,9 @@ impl Ebusd {
bail!("Set mode {} failed: {}", arg, result);
}
}

pub async fn reconnect(&mut self) -> anyhow::Result<()> {
self.connection = TcpStream::connect(self.endpoint.clone()).await?;
Ok(())
}
}
19 changes: 16 additions & 3 deletions ebusd-thermostat/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use serde_json::Value;
use std::str::FromStr;
use tokio::sync::mpsc::{channel, Receiver};
use tokio::time::{sleep, Duration, Instant};
use tokio::{pin, select};
use tokio::{io, pin, select};

#[tokio::main]
async fn main() {
Expand Down Expand Up @@ -282,7 +282,7 @@ impl Thermostat {
update_pending = false;
}

hold_timer.as_mut().reset(Instant::now() + Duration::from_secs(9999999999999));
hold_timer.as_mut().reset(Instant::now() + Duration::from_secs(999999999));
}
}
}
Expand Down Expand Up @@ -315,6 +315,18 @@ impl Thermostat {
error!("Failed setting mode: {:?}", e);
self.mode_set_fails += 1;

match e.downcast_ref::<io::Error>() {
None => {}
Some(e) => {
debug!("Original error: {}", e.to_string());
if e.to_string().to_lowercase().contains("broken pipe") {
debug!("Reconnecting...");
self.ebusd.reconnect().await?;
self.ebusd.define_message( "wi,BAI,SetModeOverride,Betriebsart,,08,B510,00,hcmode,,UCH,,,,flowtempdesired,,D1C,,,,hwctempdesired,,D1C,,,,hwcflowtempdesired,,UCH,,,,setmode1,,UCH,,,,disablehc,,BI0,,,,disablehwctapping,,BI1,,,,disablehwcload,,BI2,,,,setmode2,,UCH,,,,remoteControlHcPump,,BI0,,,,releaseBackup,,BI1,,,,releaseCooling,,BI2".to_string()).await?;
}
}
}

if self.mode_set_fails > 5 {
panic!("Ebus mode setting failed 5+ times, exiting");
}
Expand Down Expand Up @@ -400,7 +412,8 @@ impl Thermostat {
"set" => {
self.active_mode.hc_mode = HeaterMode::from_str(
String::from_utf8(publish.payload.to_vec())?.as_str(),
).map_err(|_| anyhow!("Invalid heater mode"))?;
)
.map_err(|_| anyhow!("Invalid heater mode"))?;
info!(
"New heater mode: {}",
self.active_mode.hc_mode.to_string()
Expand Down

0 comments on commit fb2731f

Please sign in to comment.