This commit is contained in:
2023-09-16 20:06:29 +00:00
parent 3f9dcb3a53
commit fa029e838a
6 changed files with 49 additions and 6 deletions

View File

@@ -1,7 +1,10 @@
use actix_web::cookie::time::error;
use actix_web::{cookie::time::error, web};
use log::{error, info};
use sqlx::{pool, postgres::PgPoolOptions, query, PgPool, Pool, Postgres, migrate};
use thiserror::Error;
use chrono::Utc;
use crate::schemas::TelemetryMessage;
#[derive(Clone)]
pub struct Database {
@@ -66,10 +69,22 @@ impl Database {
};
if exists{
info!("Device exists");
Ok(())
} else {
info!("Device does not exist");
self.add_device(device_id).await?;
Ok(())
}
}
pub async fn add_telemetry(&self, msg: &web::Json<TelemetryMessage>, device_id: &str) -> Result<(), DatabaseError> {
info!("Adding telemetry message to DB");
let current_timestamp = Utc::now().naive_utc();
query!("INSERT INTO Telemetry (timestamp, software_version, voltage, temperature, uptime, device_id)
VALUES ($1, $2, $3, $4, $5, $6);",
current_timestamp, msg.version, msg.voltage, msg.temperature, msg.uptime, device_id
).execute(&self.conn_pool).await?;
Ok(())
}
}