new mqtt topic

This commit is contained in:
Robin Appelman 2021-03-23 20:08:18 +01:00
commit f0e28ed95c
3 changed files with 7 additions and 9 deletions

View file

@ -7,13 +7,12 @@ use std::time::Instant;
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
pub struct Device {
pub topic: String,
pub hostname: String,
}
impl Device {
pub fn get_topic(&self, prefix: &str, command: &str) -> String {
format!("{}/{}/{}/{}", prefix, self.topic, self.hostname, command)
format!("{}/{}/{}", prefix, self.hostname, command)
}
}

View file

@ -7,10 +7,10 @@ pub async fn mqtt_stream(
mqtt_options: MqttOptions,
) -> Result<(AsyncClient, impl Stream<Item = Result<Publish>>)> {
let (client, event_loop) = AsyncClient::new(mqtt_options, 10);
client.subscribe("tele/+/+/LWT", QoS::AtMostOnce).await?;
client.subscribe("stat/+/+/POWER", QoS::AtMostOnce).await?;
client.subscribe("tele/+/+/SENSOR", QoS::AtMostOnce).await?;
client.subscribe("stat/+/+/RESULT", QoS::AtMostOnce).await?;
client.subscribe("tele/+/LWT", QoS::AtMostOnce).await?;
client.subscribe("stat/+/POWER", QoS::AtMostOnce).await?;
client.subscribe("tele/+/SENSOR", QoS::AtMostOnce).await?;
client.subscribe("stat/+/RESULT", QoS::AtMostOnce).await?;
let stream = event_loop_to_stream(event_loop).filter_map(|event| match event {
Ok(Event::Incoming(Packet::Publish(message))) => Some(Ok(message)),

View file

@ -13,11 +13,10 @@ pub enum Topic {
impl From<&str> for Topic {
fn from(raw: &str) -> Self {
let mut parts = raw.split('/');
if let (Some(prefix), Some(topic), Some(hostname), Some(cmd)) =
(parts.next(), parts.next(), parts.next(), parts.next())
if let (Some(prefix), Some(hostname), Some(cmd)) =
(parts.next(), parts.next(), parts.next())
{
let device = Device {
topic: topic.to_string(),
hostname: hostname.to_string(),
};
match (prefix, cmd) {