refactor: replace heim temp conversion code (#811)

This commit is contained in:
Clement Tsang 2022-09-17 23:43:40 -04:00 committed by GitHub
parent 10efe75fbd
commit cc048de3b0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 21 deletions

View File

@ -39,16 +39,12 @@ impl Default for TemperatureType {
} }
} }
cfg_if::cfg_if! { fn convert_celsius_to_kelvin(celsius: f32) -> f32 {
if #[cfg(any(feature = "nvidia", target_os = "macos", target_os = "windows"))] { celsius + 273.15
fn convert_celsius_to_kelvin(celsius: f32) -> f32 { }
celsius + 273.15
}
fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 { fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
(celsius * (9.0 / 5.0)) + 32.0 (celsius * (9.0 / 5.0)) + 32.0
}
}
} }
fn is_temp_filtered(filter: &Option<Filter>, text: &str) -> bool { fn is_temp_filtered(filter: &Option<Filter>, text: &str) -> bool {

View File

@ -1,7 +1,10 @@
//! Gets temperature data via heim. //! Gets temperature data via heim.
use super::{is_temp_filtered, temp_vec_sort, TempHarvest, TemperatureType}; use super::{is_temp_filtered, temp_vec_sort, TempHarvest, TemperatureType};
use crate::app::Filter; use crate::app::{
data_harvester::temperature::{convert_celsius_to_fahrenheit, convert_celsius_to_kelvin},
Filter,
};
/// Get temperature sensors from the linux sysfs interface `/sys/class/hwmon` /// Get temperature sensors from the linux sysfs interface `/sys/class/hwmon`
/// ///
@ -97,7 +100,6 @@ pub async fn get_temperature_data(
}; };
if is_temp_filtered(filter, &name) { if is_temp_filtered(filter, &name) {
use heim::units::{thermodynamic_temperature, ThermodynamicTemperature};
let temp = if should_read_temp { let temp = if should_read_temp {
let temp = fs::read_to_string(temp)?; let temp = fs::read_to_string(temp)?;
let temp = temp.trim_end().parse::<f32>().map_err(|e| { let temp = temp.trim_end().parse::<f32>().map_err(|e| {
@ -107,20 +109,13 @@ pub async fn get_temperature_data(
} else { } else {
0.0 0.0
}; };
let temp = ThermodynamicTemperature::new::<thermodynamic_temperature::degree_celsius>(
temp,
);
temperature_vec.push(TempHarvest { temperature_vec.push(TempHarvest {
name, name,
temperature: match temp_type { temperature: match temp_type {
TemperatureType::Celsius => { TemperatureType::Celsius => temp,
temp.get::<thermodynamic_temperature::degree_celsius>() TemperatureType::Kelvin => convert_celsius_to_kelvin(temp),
} TemperatureType::Fahrenheit => convert_celsius_to_fahrenheit(temp),
TemperatureType::Kelvin => temp.get::<thermodynamic_temperature::kelvin>(),
TemperatureType::Fahrenheit => {
temp.get::<thermodynamic_temperature::degree_fahrenheit>()
}
}, },
}); });
} }