2026-01-23 10:25:45 -05:00
|
|
|
mod pushover;
|
2026-01-21 11:09:58 -05:00
|
|
|
|
2026-01-23 15:08:58 -05:00
|
|
|
use std::time::Duration;
|
|
|
|
|
|
2026-01-23 10:25:45 -05:00
|
|
|
use crate::{device_finders::Device, settings::CONFIG};
|
2026-01-23 15:08:58 -05:00
|
|
|
use chrono::Local;
|
|
|
|
|
use duration_string::DurationString;
|
2026-01-23 10:25:45 -05:00
|
|
|
use log::{debug, info, warn};
|
|
|
|
|
|
|
|
|
|
// Private helper function to deliver messages
|
|
|
|
|
fn send_message(body: String) -> Result<(), String> {
|
|
|
|
|
debug!("About to send notification ({body}).");
|
|
|
|
|
|
|
|
|
|
match CONFIG.notifications.method.as_str() {
|
|
|
|
|
"pushover" => {
|
|
|
|
|
pushover::send_message(body)?;
|
|
|
|
|
}
|
|
|
|
|
other => {
|
|
|
|
|
warn!("Notification method set to '{other}'. Set logs to 'info' to see notifications.");
|
|
|
|
|
info!("Notification: {body}");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2026-01-21 11:09:58 -05:00
|
|
|
}
|
|
|
|
|
|
2026-01-23 10:25:45 -05:00
|
|
|
pub fn trigger_new_device(device: Device) -> Result<(), String> {
|
|
|
|
|
send_message(format!("New device found in your network: {device}"))?;
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn trigger_existing_device(existing_device: Device, new_device: Device) -> Result<(), String> {
|
2026-01-23 15:08:58 -05:00
|
|
|
// 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)
|
|
|
|
|
.to_std()
|
|
|
|
|
.unwrap_or(Duration::from_secs(0));
|
|
|
|
|
|
|
|
|
|
if elapsed_since_last_seen >= Duration::from(CONFIG.notifications.notify_when_not_seen_for) {
|
|
|
|
|
send_message(format!(
|
|
|
|
|
"Device MAC {} - IP {} - Vendor {} came back online after {}.",
|
|
|
|
|
new_device.mac_address,
|
|
|
|
|
new_device.ipv4_address,
|
|
|
|
|
new_device.vendor,
|
|
|
|
|
String::from(DurationString::from(Duration::from_secs(
|
|
|
|
|
elapsed_since_last_seen.as_secs()
|
|
|
|
|
)))
|
|
|
|
|
))?;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Notify if the devices vendor and/or IP changed
|
2026-01-23 10:25:45 -05:00
|
|
|
if (existing_device.ipv4_address != new_device.ipv4_address)
|
|
|
|
|
&& (existing_device.vendor != new_device.vendor)
|
|
|
|
|
{
|
|
|
|
|
send_message(format!(
|
|
|
|
|
"Device with MAC {} changed IP address from {} to {} and vendor from {} to {}.",
|
|
|
|
|
existing_device.mac_address,
|
|
|
|
|
existing_device.ipv4_address,
|
|
|
|
|
new_device.ipv4_address,
|
|
|
|
|
existing_device.vendor,
|
|
|
|
|
new_device.vendor
|
|
|
|
|
))?;
|
|
|
|
|
} else if existing_device.ipv4_address != new_device.ipv4_address {
|
|
|
|
|
send_message(format!(
|
|
|
|
|
"Device with MAC {} ({}) changed IP address from {} to {}.",
|
|
|
|
|
existing_device.mac_address,
|
|
|
|
|
new_device.vendor,
|
|
|
|
|
existing_device.ipv4_address,
|
|
|
|
|
new_device.ipv4_address
|
|
|
|
|
))?;
|
|
|
|
|
} else if existing_device.vendor != new_device.vendor {
|
|
|
|
|
send_message(format!(
|
|
|
|
|
"Device with MAC {} ({}) changed vendor from {} to {}.",
|
|
|
|
|
existing_device.mac_address,
|
|
|
|
|
existing_device.ipv4_address,
|
|
|
|
|
existing_device.vendor,
|
|
|
|
|
new_device.vendor
|
|
|
|
|
))?;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2026-01-21 11:09:58 -05:00
|
|
|
}
|