Use broadcast for mqtt message so we can have a queue

This commit is contained in:
2023-01-10 22:59:26 +01:00
parent 5ee8eaf8fb
commit e9d1cf554d
5 changed files with 19 additions and 39 deletions

View File

@@ -3,15 +3,15 @@ use serde::{Serialize, Deserialize};
use tracing::{error, debug};
use rumqttc::{Publish, Event, Incoming, EventLoop};
use tokio::sync::watch;
use tokio::sync::broadcast;
#[async_trait]
pub trait OnMqtt {
async fn on_mqtt(&mut self, message: &Publish);
}
pub type Receiver = watch::Receiver<Option<Publish>>;
type Sender = watch::Sender<Option<Publish>>;
pub type Receiver = broadcast::Receiver<Publish>;
type Sender = broadcast::Sender<Publish>;
pub struct Mqtt {
tx: Sender,
@@ -20,7 +20,7 @@ pub struct Mqtt {
impl Mqtt {
pub fn new(eventloop: EventLoop) -> Self {
let (tx, _rx) = watch::channel(None);
let (tx, _rx) = broadcast::channel(100);
Self { tx, eventloop }
}
@@ -35,7 +35,7 @@ impl Mqtt {
let notification = self.eventloop.poll().await;
match notification {
Ok(Event::Incoming(Incoming::Publish(p))) => {
self.tx.send(Some(p)).ok();
self.tx.send(p).ok();
},
Ok(..) => continue,
Err(err) => {