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

54
src/mqtt_interface.rs

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