Files
KairoXYZ/xyz-engine/src/handler.rs
fdiniello 962b90e1b8 Refactor Models for InfluxDB (#1)
Update to InfluxDB 2.0 and update all interfaces to work with it.

MAC has been deprecated, since current `influxdb2` doesn't support non built-in types for read/write into the DB.

Co-authored-by: Felipe Diniello <felipediniello@pm.me>
Reviewed-on: #1
2023-06-18 18:43:15 +02:00

37 lines
1.3 KiB
Rust

pub mod device {
use kairo_common::{
influx::{self, Bucket},
unit_conversion::UnitsConversion,
BeaconMeasure, DeviceReport, MAC,
};
use crate::position_solver::solve_for;
pub async fn report(device_id: &str, payload: &str) {
if let Ok(device_report) = serde_json::from_str::<DeviceReport>(payload) {
// split the report into individual measures
let measures = device_report
.data
.iter()
.map(|f| BeaconMeasure::new(device_id, &f.beacon_id, f.rssi.dBm_to_W()))
// sort them as a vector of write queries
.collect::<Vec<BeaconMeasure>>();
let more_than_three = measures.len() >= 3;
let result = influx::Client::get()
.write(Bucket::Tmp, futures::stream::iter(measures))
.await;
// If I added more than 3 valid measures it's worth to process the position
if result.is_ok() && more_than_three {
let device_id = MAC::new(device_id);
tokio::spawn(async move {
let _r = solve_for(device_id).await;
});
}
} else {
println!("Unable to parse: {}", payload);
}
}
}