Moved Devices to a models module

This commit is contained in:
Ricardo Zuasti
2026-02-14 12:30:12 -05:00
parent 3185af3fdb
commit 7c96939b2d
10 changed files with 34 additions and 29 deletions
+1
View File
@@ -276,6 +276,7 @@ dependencies = [
"iana-time-zone", "iana-time-zone",
"js-sys", "js-sys",
"num-traits", "num-traits",
"serde",
"wasm-bindgen", "wasm-bindgen",
"windows-link", "windows-link",
] ]
+1 -1
View File
@@ -13,7 +13,7 @@ serde_json = "1.0"
rusqlite = { version = "0.38.0", features = ["bundled", "chrono"] } rusqlite = { version = "0.38.0", features = ["bundled", "chrono"] }
rusqlite_migration = { version = "2.4.0", features = ["from-directory"] } rusqlite_migration = { version = "2.4.0", features = ["from-directory"] }
include_dir = "0.7.4" include_dir = "0.7.4"
chrono = "0.4.43" chrono = {version = "0.4.43", features = ["serde"]}
pushover = "0.4.0" pushover = "0.4.0"
config = "0.15.19" config = "0.15.19"
lazy_static = "1.5.0" lazy_static = "1.5.0"
+1 -1
View File
@@ -2,7 +2,7 @@ use log::{debug, error};
use rusqlite::{Connection, params}; use rusqlite::{Connection, params};
use std::sync::MutexGuard; use std::sync::MutexGuard;
use crate::device_finders::Device; use crate::model::devices::Device;
// Read device from its MAC address // Read device from its MAC address
pub fn read(conn: MutexGuard<Connection>, mac_address: String) -> Option<Device> { pub fn read(conn: MutexGuard<Connection>, mac_address: String) -> Option<Device> {
-21
View File
@@ -1,22 +1 @@
pub mod arp; 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
)
}
}
+2 -1
View File
@@ -1,6 +1,7 @@
mod packet_send_receive; 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 duration_string::DurationString;
use log::{debug, error, info, warn}; use log::{debug, error, info, warn};
use packet_send_receive::{listen_for_packets, send_packet}; use packet_send_receive::{listen_for_packets, send_packet};
@@ -1,5 +1,5 @@
use crate::device_finders::Device;
use crate::mac_vendor_finder; use crate::mac_vendor_finder;
use crate::model::devices::Device;
use chrono::Local; use chrono::Local;
use duration_string::DurationString; use duration_string::DurationString;
use log::{debug, info, trace}; use log::{debug, info, trace};
@@ -110,7 +110,7 @@ pub async fn listen_for_packets(
mac_address: packet_mac_address, mac_address: packet_mac_address,
ipv4_address: packet_ip_address, ipv4_address: packet_ip_address,
vendor: packet_vendor, vendor: packet_vendor,
last_seen: Local::now().naive_local(), last_seen: Local::now().to_utc(),
}); });
} }
} }
+3 -3
View File
@@ -2,7 +2,8 @@ mod pushover;
use std::time::Duration; use std::time::Duration;
use crate::{device_finders::Device, settings::CONFIG}; use crate::model::devices::Device;
use crate::settings::CONFIG;
use chrono::Local; use chrono::Local;
use duration_string::DurationString; use duration_string::DurationString;
use log::{debug, info, warn}; 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> { 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 // 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() let elapsed_since_last_seen: Duration = (Local::now().to_utc() - existing_device.last_seen)
- existing_device.last_seen)
.to_std() .to_std()
.unwrap_or(Duration::from_secs(0)); .unwrap_or(Duration::from_secs(0));
+1
View File
@@ -9,6 +9,7 @@ mod db;
mod device_finders; mod device_finders;
mod events; mod events;
mod mac_vendor_finder; mod mac_vendor_finder;
mod model;
mod settings; mod settings;
#[tokio::main] #[tokio::main]
+1
View File
@@ -0,0 +1 @@
pub mod devices;
+22
View File
@@ -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<Utc>,
}
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
)
}
}