mirror of
https://github.com/rzuasti/oott.git
synced 2026-07-08 19:21:54 +02:00
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:
co-authored by
Claude Sonnet 4.6
parent
b4e226d716
commit
23b8858259
@@ -8,7 +8,7 @@
|
|||||||
- [x] Record an activity log in the database
|
- [x] Record an activity log in the database
|
||||||
- [x] One event for each device appearance
|
- [x] One event for each device appearance
|
||||||
- [x] Add date filter to device event list method (from)
|
- [x] Add date filter to device event list method (from)
|
||||||
- [ ] Add a data set (json) to store maps from vendors -> device type; modify the device creation so that it uses it automatically
|
- [x] Add a data set (json) to store maps from vendors -> device type; modify the device creation so that it uses it automatically
|
||||||
|
|
||||||
## Frontend
|
## Frontend
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
use crate::mac_vendor_finder;
|
use crate::mac_vendor_finder;
|
||||||
|
use crate::vendor_device_type_finder;
|
||||||
use crate::model::devices::Device;
|
use crate::model::devices::Device;
|
||||||
use chrono::Local;
|
use chrono::Local;
|
||||||
use duration_string::DurationString;
|
use duration_string::DurationString;
|
||||||
@@ -106,12 +107,14 @@ pub async fn listen_for_packets(
|
|||||||
"Found online device - IP addr={} - MAC addr={} - vendor={}",
|
"Found online device - IP addr={} - MAC addr={} - vendor={}",
|
||||||
packet_ip_address, packet_mac_address, packet_vendor
|
packet_ip_address, packet_mac_address, packet_vendor
|
||||||
);
|
);
|
||||||
devices.push(Device::new(
|
let mut device = Device::new(
|
||||||
packet_mac_address,
|
packet_mac_address,
|
||||||
packet_ip_address,
|
packet_ip_address,
|
||||||
packet_vendor,
|
packet_vendor,
|
||||||
Local::now().to_utc(),
|
Local::now().to_utc(),
|
||||||
));
|
);
|
||||||
|
device.device_type = vendor_device_type_finder::find(&device.vendor);
|
||||||
|
devices.push(device);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ mod db;
|
|||||||
mod device_finders;
|
mod device_finders;
|
||||||
mod events;
|
mod events;
|
||||||
mod mac_vendor_finder;
|
mod mac_vendor_finder;
|
||||||
|
mod vendor_device_type_finder;
|
||||||
mod model;
|
mod model;
|
||||||
mod retention;
|
mod retention;
|
||||||
mod scanner;
|
mod scanner;
|
||||||
|
|||||||
@@ -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, "");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user