Compare commits
5 Commits
refactor
...
cb7b62a092
| Author | SHA1 | Date | |
|---|---|---|---|
| cb7b62a092 | |||
| 0ddbb21f52 | |||
| 9fd99acf2e | |||
| fff33c1e21 | |||
| 01a291f738 |
@@ -34,6 +34,7 @@ mod models {
|
|||||||
pub type DeviceReport = models::DeviceReport;
|
pub type DeviceReport = models::DeviceReport;
|
||||||
|
|
||||||
pub type Antenna = models::antenna::Antenna;
|
pub type Antenna = models::antenna::Antenna;
|
||||||
|
pub type NewAntenna<'a> = models::antenna::NewAntenna<'a>;
|
||||||
|
|
||||||
pub type KnownPosition = models::known_position::KnownPosition;
|
pub type KnownPosition = models::known_position::KnownPosition;
|
||||||
pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus;
|
pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus;
|
||||||
|
|||||||
@@ -3,16 +3,7 @@ use std::f64::consts::PI;
|
|||||||
|
|
||||||
use crate::{unit_conversion::UnitsConversion, Point};
|
use crate::{unit_conversion::UnitsConversion, Point};
|
||||||
|
|
||||||
#[derive(
|
#[derive(Debug, Clone, Default, Queryable, Selectable, serde::Serialize, serde::Deserialize)]
|
||||||
Debug,
|
|
||||||
Clone,
|
|
||||||
Queryable,
|
|
||||||
Selectable,
|
|
||||||
Insertable,
|
|
||||||
AsChangeset,
|
|
||||||
serde::Serialize,
|
|
||||||
serde::Deserialize,
|
|
||||||
)]
|
|
||||||
#[diesel(check_for_backend(diesel::pg::Pg))]
|
#[diesel(check_for_backend(diesel::pg::Pg))]
|
||||||
#[diesel(table_name = crate::schema::antennas)]
|
#[diesel(table_name = crate::schema::antennas)]
|
||||||
pub struct Antenna {
|
pub struct Antenna {
|
||||||
@@ -24,6 +15,17 @@ pub struct Antenna {
|
|||||||
pub comment: Option<String>,
|
pub comment: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, Insertable, AsChangeset, serde::Deserialize)]
|
||||||
|
#[diesel(table_name = crate::schema::antennas)]
|
||||||
|
pub struct NewAntenna<'a> {
|
||||||
|
pub id: &'a str,
|
||||||
|
pub tssi: f64,
|
||||||
|
pub pos_x: f64,
|
||||||
|
pub pos_y: f64,
|
||||||
|
pub pos_z: f64,
|
||||||
|
pub comment: Option<&'a str>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Antenna {
|
impl Antenna {
|
||||||
const C: f64 = 2.99e8;
|
const C: f64 = 2.99e8;
|
||||||
const F: f64 = 2.4e9;
|
const F: f64 = 2.4e9;
|
||||||
|
|||||||
@@ -62,51 +62,39 @@ pub mod for_sync {
|
|||||||
use paho_mqtt as mqtt;
|
use paho_mqtt as mqtt;
|
||||||
use std::{process, time::Duration};
|
use std::{process, time::Duration};
|
||||||
|
|
||||||
pub struct MqttClient {
|
pub fn get_mqtt_cli() -> mqtt::Client {
|
||||||
cli: mqtt::Client,
|
let host = dotenv::var("MQTT_BROKER").unwrap_or_else(|_| {
|
||||||
}
|
|
||||||
|
|
||||||
impl MqttClient {
|
|
||||||
pub fn new(client_id: Option<&str>) -> MqttClient {
|
|
||||||
let host = dotenv::var("MQTT_BROKER").unwrap_or_else(|_| {
|
|
||||||
println! {"MQTT_BROKER not found in .evn file, using default: tcp://localhost:1883"};
|
println! {"MQTT_BROKER not found in .evn file, using default: tcp://localhost:1883"};
|
||||||
"tcp://localhost:1883".to_string()
|
"tcp://localhost:1883".to_string()
|
||||||
});
|
});
|
||||||
|
|
||||||
let mut cli = if let Some(client_id) = client_id {
|
let mut cli = mqtt::Client::new(host).unwrap_or_else(|e| {
|
||||||
mqtt::Client::new((host, String::from(client_id))).unwrap_or_else(|e| {
|
println!("Error creating the client: {:?}", e);
|
||||||
println!("Error creating the client: {:?}", e);
|
process::exit(1);
|
||||||
process::exit(1);
|
});
|
||||||
})
|
|
||||||
} else {
|
|
||||||
mqtt::Client::new(host).unwrap_or_else(|e| {
|
|
||||||
println!("Error creating the client: {:?}", e);
|
|
||||||
process::exit(1);
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
// Use 5sec timeouts for sync calls.
|
// Use 5sec timeouts for sync calls.
|
||||||
cli.set_timeout(Duration::from_secs(5));
|
cli.set_timeout(Duration::from_secs(5));
|
||||||
|
|
||||||
// Connect and wait for it to complete or fail
|
// Connect and wait for it to complete or fail
|
||||||
if let Err(e) = cli.connect(None) {
|
if let Err(e) = cli.connect(None) {
|
||||||
println!("Unable to connect: {:?}", e);
|
println!("Unable to connect: {:?}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
|
||||||
|
|
||||||
MqttClient { cli }
|
|
||||||
}
|
|
||||||
pub fn publish(&self, topic: &str, payload: Option<&str>) -> Result<(), paho_mqtt::Error> {
|
|
||||||
let msg = if let Some(payload) = payload {
|
|
||||||
mqtt::MessageBuilder::new()
|
|
||||||
.topic(topic)
|
|
||||||
.qos(0)
|
|
||||||
.payload(payload)
|
|
||||||
.finalize()
|
|
||||||
} else {
|
|
||||||
mqtt::MessageBuilder::new().topic(topic).qos(0).finalize()
|
|
||||||
};
|
|
||||||
self.cli.publish(msg)
|
|
||||||
}
|
}
|
||||||
|
cli
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn mqtt_pub(
|
||||||
|
client: &mqtt::Client,
|
||||||
|
topic: &str,
|
||||||
|
payload: &str,
|
||||||
|
) -> Result<(), paho_mqtt::Error> {
|
||||||
|
let msg = mqtt::MessageBuilder::new()
|
||||||
|
.topic(topic)
|
||||||
|
.payload(payload)
|
||||||
|
.qos(0)
|
||||||
|
.finalize();
|
||||||
|
|
||||||
|
client.publish(msg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,19 +10,15 @@ pub trait UnitsConversion {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl UnitsConversion for f64 {
|
impl UnitsConversion for f64 {
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn dBm_to_W(&self) -> f64 {
|
fn dBm_to_W(&self) -> f64 {
|
||||||
10.0_f64.powf((self - 30.0) / 10.0)
|
10.0_f64.powf((self - 30.0) / 10.0)
|
||||||
}
|
}
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn W_to_dBm(&self) -> f64 {
|
fn W_to_dBm(&self) -> f64 {
|
||||||
30.0 + 10.0 * f64::log10(*self)
|
30.0 + 10.0 * f64::log10(*self)
|
||||||
}
|
}
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn from_dB(&self) -> f64 {
|
fn from_dB(&self) -> f64 {
|
||||||
10.0_f64.powf((*self) / 10.0)
|
10.0_f64.powf((*self) / 10.0)
|
||||||
}
|
}
|
||||||
#[allow(non_snake_case)]
|
|
||||||
fn to_dB(&self) -> f64 {
|
fn to_dB(&self) -> f64 {
|
||||||
10.0 * f64::log10(*self)
|
10.0 * f64::log10(*self)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,32 +1,10 @@
|
|||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use rocket::{http::Status, serde::json::Json, Route, State};
|
use rocket::{http::Status, serde::json::Json, State};
|
||||||
|
|
||||||
use kairo_common::{postgres, schema::antennas, Antenna};
|
use kairo_common::{postgres, schema::antennas, Antenna, NewAntenna};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
pub fn routes() -> Vec<Route> {
|
|
||||||
routes![get_by_id, get_list, new, update, delete, purge]
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(not(debug_assertions))]
|
|
||||||
pub fn routes() -> Vec<Route> {
|
|
||||||
routes![get_by_id, get_list, new, update, delete]
|
|
||||||
}
|
|
||||||
|
|
||||||
fn notify_changes() {
|
|
||||||
use kairo_common::mqtt::for_sync::MqttClient;
|
|
||||||
|
|
||||||
MqttClient::new(Some("antennas_mqtt_client"))
|
|
||||||
.publish("antennas/refresh", None)
|
|
||||||
.unwrap_or_else(|e| {
|
|
||||||
println!("Unable to publish antennas update: {}", e);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rocket::get("/id/<id>")]
|
#[rocket::get("/id/<id>")]
|
||||||
fn get_by_id(db_pool: &State<postgres::DbPool>, id: String) -> Option<Json<Antenna>> {
|
pub fn get_by_id(db_pool: &State<postgres::DbPool>, id: String) -> Option<Json<Antenna>> {
|
||||||
let mut db = db_pool.get().unwrap();
|
let mut db = db_pool.get().unwrap();
|
||||||
|
|
||||||
let res = antennas::table
|
let res = antennas::table
|
||||||
@@ -41,7 +19,7 @@ fn get_by_id(db_pool: &State<postgres::DbPool>, id: String) -> Option<Json<Anten
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::get("/")]
|
#[rocket::get("/")]
|
||||||
fn get_list(db_pool: &State<postgres::DbPool>) -> Json<Vec<Antenna>> {
|
pub fn get_list(db_pool: &State<postgres::DbPool>) -> Json<Vec<Antenna>> {
|
||||||
let mut db = db_pool.get().unwrap();
|
let mut db = db_pool.get().unwrap();
|
||||||
|
|
||||||
let res = antennas::table
|
let res = antennas::table
|
||||||
@@ -55,70 +33,31 @@ fn get_list(db_pool: &State<postgres::DbPool>) -> Json<Vec<Antenna>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::post("/new", format = "json", data = "<antenna>")]
|
#[rocket::post("/new", format = "json", data = "<antenna>")]
|
||||||
fn new(db_pool: &State<postgres::DbPool>, antenna: Json<Antenna>) -> Status {
|
pub fn new(db_pool: &State<postgres::DbPool>, antenna: Json<NewAntenna<'_>>) -> Status {
|
||||||
let mut db = db_pool.get().unwrap();
|
let mut db = db_pool.get().unwrap();
|
||||||
|
|
||||||
let res = diesel::insert_into(antennas::table)
|
let res = diesel::insert_into(antennas::table)
|
||||||
.values(antenna.0)
|
.values(*antenna)
|
||||||
.execute(&mut db);
|
.execute(&mut db);
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(_) => {
|
Ok(_) => Status::Ok,
|
||||||
notify_changes();
|
|
||||||
Status::Ok
|
|
||||||
}
|
|
||||||
_ => Status::NotAcceptable,
|
_ => Status::NotAcceptable,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::patch("/update", format = "json", data = "<antenna>")]
|
#[rocket::patch("/update", format = "json", data = "<antenna>")]
|
||||||
fn update(db_pool: &State<postgres::DbPool>, antenna: Json<Antenna>) -> Status {
|
pub fn update(db_pool: &State<postgres::DbPool>, antenna: Json<NewAntenna<'_>>) -> Status {
|
||||||
let mut db = db_pool.get().unwrap();
|
let mut db = db_pool.get().unwrap();
|
||||||
|
|
||||||
let res = diesel::update(antennas::table)
|
let res = diesel::update(antennas::table)
|
||||||
.filter(antennas::id.eq(antenna.id.clone()))
|
.filter(antennas::id.eq(antenna.id))
|
||||||
.set(antenna.0)
|
.set(*antenna)
|
||||||
.execute(&mut db);
|
.execute(&mut db);
|
||||||
|
|
||||||
match res {
|
match res {
|
||||||
Ok(0) => Status::NotModified,
|
Ok(0) => Status::NotModified,
|
||||||
Ok(1) => {
|
Ok(1) => Status::Ok,
|
||||||
notify_changes();
|
|
||||||
Status::Ok
|
|
||||||
}
|
|
||||||
_ => Status::BadRequest,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[rocket::delete("/delete/<id>")]
|
|
||||||
fn delete(db_pool: &State<postgres::DbPool>, id: String) -> Status {
|
|
||||||
let mut db = db_pool.get().unwrap();
|
|
||||||
|
|
||||||
let res = diesel::delete(antennas::table)
|
|
||||||
.filter(antennas::id.eq(id))
|
|
||||||
.execute(&mut db);
|
|
||||||
|
|
||||||
match res {
|
|
||||||
Ok(1) => {
|
|
||||||
notify_changes();
|
|
||||||
Status::Ok
|
|
||||||
}
|
|
||||||
_ => Status::BadRequest,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
|
||||||
#[rocket::delete("/purge")]
|
|
||||||
fn purge(db_pool: &State<postgres::DbPool>) -> Status {
|
|
||||||
let mut db = db_pool.get().unwrap();
|
|
||||||
|
|
||||||
let res = diesel::delete(antennas::table).execute(&mut db);
|
|
||||||
|
|
||||||
match res {
|
|
||||||
Ok(1) => {
|
|
||||||
notify_changes();
|
|
||||||
Status::Ok
|
|
||||||
}
|
|
||||||
_ => Status::BadRequest,
|
_ => Status::BadRequest,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,5 +23,8 @@ fn rocket() -> _ {
|
|||||||
rocket::build()
|
rocket::build()
|
||||||
.manage(postgres::init_pool())
|
.manage(postgres::init_pool())
|
||||||
.mount("/static", routes![serve_file])
|
.mount("/static", routes![serve_file])
|
||||||
.mount("/antennas", antennas::routes())
|
.mount("/antennas/", routes![antennas::get_by_id])
|
||||||
|
.mount("/antennas/", routes![antennas::get_list])
|
||||||
|
.mount("/antennas/", routes![antennas::update])
|
||||||
|
.mount("/antennas/", routes![antennas::new])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use std::{thread, time};
|
|||||||
|
|
||||||
mod error_report;
|
mod error_report;
|
||||||
|
|
||||||
use kairo_common::mqtt::for_sync::MqttClient;
|
use kairo_common::mqtt::for_sync::{get_mqtt_cli, mqtt_pub};
|
||||||
use kairo_common::{Antenna, BeaconMeasure, DeviceReport, Point};
|
use kairo_common::{Antenna, BeaconMeasure, DeviceReport, Point};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
@@ -29,7 +29,7 @@ async fn main() {
|
|||||||
// });
|
// });
|
||||||
// }
|
// }
|
||||||
|
|
||||||
let client = MqttClient::new(None);
|
let client = get_mqtt_cli();
|
||||||
|
|
||||||
let mut position = Point::new(config.radius, 0.0);
|
let mut position = Point::new(config.radius, 0.0);
|
||||||
|
|
||||||
@@ -57,9 +57,7 @@ async fn main() {
|
|||||||
.push(BeaconMeasure::new(&config.id, &ant.id, rssi + noise));
|
.push(BeaconMeasure::new(&config.id, &ant.id, rssi + noise));
|
||||||
}
|
}
|
||||||
let payload = serde_json::to_string(&report).unwrap_or_else(|_| "".to_string());
|
let payload = serde_json::to_string(&report).unwrap_or_else(|_| "".to_string());
|
||||||
client
|
mqtt_pub(&client, topic.as_str(), payload.as_str()).expect("Pub error");
|
||||||
.publish(topic.as_str(), Some(payload.as_str()))
|
|
||||||
.expect("Pub error");
|
|
||||||
|
|
||||||
// if config.real {
|
// if config.real {
|
||||||
// let _r = KnownPosition::new(position).write_for("real").await;
|
// let _r = KnownPosition::new(position).write_for("real").await;
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ pub async fn solve_for(device_id: MAC) -> Result<Point, ()> {
|
|||||||
fn trilat(a: &KnownDistance, b: &KnownDistance, c: &KnownDistance) -> Option<KnownDistance> {
|
fn trilat(a: &KnownDistance, b: &KnownDistance, c: &KnownDistance) -> Option<KnownDistance> {
|
||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
let points = [a.point, b.point, c.point];
|
let points = vec![a.point, b.point, c.point];
|
||||||
for &p in points.iter() {
|
for &p in points.iter() {
|
||||||
if !p.is_valid() {
|
if !p.is_valid() {
|
||||||
return None;
|
return None;
|
||||||
|
|||||||
Reference in New Issue
Block a user