1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
use heim_common::{prelude::StreamExt, units::thermodynamic_temperature};
use sysinfo::{ComponentExt, System, SystemExt};
#[derive(Clone)]
pub struct TempData {
pub component_name : Box<str>,
pub temperature : f32,
}
#[derive(Clone, Debug)]
pub enum TemperatureType {
Celsius,
Kelvin,
Fahrenheit,
}
impl Default for TemperatureType {
fn default() -> Self {
TemperatureType::Celsius
}
}
pub async fn get_temperature_data(sys : &System, temp_type : &TemperatureType) -> crate::utils::error::Result<Vec<TempData>> {
let mut temperature_vec : Vec<TempData> = Vec::new();
if cfg!(target_os = "linux") {
let mut sensor_data = heim::sensors::temperatures();
while let Some(sensor) = sensor_data.next().await {
if let Ok(sensor) = sensor {
temperature_vec.push(TempData {
component_name : Box::from(sensor.unit()),
temperature : match temp_type {
TemperatureType::Celsius => sensor.current().get::<thermodynamic_temperature::degree_celsius>(),
TemperatureType::Kelvin => sensor.current().get::<thermodynamic_temperature::kelvin>(),
TemperatureType::Fahrenheit => sensor.current().get::<thermodynamic_temperature::degree_fahrenheit>(),
},
});
}
}
}
else if cfg!(target_os = "windows") {
let sensor_data = sys.get_components_list();
for component in sensor_data {
temperature_vec.push(TempData {
component_name : Box::from(component.get_label()),
temperature : match temp_type {
TemperatureType::Celsius => component.get_temperature(),
TemperatureType::Kelvin => component.get_temperature() + 273.15,
TemperatureType::Fahrenheit => (component.get_temperature() * (9.0 / 5.0)) + 32.0,
},
});
}
}
// By default, sort temperature, then by alphabetically! Allow for configuring this...
// Note we sort in reverse here; we want greater temps to be higher priority.
temperature_vec.sort_by(|a, b| {
if a.temperature > b.temperature {
std::cmp::Ordering::Less
}
else if a.temperature < b.temperature {
std::cmp::Ordering::Greater
}
else {
std::cmp::Ordering::Equal
}
});
temperature_vec.sort_by(|a, b| {
if a.component_name > b.component_name {
std::cmp::Ordering::Greater
}
else if a.component_name < b.component_name {
std::cmp::Ordering::Less
}
else {
std::cmp::Ordering::Equal
}
});
Ok(temperature_vec)
}
|