2026-01-20 14:28:31 -05:00
|
|
|
use log::{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-14 09:53:01 -05:00
|
|
|
mod config;
|
2026-01-19 19:03:08 -05:00
|
|
|
mod db;
|
2026-01-13 07:57:47 -05:00
|
|
|
mod device_finders;
|
2026-01-19 12:45:52 -05:00
|
|
|
mod mac_vendor_finder;
|
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-14 09:53:01 -05:00
|
|
|
env_logger::init();
|
|
|
|
|
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
|
|
|
|
|
info!(
|
|
|
|
|
"Device found in database {}. Updating to {}.",
|
|
|
|
|
recorded_device, device
|
|
|
|
|
);
|
|
|
|
|
// TODO: If IPv4 or vendor changed do something
|
|
|
|
|
db::devices::update(db_conn_clone.lock().unwrap(), device.clone());
|
|
|
|
|
}
|
|
|
|
|
None => {
|
|
|
|
|
// If it doesn't exist insert it
|
|
|
|
|
info!(
|
|
|
|
|
"Device with MAC address {} not found in database. Inserting it.",
|
|
|
|
|
device.mac_address
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
db::devices::insert(db_conn_clone.lock().unwrap(), device.clone());
|
|
|
|
|
}
|
|
|
|
|
};
|
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
|
|
|
}
|