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
use super::colors::colorize;
use super::config::Config;
use futures_lite::stream::StreamExt;
use heim::sensors;
const KELVIN_TO_CELSIUS: f32 = 273.15;
async fn max_temperature() -> Result<Option<f32>, heim::Error> {
let mut sensor_data = sensors::temperatures().boxed_local();
let mut max_temp: Option<f32> = None;
while let Some(sensor) = sensor_data.next().await {
if let Ok(sensor) = sensor {
let temp = (sensor.current().value - KELVIN_TO_CELSIUS).round();
max_temp = match max_temp {
Some(current_max) => {
if temp > current_max {
Some(temp)
} else {
None
}
}
None => Some(temp),
};
}
}
Ok(max_temp)
}
pub async fn temperature(config: &Config) -> Result<String, heim::Error> {
Ok(max_temperature()
.await?
.map(|t| {
format!(
"{}°C",
colorize(t, config.threshold_temp_hot, config.threshold_temp_warm)
)
})
.unwrap_or(String::from("N/A")))
}