From 3c904db36fb51378a051ac98b46808030e349dbb Mon Sep 17 00:00:00 2001 From: Gvidas Juknevicius Date: Thu, 20 Aug 2026 16:22:35 +0300 Subject: [PATCH] More stuff, lost a lot of progress to this shitty pc with a shitty os --- Cargo.lock | 190 ++++++++++++++++++ Cargo.toml | 3 +- diesel.toml | 2 +- migrations/.diesel_lock | 0 .../2026-08-20-121835-0000_initial/down.sql | 2 + .../2026-08-20-121835-0000_initial/up.sql | 17 ++ src/db/mod.rs | 4 +- src/db/models.rs | 41 ++++ src/db/schema.rs | 23 +++ src/main.rs | 25 ++- src/mt_commander/mod.rs | 27 ++- 11 files changed, 319 insertions(+), 15 deletions(-) create mode 100644 migrations/.diesel_lock create mode 100644 migrations/2026-08-20-121835-0000_initial/down.sql create mode 100644 migrations/2026-08-20-121835-0000_initial/up.sql create mode 100644 src/db/models.rs create mode 100644 src/db/schema.rs diff --git a/Cargo.lock b/Cargo.lock index a51cbb1..e785705 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,15 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "android_system_properties" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc" +dependencies = [ + "libc", +] + [[package]] name = "ansi-str" version = "0.9.0" @@ -98,6 +107,12 @@ version = "0.7.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3fb67a6e08acf24fdeccbac2cb6ac4305825bd1f117462e0e6f2f193345ad56" +[[package]] +name = "autocfg" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53" + [[package]] name = "bitflags" version = "1.3.2" @@ -110,6 +125,12 @@ version = "2.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4388bee8683e3d04af747c73422af53102d2bd24d9eadb6cbc100baef4b43f8" +[[package]] +name = "bumpalo" +version = "3.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" + [[package]] name = "bytecount" version = "0.6.9" @@ -153,6 +174,19 @@ version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" +[[package]] +name = "chrono" +version = "0.4.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327" +dependencies = [ + "iana-time-zone", + "js-sys", + "num-traits", + "wasm-bindgen", + "windows-link", +] + [[package]] name = "clap" version = "4.6.1" @@ -199,6 +233,12 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "core-foundation-sys" +version = "0.8.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "773648b94d0e5d620f64f280777445740e61fe701025087ec8b57f45c791888b" + [[package]] name = "darling" version = "0.21.3" @@ -274,6 +314,7 @@ checksum = "715377c6e464cb44bb89bd8487584240516c8d5052bc645d6babc50bb8be46c3" dependencies = [ "bitflags 2.13.0", "byteorder", + "chrono", "diesel_derives", "downcast-rs", "itoa", @@ -523,6 +564,30 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "iana-time-zone" +version = "0.1.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "log", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "ident_case" version = "1.0.1" @@ -576,6 +641,17 @@ dependencies = [ "syn 2.0.118", ] +[[package]] +name = "js-sys" +version = "0.3.104" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0e0c1080212aad755ea003d18543e8768dd432c48819efd73a7bf1e39b7a5a3a" +dependencies = [ + "cfg-if", + "futures-util", + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -691,6 +767,15 @@ dependencies = [ "minimal-lexical", ] +[[package]] +name = "num-traits" +version = "0.2.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841" +dependencies = [ + "autocfg", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -908,6 +993,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustversion" +version = "1.0.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf54715a573b99ac80df0bc206da022bcd442c974952c7b9720069370852e21f" + [[package]] name = "scopeguard" version = "1.2.0" @@ -1248,6 +1339,7 @@ version = "0.1.0" dependencies = [ "anyhow", "camino", + "chrono", "clap", "diesel", "diesel_migrations", @@ -1270,12 +1362,110 @@ version = "0.11.1+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" +[[package]] +name = "wasm-bindgen" +version = "0.2.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b70935747edd64d89de3efa29d73789b806c15798f8e7dca4d8ac356b50ce70" +dependencies = [ + "cfg-if", + "once_cell", + "rustversion", + "wasm-bindgen-macro", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77775f8f3f7217702089053b94958f8f54061a3f663417df76e19cbdcca29bc1" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e11d33f857dc2fb11b8bc75aee111aa9cbeb12cd9f25efd3d4c2a3dd4e235284" +dependencies = [ + "bumpalo", + "proc-macro2", + "quote", + "syn 2.0.118", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.127" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef64dbcc55df09c7e5a46182d181c2cfa3e925f3da937ea764728b4bbb9dcbf" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "windows-core" +version = "0.62.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.118", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.118", +] + [[package]] name = "windows-link" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" +[[package]] +name = "windows-result" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5" +dependencies = [ + "windows-link", +] + +[[package]] +name = "windows-strings" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 9cfa9b1..0ef1c92 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,8 +10,9 @@ authors = [ [dependencies] anyhow = "1.0.103" camino = { version = "1.2.4", features = ["serde1"] } +chrono = "0.4.45" clap = { version = "4.6.1", features = ["derive"] } -diesel = { version = "2.3.12", features = ["postgres"] } +diesel = { version = "2.3.12", features = ["chrono", "postgres"] } diesel_migrations = "2.3.2" env_logger = "0.11.11" futures = "0.3.34" diff --git a/diesel.toml b/diesel.toml index a0d61bf..bb1d1f7 100644 --- a/diesel.toml +++ b/diesel.toml @@ -2,7 +2,7 @@ # see https://diesel.rs/guides/configuring-diesel-cli [print_schema] -file = "src/schema.rs" +file = "src/db/schema.rs" custom_type_derives = ["diesel::query_builder::QueryId", "Clone"] [migrations_directory] diff --git a/migrations/.diesel_lock b/migrations/.diesel_lock new file mode 100644 index 0000000..e69de29 diff --git a/migrations/2026-08-20-121835-0000_initial/down.sql b/migrations/2026-08-20-121835-0000_initial/down.sql new file mode 100644 index 0000000..ffff1a0 --- /dev/null +++ b/migrations/2026-08-20-121835-0000_initial/down.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS client_readings; +DROP TABLE IF EXISTS antennas; diff --git a/migrations/2026-08-20-121835-0000_initial/up.sql b/migrations/2026-08-20-121835-0000_initial/up.sql new file mode 100644 index 0000000..78c132d --- /dev/null +++ b/migrations/2026-08-20-121835-0000_initial/up.sql @@ -0,0 +1,17 @@ + +CREATE TABLE IF NOT EXISTS antennas ( + id BIGSERIAL PRIMARY KEY NOT NULL, + name TEXT NOT NULL, + ip TEXT NOT NULL UNIQUE, + error TEXT +); + + +CREATE TABLE IF NOT EXISTS client_readings ( + id BIGSERIAL PRIMARY KEY NOT NULL, + ip TEXT NOT NULL, + mac TEXT NOT NULL, + antenna_ip TEXT NOT NULL REFERENCES antennas(ip), + db_reading INTEGER NOT NULL, + read_at TIMESTAMP NOT NULL DEFAULT NOW() +); diff --git a/src/db/mod.rs b/src/db/mod.rs index 35f9302..ac85dcf 100644 --- a/src/db/mod.rs +++ b/src/db/mod.rs @@ -2,6 +2,9 @@ use diesel::pg::PgConnection; use diesel::prelude::*; use diesel_migrations::{EmbeddedMigrations, MigrationHarness, embed_migrations}; +pub mod models; +pub mod schema; + pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); pub fn init_db(database_url: &str) -> PgConnection { @@ -14,4 +17,3 @@ pub fn init_db(database_url: &str) -> PgConnection { connection } - diff --git a/src/db/models.rs b/src/db/models.rs new file mode 100644 index 0000000..db6bb1f --- /dev/null +++ b/src/db/models.rs @@ -0,0 +1,41 @@ +use chrono::NaiveDateTime; +use diesel::prelude::*; + +#[derive(Queryable, Selectable)] +#[diesel(table_name = crate::db::schema::antennas)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct Antenna { + pub id: i64, + pub name: String, + pub ip: String, + pub error: Option, +} + +#[derive(Insertable)] +#[diesel(table_name = crate::db::schema::antennas)] +pub struct NewAntenna { + pub name: String, + pub ip: String, + pub error: Option, +} + +#[derive(Queryable, Selectable)] +#[diesel(table_name = crate::db::schema::client_readings)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct ClientReading { + pub id: i64, + pub ip: String, + pub mac: String, + pub antenna_ip: String, + pub db_reading: i32, + pub read_at: NaiveDateTime, +} + +#[derive(Insertable)] +#[diesel(table_name = crate::db::schema::client_readings)] +pub struct NewClientReading { + pub ip: String, + pub mac: String, + pub antenna_ip: String, + pub db_reading: i32, +} diff --git a/src/db/schema.rs b/src/db/schema.rs new file mode 100644 index 0000000..f988cf1 --- /dev/null +++ b/src/db/schema.rs @@ -0,0 +1,23 @@ +// @generated automatically by Diesel CLI. + +diesel::table! { + antennas (id) { + id -> Int8, + name -> Text, + ip -> Text, + error -> Nullable, + } +} + +diesel::table! { + client_readings (id) { + id -> Int8, + ip -> Text, + mac -> Text, + antenna_ip -> Text, + db_reading -> Int4, + read_at -> Timestamp, + } +} + +diesel::allow_tables_to_appear_in_same_query!(antennas, client_readings,); diff --git a/src/main.rs b/src/main.rs index 4ea50ab..5b7c6f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -50,7 +50,30 @@ async fn main() -> anyhow::Result<()> { mt_commander::MtCommander::init(cfg.clone())?; match &cfg.cli.command { - CliCommand::BesWifi => {} + CliCommand::BesWifi => { + let v6_hosts = cfg + .hosts + .iter() + .filter(|v| v.tags.contains(&"v6".to_string())) + .cloned() + .collect::>(); + + let v7_hosts = cfg + .hosts + .iter() + .filter(|v| v.tags.contains(&"v7".to_string())) + .cloned() + .collect::>(); + + let v6_cmd = "/interface/wireless/registration-table/print"; + let v7_cmd = "/interface/wifi/registration-table/print"; + + let (v6_res, v7_res) = futures::join!( + mt_commander::MtCommander::run_command_on_hosts(&v6_hosts, v6_cmd), + mt_commander::MtCommander::run_command_on_hosts(&v7_hosts, v7_cmd), + ); + let (v6_res, v7_res) = (v6_res?, v7_res?); + } CliCommand::Run { all, devices, diff --git a/src/mt_commander/mod.rs b/src/mt_commander/mod.rs index 938701b..f624542 100644 --- a/src/mt_commander/mod.rs +++ b/src/mt_commander/mod.rs @@ -38,19 +38,24 @@ impl MtCommander { Ok(()) } - pub async fn run_command_on_hosts( + pub fn run_command_on_hosts( hosts: &[ConfigHost], command: &str, - ) -> anyhow::Result>>>>> - { - let results = join_all(hosts.iter().map(|host| async move { - let res = Self::run_command_on_host(host, command).await; - (host.clone(), res) - })) - .await; + ) -> impl Future< + Output = anyhow::Result< + HashMap>>>>, + >, + > { + async move { + let results = join_all(hosts.iter().map(|host| async move { + let res = Self::run_command_on_host(host, command).await; + (host.clone(), res) + })) + .await; - let res_map: HashMap<_, _> = results.into_iter().collect(); - Ok(res_map) + let res_map: HashMap<_, _> = results.into_iter().collect(); + Ok(res_map) + } } pub async fn run_command_on_host( host: &ConfigHost, @@ -130,7 +135,7 @@ impl MtCommanderInternal { let dev = match dev_res { Ok(dev) => { - log::info!("({addr}) Connected successfully!"); + log::debug!("({addr}) Connected successfully!"); dev } Err(e) => {