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
This commit was merged in pull request #1.
This commit is contained in:
@@ -1,23 +1,29 @@
|
||||
pub mod device {
|
||||
use kairo_common::influxdb_models::BeaconMeasure;
|
||||
use kairo_common::{unit_conversion::UnitsConversion, DeviceReport, MAC};
|
||||
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) {
|
||||
// device_report.data.sort_by(|a, b| b.pwr.cmp(&a.pwr));
|
||||
// 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 mut count = 0;
|
||||
for beacon in device_report.data.iter() {
|
||||
let measure = BeaconMeasure::new(&beacon.beacon_id, beacon.rssi.dBm_to_W());
|
||||
if (measure.write_for(device_id).await).is_ok() {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
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 count >= 3 {
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user