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 -1
View File
@@ -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<Connection>, mac_address: String) -> Option<Device> {
-21
View File
@@ -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
)
}
}
+2 -1
View File
@@ -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};
@@ -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(),
});
}
}
+3 -3
View File
@@ -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));
+1
View File
@@ -9,6 +9,7 @@ mod db;
mod device_finders;
mod events;
mod mac_vendor_finder;
mod model;
mod settings;
#[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
)
}
}