change database structure ... again...

This commit is contained in:
Mika Bomm 2024-10-13 22:06:20 +02:00
parent a696a05595
commit 38a0f4d189
8 changed files with 110 additions and 28 deletions

43
Cargo.lock generated
View file

@ -544,6 +544,7 @@ dependencies = [
"actix-web",
"argon2",
"chrono",
"deku",
"dotenvy",
"entity",
"eui48",
@ -551,6 +552,7 @@ dependencies = [
"jsonwebtoken",
"sea-orm",
"serde",
"tokio",
"uuid",
]
@ -954,6 +956,7 @@ dependencies = [
"ident_case",
"proc-macro2",
"quote",
"strsim",
"syn 2.0.79",
]
@ -968,6 +971,31 @@ dependencies = [
"syn 2.0.79",
]
[[package]]
name = "deku"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a9711031e209dc1306d66985363b4397d4c7b911597580340b93c9729b55f6eb"
dependencies = [
"bitvec",
"deku_derive",
"no_std_io2",
"rustversion",
]
[[package]]
name = "deku_derive"
version = "0.18.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "58cb0719583cbe4e81fb40434ace2f0d22ccc3e39a74bb3796c22b451b4f139d"
dependencies = [
"darling",
"proc-macro-crate",
"proc-macro2",
"quote",
"syn 2.0.79",
]
[[package]]
name = "der"
version = "0.7.9"
@ -1772,6 +1800,15 @@ dependencies = [
"libc",
]
[[package]]
name = "no_std_io2"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a3564ce7035b1e4778d8cb6cacebb5d766b5e8fe5a75b9e441e33fb61a872c6"
dependencies = [
"memchr",
]
[[package]]
name = "nom"
version = "7.1.3"
@ -2385,6 +2422,12 @@ dependencies = [
"untrusted",
]
[[package]]
name = "rustversion"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6"
[[package]]
name = "ryu"
version = "1.0.18"

View file

@ -20,3 +20,5 @@ jsonwebtoken = "*"
futures = "*"
chrono = "*"
eui48 = "*"
tokio = { version = "1", features = ["full"] }
deku = "*"

View file

@ -1,13 +1,12 @@
use crate::AppState;
use actix_web::{
error::{ErrorBadRequest, ErrorInternalServerError},
web, HttpResponse, Responder,
web, Responder,
};
use chrono::Utc;
use entity::{node, node_group, sensor_data};
use sea_orm::{entity::*, query::*, ActiveModelTrait, ActiveValue, DbBackend, EntityTrait};
use sea_orm::{entity::*, query::*, ActiveModelTrait, ActiveValue, EntityTrait};
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Serialize)]
struct NodeWithSensorData {
@ -23,6 +22,10 @@ pub struct CreateGroupWithoutId {
#[derive(Deserialize, Serialize)]
pub struct NodeWithMac {
mac: String,
coord_la: f64,
coord_lo: f64,
battery_minimum: f64,
battery_maximum: f64,
group: uuid::Uuid,
}
@ -35,6 +38,10 @@ impl From<node::Model> for NodeWithMac {
Self {
mac,
coord_la: value.coord_la,
coord_lo: value.coord_lo,
battery_minimum: value.battery_minimum,
battery_maximum: value.battery_maximum,
group: value.group,
}
}
@ -51,6 +58,10 @@ impl TryInto<node::Model> for NodeWithMac {
let mac_id = i64::from_be_bytes(mac_id_bytes);
Ok(node::Model {
id: mac_id,
coord_la: self.coord_la,
coord_lo: self.coord_lo,
battery_minimum: self.battery_minimum,
battery_maximum: self.battery_maximum,
group: self.group,
})
}

View file

@ -1,6 +1,8 @@
use actix_web::{web, App, HttpServer};
use deku::prelude::*;
use sea_orm::{Database, DatabaseConnection};
use std::env;
use tokio::{io::AsyncReadExt, net::TcpListener};
mod controller;
@ -13,6 +15,14 @@ struct AppState {
secret: String,
}
#[derive(DekuRead, DekuWrite, Debug)]
struct Data {
mac: [u8; 6],
temp: f32,
battery_voltage: f32,
up_time: u32,
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
#[cfg(debug_assertions)]
@ -30,10 +40,30 @@ async fn main() -> std::io::Result<()> {
println!("Finished running migrations");
let state = AppState {
db: conn,
db: conn.clone(),
secret: jwt_secret,
};
tokio::spawn(async {
let db = conn;
let listener = TcpListener::bind("0.0.0.0:7999")
.await
.expect("Couldnt bind to port 7999");
loop {
if let Ok((mut stream, _)) = listener.accept().await {
let mut buffer = vec![0; 1024];
if let Ok(size) = stream.read(&mut buffer).await {
buffer.truncate(size);
if let Ok((data, _)) = Data::from_bytes((&buffer, 0)) {
println!("Received: {:?}", data);
// Process the data or save it to the database
}
}
}
}
});
println!("Listening for connections...");
HttpServer::new(move || {
let cors = if cfg!(debug_assertions) {

View file

@ -3,11 +3,19 @@
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "node")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i64,
#[sea_orm(column_type = "Double")]
pub coord_la: f64,
#[sea_orm(column_type = "Double")]
pub coord_lo: f64,
#[sea_orm(column_type = "Double")]
pub battery_minimum: f64,
#[sea_orm(column_type = "Double")]
pub battery_maximum: f64,
pub group: Uuid,
}

View file

@ -10,19 +10,9 @@ pub struct Model {
pub id: i32,
#[sea_orm(primary_key, auto_increment = false)]
pub timestamp: DateTime,
#[sea_orm(column_type = "Double")]
pub coord_la: f64,
#[sea_orm(column_type = "Double")]
pub coord_lo: f64,
#[sea_orm(column_type = "Float")]
pub temperature: f32,
#[sea_orm(column_type = "Double")]
pub battery_minimum: f64,
#[sea_orm(column_type = "Double")]
pub battery_current: f64,
#[sea_orm(column_type = "Double")]
pub battery_maximum: f64,
#[sea_orm(column_type = "Double")]
pub voltage: f64,
pub uptime: i64,
pub node_id: i64,

View file

@ -27,6 +27,10 @@ impl MigrationTrait for Migration {
.table(Node::Table)
.if_not_exists()
.col(big_unsigned(Node::Id).primary_key())
.col(double(Node::CoordLa))
.col(double(Node::CoordLo))
.col(double(Node::BatteryMinimum).default(-127))
.col(double(Node::BatteryMaximum).default(-127))
.col(uuid(Node::Group))
.foreign_key(
ForeignKey::create()
@ -60,6 +64,10 @@ impl MigrationTrait for Migration {
pub enum Node {
Table,
Id, // Mac address
CoordLa,
CoordLo,
BatteryMinimum, // def: -127
BatteryMaximum, // def: -127
Group,
}

View file

@ -19,12 +19,7 @@ impl MigrationTrait for Migration {
.col(SensorData::Id)
.col(SensorData::Timestamp),
)
.col(double(SensorData::CoordLa))
.col(double(SensorData::CoordLo))
.col(float(SensorData::Temperature).default(-127))
.col(double(SensorData::BatteryMinimum).default(-127))
.col(double(SensorData::BatteryCurrent).default(-127))
.col(double(SensorData::BatteryMaximum).default(-127))
.col(double(SensorData::Voltage).default(-127))
.col(big_unsigned(SensorData::Uptime).default(0))
.col(big_unsigned(SensorData::NodeId))
@ -55,13 +50,8 @@ enum SensorData {
Table,
Id, // Mac address
Timestamp,
CoordLa,
CoordLo,
Temperature, // def: -127
BatteryMinimum, // def: -127
BatteryCurrent, // def: -127
BatteryMaximum, // def: -127
Voltage, // def: -127
Uptime, // def: 0
Temperature, // def: -127
Voltage, // def: -127
Uptime, // def: 0
NodeId,
}