Browse Source

Publish programs

master
Alex Mikhalev 4 years ago
parent
commit
239a289e4e
  1. 1
      src/main.rs
  2. 54
      src/mqtt_interface.rs

1
src/main.rs

@ -65,6 +65,7 @@ async fn main() -> Result<()> {
program_runner.update_sections(sections.clone()).await?; program_runner.update_sections(sections.clone()).await?;
mqtt_interface.publish_sections(&sections).await?; mqtt_interface.publish_sections(&sections).await?;
program_runner.update_programs(programs.clone()).await?; program_runner.update_programs(programs.clone()).await?;
mqtt_interface.publish_programs(&programs).await?;
info!("sprinklers_rs initialized"); info!("sprinklers_rs initialized");

54
src/mqtt_interface.rs

@ -1,4 +1,4 @@
use crate::model::{Section, SectionId, Sections}; use crate::model::{Section, SectionId, Sections, Programs, Program, ProgramId};
use eyre::WrapErr; use eyre::WrapErr;
use rumqttc::{LastWill, MqttOptions, QoS}; use rumqttc::{LastWill, MqttOptions, QoS};
use std::{ use std::{
@ -36,6 +36,14 @@ where
fn section_data(&self, section_id: SectionId) -> String { fn section_data(&self, section_id: SectionId) -> String {
format!("{}/sections/{}", self.prefix.as_ref(), section_id) format!("{}/sections/{}", self.prefix.as_ref(), section_id)
} }
fn programs(&self) -> String {
format!("{}/programs", self.prefix.as_ref())
}
fn program_data(&self, program_id: ProgramId) -> String {
format!("{}/programs/{}", self.prefix.as_ref(), program_id)
}
} }
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
@ -105,6 +113,23 @@ impl MqttInterface {
(Self { client, topics }, event_loop) (Self { client, topics }, event_loop)
} }
async fn publish_connected(&mut self, connected: bool) -> eyre::Result<()> {
self.client
.publish(
self.topics.connected(),
QoS::AtLeastOnce,
true,
connected.to_string(),
)
.await
.wrap_err("failed to publish connected topic")?;
Ok(())
}
async fn cancel(&mut self) -> Result<(), rumqttc::ClientError> {
self.client.cancel().await
}
pub async fn publish_sections(&mut self, sections: &Sections) -> eyre::Result<()> { pub async fn publish_sections(&mut self, sections: &Sections) -> eyre::Result<()> {
let section_ids: Vec<_> = sections.keys().cloned().collect(); let section_ids: Vec<_> = sections.keys().cloned().collect();
let section_ids_payload = serde_json::to_vec(&section_ids)?; let section_ids_payload = serde_json::to_vec(&section_ids)?;
@ -135,21 +160,34 @@ impl MqttInterface {
Ok(()) Ok(())
} }
async fn publish_connected(&mut self, connected: bool) -> eyre::Result<()> { 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)?;
self.client self.client
.publish( .publish(
self.topics.connected(), self.topics.programs(),
QoS::AtLeastOnce, QoS::AtLeastOnce,
true, true,
connected.to_string(), program_ids_payload,
) )
.await .await?;
.wrap_err("failed to publish connected topic")?; for program in programs.values() {
self.publish_program(program).await?;
}
Ok(()) Ok(())
} }
async fn cancel(&mut self) -> Result<(), rumqttc::ClientError> { pub async fn publish_program(&mut self, program: &Program) -> eyre::Result<()> {
self.client.cancel().await let payload = serde_json::to_vec(program).wrap_err("failed to serialize program")?;
self.client
.publish(
self.topics.program_data(program.id),
QoS::AtLeastOnce,
true,
payload,
)
.await?;
Ok(())
} }
} }

Loading…
Cancel
Save