Auto-populate device_type from vendor mapping on first device discovery

Loads vendor-device-type.json at compile time (same pattern as mac-vendors-export.json)
and sets device_type when a new device is detected by the ARP scanner.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
rzuasti
2026-05-28 16:30:54 -04:00
co-authored by Claude Sonnet 4.6
parent b4e226d716
commit 23b8858259
4 changed files with 60 additions and 3 deletions
@@ -1,4 +1,5 @@
use crate::mac_vendor_finder;
use crate::vendor_device_type_finder;
use crate::model::devices::Device;
use chrono::Local;
use duration_string::DurationString;
@@ -106,12 +107,14 @@ pub async fn listen_for_packets(
"Found online device - IP addr={} - MAC addr={} - vendor={}",
packet_ip_address, packet_mac_address, packet_vendor
);
devices.push(Device::new(
let mut device = Device::new(
packet_mac_address,
packet_ip_address,
packet_vendor,
Local::now().to_utc(),
));
);
device.device_type = vendor_device_type_finder::find(&device.vendor);
devices.push(device);
}
}
}
+1
View File
@@ -6,6 +6,7 @@ mod db;
mod device_finders;
mod events;
mod mac_vendor_finder;
mod vendor_device_type_finder;
mod model;
mod retention;
mod scanner;
+53
View File
@@ -0,0 +1,53 @@
use lazy_static::lazy_static;
use log::{debug, error, info};
use std::collections::HashMap;
// Initialize vendor device type database as a static lazy loaded unit
lazy_static! {
static ref VENDOR_DEVICE_TYPE_DATABASE: HashMap<String, String> = {
info!("Loading vendor device type database into memory");
let data = include_str!("../data/vendor-device-type.json");
let database: HashMap<String, String> = match serde_json::from_str(data) {
Ok(value) => value,
Err(error) => {
error!(
"Error parsing vendor device type database (data/vendor-device-type.json): {error}"
);
panic!(
"Error parsing vendor device type database (data/vendor-device-type.json): {error}"
);
}
};
debug!("Found {} records in the database", database.len());
info!("Vendor device type database loaded");
database
};
}
// Find a device type based on the vendor name
pub fn find(vendor: &str) -> String {
VENDOR_DEVICE_TYPE_DATABASE
.get(vendor)
.unwrap_or(&"".to_string())
.to_string()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn known_vendor_returns_device_type() {
let result = find("Apple, Inc.");
assert!(!result.is_empty(), "Expected a device type for Apple, Inc.");
}
#[test]
fn unknown_vendor_returns_empty_string() {
let result = find("This Vendor Does Not Exist XYZ");
assert_eq!(result, "");
}
}