Compare commits
5 Commits
develop
...
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,28 +62,16 @@ 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,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl MqttClient {
|
|
||||||
pub fn new(client_id: Option<&str>) -> MqttClient {
|
|
||||||
let host = dotenv::var("MQTT_BROKER").unwrap_or_else(|_| {
|
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));
|
||||||
@@ -93,20 +81,20 @@ pub mod for_sync {
|
|||||||
println!("Unable to connect: {:?}", e);
|
println!("Unable to connect: {:?}", e);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
cli
|
||||||
|
}
|
||||||
|
|
||||||
MqttClient { cli }
|
pub fn mqtt_pub(
|
||||||
}
|
client: &mqtt::Client,
|
||||||
pub fn publish(&self, topic: &str, payload: Option<&str>) -> Result<(), paho_mqtt::Error> {
|
topic: &str,
|
||||||
let msg = if let Some(payload) = payload {
|
payload: &str,
|
||||||
mqtt::MessageBuilder::new()
|
) -> Result<(), paho_mqtt::Error> {
|
||||||
|
let msg = mqtt::MessageBuilder::new()
|
||||||
.topic(topic)
|
.topic(topic)
|
||||||
.qos(0)
|
|
||||||
.payload(payload)
|
.payload(payload)
|
||||||
.finalize()
|
.qos(0)
|
||||||
} else {
|
.finalize();
|
||||||
mqtt::MessageBuilder::new().topic(topic).qos(0).finalize()
|
|
||||||
};
|
client.publish(msg)
|
||||||
self.cli.publish(msg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use rocket::{http::Status, serde::json::Json, State};
|
use rocket::{http::Status, serde::json::Json, State};
|
||||||
|
|
||||||
use kairo_common::{postgres, schema::antennas, Antenna};
|
use kairo_common::{postgres, schema::antennas, Antenna, NewAntenna};
|
||||||
|
|
||||||
#[rocket::get("/id/<id>")]
|
#[rocket::get("/id/<id>")]
|
||||||
pub 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>> {
|
||||||
@@ -33,11 +33,11 @@ pub 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>")]
|
||||||
pub 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 {
|
||||||
@@ -47,12 +47,12 @@ pub fn new(db_pool: &State<postgres::DbPool>, antenna: Json<Antenna>) -> Status
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::patch("/update", format = "json", data = "<antenna>")]
|
#[rocket::patch("/update", format = "json", data = "<antenna>")]
|
||||||
pub 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 {
|
||||||
@@ -61,17 +61,3 @@ pub fn update(db_pool: &State<postgres::DbPool>, antenna: Json<Antenna>) -> Stat
|
|||||||
_ => Status::BadRequest,
|
_ => Status::BadRequest,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[rocket::delete("/delete/<id>")]
|
|
||||||
pub 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) => Status::Ok,
|
|
||||||
_ => Status::BadRequest,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -25,7 +25,6 @@ fn rocket() -> _ {
|
|||||||
.mount("/static", routes![serve_file])
|
.mount("/static", routes![serve_file])
|
||||||
.mount("/antennas/", routes![antennas::get_by_id])
|
.mount("/antennas/", routes![antennas::get_by_id])
|
||||||
.mount("/antennas/", routes![antennas::get_list])
|
.mount("/antennas/", routes![antennas::get_list])
|
||||||
.mount("/antennas/", routes![antennas::delete])
|
|
||||||
.mount("/antennas/", routes![antennas::update])
|
.mount("/antennas/", routes![antennas::update])
|
||||||
.mount("/antennas/", routes![antennas::new])
|
.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;
|
||||||
|
|||||||
Reference in New Issue
Block a user