From 7c96939b2d35ed59f46cc1445b55db5edffb325f Mon Sep 17 00:00:00 2001 From: Ricardo Zuasti Date: Sat, 14 Feb 2026 12:30:12 -0500 Subject: [PATCH] Moved Devices to a models module --- backend/Cargo.lock | 1 + backend/Cargo.toml | 2 +- backend/src/db/devices.rs | 2 +- backend/src/device_finders.rs | 21 ------------------ backend/src/device_finders/arp.rs | 3 ++- .../device_finders/arp/packet_send_receive.rs | 4 ++-- backend/src/events.rs | 6 ++--- backend/src/main.rs | 1 + backend/src/model.rs | 1 + backend/src/model/devices.rs | 22 +++++++++++++++++++ 10 files changed, 34 insertions(+), 29 deletions(-) create mode 100644 backend/src/model.rs create mode 100644 backend/src/model/devices.rs diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 746b8fb..7a2eff8 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -276,6 +276,7 @@ dependencies = [ "iana-time-zone", "js-sys", "num-traits", + "serde", "wasm-bindgen", "windows-link", ] diff --git a/backend/Cargo.toml b/backend/Cargo.toml index 5bb347d..6164b44 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -13,7 +13,7 @@ serde_json = "1.0" rusqlite = { version = "0.38.0", features = ["bundled", "chrono"] } rusqlite_migration = { version = "2.4.0", features = ["from-directory"] } include_dir = "0.7.4" -chrono = "0.4.43" +chrono = {version = "0.4.43", features = ["serde"]} pushover = "0.4.0" config = "0.15.19" lazy_static = "1.5.0" diff --git a/backend/src/db/devices.rs b/backend/src/db/devices.rs index 8a3244a..585a571 100644 --- a/backend/src/db/devices.rs +++ b/backend/src/db/devices.rs @@ -2,7 +2,7 @@ use log::{debug, error}; use rusqlite::{Connection, params}; use std::sync::MutexGuard; -use crate::device_finders::Device; +use crate::model::devices::Device; // Read device from its MAC address pub fn read(conn: MutexGuard, mac_address: String) -> Option { diff --git a/backend/src/device_finders.rs b/backend/src/device_finders.rs index 677cda9..6a5d36a 100644 --- a/backend/src/device_finders.rs +++ b/backend/src/device_finders.rs @@ -1,22 +1 @@ pub mod arp; - -use chrono::NaiveDateTime; -use std::fmt; - -#[derive(Clone)] -pub struct Device { - pub mac_address: String, - pub ipv4_address: String, - pub vendor: String, - pub last_seen: NaiveDateTime, -} - -impl fmt::Display for Device { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!( - f, - "mac={}, ip={}, vendor={}, last_seen={}", - self.mac_address, self.ipv4_address, self.vendor, self.last_seen - ) - } -} diff --git a/backend/src/device_finders/arp.rs b/backend/src/device_finders/arp.rs index 336811a..7e05188 100644 --- a/backend/src/device_finders/arp.rs +++ b/backend/src/device_finders/arp.rs @@ -1,6 +1,7 @@ mod packet_send_receive; -use crate::{device_finders::Device, settings::CONFIG}; +use crate::model::devices::Device; +use crate::settings::CONFIG; use duration_string::DurationString; use log::{debug, error, info, warn}; use packet_send_receive::{listen_for_packets, send_packet}; diff --git a/backend/src/device_finders/arp/packet_send_receive.rs b/backend/src/device_finders/arp/packet_send_receive.rs index a210ab6..3d53172 100644 --- a/backend/src/device_finders/arp/packet_send_receive.rs +++ b/backend/src/device_finders/arp/packet_send_receive.rs @@ -1,5 +1,5 @@ -use crate::device_finders::Device; use crate::mac_vendor_finder; +use crate::model::devices::Device; use chrono::Local; use duration_string::DurationString; use log::{debug, info, trace}; @@ -110,7 +110,7 @@ pub async fn listen_for_packets( mac_address: packet_mac_address, ipv4_address: packet_ip_address, vendor: packet_vendor, - last_seen: Local::now().naive_local(), + last_seen: Local::now().to_utc(), }); } } diff --git a/backend/src/events.rs b/backend/src/events.rs index 84c948c..4f06754 100644 --- a/backend/src/events.rs +++ b/backend/src/events.rs @@ -2,7 +2,8 @@ mod pushover; use std::time::Duration; -use crate::{device_finders::Device, settings::CONFIG}; +use crate::model::devices::Device; +use crate::settings::CONFIG; use chrono::Local; use duration_string::DurationString; use log::{debug, info, warn}; @@ -31,8 +32,7 @@ pub fn trigger_new_device(device: Device) -> Result<(), String> { pub fn trigger_existing_device(existing_device: Device, new_device: Device) -> Result<(), String> { // Notify if the device comes back online after not being seen for the configured period - let elapsed_since_last_seen: Duration = (Local::now().naive_local() - - existing_device.last_seen) + let elapsed_since_last_seen: Duration = (Local::now().to_utc() - existing_device.last_seen) .to_std() .unwrap_or(Duration::from_secs(0)); diff --git a/backend/src/main.rs b/backend/src/main.rs index 7ca76af..0c92401 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -9,6 +9,7 @@ mod db; mod device_finders; mod events; mod mac_vendor_finder; +mod model; mod settings; #[tokio::main] diff --git a/backend/src/model.rs b/backend/src/model.rs new file mode 100644 index 0000000..42df357 --- /dev/null +++ b/backend/src/model.rs @@ -0,0 +1 @@ +pub mod devices; diff --git a/backend/src/model/devices.rs b/backend/src/model/devices.rs new file mode 100644 index 0000000..2cc67cb --- /dev/null +++ b/backend/src/model/devices.rs @@ -0,0 +1,22 @@ +use chrono::{DateTime, Utc, serde::ts_seconds}; +use serde::{Deserialize, Serialize}; +use std::fmt; + +#[derive(Clone, Serialize, Deserialize)] +pub struct Device { + pub mac_address: String, + pub ipv4_address: String, + pub vendor: String, + #[serde(with = "ts_seconds")] + pub last_seen: DateTime, +} + +impl fmt::Display for Device { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!( + f, + "mac={}, ip={}, vendor={}, last_seen={}", + self.mac_address, self.ipv4_address, self.vendor, self.last_seen + ) + } +}