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,10 +1,9 @@
|
||||
use chrono::{DateTime, Utc};
|
||||
use influxdb::InfluxDbWriteable;
|
||||
use kairo_common::helper::for_async::get_influx_cli;
|
||||
use serde::Serialize;
|
||||
use tokio::time;
|
||||
|
||||
use kairo_common::{influxdb_models::KnownPosition, Point};
|
||||
use kairo_common::Point;
|
||||
|
||||
use crate::Config;
|
||||
|
||||
@@ -14,7 +13,7 @@ pub struct Error {
|
||||
speed: f64,
|
||||
time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub async fn thread(config: Config) {
|
||||
let period = time::Duration::from_millis(500);
|
||||
|
||||
@@ -23,37 +22,37 @@ pub async fn thread(config: Config) {
|
||||
position.rotate_by(f64::to_radians(config.angle_step));
|
||||
speed -= position;
|
||||
|
||||
let speed = speed.module();
|
||||
let _speed = speed.module();
|
||||
|
||||
loop {
|
||||
let start = time::Instant::now();
|
||||
|
||||
let real = KnownPosition::get_last_for("real", 1).await;
|
||||
let calc = KnownPosition::get_last_for(config.id.as_str(), 1).await;
|
||||
if real.is_ok() && calc.is_ok() {
|
||||
let real = real.unwrap();
|
||||
let calc = calc.unwrap();
|
||||
// let real = KnownPosition::get_last_for("real", 1).await;
|
||||
// let calc = KnownPosition::get_last_for(config.id.as_str(), 1).await;
|
||||
// if real.is_ok() && calc.is_ok() {
|
||||
// let real = real.unwrap();
|
||||
// let calc = calc.unwrap();
|
||||
|
||||
if real.is_some() && calc.is_some() {
|
||||
let real = real.unwrap();
|
||||
let calc = calc.unwrap();
|
||||
#[allow(non_snake_case)]
|
||||
let Δx = real.x - calc.x;
|
||||
#[allow(non_snake_case)]
|
||||
let Δy = real.y - calc.y;
|
||||
let error = Error {
|
||||
speed,
|
||||
error: f64::sqrt(Δx.powi(2) + Δy.powi(2)),
|
||||
time: chrono::Utc::now(),
|
||||
};
|
||||
// if real.is_some() && calc.is_some() {
|
||||
// let real = real.unwrap();
|
||||
// let calc = calc.unwrap();
|
||||
// #[allow(non_snake_case)]
|
||||
// let Δx = real.x - calc.x;
|
||||
// #[allow(non_snake_case)]
|
||||
// let Δy = real.y - calc.y;
|
||||
// let error = Error {
|
||||
// speed,
|
||||
// error: f64::sqrt(Δx.powi(2) + Δy.powi(2)),
|
||||
// time: chrono::Utc::now(),
|
||||
// };
|
||||
|
||||
let table_name = format!("error_{}", config.id.as_str());
|
||||
get_influx_cli()
|
||||
.query(error.into_query(table_name.as_str()))
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
time::sleep(period - start.elapsed()).await;
|
||||
}
|
||||
// let table_name = format!("error_{}", config.id.as_str());
|
||||
// get_influx_cli()
|
||||
// .query(error.into_query(table_name.as_str()))
|
||||
// .await
|
||||
// .unwrap();
|
||||
// }
|
||||
time::sleep(period - start.elapsed()).await;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,7 @@ use std::{thread, time};
|
||||
mod error_report;
|
||||
|
||||
use kairo_common::helper::for_sync::{get_mqtt_cli, mqtt_pub};
|
||||
use kairo_common::{
|
||||
influxdb_models::{BeaconMeasure, KnownPosition},
|
||||
Antenna, DeviceReport, Point,
|
||||
};
|
||||
use kairo_common::{Antenna, BeaconMeasure, DeviceReport, Point};
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Config {
|
||||
@@ -25,12 +22,12 @@ async fn main() {
|
||||
let period = time::Duration::from_millis(config.period_ms);
|
||||
let noise_gen = Normal::new(0.0, config.noise_level).unwrap();
|
||||
|
||||
if config.real {
|
||||
let config = config.clone();
|
||||
tokio::spawn(async move {
|
||||
error_report::thread(config).await;
|
||||
});
|
||||
}
|
||||
// if config.real {
|
||||
// let config = config.clone();
|
||||
// tokio::spawn(async move {
|
||||
// error_report::thread(config).await;
|
||||
// });
|
||||
// }
|
||||
|
||||
let client = get_mqtt_cli();
|
||||
|
||||
@@ -55,14 +52,16 @@ async fn main() {
|
||||
|
||||
let noise: f64 = noise_gen.sample(&mut rand::thread_rng());
|
||||
|
||||
report.data.push(BeaconMeasure::new(&ant.id, rssi + noise));
|
||||
report
|
||||
.data
|
||||
.push(BeaconMeasure::new(&config.id, &ant.id, rssi + noise));
|
||||
}
|
||||
let payload = serde_json::to_string(&report).unwrap_or_else(|_| "".to_string());
|
||||
mqtt_pub(&client, topic.as_str(), payload.as_str()).expect("Pub error");
|
||||
|
||||
if config.real {
|
||||
let _r = KnownPosition::new(position).write_for("real").await;
|
||||
}
|
||||
// if config.real {
|
||||
// let _r = KnownPosition::new(position).write_for("real").await;
|
||||
// }
|
||||
|
||||
position.rotate_by(f64::to_radians(config.angle_step));
|
||||
thread::sleep(period - start.elapsed());
|
||||
|
||||
Reference in New Issue
Block a user