test for winapi
This commit is contained in:
@@ -415,16 +415,108 @@ impl MetricsCollector {
|
||||
|
||||
fn get_cpu_temp() -> Option<f32> {
|
||||
println!("Attempting to get CPU temperature...");
|
||||
let mut sys = System::new_all();
|
||||
//let components = Components::new_with_refreshed_list();
|
||||
sys.refresh_all();
|
||||
for component in sys.components() {
|
||||
if let Some(temperature) = component.temperature() {
|
||||
println!("{temperature}°C");
|
||||
#[cfg(target_os = "linux")]
|
||||
{
|
||||
let mut sys = System::new_all();
|
||||
//let components = Components::new_with_refreshed_list();
|
||||
sys.refresh_all();
|
||||
for component in sys.components() {
|
||||
if let Some(temperature) = component.temperature() {
|
||||
println!(
|
||||
"Component: {}, Temperature: {}°C",
|
||||
component.label(),
|
||||
temperature
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Some(0.0)
|
||||
}
|
||||
#[cfg(target_os = "windows")]
|
||||
{
|
||||
use std::ffi::OsStr;
|
||||
use std::os::windows::ffi::OsStrExt;
|
||||
use std::ptr;
|
||||
use winapi::shared::winerror::ERROR_SUCCESS;
|
||||
use winapi::um::pdh::PDH_FMT_COUNTERVALUE;
|
||||
use winapi::um::pdh::{
|
||||
PdhAddCounterW, PdhCollectQueryData, PdhGetFormattedCounterValue, PdhOpenQueryW,
|
||||
PDH_FMT_DOUBLE,
|
||||
};
|
||||
|
||||
fn get_cpu_temp() -> Option<f32> {
|
||||
println!("Attempting to get CPU temperature on Windows...");
|
||||
|
||||
unsafe {
|
||||
let mut query = ptr::null_mut();
|
||||
let mut counter = ptr::null_mut();
|
||||
|
||||
// Open a query
|
||||
if PdhOpenQueryW(ptr::null(), 0, &mut query) != ERROR_SUCCESS as i32 {
|
||||
println!("Failed to open PDH query");
|
||||
return None;
|
||||
}
|
||||
|
||||
// Create the counter path (this is system-dependent and may need adjustment)
|
||||
let counter_path: Vec<u16> =
|
||||
OsStr::new("\\Thermal Zone Temperature(_TZ.TZ00._TMP)")
|
||||
.encode_wide()
|
||||
.chain(Some(0))
|
||||
.collect();
|
||||
|
||||
// Add counter
|
||||
if PdhAddCounterW(query, counter_path.as_ptr(), 0, &mut counter)
|
||||
!= ERROR_SUCCESS as i32
|
||||
{
|
||||
println!("Failed to add PDH counter");
|
||||
PdhCloseQuery(query);
|
||||
return None;
|
||||
}
|
||||
|
||||
// Collect data
|
||||
if PdhCollectQueryData(query) != ERROR_SUCCESS as i32 {
|
||||
println!("Failed to collect query data");
|
||||
PdhCloseQuery(query);
|
||||
return None;
|
||||
}
|
||||
|
||||
// Get formatted value
|
||||
let mut value = PDH_FMT_COUNTERVALUE {
|
||||
CStatus: 0,
|
||||
u: std::mem::zeroed(),
|
||||
};
|
||||
|
||||
if PdhGetFormattedCounterValue(counter, PDH_FMT_DOUBLE, ptr::null_mut(), &mut value)
|
||||
!= ERROR_SUCCESS as i32
|
||||
{
|
||||
println!("Failed to get formatted counter value");
|
||||
PdhCloseQuery(query);
|
||||
return None;
|
||||
}
|
||||
|
||||
PdhCloseQuery(query);
|
||||
|
||||
if value.CStatus == ERROR_SUCCESS as u32 {
|
||||
Some(*value.u.doubleValue() as f32)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function to close the PDH query
|
||||
unsafe fn PdhCloseQuery(query: *mut winapi::ctypes::c_void) {
|
||||
use winapi::um::pdh::PdhCloseQuery;
|
||||
PdhCloseQuery(query);
|
||||
}
|
||||
}
|
||||
|
||||
Some(0.0) // Placeholder, actual implementation depends on platform
|
||||
#[cfg(not(any(target_os = "linux", target_os = "windows")))]
|
||||
{
|
||||
// Fallback for unsupported OS
|
||||
println!("CPU temperature retrieval not supported on this OS.");
|
||||
None
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user