updates
This commit is contained in:
@@ -7,8 +7,10 @@ edition = "2021"
|
|||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.4.0"
|
actix-web = "4.4.0"
|
||||||
|
chrono = "0.4.31"
|
||||||
env_logger = "0.10.0"
|
env_logger = "0.10.0"
|
||||||
log = "0.4.20"
|
log = "0.4.20"
|
||||||
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "postgres", "migrate"] }
|
serde = "1.0.188"
|
||||||
|
sqlx = { version = "0.7", features = [ "runtime-tokio", "tls-rustls", "postgres", "migrate", "chrono"] }
|
||||||
sqlx-cli = "0.7.1"
|
sqlx-cli = "0.7.1"
|
||||||
thiserror = "1.0.48"
|
thiserror = "1.0.48"
|
||||||
|
|||||||
5
build.rs
Normal file
5
build.rs
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
// generated by `sqlx migrate build-script`
|
||||||
|
fn main() {
|
||||||
|
// trigger recompilation when a new migration is added
|
||||||
|
println!("cargo:rerun-if-changed=migrations");
|
||||||
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
-- Add migration script here
|
-- Add migration script here
|
||||||
CREATE TABLE Telemetry (
|
CREATE TABLE Telemetry (
|
||||||
timestamp TIMESTAMP NOT NULL,
|
timestamp TIMESTAMP NOT NULL,
|
||||||
Software_Version VARCHAR(255),
|
Software_Version INT,
|
||||||
Voltage FLOAT,
|
Voltage FLOAT,
|
||||||
Temperature FLOAT,
|
Temperature FLOAT,
|
||||||
uptime FLOAT,
|
uptime INT,
|
||||||
device_id CHAR(32),
|
device_id CHAR(32),
|
||||||
FOREIGN KEY (device_id) REFERENCES Devices(ID)
|
FOREIGN KEY (device_id) REFERENCES Devices(ID)
|
||||||
);
|
);
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
use actix_web::cookie::time::error;
|
use actix_web::{cookie::time::error, web};
|
||||||
use log::{error, info};
|
use log::{error, info};
|
||||||
use sqlx::{pool, postgres::PgPoolOptions, query, PgPool, Pool, Postgres, migrate};
|
use sqlx::{pool, postgres::PgPoolOptions, query, PgPool, Pool, Postgres, migrate};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
use chrono::Utc;
|
||||||
|
|
||||||
|
use crate::schemas::TelemetryMessage;
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Database {
|
pub struct Database {
|
||||||
@@ -66,10 +69,22 @@ impl Database {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if exists{
|
if exists{
|
||||||
|
info!("Device exists");
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
|
info!("Device does not exist");
|
||||||
self.add_device(device_id).await?;
|
self.add_device(device_id).await?;
|
||||||
Ok(())
|
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(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
16
src/main.rs
16
src/main.rs
@@ -1,8 +1,10 @@
|
|||||||
use actix_web::{post, web, App, HttpServer, Responder, http::StatusCode, HttpResponse};
|
use actix_web::{post, web, App, HttpServer, Responder, http::StatusCode, HttpResponse};
|
||||||
use database::Database;
|
use database::Database;
|
||||||
use log::info;
|
use log::{info, error, debug};
|
||||||
|
use crate::schemas::TelemetryMessage;
|
||||||
|
|
||||||
mod database;
|
mod database;
|
||||||
|
mod schemas;
|
||||||
|
|
||||||
struct AppState {
|
struct AppState {
|
||||||
db: Database,
|
db: Database,
|
||||||
@@ -12,8 +14,18 @@ struct AppState {
|
|||||||
async fn receive_telemetry(
|
async fn receive_telemetry(
|
||||||
device_id: web::Path<String>,
|
device_id: web::Path<String>,
|
||||||
data: web::Data<AppState>,
|
data: web::Data<AppState>,
|
||||||
|
telemetry_message: web::Json<TelemetryMessage>
|
||||||
) -> impl Responder {
|
) -> impl Responder {
|
||||||
data.db.create_device_if_not_exists(&device_id);
|
info!("POST - telementry - Processing device id {}", device_id);
|
||||||
|
match data.db.create_device_if_not_exists(&device_id).await{
|
||||||
|
Ok(_) => {},
|
||||||
|
Err(e) => {
|
||||||
|
error!("Error creating new device: {}", e);
|
||||||
|
return HttpResponse::InternalServerError();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
debug!("{:?}", telemetry_message);
|
||||||
|
data.db.add_telemetry(&telemetry_message, &device_id).await;
|
||||||
|
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
}
|
}
|
||||||
|
|||||||
9
src/schemas.rs
Normal file
9
src/schemas.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
use serde::Deserialize;
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct TelemetryMessage {
|
||||||
|
pub uptime: i32,
|
||||||
|
pub voltage: Option<f64>,
|
||||||
|
pub temperature: Option<f64>,
|
||||||
|
pub version: i32
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user