From cfde268603377f461b85ed73bc4f5168d46bbee4 Mon Sep 17 00:00:00 2001 From: Alex Mikhalev Date: Sun, 27 Sep 2020 18:27:14 -0600 Subject: [PATCH] Add publishing of section states --- src/main.rs | 6 ++++++ src/mqtt_interface.rs | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main.rs b/src/main.rs index 34df3f9..793d90b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ mod section_interface; mod section_runner; #[cfg(test)] mod trace_listeners; +mod update_listener; use eyre::Result; use std::sync::Arc; @@ -64,6 +65,11 @@ async fn main() -> Result<()> { program_runner.update_sections(sections.clone()).await?; mqtt_interface.publish_sections(§ions).await?; + for section_id in sections.keys() { + mqtt_interface + .publish_section_state(*section_id, false) + .await?; + } program_runner.update_programs(programs.clone()).await?; mqtt_interface.publish_programs(&programs).await?; diff --git a/src/mqtt_interface.rs b/src/mqtt_interface.rs index 0a1ee9f..3b24dec 100644 --- a/src/mqtt_interface.rs +++ b/src/mqtt_interface.rs @@ -37,6 +37,10 @@ where format!("{}/sections/{}", self.prefix.as_ref(), section_id) } + fn section_state(&self, section_id: SectionId) -> String { + format!("{}/sections/{}/state", self.prefix.as_ref(), section_id) + } + fn programs(&self) -> String { format!("{}/programs", self.prefix.as_ref()) } @@ -160,6 +164,19 @@ impl MqttInterface { Ok(()) } + pub async fn publish_section_state(&mut self, section_id: SectionId, state: bool) -> eyre::Result<()> { + let payload: Vec = state.to_string().into(); + self.client + .publish( + self.topics.section_state(section_id), + QoS::AtLeastOnce, + true, + payload, + ) + .await?; + Ok(()) + } + pub async fn publish_programs(&mut self, programs: &Programs) -> eyre::Result<()> { let program_ids: Vec<_> = programs.keys().cloned().collect(); let program_ids_payload = serde_json::to_vec(&program_ids)?;