mirror of
https://github.com/csehviktor/status-monitor.git
synced 2026-04-29 00:27:35 +02:00
cleaup code
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
use rumqttd::{local::LinkRx, Broker, Notification};
|
||||
use common::{StatusMessage, MQTT_TOPIC};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::bridge::ClientManager;
|
||||
|
||||
pub struct MqttSubscriber {
|
||||
link_rx: LinkRx,
|
||||
clients: Arc<ClientManager>,
|
||||
}
|
||||
|
||||
impl MqttSubscriber {
|
||||
pub fn new(broker: &Broker, clients: Arc<ClientManager>) -> Self {
|
||||
let (mut link_tx, link_rx) = broker.link("internal-subscriber").unwrap();
|
||||
link_tx.subscribe(MQTT_TOPIC).unwrap();
|
||||
|
||||
Self {
|
||||
link_rx,
|
||||
clients,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn run(&mut self) -> anyhow::Result<()> {
|
||||
while let Ok(notification) = self.link_rx.next().await {
|
||||
if let Notification::Forward(forward) = notification.unwrap() {
|
||||
let payload = StatusMessage::try_from(&forward.publish.payload[..])?;
|
||||
|
||||
if let Ok(payload_str) = payload.to_string() {
|
||||
self.clients.broadcast(payload_str).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user