From 160535bbb35ce8358a56c54f876b53b42d30a793 Mon Sep 17 00:00:00 2001 From: Dmitriy Kovalenko Date: Sun, 14 Jan 2024 20:15:53 -0700 Subject: [PATCH] feat: Improve connecting experience (#21) Increate connection timeout Fail properly on discover services exception Add login to the file --- Cargo.lock | 118 ++++++++++++++++++++++++++++++++---------------- Cargo.toml | 3 +- src/cli_args.rs | 15 ++++-- src/main.rs | 13 ++++++ src/route.rs | 12 +++-- 5 files changed, 112 insertions(+), 49 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4ffee4..677159b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -109,9 +109,9 @@ checksum = "9c7d0618f0e0b7e8ff11427422b64564d5fb0be1940354bfe2e0529b18a9d9b8" [[package]] name = "async-trait" -version = "0.1.74" +version = "0.1.77" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a66537f1bb974b254c98ed142ff995236e81b9d0fe4db0575f46612cb15eb0f9" +checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" dependencies = [ "proc-macro2", "quote", @@ -183,6 +183,7 @@ dependencies = [ "tokio", "tracing", "tracing-appender", + "tracing-subscriber", "uuid", ] @@ -224,9 +225,9 @@ dependencies = [ [[package]] name = "btleplug" -version = "0.11.3" +version = "0.11.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0743700186f3d4c46f91b5ac9534e0c271a5de1c3ebd7dca1b5b4216132f72a0" +checksum = "ba345f9db94939c72959b2008abe1ffcdbcaa235243fd92ad12436532ff199cf" dependencies = [ "async-trait", "bitflags 2.4.1", @@ -675,9 +676,9 @@ checksum = "aa9a19cbb55df58761df49b23516a86d432839add4af60fc256da840f66ed35b" [[package]] name = "futures" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da0290714b38af9b4a7b094b8a37086d1b4e61f2df9122c3cad2577669145335" +checksum = "645c6916888f6cb6350d2550b80fb63e734897a8498abe35cfb732b6487804b0" dependencies = [ "futures-channel", "futures-core", @@ -690,9 +691,9 @@ dependencies = [ [[package]] name = "futures-channel" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ff4dd66668b557604244583e3e1e1eada8c5c2e96a6d0d6653ede395b78bbacb" +checksum = "eac8f7d7865dcb88bd4373ab671c8cf4508703796caa2b1985a9ca867b3fcb78" dependencies = [ "futures-core", "futures-sink", @@ -700,15 +701,15 @@ dependencies = [ [[package]] name = "futures-core" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "eb1d22c66e66d9d72e1758f0bd7d4fd0bee04cad842ee34587d68c07e45d088c" +checksum = "dfc6580bb841c5a68e9ef15c77ccc837b40a7504914d52e47b8b0e9bbda25a1d" [[package]] name = "futures-executor" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0f4fb8693db0cf099eadcca0efe2a5a22e4550f98ed16aba6c48700da29597bc" +checksum = "a576fc72ae164fca6b9db127eaa9a9dda0d61316034f33a0a0d4eda41f02b01d" dependencies = [ "futures-core", "futures-task", @@ -717,15 +718,15 @@ dependencies = [ [[package]] name = "futures-io" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8bf34a163b5c4c52d0478a4d757da8fb65cabef42ba90515efee0f6f9fa45aaa" +checksum = "a44623e20b9681a318efdd71c299b6b222ed6f231972bfe2f224ebad6311f0c1" [[package]] name = "futures-macro" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53b153fd91e4b0147f4aced87be237c98248656bb01050b96bf3ee89220a8ddb" +checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", @@ -734,21 +735,21 @@ dependencies = [ [[package]] name = "futures-sink" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e36d3378ee38c2a36ad710c5d30c2911d752cb941c00c72dbabfb786a7970817" +checksum = "9fb8e00e87438d937621c1c6269e53f536c14d3fbd6a042bb24879e57d474fb5" [[package]] name = "futures-task" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "efd193069b0ddadc69c46389b740bbccdd97203899b48d09c5f7969591d6bae2" +checksum = "38d84fa142264698cdce1a9f9172cf383a0c82de1bddcf3092901442c4097004" [[package]] name = "futures-util" -version = "0.3.29" +version = "0.3.30" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a19526d624e703a3179b3d322efec918b6246ea0fa51d41124525f00f1cc8104" +checksum = "3d6401deb83407ab3da39eba7e33987a73c3df0c82b4bb5813ee871c19c41d48" dependencies = [ "futures-channel", "futures-core", @@ -955,9 +956,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.150" +version = "0.2.152" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" +checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" [[package]] name = "libdbus-sys" @@ -1083,6 +1084,16 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-traits" version = "0.2.15" @@ -1142,9 +1153,15 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.18.0" +version = "1.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "owo-colors" @@ -1195,18 +1212,18 @@ checksum = "26072860ba924cbfa98ea39c8c19b4dd6a4a25423dbdf219c1eca91aa0cf6964" [[package]] name = "proc-macro2" -version = "1.0.66" +version = "1.0.76" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18fb31db3f9bddb2ea821cde30a9f70117e3f119938b5ee630b7403aa6e2ead9" +checksum = "95fc56cda0b5c3325f5fbbd7ff9fda9e02bb00bb3dac51252d2f1bfa1cb8cc8c" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.33" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -1445,9 +1462,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.29" +version = "2.0.48" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c324c494eba9d92503e6f1ef2e6df781e78f6a7705a0202d9801b198807d518a" +checksum = "0f3531638e407dfc0814761abb7c00a5b54992b849452a0646b7f65c9f770f3f" dependencies = [ "proc-macro2", "quote", @@ -1466,18 +1483,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.50" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a7210f5c9a7156bb50aa36aed4c95afb51df0df00713949448cf9e97d382d2" +checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.50" +version = "1.0.56" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "266b2e40bc00e5a6c09c3584011e08b06f123c00362c92b975ba9843aaaa14b8" +checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", @@ -1534,9 +1551,9 @@ dependencies = [ [[package]] name = "tokio" -version = "1.34.0" +version = "1.35.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d0c014766411e834f7af5b8f4cf46257aab4036ca95e9d2c144a10f59ad6f5b9" +checksum = "c89b4efa943be685f629b149f53829423f8f5531ea21249408e8e2f8671ec104" dependencies = [ "backtrace", "bytes", @@ -1629,17 +1646,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", ] [[package]] name = "tracing-subscriber" -version = "0.3.17" +version = "0.3.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" dependencies = [ + "nu-ansi-term", "sharded-slab", + "smallvec", "thread_local", "tracing-core", + "tracing-log", ] [[package]] @@ -1678,6 +1710,12 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "walkdir" version = "2.3.3" diff --git a/Cargo.toml b/Cargo.toml index 3adac2a..cf77806 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } @@ -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" diff --git a/src/cli_args.rs b/src/cli_args.rs index 574600d..7c90b1e 100644 --- a/src/cli_args.rs +++ b/src/cli_args.rs @@ -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(&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)] @@ -143,4 +144,10 @@ pub struct Args { #[clap(long)] #[arg(value_enum)] pub sort: Option, + + /// 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, } diff --git a/src/main.rs b/src/main.rs index 961d945..37d0b8c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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}; @@ -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), diff --git a/src/route.rs b/src/route.rs index 1618a02..fe11f2b 100644 --- a/src/route.rs +++ b/src/route.rs @@ -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."); } @@ -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,