From 3389ca46c3b64f59577f156ee18d3653cc77b577 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=B3r=C3=A1nt=20Pint=C3=A9r?= Date: Fri, 9 Aug 2024 18:07:31 +0200 Subject: [PATCH] Wait for MQTT connection --- src/kernel/mod.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/kernel/mod.rs b/src/kernel/mod.rs index c059b9b..10eeb71 100644 --- a/src/kernel/mod.rs +++ b/src/kernel/mod.rs @@ -83,10 +83,12 @@ impl Device { #[allow(clippy::arc_with_non_send_sync)] let conn = Arc::new(Mutex::new(conn)); + let connected = Arc::new(Signal::::new()); Spawner::for_current_executor() .await - .spawn(handle_mqtt_events(conn.clone())) + .spawn(handle_mqtt_events(conn.clone(), connected.clone())) .expect("Couldn't spawn MQTT handler"); + connected.wait().await; Ok(Self { config, @@ -182,7 +184,10 @@ async fn init_mqtt( } #[embassy_executor::task] -async fn handle_mqtt_events(conn: Arc>) { +async fn handle_mqtt_events( + conn: Arc>, + connected: Arc>, +) { let mut conn = conn.lock().await; loop { let event = conn.next().await.expect("Cannot receive message"); @@ -201,6 +206,13 @@ async fn handle_mqtt_events(conn: Arc { + log::info!( + "Connected to MQTT broker (session present: {})", + session_present + ); + connected.signal(()); + } EventPayload::Disconnected => { log::info!("Disconnected from MQTT broker"); // TODO Reconnect