summaryrefslogtreecommitdiffstats
path: root/src/app/data_harvester/temperature.rs
blob: c3925ceb007156fb6fb6045dd8daaea0165a99a0 (plain)
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use std::cmp::Ordering;

#[derive(Default, Debug, Clone)]
pub struct TempHarvest {
    pub component_name: Option<String>,
    pub component_label: Option<String>,
    pub temperature: f32,
}

#[derive(Clone, Debug)]
pub enum TemperatureType {
    Celsius,
    Kelvin,
    Fahrenheit,
}

impl Default for TemperatureType {
    fn default() -> Self {
        TemperatureType::Celsius
    }
}

#[cfg(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm"))]
pub async fn get_temperature_data(
    sys: &sysinfo::System, temp_type: &TemperatureType, actually_get: bool,
) -> crate::utils::error::Result<Option<Vec<TempHarvest>>> {
    use sysinfo::{ComponentExt, SystemExt};

    if !actually_get {
        return Ok(None);
    }

    fn convert_celsius_to_kelvin(celsius: f32) -> f32 {
        celsius + 273.15
    }

    fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
        (celsius * (9.0 / 5.0)) + 32.0
    }

    let mut temperature_vec: Vec<TempHarvest> = Vec::new();

    let sensor_data = sys.get_components();
    for component in sensor_data {
        temperature_vec.push(TempHarvest {
            component_name: None,
            component_label: Some(component.get_label().to_string()),
            temperature: match temp_type {
                TemperatureType::Celsius => component.get_temperature(),
                TemperatureType::Kelvin => convert_celsius_to_kelvin(component.get_temperature()),
                TemperatureType::Fahrenheit => {
                    convert_celsius_to_fahrenheit(component.get_temperature())
                }
            },
        });
    }

    temp_vec_sort(&mut temperature_vec);
    Ok(Some(temperature_vec))
}

#[cfg(not(any(not(target_os = "linux"), target_arch = "aarch64", target_arch = "arm")))]
pub async fn get_temperature_data(
    temp_type: &TemperatureType, actually_get: bool,
) -> crate::utils::error::Result<Option<Vec<TempHarvest>>> {
    use futures::StreamExt;

    if !actually_get {
        return Ok(None);
    }

    let mut temperature_vec: Vec<TempHarvest> = Vec::new();

    use heim::units::thermodynamic_temperature;
    let mut sensor_data = heim::sensors::temperatures();
    while let Some(sensor) = sensor_data.next().await {
        if let Ok(sensor) = sensor {
            temperature_vec.push(TempHarvest {
                component_name: Some(sensor.unit().to_string()),
                component_label: if let Some(label) = sensor.label() {
                    Some(label.to_string())
                } else {
                    None
                },
                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>(
                    ),
                },
            });
        }
    }

    temp_vec_sort(&mut temperature_vec);
    Ok(Some(temperature_vec))
}

fn temp_vec_sort(temperature_vec: &mut Vec<TempHarvest>) {
    // By default, sort temperature, then by alphabetically!
    // TODO: [TEMPS] Allow users to control this.

    // Note we sort in reverse here; we want greater temps to be higher priority.
    temperature_vec.sort_by(|a, b| match a.temperature.partial_cmp(&b.temperature) {
        Some(x) => match x {
            Ordering::Less => Ordering::Greater,
            Ordering::Greater => Ordering::Less,
            Ordering::Equal => Ordering::Equal,
        },
        None => Ordering::Equal,
    });

    temperature_vec.sort_by(|a, b| {
        a.component_name
            .partial_cmp(&b.component_name)
            .unwrap_or(Ordering::Equal)
    });
}