Add clap parser to main command

This commit is contained in:
Maxime Augier 2024-04-20 12:29:55 +02:00
parent 556599609a
commit eedf62e304

View File

@ -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<String>,
}
#[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<Mutex<DataStore>>| async move { Ok::<_,Infallible>(store.lock().await.scrape()) })
.or(warp::path!().map(|| { warp::reply::html("<h1>Sensirion BLE exporter</h1>") }));
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)
}