From eedf62e304dc7a6f2cf72177b3c26aded8d2ac67 Mon Sep 17 00:00:00 2001 From: Maxime Augier Date: Sat, 20 Apr 2024 12:29:55 +0200 Subject: [PATCH] Add clap parser to main command --- src/main.rs | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 51eecb2..d89ece3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ use std::{collections::HashMap, convert::Infallible, sync::Arc, time::{SystemTim use bluer::{self, Adapter, AdapterEvent, Address, DiscoveryFilter, DiscoveryTransport}; -use anyhow::Result; +use anyhow::{Result, anyhow}; +use clap::Parser; use futures::{Stream, StreamExt}; use tokio; use warp::Filter; @@ -70,15 +71,34 @@ impl DataStore { } +#[derive(Debug,clap::Parser)] +struct CLI { + /// Address to bind + #[arg(short, long, default_value = "127.0.0.1:9174")] + bind: String, + + /// BLE HCI adapter to use + #[arg(short, long)] + interface: Option, + +} + #[tokio::main(flavor="current_thread")] async fn main() -> Result<()> { + let args = CLI::parse(); + let session = bluer::Session::new().await?; - let adapter = Arc::new(session.default_adapter().await?); + let adapter = match &args.interface { + Some(hci) => session.adapter(&*hci), + None => session.default_adapter().await, + }?; + + let adapter = Arc::new(adapter); let store = Arc::new(Mutex::new(DataStore::default())); let mut stream = Box::pin(sensor_reports(adapter).await?); - + let sniffer_store = Arc::clone(&store); tokio::spawn(async move { while let Some(data) = stream.next().await { @@ -93,7 +113,10 @@ async fn main() -> Result<()> { .and_then(|store: Arc>| async move { Ok::<_,Infallible>(store.lock().await.scrape()) }) .or(warp::path!().map(|| { warp::reply::html("

Sensirion BLE exporter

") })); - Ok(warp::serve(filter).run(([127,0,0,1],9177)).await) + let host = tokio::net::lookup_host(&args.bind).await? + .next() + .ok_or(anyhow!("Cannot resolve host to bind {}", &args.bind))?; + Ok(warp::serve(filter).run(host).await) }