Skip to content

Commit

Permalink
update deps and add generic measurement
Browse files Browse the repository at this point in the history
  • Loading branch information
Frixxie committed Apr 19, 2024
1 parent a66255b commit d3e1553
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions backend/src/handlers/measurements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ pub async fn store_measurements(
HandlerError::new(500, format!("Failed to store data in database: {}", e))
})?;
}
Sensors::Measurement(measurement) => {
info!("Got measurement {}", measurement);
measurement.create(pg_pool).await.map_err(|e| {
warn!("Failed with error: {}", e);
HandlerError::new(500, format!("Failed to store data in database: {}", e))
})?;
}
};

Ok("OK".to_string())
Expand Down
27 changes: 25 additions & 2 deletions backend/src/measurements.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use chrono::{DateTime, Utc};
use sensors::{Dht11, Temperature};
use sensors::{Dht11, Measurement as GenericMeasurement, Temperature};
use serde::Serialize;
use sqlx::FromRow;

Expand Down Expand Up @@ -53,6 +53,19 @@ impl Create<Postgres> for Dht11 {
}
}

impl Create<Postgres> for GenericMeasurement {
async fn create(self, connection: Postgres) -> Result<()> {
let pool = connection.get_connection().await;
sqlx::query("INSERT INTO measurements (ts, device_id, sensor_id, value) VALUES (CURRENT_TIMESTAMP, $1, $2, $3)")
.bind(self.device)
.bind(self.sensor)
.bind(self.measurement)
.execute(&pool)
.await?;
Ok(())
}
}

impl Create<Postgres> for Temperature {
async fn create(self, connection: Postgres) -> Result<()> {
let pool = connection.get_connection().await;
Expand Down Expand Up @@ -100,8 +113,8 @@ impl Read<Postgres> for Vec<Measurement> {
}

#[cfg(test)]

mod tests {
use sensors::Measurement as GenericMeasurement;
use sensors::{Dht11, Temperature};
use sqlx::PgPool;

Expand Down Expand Up @@ -131,6 +144,16 @@ mod tests {
temperature_measurement.create(postgres).await.unwrap();
}

#[sqlx::test]
fn measurement_insert(pool: PgPool) {
let postgres = Postgres::new(pool);

let device = Device::new("test".to_string(), "test".to_string());
device.create(postgres.clone()).await.unwrap();
let measurement = GenericMeasurement::new(1, 1, 1.0);
measurement.create(postgres).await.unwrap();
}

#[sqlx::test]
fn measurements_read(pool: PgPool) {
let postgres = Postgres::new(pool);
Expand Down
19 changes: 19 additions & 0 deletions sensors/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
mod dht11;
mod measurement;
mod temperature;

pub use dht11::Dht11;
pub use measurement::Measurement;
use serde::{Deserialize, Serialize};
pub use temperature::Temperature;

Expand All @@ -10,6 +12,7 @@ pub use temperature::Temperature;
pub enum Sensors {
Temperature(temperature::Temperature),
Dht11(dht11::Dht11),
Measurement(measurement::Measurement),
}

#[cfg(test)]
Expand Down Expand Up @@ -46,5 +49,21 @@ mod tests {
}
_ => panic!("this is illegal"),
}

let measurement = super::Measurement {
device: 1,
sensor: 1,
measurement: 1.0,
};
let measurement_str = serde_json::to_string(&measurement).unwrap();
let measurement_de: super::Sensors = serde_json::from_str(&measurement_str).unwrap();
match measurement_de {
crate::Sensors::Measurement(measurement) => {
assert_eq!(measurement.device, 1);
assert_eq!(measurement.sensor, 1);
assert_eq!(measurement.measurement, 1.0);
}
_ => panic!("this is illegal"),
}
}
}
26 changes: 26 additions & 0 deletions sensors/src/measurement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use std::fmt;

use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Measurement {
pub device: i32,
pub sensor: i32,
pub measurement: f32,
}

impl Measurement {
pub fn new(device: i32, sensor: i32, measurement: f32) -> Self {
Self {
device,
sensor,
measurement,
}
}
}

impl fmt::Display for Measurement {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{},{},{}", self.device, self.sensor, self.measurement)
}
}

0 comments on commit d3e1553

Please sign in to comment.