connection error
This commit is contained in:
+28
-12
@@ -15,6 +15,7 @@ const METEO_MUNICH_API_URL: &str =
|
||||
const LATITUDE_MUC: f64 = 48.163142;
|
||||
const LONGITUDE_MUC: f64 = 11.542922;
|
||||
const PUBLISH_COUNT: i32 = 5;
|
||||
const MQTT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
async fn main() {
|
||||
@@ -26,21 +27,36 @@ async fn main() {
|
||||
task::spawn(async move {
|
||||
publish(client.clone(), &weather).await;
|
||||
});
|
||||
println!("test{}", 100);
|
||||
let mut count = 0;
|
||||
loop {
|
||||
let event = eventloop.poll().await;
|
||||
match &event {
|
||||
Ok(_) => {
|
||||
if count == PUBLISH_COUNT * 2 {
|
||||
break;
|
||||
println!("{}", 100);
|
||||
|
||||
let poll_result = time::timeout(MQTT_TIMEOUT, async {
|
||||
let mut count = 0;
|
||||
let mut err_count = 0;
|
||||
loop {
|
||||
let event = eventloop.poll().await;
|
||||
match &event {
|
||||
Ok(_) => {
|
||||
if count >= PUBLISH_COUNT * 2 {
|
||||
break;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
println!("MQTT Error = {e:?}");
|
||||
err_count += 1;
|
||||
if err_count >= 5 {
|
||||
eprintln!("Exiting MQTT loop after 5 consecutive errors.");
|
||||
break;
|
||||
}
|
||||
time::sleep(Duration::from_millis(200)).await;
|
||||
}
|
||||
count += 1;
|
||||
}
|
||||
Err(e) => {
|
||||
println!("Error = {e:?}");
|
||||
}
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
if poll_result.is_err() {
|
||||
eprintln!("Timed out waiting for MQTT publishing to complete.");
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
|
||||
Reference in New Issue
Block a user