Skip to content

Commit

Permalink
Simplify update prompt logic, Trigger BLE Scan on connect error
Browse files Browse the repository at this point in the history
  • Loading branch information
nullstalgia committed Oct 20, 2024
1 parent 136e541 commit 5db6276
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 34 deletions.
3 changes: 3 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# # For console-subscriber
# [build]
# rustflags = ["--cfg", "tokio_unstable"]
4 changes: 0 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,3 @@ test-log = { version = "0.2.16", default-features = false, features = [
"trace",
"unstable",
] }

# For console-subscriber
# [build]
# rustflags = ["--cfg", "tokio_unstable"]
17 changes: 4 additions & 13 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
23 changes: 15 additions & 8 deletions src/heart_rate/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
Expand Down
9 changes: 1 addition & 8 deletions src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/updates/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -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();
}
}
Expand Down

0 comments on commit 5db6276

Please sign in to comment.