Include Antenna model diesel macros

This commit is contained in:
2023-06-19 18:42:56 +02:00
parent 990e8955e4
commit 01a291f738
11 changed files with 129 additions and 3 deletions

View File

@@ -4,6 +4,7 @@
#![allow(confusable_idents)]
pub mod influx;
pub mod postgres;
// random functions for mqtt
pub mod mqtt;
@@ -18,7 +19,8 @@ mod types {
pub type Point = types::point::Point;
pub type MAC = types::mac::MAC;
// DB models: for SQL with Diesel and InfluxDB and influxdb-derive
mod schema;
mod models {
pub mod antenna;
pub mod beacon_measure;
@@ -30,8 +32,11 @@ mod models {
pub data: Vec<crate::models::beacon_measure::BeaconMeasure>,
}
}
pub type Antenna = models::antenna::Antenna;
pub type DeviceReport = models::DeviceReport;
pub type Antenna = models::antenna::Antenna;
pub type NewAntenna<'a> = models::antenna::NewAntenna<'a>;
pub type KnownPosition = models::known_position::KnownPosition;
pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus;
pub type BeaconMeasure = models::beacon_measure::BeaconMeasure;

View File

@@ -1,8 +1,10 @@
use std::f64::consts::PI;
use diesel::prelude::*;
use crate::{unit_conversion::UnitsConversion, Point};
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone, Default,Queryable, Selectable)]
#[diesel(table_name = crate::schema::antennas)]
pub struct Antenna {
pub id: String,
pub tssi: f64,
@@ -12,6 +14,17 @@ pub struct Antenna {
pos_z: f64,
}
#[derive(Insertable)]
#[diesel(table_name = crate::schema::antennas)]
pub struct NewAntenna<'a> {
id: &'a str,
comment: Option<&'a str>,
tssi: f64,
pos_x: f64,
pos_y: f64,
pos_z: f64,
}
impl Antenna {
const C: f64 = 2.99e8;
const F: f64 = 2.4e9;

View File

@@ -0,0 +1,29 @@
use diesel::r2d2::{ConnectionManager, Pool, PooledConnection};
use diesel::prelude::*;
pub type DbConn = diesel::pg::PgConnection;
// pub type DbPooledConn = PooledConnection<ConnectionManager<PgConnection>>;
pub type DbPool = Pool<ConnectionManager<PgConnection>>;
pub struct DbPooledConn(pub PooledConnection<ConnectionManager<PgConnection>>);
pub fn establish_connection() -> DbConn {
let database_url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set");
PgConnection::establish(&database_url)
.unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
}
pub fn init_pool() -> DbPool {
let database_url = dotenv::var("DATABASE_URL").expect("DATABASE_URL must be set");
let manager = ConnectionManager::<PgConnection>::new(database_url);
DbPool::new(manager).expect("Error connecting to DB")
}
impl std::ops::Deref for DbPooledConn {
type Target = PgConnection;
fn deref(&self) -> &Self::Target {
&self.0
}
}

View File

@@ -0,0 +1,13 @@
// @generated automatically by Diesel CLI.
diesel::table! {
antennas (id) {
#[max_length = 17]
id -> Varchar,
tssi -> Nullable<Float8>,
pos_x -> Nullable<Float8>,
pos_y -> Nullable<Float8>,
pos_z -> Nullable<Float8>,
comment -> Nullable<Text>,
}
}