diff --git a/src/main.rs b/src/main.rs index f18761c..bb8a16e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,8 @@ -use std::{collections::HashMap, sync::Arc, time::{SystemTime, UNIX_EPOCH}}; +use std::{collections::HashMap, convert::Infallible, sync::Arc, time::{SystemTime, UNIX_EPOCH}}; use bluer::{self, Adapter, AdapterEvent, Address}; -use anyhow::{Result, Context}; +use anyhow::Result; use futures::{Stream, StreamExt}; use tokio; use warp::Filter; @@ -61,8 +61,8 @@ impl DataStore { let Data { addr, temperature, humidity, co2 } = data; let time = point.duration_since(UNIX_EPOCH).expect("Time went backwards").as_millis(); scrape += &format!("sensirion_temperature_celsius{{id=\"{addr}\"}} {temperature} {time}\n"); - scrape += &format!("sensirion_humidity_percent{{id=\"{addr}\"}} {temperature} {time}\n"); - scrape += &format!("sensirion_co2_ppm{{id=\"{addr}\"}} {temperature} {time}\n"); + scrape += &format!("sensirion_humidity_percent{{id=\"{addr}\"}} {humidity} {time}\n"); + scrape += &format!("sensirion_co2_ppm{{id=\"{addr}\"}} {co2} {time}\n"); } scrape @@ -79,19 +79,21 @@ async fn main() -> Result<()> { let mut stream = Box::pin(sensor_reports(adapter).await?); - let (tx, rx)= tokio::sync::mpsc::channel(256); - - let sniffer: tokio::task::JoinHandle<()> = tokio::spawn(async move { + let sniffer_store = Arc::clone(&store); + tokio::spawn(async move { while let Some(data) = stream.next().await { - tx.send(data).await; + eprintln!("Data point: {:?}", data); + sniffer_store.lock().await.insert(data, SystemTime::now()); } }); let filter = - warp::path!("metrics").map(move || store.lock().unwrap().scrape()) - .or(warp::path!().map(move || { warp::reply::html("

Sensirion BLE exporter

") })); + warp::path!("metrics") + .map(move || Arc::clone(&store)) + .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) + Ok(warp::serve(filter).run(([127,0,0,1],9177)).await) }