mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Moved Devices to a models module
This commit is contained in:
@@ -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> {
|
||||
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ mod db;
|
||||
mod device_finders;
|
||||
mod events;
|
||||
mod mac_vendor_finder;
|
||||
mod model;
|
||||
mod settings;
|
||||
|
||||
#[tokio::main]
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
pub mod devices;
|
||||
@@ -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
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user