1
0
Fork 0

Publish connection errors to MQTT

Also move `raw` debug output to console output, hex-formatted.
gh-action
Bo Jeanes 2022-09-10 08:44:16 +10:00
parent 2ee2f597d0
commit d2e37ebfbf
2 changed files with 20 additions and 15 deletions

View File

@ -49,8 +49,14 @@ pub(crate) async fn run(
let _ = connection_is_ready.send(()); let _ = connection_is_ready.send(());
if let Err(error) = conn.run().await { let result = conn.run().await;
if let Err(error) = result {
error!(?error, "Modbus connection failed"); error!(?error, "Modbus connection failed");
mqtt.publish("state", "errorered").await.unwrap();
mqtt.publish("last_error", format!("{error:?}"))
.await
.unwrap();
tokio::time::sleep(std::time::Duration::from_secs(current_wait as u64)) tokio::time::sleep(std::time::Duration::from_secs(current_wait as u64))
.await; .await;
@ -73,7 +79,10 @@ pub(crate) async fn run(
} }
}); });
is_connection_ready.changed().await; is_connection_ready
.changed()
.await
.map_err(|_| Error::RecvError)?;
Ok(handle) Ok(handle)
} }

View File

@ -31,21 +31,17 @@ impl Monitor {
loop { loop {
interval.tick().await; interval.tick().await;
if let Ok(words) = self.read().await { if let Ok(words) = self.read().await {
debug!(address=%self.register.address, "type"=?self.register.register_type, ?words);
#[cfg(feature = "raw")]
self.mqtt
.publish("raw", serde_json::to_vec(&words).unwrap())
.await
.unwrap();
let value = self.register.parse_words(&words); let value = self.register.parse_words(&words);
let value = serde_json::to_string(&value).unwrap();
if let Err(error) = self debug!(
.mqtt address=%self.register.address,
.publish("state", serde_json::to_vec(&value).unwrap()) "type"=?self.register.register_type,
.await %value,
{ raw=%format!("{:04x?}", &words),
);
if let Err(error) = self.mqtt.publish("state", value).await {
warn!(?error); warn!(?error);
break; break;
} }