khora_infra/platform/
sysinfo_impl.rs1use khora_core::platform::{BatteryLevel, HardwareMonitor, ThermalStatus};
18use std::sync::{Arc, Mutex};
19use sysinfo::{Components, System};
20
21pub struct SysinfoMonitor {
23 system: Arc<Mutex<System>>,
24}
25
26impl SysinfoMonitor {
27 pub fn new() -> Self {
29 let mut system = System::new_all();
30 system.refresh_all();
31 Self {
32 system: Arc::new(Mutex::new(system)),
33 }
34 }
35
36 pub fn refresh(&self) {
38 if let Ok(mut system) = self.system.lock() {
39 system.refresh_cpu_all();
40 }
42 }
43}
44
45impl HardwareMonitor for SysinfoMonitor {
46 fn thermal_status(&self) -> ThermalStatus {
47 let components = Components::new_with_refreshed_list();
48 let mut max_temp = 0.0;
49
50 for component in &components {
51 let label = component.label().to_lowercase();
52 if label.contains("cpu") || label.contains("core") {
53 if let Some(temp) = component.temperature() {
54 max_temp = f32::max(max_temp, temp);
55 }
56 }
57 }
58
59 if max_temp == 0.0 {
60 return ThermalStatus::Cool; }
62
63 if max_temp > 90.0 {
64 ThermalStatus::Critical
65 } else if max_temp > 80.0 {
66 ThermalStatus::Throttling
67 } else if max_temp > 60.0 {
68 ThermalStatus::Warm
69 } else {
70 ThermalStatus::Cool
71 }
72 }
73
74 fn battery_level(&self) -> BatteryLevel {
75 BatteryLevel::Mains
78 }
79
80 fn cpu_load(&self) -> f32 {
81 if let Ok(system) = self.system.lock() {
82 system.global_cpu_usage() / 100.0
83 } else {
84 0.0
85 }
86 }
87}
88
89impl Default for SysinfoMonitor {
90 fn default() -> Self {
91 Self::new()
92 }
93}