From 38a0f4d189824bb9f9e3bc235f890e87d9e5e144 Mon Sep 17 00:00:00 2001 From: Mika Date: Sun, 13 Oct 2024 22:06:20 +0200 Subject: [PATCH] change database structure ... again... --- Cargo.lock | 43 +++++++++++++++++++ crates/backend/Cargo.toml | 2 + crates/backend/src/controller/node.rs | 17 ++++++-- crates/backend/src/main.rs | 32 +++++++++++++- crates/entity/src/node.rs | 10 ++++- crates/entity/src/sensor_data.rs | 10 ----- .../src/m20241008_095058_create_table_node.rs | 8 ++++ ...0241013_134422_create_table_sensor_data.rs | 16 ++----- 8 files changed, 110 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5cbe2bd..6af954d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/crates/backend/Cargo.toml b/crates/backend/Cargo.toml index 37a16b3..ebfe4e2 100644 --- a/crates/backend/Cargo.toml +++ b/crates/backend/Cargo.toml @@ -20,3 +20,5 @@ jsonwebtoken = "*" futures = "*" chrono = "*" eui48 = "*" +tokio = { version = "1", features = ["full"] } +deku = "*" diff --git a/crates/backend/src/controller/node.rs b/crates/backend/src/controller/node.rs index eb93a0d..8f38b17 100644 --- a/crates/backend/src/controller/node.rs +++ b/crates/backend/src/controller/node.rs @@ -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 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 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, }) } diff --git a/crates/backend/src/main.rs b/crates/backend/src/main.rs index dab6a2b..de98cb3 100644 --- a/crates/backend/src/main.rs +++ b/crates/backend/src/main.rs @@ -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) { diff --git a/crates/entity/src/node.rs b/crates/entity/src/node.rs index e09591f..d511a3a 100644 --- a/crates/entity/src/node.rs +++ b/crates/entity/src/node.rs @@ -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, } diff --git a/crates/entity/src/sensor_data.rs b/crates/entity/src/sensor_data.rs index d6aacc1..6d6ad23 100644 --- a/crates/entity/src/sensor_data.rs +++ b/crates/entity/src/sensor_data.rs @@ -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, diff --git a/crates/migration/src/m20241008_095058_create_table_node.rs b/crates/migration/src/m20241008_095058_create_table_node.rs index bfb75a4..026d271 100644 --- a/crates/migration/src/m20241008_095058_create_table_node.rs +++ b/crates/migration/src/m20241008_095058_create_table_node.rs @@ -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, } diff --git a/crates/migration/src/m20241013_134422_create_table_sensor_data.rs b/crates/migration/src/m20241013_134422_create_table_sensor_data.rs index ba53196..f1a0128 100644 --- a/crates/migration/src/m20241013_134422_create_table_sensor_data.rs +++ b/crates/migration/src/m20241013_134422_create_table_sensor_data.rs @@ -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, }