diff --git a/src/mqtt_interface.rs b/src/mqtt_interface.rs index 81615d9..9026e3f 100644 --- a/src/mqtt_interface.rs +++ b/src/mqtt_interface.rs @@ -125,34 +125,34 @@ impl MqttInterface { (Self { client, topics }, event_loop) } - async fn publish_connected(&mut self, connected: bool) -> eyre::Result<()> { + async fn publish_data
(&mut self, topic: String, payload: &P) -> eyre::Result<()>
+ where
+ P: serde::Serialize,
+ {
+ let payload_vec =
+ serde_json::to_vec(payload).wrap_err("failed to serialize publish payload")?;
self.client
- .publish(
- self.topics.connected(),
- QoS::AtLeastOnce,
- true,
- connected.to_string(),
- )
+ .publish(topic, QoS::AtLeastOnce, true, payload_vec)
.await
- .wrap_err("failed to publish connected topic")?;
+ .wrap_err("failed to publish")?;
Ok(())
}
+ async fn publish_connected(&mut self, connected: bool) -> eyre::Result<()> {
+ self.publish_data(self.topics.connected(), &connected)
+ .await
+ .wrap_err("failed to publish connected topic")
+ }
+
async fn cancel(&mut self) -> Result<(), rumqttc::ClientError> {
self.client.cancel().await
}
pub async fn publish_sections(&mut self, sections: &Sections) -> eyre::Result<()> {
let section_ids: Vec<_> = sections.keys().cloned().collect();
- let section_ids_payload = serde_json::to_vec(§ion_ids)?;
- self.client
- .publish(
- self.topics.sections(),
- QoS::AtLeastOnce,
- true,
- section_ids_payload,
- )
- .await?;
+ self.publish_data(self.topics.sections(), §ion_ids)
+ .await
+ .wrap_err("failed to publish section ids")?;
for section in sections.values() {
self.publish_section(section).await?;
}
@@ -160,16 +160,9 @@ impl MqttInterface {
}
pub async fn publish_section(&mut self, section: &Section) -> eyre::Result<()> {
- let payload = serde_json::to_vec(section).wrap_err("failed to serialize section")?;
- self.client
- .publish(
- self.topics.section_data(section.id),
- QoS::AtLeastOnce,
- true,
- payload,
- )
- .await?;
- Ok(())
+ self.publish_data(self.topics.section_data(section.id), section)
+ .await
+ .wrap_err("failed to publish section")
}
pub async fn publish_section_state(
@@ -177,29 +170,16 @@ impl MqttInterface {
section_id: SectionId,
state: bool,
) -> eyre::Result<()> {
- let payload: Vec