Include Antenna model diesel macros
This commit is contained in:
@@ -20,3 +20,5 @@ influxdb2-structmap = "0.2"
|
|||||||
influxdb2-derive = "0.1.1"
|
influxdb2-derive = "0.1.1"
|
||||||
futures = "0.3.28"
|
futures = "0.3.28"
|
||||||
num-traits = "0.2"
|
num-traits = "0.2"
|
||||||
|
diesel = { version = "2.1.0", features = ["postgres"] }
|
||||||
|
diesel-async = { version = "0.3.1", features = ["postgres"] }
|
||||||
|
|||||||
9
kairo-common/diesel.toml
Normal file
9
kairo-common/diesel.toml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# For documentation on how to configure this file,
|
||||||
|
# see https://diesel.rs/guides/configuring-diesel-cli
|
||||||
|
|
||||||
|
[print_schema]
|
||||||
|
file = "src/schema.rs"
|
||||||
|
custom_type_derives = ["diesel::query_builder::QueryId"]
|
||||||
|
|
||||||
|
[migrations_directory]
|
||||||
|
dir = "migrations"
|
||||||
0
kairo-common/migrations/.keep
Normal file
0
kairo-common/migrations/.keep
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
DROP FUNCTION IF EXISTS diesel_manage_updated_at(_tbl regclass);
|
||||||
|
DROP FUNCTION IF EXISTS diesel_set_updated_at();
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
-- This file was automatically created by Diesel to setup helper functions
|
||||||
|
-- and other internal bookkeeping. This file is safe to edit, any future
|
||||||
|
-- changes will be added to existing projects as new migrations.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- Sets up a trigger for the given table to automatically set a column called
|
||||||
|
-- `updated_at` whenever the row is modified (unless `updated_at` was included
|
||||||
|
-- in the modified columns)
|
||||||
|
--
|
||||||
|
-- # Example
|
||||||
|
--
|
||||||
|
-- ```sql
|
||||||
|
-- CREATE TABLE users (id SERIAL PRIMARY KEY, updated_at TIMESTAMP NOT NULL DEFAULT NOW());
|
||||||
|
--
|
||||||
|
-- SELECT diesel_manage_updated_at('users');
|
||||||
|
-- ```
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_manage_updated_at(_tbl regclass) RETURNS VOID AS $$
|
||||||
|
BEGIN
|
||||||
|
EXECUTE format('CREATE TRIGGER set_updated_at BEFORE UPDATE ON %s
|
||||||
|
FOR EACH ROW EXECUTE PROCEDURE diesel_set_updated_at()', _tbl);
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
|
|
||||||
|
CREATE OR REPLACE FUNCTION diesel_set_updated_at() RETURNS trigger AS $$
|
||||||
|
BEGIN
|
||||||
|
IF (
|
||||||
|
NEW IS DISTINCT FROM OLD AND
|
||||||
|
NEW.updated_at IS NOT DISTINCT FROM OLD.updated_at
|
||||||
|
) THEN
|
||||||
|
NEW.updated_at := current_timestamp;
|
||||||
|
END IF;
|
||||||
|
RETURN NEW;
|
||||||
|
END;
|
||||||
|
$$ LANGUAGE plpgsql;
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
-- This file should undo anything in `up.sql`
|
||||||
|
|
||||||
|
DROP TABLE antennas;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
-- Your SQL goes here
|
||||||
|
|
||||||
|
CREATE TABLE antennas (
|
||||||
|
id VARCHAR(17) PRIMARY KEY,
|
||||||
|
tssi DOUBLE PRECISION,
|
||||||
|
pos_x DOUBLE PRECISION,
|
||||||
|
pos_y DOUBLE PRECISION,
|
||||||
|
pos_z DOUBLE PRECISION,
|
||||||
|
comment TEXT
|
||||||
|
);
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
#![allow(confusable_idents)]
|
#![allow(confusable_idents)]
|
||||||
|
|
||||||
pub mod influx;
|
pub mod influx;
|
||||||
|
pub mod postgres;
|
||||||
|
|
||||||
// random functions for mqtt
|
// random functions for mqtt
|
||||||
pub mod mqtt;
|
pub mod mqtt;
|
||||||
@@ -18,7 +19,8 @@ mod types {
|
|||||||
pub type Point = types::point::Point;
|
pub type Point = types::point::Point;
|
||||||
pub type MAC = types::mac::MAC;
|
pub type MAC = types::mac::MAC;
|
||||||
|
|
||||||
|
/// DB models: for SQL with Diesel and InfluxDB and influxdb-derive
|
||||||
|
mod schema;
|
||||||
mod models {
|
mod models {
|
||||||
pub mod antenna;
|
pub mod antenna;
|
||||||
pub mod beacon_measure;
|
pub mod beacon_measure;
|
||||||
@@ -30,8 +32,11 @@ mod models {
|
|||||||
pub data: Vec<crate::models::beacon_measure::BeaconMeasure>,
|
pub data: Vec<crate::models::beacon_measure::BeaconMeasure>,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub type Antenna = models::antenna::Antenna;
|
|
||||||
pub type DeviceReport = models::DeviceReport;
|
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 KnownPosition = models::known_position::KnownPosition;
|
||||||
pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus;
|
pub type DynamicDeviceStatus = models::dynamic_device_status::DynamicDeviceStatus;
|
||||||
pub type BeaconMeasure = models::beacon_measure::BeaconMeasure;
|
pub type BeaconMeasure = models::beacon_measure::BeaconMeasure;
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
use std::f64::consts::PI;
|
use std::f64::consts::PI;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
use crate::{unit_conversion::UnitsConversion, Point};
|
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 struct Antenna {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub tssi: f64,
|
pub tssi: f64,
|
||||||
@@ -12,6 +14,17 @@ pub struct Antenna {
|
|||||||
pos_z: f64,
|
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 {
|
impl Antenna {
|
||||||
const C: f64 = 2.99e8;
|
const C: f64 = 2.99e8;
|
||||||
const F: f64 = 2.4e9;
|
const F: f64 = 2.4e9;
|
||||||
|
|||||||
10
kairo-common/src/postgres.rs
Normal file
10
kairo-common/src/postgres.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
pub mod for_sync {
|
||||||
|
use diesel::pg::PgConnection;
|
||||||
|
use diesel::prelude::*;
|
||||||
|
|
||||||
|
pub fn establish_connection() -> PgConnection {
|
||||||
|
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))
|
||||||
|
}
|
||||||
|
}
|
||||||
13
kairo-common/src/schema.rs
Normal file
13
kairo-common/src/schema.rs
Normal 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>,
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user