From 5db6276a2a5b4e18bcf9ad8e58231b74143426fb Mon Sep 17 00:00:00 2001 From: nullstalgia Date: Sat, 19 Oct 2024 18:24:22 -0700 Subject: [PATCH] Simplify update prompt logic, Trigger BLE Scan on connect error --- .cargo/config.toml | 3 +++ Cargo.toml | 4 ---- src/app.rs | 17 ++++------------- src/heart_rate/ble.rs | 23 +++++++++++++++-------- src/scan.rs | 9 +-------- src/updates/mod.rs | 3 ++- 6 files changed, 25 insertions(+), 34 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..be5ba26 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,3 @@ +# # For console-subscriber +# [build] +# rustflags = ["--cfg", "tokio_unstable"] diff --git a/Cargo.toml b/Cargo.toml index 981b0e8..4c63384 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -92,7 +92,3 @@ test-log = { version = "0.2.16", default-features = false, features = [ "trace", "unstable", ] } - -# For console-subscriber -# [build] -# rustflags = ["--cfg", "tokio_unstable"] diff --git a/src/app.rs b/src/app.rs index fde8f6c..b272de2 100644 --- a/src/app.rs +++ b/src/app.rs @@ -487,25 +487,16 @@ impl App { } pub fn auto_update_prompt(&mut self) { - // Check if we've allowed checking for updates - // If we have, or just did, spawn the update checking task - // It'll have a oneshot it can send a new version to ask for confirmation for + self.sub_state = SubState::None; - // In case the user manually set updates true without also changing prompt + // In case the user set updates true externally without also changing prompt if self.settings.updates.allow_checking_for_updates { - self.sub_state = SubState::None; self.spawn_update_check(); - return; - } - - // If we haven't asked the user yet, do that first. - if self.settings.updates.update_check_prompt { + } else if self.settings.updates.update_check_prompt { + // But if we haven't asked the user yet, do that first. self.prompt_state.select(Some(0)); self.sub_state = SubState::UpdateAllowCheckPrompt; - return; } - - self.sub_state = SubState::None; } fn spawn_update_check(&mut self) { diff --git a/src/heart_rate/ble.rs b/src/heart_rate/ble.rs index a724218..6c150a6 100644 --- a/src/heart_rate/ble.rs +++ b/src/heart_rate/ble.rs @@ -107,25 +107,32 @@ impl BleMonitorActor { info!("Heart Rate Monitor stream closed!"); device.disconnect().await?; + if self.cancel_token.is_cancelled() { + break 'connection; + } broadcast!(broadcast_tx, ErrorPopup::Intermittent( "Connection timed out".into(), )); } Err(e) => { + device.disconnect().await?; + error!("BLE Connection error: {}", e); broadcast!(broadcast_tx, ErrorPopup::Intermittent(format!( "BLE Connection error: {}", e ))); - // This is the "Device Unreachable" error - // Weirdly enough, the Central manager doesn't get this error, only we do here at the HR level - // So, we'll just restart the BLE manager to try to avoid continuous failed reconnects - // And wait a moment for the manager to get it's bearings. - if let btleplug::Error::NotConnected = e { - device.disconnect().await?; - restart_tx.send(()).await.expect("Couldn't restart BLE Manager!"); - tokio::time::sleep(Duration::from_secs(20)).await; + // `NotConnected` is the "Device Unreachable" error + // Weirdly enough, the Central manager doesn't get that error, only we do here at the HR level + // `DeviceNotFound` can also occur when being disconnected for a long time + // Telling the manager to restart its scan when these crop up help avoid needing to restart the whole app + match e { + btleplug::Error::NotConnected | btleplug::Error::DeviceNotFound => { + restart_tx.send(()).await.expect("Couldn't restart BLE Manager!"); + }, + _ => {} } + tokio::time::sleep(Duration::from_secs(20)).await; } } } diff --git a/src/scan.rs b/src/scan.rs index db4bd25..67e7739 100644 --- a/src/scan.rs +++ b/src/scan.rs @@ -126,6 +126,7 @@ pub async fn bluetooth_event_thread( tokio::select! { Some(event) = events.next() => { + // debug!("{:?}", event); match event { CentralEvent::DeviceDiscovered(id) | CentralEvent::DeviceUpdated(id) => { if let Ok(device) = central.peripheral(&id).await { @@ -184,20 +185,12 @@ pub async fn bluetooth_event_thread( debug!("CentralEvent timeout"); if !pause_signal.load(Ordering::SeqCst) { warn!("Restarting scan!"); - if scanning { - let _ = central.stop_scan().await; - scanning = false; - } } } Some(()) = restart_signal.recv() => { warn!("Got signal to restart scan from HR Notif thread!"); // debug!("Central State was: {central:#?}"); pause_signal.store(false, Ordering::SeqCst); - if scanning { - let _ = central.stop_scan().await; - scanning = false; - } } } } diff --git a/src/updates/mod.rs b/src/updates/mod.rs index f86f03e..b3944fa 100644 --- a/src/updates/mod.rs +++ b/src/updates/mod.rs @@ -272,9 +272,11 @@ impl UpdateBackend { } Ok(Err(err)) => { error!("Error getting latest release: {}", err); + self.command_rx.close(); } Err(err) => { error!("Error joining get_latest_release: {}", err); + self.command_rx.close(); } } } @@ -433,7 +435,6 @@ impl App { self.settings.updates.allow_checking_for_updates = true; self.settings.updates.update_check_prompt = false; self.try_save_settings(); - self.sub_state = SubState::None; self.auto_update_prompt(); } }