Skip to content

Commit

Permalink
Add error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Robert-Steiner committed Mar 29, 2020
1 parent c0a8a51 commit 97e9518
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
11 changes: 11 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ ruuvi-sensor-protocol = "0.4.1"
structopt = { version = "0.3", features = [ "paw" ] }
paw = "1.0"
colored = "1.9"
failure = "0.1.7"
exitfailure = "0.5.1"

[[bin]]
name = "ruuvi-cli"
Expand Down
15 changes: 10 additions & 5 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use btleplug::api::{BDAddr, Central, ParseBDAddrError};
use exitfailure::ExitFailure;
use failure::ResultExt;
use ruuvitag_sensor_rs::ble::{get_central, register_event_handler};
use ruuvitag_sensor_rs::controller::Controller;
use std::str::FromStr;
Expand Down Expand Up @@ -42,21 +44,26 @@ enum Args {
}

#[paw::main]
fn main(args: Args) -> Result<(), std::io::Error> {
fn main(args: Args) -> Result<(), ExitFailure> {
let (controller, event_tx) = Controller::new();

let central = get_central();
let central =
get_central().with_context(|_| "could not initialize bluetooth adapter".to_string())?;

central.active(false);
register_event_handler(event_tx, &central);

central
.start_scan()
.with_context(|_| "could not start bluetooth scan".to_string())?;

match args {
Args::Collect {
ruuvitags_macs,
influxdb_url,
influxdb_db_name,
influxdb_measurement_name,
} => {
central.start_scan().unwrap();
controller.collect(
&ruuvitags_macs,
&influxdb_url,
Expand All @@ -65,11 +72,9 @@ fn main(args: Args) -> Result<(), std::io::Error> {
);
}
Args::Find {} => {
central.start_scan().unwrap();
controller.find();
}
Args::Show {} => {
central.start_scan().unwrap();
controller.show();
}
}
Expand Down
24 changes: 13 additions & 11 deletions src/ble.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
use crate::ruuvitag::{is_ruuvitag, RuuviTag};
use btleplug::api::{
Central,
CentralEvent::{DeviceDiscovered, DeviceUpdated},
Peripheral,
use btleplug::{
api::{
Central,
CentralEvent::{DeviceDiscovered, DeviceUpdated},
Peripheral,
},
bluez::{adapter::ConnectedAdapter, manager::Manager},
};
use btleplug::bluez::{adapter::ConnectedAdapter, manager::Manager};
use std::sync::mpsc::Sender;

pub fn get_central() -> ConnectedAdapter {
let manager = Manager::new().unwrap();
let adapters = manager.adapters().unwrap();
pub fn get_central() -> Result<ConnectedAdapter, btleplug::Error> {
let manager = Manager::new()?;
let adapters = manager.adapters()?;
let mut adapter = adapters.into_iter().next().unwrap();

// reset the adapter -- clears out any errant state
adapter = manager.down(&adapter).unwrap();
adapter = manager.up(&adapter).unwrap();
adapter = manager.down(&adapter)?;
adapter = manager.up(&adapter)?;

adapter.connect().unwrap()
Ok(adapter.connect()?)
}

pub enum Event {
Expand Down

0 comments on commit 97e9518

Please sign in to comment.