This should reconnect when the connection is lost
This commit is contained in:
parent
92580d86d2
commit
f42b201791
3 changed files with 34 additions and 13 deletions
|
@ -113,6 +113,8 @@
|
||||||
rust-analyzer
|
rust-analyzer
|
||||||
taplo
|
taplo
|
||||||
gitflow
|
gitflow
|
||||||
|
|
||||||
|
dsniff # has the tcpkill command to test dropped connections
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -284,7 +284,7 @@ impl HomeAssistant {
|
||||||
"Received '{message_type}', we're currently not handling that."
|
"Received '{message_type}', we're currently not handling that."
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
HomeAssistantError::JsonError(e) => error!("{e}"),
|
HomeAssistantError::JsonError(e) => error!("Error deserializing JSON: {e}"),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
error!("{e}");
|
error!("{e}");
|
||||||
|
@ -294,10 +294,14 @@ impl HomeAssistant {
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error!("Error in receiving message: {e}");
|
error!("Error in receiving message: {e}");
|
||||||
|
// break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
// I recon we should reconnect here. Probably.
|
||||||
|
error!("Received 'None' from handle_message");
|
||||||
break;
|
break;
|
||||||
}
|
},
|
||||||
}
|
|
||||||
None => break,
|
|
||||||
},
|
},
|
||||||
_ = ping_interval.tick() => {
|
_ = ping_interval.tick() => {
|
||||||
if self.auth_complete {
|
if self.auth_complete {
|
||||||
|
@ -307,7 +311,7 @@ impl HomeAssistant {
|
||||||
|
|
||||||
ws_stream.send(ping_message.to_message()).await?;
|
ws_stream.send(ping_message.to_message()).await?;
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
29
src/main.rs
29
src/main.rs
|
@ -5,7 +5,8 @@ use clap::Parser;
|
||||||
use color_eyre::{eyre::WrapErr, Result};
|
use color_eyre::{eyre::WrapErr, Result};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use tracing::debug;
|
use tokio::signal::unix::SignalKind;
|
||||||
|
use tracing::{debug, error};
|
||||||
use tracing_error::ErrorLayer;
|
use tracing_error::ErrorLayer;
|
||||||
use tracing_subscriber::{prelude::*, EnvFilter, Registry};
|
use tracing_subscriber::{prelude::*, EnvFilter, Registry};
|
||||||
use tracing_tree::HierarchicalLayer;
|
use tracing_tree::HierarchicalLayer;
|
||||||
|
@ -127,16 +128,30 @@ async fn main() -> Result<()> {
|
||||||
.wrap_err_with(|| format!("Unable to execute command {cmd}"))?;
|
.wrap_err_with(|| format!("Unable to execute command {cmd}"))?;
|
||||||
} else {
|
} else {
|
||||||
debug!("Retrieving state of '{entity}' on '{host}'");
|
debug!("Retrieving state of '{entity}' on '{host}'");
|
||||||
let ha = homeassistant::HomeAssistant::builder()
|
loop {
|
||||||
.host(host)
|
tokio::select! {
|
||||||
.entity(entity)
|
_ = homeassistant::HomeAssistant::builder()
|
||||||
.token(token)
|
.host(host.clone())
|
||||||
|
.entity(entity.clone())
|
||||||
|
.token(token.clone())
|
||||||
.insecure(insecure)
|
.insecure(insecure)
|
||||||
.format(format)
|
.format(format)
|
||||||
.build();
|
.build().open_connection() => {
|
||||||
ha.open_connection().await.unwrap_or(());
|
error!("Connection closed? Reconnecting...");
|
||||||
|
},
|
||||||
|
_ = tokio::signal::ctrl_c() => break,
|
||||||
|
_ = sig_term() => break,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sig_term() -> Result<()> {
|
||||||
|
tokio::signal::unix::signal(SignalKind::terminate())?
|
||||||
|
.recv()
|
||||||
|
.await;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue