mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
Consolidate ARP/mDNS modules under scanners::{arp,mdns}::{finder,scanner,status}
Group each discovery protocol's primitive (finder), orchestration loop (scanner), and status state under one module tree instead of splitting them between device_finders/ and crate-root *_scanner / *_scanner_status files. Also includes incidental rustfmt fixes to a few pre-existing long lines. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 4.7
parent
54211c57d7
commit
c12e700c27
@@ -0,0 +1,65 @@
|
||||
use super::finder;
|
||||
use super::status as arp_scanner_status;
|
||||
use crate::db;
|
||||
use crate::events;
|
||||
use crate::settings::get_settings;
|
||||
use chrono::Utc;
|
||||
use log::{debug, info};
|
||||
use tokio::time::{Duration, sleep};
|
||||
|
||||
pub async fn scan() -> Result<(), Box<dyn std::error::Error>> {
|
||||
loop {
|
||||
arp_scanner_status::set_running();
|
||||
|
||||
// Find online devices via ARP
|
||||
let devices = finder::find(get_settings().networking.interface.clone()).await?;
|
||||
|
||||
info!("Done with ARP probes");
|
||||
info!("Found {} online devices", devices.len());
|
||||
|
||||
// Process found devices
|
||||
for device in devices.iter() {
|
||||
debug!("Online device found {}", device);
|
||||
|
||||
// Read device from database
|
||||
let recorded_device_result = db::devices::read(device.mac_address.clone());
|
||||
|
||||
match recorded_device_result {
|
||||
Some(recorded_device) => {
|
||||
// If it exists update its last seen date
|
||||
debug!(
|
||||
"Device found in database {}. Updating to {}.",
|
||||
recorded_device, device
|
||||
);
|
||||
db::devices::seen(
|
||||
device.mac_address.clone(),
|
||||
device.ipv4_address.clone(),
|
||||
device.vendor.clone(),
|
||||
device.device_type.clone(),
|
||||
device.name.clone(),
|
||||
)?;
|
||||
events::trigger_existing_device(recorded_device, device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails
|
||||
}
|
||||
None => {
|
||||
// If it doesn't exist insert it
|
||||
debug!(
|
||||
"Device with MAC address {} not found in database. Inserting it.",
|
||||
device.mac_address
|
||||
);
|
||||
|
||||
db::devices::insert(device.clone())?;
|
||||
events::trigger_new_device(device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
let wait = Duration::from(get_settings().arp_scanner.wait_between_scans);
|
||||
let next_scan_at = Utc::now() + chrono::Duration::from_std(wait).unwrap();
|
||||
info!(
|
||||
"Scan finished. Sleeping for {}",
|
||||
get_settings().arp_scanner.wait_between_scans
|
||||
);
|
||||
arp_scanner_status::set_waiting(next_scan_at);
|
||||
sleep(wait).await;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user