|
|
@ -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(§ion_ids)?; |
|
|
|
let section_ids_payload = serde_json::to_vec(§ion_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(()) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|