wip: saving harmony inventory, currently messing with async stuff, properly understanding stuff now so I should fix it soon. The recv in the inventory agent is sync and blocking the whole thread so the request cannot be sent until the recv is killed, which is wrong. Will fix this by isolating on another thread

This commit is contained in:
2025-08-31 00:31:55 -04:00
parent e548bf619a
commit d9c26f43ee
12 changed files with 233 additions and 192 deletions

View File

@@ -1,14 +1,15 @@
use crate::hwinfo::PhysicalHost;
pub fn get_host_inventory(host: &str, port: u16) -> Result<PhysicalHost, String> {
pub async fn get_host_inventory(host: &str, port: u16) -> Result<PhysicalHost, String> {
let url = format!("http://{host}:{port}/inventory");
let client = reqwest::blocking::Client::new();
let client = reqwest::Client::new();
let response = client
.get(url)
.send()
.await
.map_err(|e| format!("Failed to download file: {e}"))?;
let host = response.json().map_err(|e| e.to_string())?;
let host = response.json().await.map_err(|e| e.to_string())?;
Ok(host)
}

View File

@@ -19,7 +19,7 @@ pub struct PhysicalHost {
pub host_uuid: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct StorageDrive {
pub name: String,
pub model: String,
@@ -33,13 +33,30 @@ pub struct StorageDrive {
pub smart_status: Option<String>,
}
impl StorageDrive {
pub fn dummy() -> Self {
Self {
name: String::new(),
model: String::new(),
serial: String::new(),
size_bytes: 0,
logical_block_size: 0,
physical_block_size: 0,
rotational: false,
wwn: None,
interface_type: String::new(),
smart_status: None,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
pub struct StorageController {
pub name: String,
pub driver: String,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MemoryModule {
pub size_bytes: u64,
pub speed_mhz: Option<u32>,
@@ -49,7 +66,7 @@ pub struct MemoryModule {
pub rank: Option<u8>,
}
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CPU {
pub model: String,
pub vendor: String,
@@ -77,7 +94,6 @@ pub struct NetworkInterface {
pub firmware_version: Option<String>,
}
#[cfg(test)]
impl NetworkInterface {
pub fn dummy() -> Self {
use harmony_macros::mac_address;

View File

@@ -1,3 +1,3 @@
pub mod client;
pub mod hwinfo;
pub mod local_presence;
pub mod client;

View File

@@ -5,10 +5,10 @@ use crate::local_presence::SERVICE_NAME;
pub type DiscoveryEvent = ServiceEvent;
pub fn discover_agents(
timeout: Option<u64>,
on_event: impl Fn(DiscoveryEvent) -> Result<(), String> + Send + 'static,
) {
pub async fn discover_agents<F>(timeout: Option<u64>, on_event: F)
where
F: FnOnce(DiscoveryEvent) -> Result<(), String> + Send + 'static + Copy,
{
// Create a new mDNS daemon.
let mdns = ServiceDaemon::new().expect("Failed to create mDNS daemon");
@@ -16,7 +16,7 @@ pub fn discover_agents(
// The receiver will be a stream of events.
let receiver = mdns.browse(SERVICE_NAME).expect("Failed to browse");
std::thread::spawn(move || {
tokio::spawn(async move {
while let Ok(event) = receiver.recv() {
if let Err(e) = on_event(event.clone()) {
error!("Event callback failed : {e}");
@@ -33,8 +33,7 @@ pub fn discover_agents(
});
if let Some(timeout) = timeout {
// Gracefully shutdown the daemon.
std::thread::sleep(std::time::Duration::from_secs(timeout));
tokio::time::sleep(std::time::Duration::from_secs(timeout)).await;
mdns.shutdown().unwrap();
}
}