Skip to content

Commit

Permalink
feat: Improve connecting experience (#21)
Browse files Browse the repository at this point in the history
Increate connection timeout
Fail properly on discover services exception
Add login to the file
  • Loading branch information
dmtrKovalenko authored Jan 15, 2024
1 parent 1198720 commit 160535b
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 49 deletions.
118 changes: 78 additions & 40 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ doc = false

[dependencies]
ansi-to-tui = "3.0.0"
btleplug = "0.11.3"
btleplug = "0.11.5"
byteorder = "1.4.3"
chrono = "0.4.26"
clap = { version = "4.3.4", features = ["derive"] }
Expand All @@ -27,6 +27,7 @@ regex = "1.8.4"
tokio = { version = "1.28", features = ["full", "tracing"] }
tracing = "0.1.37"
tracing-appender = "0.2.2"
tracing-subscriber = "0.3.18"
tui = { package = "ratatui", version = "0.21.0" }
uuid = "1.3.4"

Expand Down
15 changes: 11 additions & 4 deletions src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,11 @@ pub trait GeneralSortable {
fn cmp(&self, sort: &GeneralSort, a: &Self, b: &Self) -> std::cmp::Ordering;
}

impl GeneralSort {
pub fn apply_sort<T: GeneralSortable>(&self, a: &T, b: &T) -> std::cmp::Ordering {
a.cmp(self, a, b)
}
#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, clap::ValueEnum)]
pub enum LogLevel {
Debug,
#[default]
Error,
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -143,4 +144,10 @@ pub struct Args {
#[clap(long)]
#[arg(value_enum)]
pub sort: Option<GeneralSort>,

/// Log level for the CLI.
/// Logs are located at the $TMPDIR/blendr/*-cli.log and rotated daily.
#[clap(long)]
#[arg(value_enum)]
pub log_level: Option<LogLevel>,
}
13 changes: 13 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use btleplug::platform::Manager;
use clap::Parser;
use cli_args::Args;
use general_options::GeneralOptions;
use std::env;
use std::sync::RwLock;
use std::sync::{Arc, Mutex, RwLockReadGuard};

Expand Down Expand Up @@ -39,6 +40,18 @@ impl Ctx {
#[tokio::main]
async fn main() {
let args = Args::parse();
let file_appender = tracing_appender::rolling::daily(env::temp_dir().join("blendr"), "cli.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);

tracing_subscriber::fmt()
.with_writer(non_blocking)
.with_max_level(match args.log_level.unwrap_or_default() {
cli_args::LogLevel::Debug => tracing::Level::DEBUG,
cli_args::LogLevel::Error => tracing::Level::ERROR,
})
.pretty()
.init();

let ctx = Arc::new(Ctx {
latest_scan: RwLock::new(None),
active_route: RwLock::new(route::Route::PeripheralList),
Expand Down
12 changes: 8 additions & 4 deletions src/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ impl Route {
) -> error::Result<()> {
match (previous, self) {
(Route::PeripheralList, Route::PeripheralWaitingView { peripheral, retry }) => {
while peripheral
while !peripheral
.ble_peripheral
.is_connected()
.await
.map(|c| !c)
.unwrap_or(true)
.unwrap_or(false)
{
tracing::debug!("Connecting to peripheral.");
if let Err(e) =
timeout(Duration::from_secs(2), peripheral.ble_peripheral.connect()).await
timeout(Duration::from_secs(10), peripheral.ble_peripheral.connect()).await
{
tracing::error!(?e, "Failed to connect to peripheral.");
}
Expand Down Expand Up @@ -129,6 +129,10 @@ impl Route {
.read(&characteristic.ble_characteristic)
.await
{
if !ble_peripheral.is_connected().await.unwrap_or(false) {
break;
}

history.write().unwrap().push(CharacteristicValue {
time: chrono::Local::now(),
data,
Expand Down

0 comments on commit 160535b

Please sign in to comment.