another cpu temp lib

This commit is contained in:
2025-07-31 18:23:49 +02:00
parent e3498d8916
commit f26085d56b
2 changed files with 45 additions and 48 deletions

View File

@@ -18,7 +18,7 @@ chrono = "0.4"
nvml-wrapper = "0.10" nvml-wrapper = "0.10"
[target.'cfg(windows)'.dependencies] [target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["winuser"] } winapi = { version = "0.3", features = ["winuser", "pdh", "pdhmsg"] }
[target.'cfg(unix)'.dependencies] [target.'cfg(unix)'.dependencies]
glob = "0.3" glob = "0.3"

View File

@@ -107,7 +107,7 @@ impl HardwareInfo {
.map(|c| c.brand().to_string()) .map(|c| c.brand().to_string())
.unwrap_or("Unknown CPU".to_string()); .unwrap_or("Unknown CPU".to_string());
let cpu_cores = cpus.len() as i32; let cpu_cores = cpus.len() as i32;
let ram_gb = (sys.total_memory() as f64) / 1024.0 / 1024.0; let ram_gb = (sys.total_memory() as f64); // in Bytes
let gpu_type = Self::detect_gpu_name(); let gpu_type = Self::detect_gpu_name();
let ip_address = local_ip_address::local_ip()?.to_string(); let ip_address = local_ip_address::local_ip()?.to_string();
@@ -514,40 +514,49 @@ fn get_cpu_temp() -> Option<f32> {
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ fn get_cpu_temp() -> Option<f32> {
// Windows: OpenHardwareMonitor über WMI abfragen use winapi::um::pdh::{
let output = Command::new("wmic") PdhAddCounter, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQuery,
.args(&[ PDH_FMT_DOUBLE,
"/namespace:\\root\\OpenHardwareMonitor", };
"path", use winapi::um::pdhmsg::PDH_FMT_COUNTERVALUE;
"Sensor",
"get",
"Value,Name",
"/format:list",
])
.output()
.ok()?;
let stdout = String::from_utf8_lossy(&output.stdout); unsafe {
for line in stdout.lines() { let mut query_handle = 0 as u64;
if line.contains("Name=CPU Package") && line.contains("Value=") { if PdhOpenQuery(std::ptr::null_mut(), 0, &mut query_handle) != 0 {
if let Some(value) = line.split("Value=").nth(1) { return None;
return value.trim().parse::<f32>().ok();
}
} }
}
// Fallback: Standard WMI let mut counter_handle = 0 as u64;
let output = Command::new("wmic") if PdhAddCounter(
.args(&["cpu", "get", "Temperature", "/Value"]) query_handle,
.output() "\\Processor Information(_Total)\\Temperature",
.ok()?; 0,
&mut counter_handle,
let stdout = String::from_utf8_lossy(&output.stdout); ) != 0
for line in stdout.lines() { {
if line.starts_with("Temperature=") { return None;
return line.replace("Temperature=", "").trim().parse::<f32>().ok();
} }
if PdhCollectQueryData(query_handle) != 0 {
return None;
}
let mut counter_value = PDH_FMT_COUNTERVALUE {
CStatus: 0,
union: PDH_FMT_DOUBLE { doubleValue: 0.0 },
};
if PdhGetFormattedCounterValue(
counter_handle,
PDH_FMT_DOUBLE,
std::ptr::null_mut(),
&mut counter_value,
) != 0
{
return None;
}
Some(counter_value.union.doubleValue as f32)
} }
} }
@@ -574,8 +583,8 @@ fn get_disk_info() -> (f64, f64, f64) {
} }
// Berechnungen // Berechnungen
let size_gb = if count > 0 { let size_b = if count > 0 {
total_size as f64 / (1024.0 * 1024.0 * 1024.0) total_size as f64 // in Bytes
} else { } else {
// Fallback: Versuche df unter Linux // Fallback: Versuche df unter Linux
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
@@ -598,7 +607,7 @@ fn get_disk_info() -> (f64, f64, f64) {
} }
} }
} }
total_size as f64 / (1024.0 * 1024.0 * 1024.0) total_size as f64 // in Bytes
} else { } else {
0.0 0.0
} }
@@ -615,7 +624,7 @@ fn get_disk_info() -> (f64, f64, f64) {
0.0 0.0
}; };
(size_gb, usage, 0.0) // Disk-Temp bleibt 0.0 ohne spezielle Hardware (size_b, usage, 0.0) // Disk-Temp bleibt 0.0 ohne spezielle Hardware
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
@@ -745,18 +754,6 @@ mod tests {
); );
} }
// Test network traffic collection
#[test]
fn test_network_traffic() {
let traffic = get_network_traffic();
println!("Network Traffic: {:?}", traffic);
if let Some((rx, tx)) = traffic {
assert!(rx >= 0, "RX bytes should be non-negative");
assert!(tx >= 0, "TX bytes should be non-negative");
}
}
// Test hardware info collection // Test hardware info collection
#[tokio::test] #[tokio::test]
async fn test_hardware_info() { async fn test_hardware_info() {