cpu-temp 0.1.0

An Intel CPU temperature monitoring library for Windows and Linux using MSR access
Documentation
#![cfg(feature = "cli")]

use std::time::{Duration, Instant};

use cpu_temp::{
    cpu::info::{CpuInfo, Vendor},
    ui::{CpuTempApp, TemperatureMessage},
};

#[cfg(target_os = "windows")]
use cpu_temp::msr::intel_msr::IntelMsr;

fn main() -> anyhow::Result<()> {
    // 获取CPU信息
    let cpuid = match CpuInfo::new() {
        Some(cpuid) => cpuid,
        None => {
            anyhow::bail!("Failed to get CPU information");
        }
    };

    // 检查是否为Intel CPU
    if cpuid.get_vendor() != Vendor::Intel {
        anyhow::bail!("This implementation only supports Intel CPUs");
    }

    let cpu_name = cpuid.get_name().to_string();
    let core_count = CpuInfo::get_core_cpu_mapping()
        .map(|m| {
            m.iter()
                .filter_map(|c| c.first().copied())
                .collect::<Vec<_>>()
                .len()
        })
        .unwrap_or(0);

    // 创建UI应用状态
    let mut app = CpuTempApp {
        cpu_name: format!("{} ({} cores)", cpu_name, core_count),
        ..Default::default()
    };

    // 创建通道用于发送温度数据
    let (tx, rx) = crossbeam_channel::unbounded();
    let update_interval = Duration::from_millis(500);

    // 启动后台线程读取温度
    std::thread::spawn(move || {
        use cpu_temp::cpu::intel::IntelCpuTemperature;
        let mut last_update = Instant::now();

        // 创建温度传感器
        let temperature_monitor = match IntelCpuTemperature::new() {
            Ok(m) => m,
            Err(e) => {
                let _ = tx.send(TemperatureMessage::InitError(e.to_string()));
                return;
            }
        };

        #[cfg(target_os = "windows")]
        {
            // 创建MSR访问器
            let mut msr = match IntelMsr::new() {
                Ok(msr) => msr,
                Err(e) => {
                    let _ = tx.send(TemperatureMessage::InitError(e.to_string()));
                    return;
                }
            };

            loop {
                if last_update.elapsed() >= update_interval {
                    last_update = Instant::now();
                    let _ = match temperature_monitor.get_temperatures(&mut msr) {
                        Ok(d) => tx.send(TemperatureMessage::Ok(d)),
                        Err(e) => tx.send(TemperatureMessage::RuntimeError(e)),
                    };
                }
                std::thread::sleep(Duration::from_millis(100));
            }
        }

        #[cfg(target_os = "linux")]
        {
            let mut dummy = ();
            loop {
                if last_update.elapsed() >= update_interval {
                    last_update = Instant::now();
                    let _ = match temperature_monitor.get_temperatures(&mut dummy) {
                        Ok(d) => tx.send(TemperatureMessage::Ok(d)),
                        Err(e) => tx.send(TemperatureMessage::RuntimeError(e)),
                    };
                }
                std::thread::sleep(Duration::from_millis(100));
            }
        }
    });

    // 运行UI,从通道接收温度数据
    app.run_with_data_receiver(rx)?;

    Ok(())
}