From 222f1f93e84a149949ffaf5727958357b1d7df27 Mon Sep 17 00:00:00 2001 From: Serguey Parkhomovsky Date: Sat, 21 Mar 2026 00:09:46 -0700 Subject: Subscribe and publish in the connack branch, in case we lose connection. --- src/main.rs | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 410c651..3d019a7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -102,32 +102,33 @@ async fn poll_bambu( mqttoptions.set_transport(Transport::tls_with_config(connector.into())); let (client, mut eventloop) = AsyncClient::new(mqttoptions, 10); - client - .subscribe(format!("device/{}/report", serial_number), QoS::AtMostOnce) - .await - .unwrap(); - - // Send an initial request to push all values to us; otherwise we'll have to wait for the values to come in incremental updates. - client - .publish( - format!("device/{}/request", serial_number), - QoS::AtMostOnce, - false, - r#"{ - "pushing": { - "sequence_id": "0", - "command": "pushall", - "version": 1, - "push_target": 1 - } - }"#, - ) - .await - .unwrap(); loop { // eventloop.poll() yields back to Tokio when there's no data match eventloop.poll().await { + Ok(Event::Incoming(Packet::ConnAck(_))) => { + client + .subscribe(format!("device/{}/report", serial_number), QoS::AtMostOnce) + .await + .unwrap(); + // Send a request to push all values to us; otherwise we'll have to wait for the values to come in incremental updates. + client + .publish( + format!("device/{}/request", serial_number), + QoS::AtMostOnce, + false, + r#"{ + "pushing": { + "sequence_id": "0", + "command": "pushall", + "version": 1, + "push_target": 1 + } + }"#, + ) + .await + .unwrap(); + } Ok(Event::Incoming(Packet::Publish(p))) => { tracing::debug!(payload = ?p.payload, "Received Bambu payload"); match serde_json::from_slice::(&p.payload) { -- cgit v1.2.3