Make sure the setup is done before we start listening for events

This commit is contained in:
2023-01-10 01:02:04 +01:00
parent 9d6488183f
commit 5ee8eaf8fb
3 changed files with 57 additions and 32 deletions

View File

@@ -11,29 +11,43 @@ pub trait OnMqtt {
}
pub type Receiver = watch::Receiver<Option<Publish>>;
type Sender = watch::Sender<Option<Publish>>;
pub fn start(mut eventloop: EventLoop) -> Receiver {
let (tx, rx) = watch::channel(None);
tokio::spawn(async move {
debug!("Listening for MQTT events");
loop {
let notification = eventloop.poll().await;
match notification {
Ok(Event::Incoming(Incoming::Publish(p))) => {
tx.send(Some(p)).ok();
},
Ok(..) => continue,
Err(err) => {
error!("{}", err);
break
},
pub struct Mqtt {
tx: Sender,
eventloop: EventLoop,
}
impl Mqtt {
pub fn new(eventloop: EventLoop) -> Self {
let (tx, _rx) = watch::channel(None);
Self { tx, eventloop }
}
pub fn subscribe(&self) -> Receiver {
self.tx.subscribe()
}
pub fn start(mut self) {
tokio::spawn(async move {
debug!("Listening for MQTT events");
loop {
let notification = self.eventloop.poll().await;
match notification {
Ok(Event::Incoming(Incoming::Publish(p))) => {
self.tx.send(Some(p)).ok();
},
Ok(..) => continue,
Err(err) => {
error!("{}", err);
break
},
}
}
}
todo!("Error in MQTT (most likely lost connection to mqtt server), we need to handle these errors!");
});
return rx;
todo!("Error in MQTT (most likely lost connection to mqtt server), we need to handle these errors!");
});
}
}
#[derive(Debug, Serialize, Deserialize)]