2026-01-21 15:36:27 -05:00
|
|
|
use log::{LevelFilter, debug, info};
|
2026-01-21 10:30:37 -05:00
|
|
|
use std::sync::{Arc, Mutex};
|
2026-01-19 19:03:08 -05:00
|
|
|
|
2026-01-21 15:36:27 -05:00
|
|
|
use crate::settings::CONFIG;
|
|
|
|
|
|
2026-01-19 19:03:08 -05:00
|
|
|
mod db;
|
2026-01-13 07:57:47 -05:00
|
|
|
mod device_finders;
|
2026-01-21 11:09:58 -05:00
|
|
|
mod events;
|
2026-01-19 12:45:52 -05:00
|
|
|
mod mac_vendor_finder;
|
2026-01-21 15:36:27 -05:00
|
|
|
mod settings;
|
2026-01-13 07:57:47 -05:00
|
|
|
|
2026-01-13 15:48:17 -05:00
|
|
|
#[tokio::main]
|
2026-01-21 10:30:37 -05:00
|
|
|
async fn main() -> Result<(), String> {
|
2026-01-21 15:36:27 -05:00
|
|
|
// Initialize logging
|
|
|
|
|
let log_level = match CONFIG.log.level.as_str() {
|
|
|
|
|
"off" => LevelFilter::Off,
|
|
|
|
|
"error" => LevelFilter::Error,
|
|
|
|
|
"warn" => LevelFilter::Warn,
|
|
|
|
|
"info" => LevelFilter::Info,
|
|
|
|
|
"debug" => LevelFilter::Debug,
|
|
|
|
|
"trace" => LevelFilter::Trace,
|
|
|
|
|
_ => LevelFilter::Error,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
env_logger::Builder::new()
|
|
|
|
|
.filter(None, log_level)
|
|
|
|
|
.write_style(env_logger::WriteStyle::Always)
|
|
|
|
|
.init();
|
|
|
|
|
|
|
|
|
|
// Now onto the important stuff
|
2026-01-14 09:53:01 -05:00
|
|
|
info!("Starting up oott");
|
2026-01-13 07:57:47 -05:00
|
|
|
|
2026-01-21 10:30:37 -05:00
|
|
|
// Get database connection - thread protected
|
|
|
|
|
let db_conn = Arc::new(Mutex::new(db::init_db().unwrap()));
|
2026-01-19 12:45:52 -05:00
|
|
|
|
2026-01-21 10:30:37 -05:00
|
|
|
// Find online devices via ARP
|
2026-01-20 14:28:31 -05:00
|
|
|
let devices = device_finders::arp::find("eno1").await.unwrap();
|
2026-01-14 09:53:01 -05:00
|
|
|
|
2026-01-20 14:28:31 -05:00
|
|
|
info!("Done with ARP probes");
|
|
|
|
|
info!("Found {} online devices", devices.iter().count());
|
|
|
|
|
|
2026-01-21 10:30:37 -05:00
|
|
|
// Process found devices
|
2026-01-20 14:28:31 -05:00
|
|
|
for device in devices.iter() {
|
2026-01-21 10:30:37 -05:00
|
|
|
debug!("Online device found {}", device);
|
|
|
|
|
|
|
|
|
|
// Using just one connection for now, need to update if moved DB portion to multi-thread
|
|
|
|
|
// need to change to a clone if we need multiple threads
|
|
|
|
|
let db_conn_clone = Arc::clone(&db_conn);
|
|
|
|
|
|
|
|
|
|
// Read device from database
|
|
|
|
|
let recorded_device_result =
|
|
|
|
|
db::devices::read(db_conn_clone.lock().unwrap(), device.mac_address.clone());
|
|
|
|
|
|
|
|
|
|
match recorded_device_result {
|
|
|
|
|
Some(recorded_device) => {
|
|
|
|
|
// If it exists update its last seen date
|
2026-01-21 11:09:58 -05:00
|
|
|
debug!(
|
2026-01-21 10:30:37 -05:00
|
|
|
"Device found in database {}. Updating to {}.",
|
|
|
|
|
recorded_device, device
|
|
|
|
|
);
|
2026-01-21 11:09:58 -05:00
|
|
|
db::devices::update(db_conn_clone.lock().unwrap(), device.clone())?;
|
2026-01-23 10:25:45 -05:00
|
|
|
events::trigger_existing_device(recorded_device, device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails
|
2026-01-21 10:30:37 -05:00
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
// If it doesn't exist insert it
|
2026-01-21 11:09:58 -05:00
|
|
|
debug!(
|
2026-01-21 10:30:37 -05:00
|
|
|
"Device with MAC address {} not found in database. Inserting it.",
|
|
|
|
|
device.mac_address
|
|
|
|
|
);
|
|
|
|
|
|
2026-01-21 11:09:58 -05:00
|
|
|
db::devices::insert(db_conn_clone.lock().unwrap(), device.clone())?;
|
2026-01-23 10:25:45 -05:00
|
|
|
events::trigger_new_device(device.clone()).ok(); // Ignoring errors here, do not stop loop if notification delivery fails
|
2026-01-21 10:30:37 -05:00
|
|
|
}
|
|
|
|
|
};
|
2026-01-20 14:28:31 -05:00
|
|
|
}
|
2026-01-19 12:45:52 -05:00
|
|
|
|
2026-01-14 09:53:01 -05:00
|
|
|
info!("Exiting");
|
2026-01-21 10:30:37 -05:00
|
|
|
|
|
|
|
|
Ok(())
|
2026-01-13 07:57:47 -05:00
|
|
|
}
|