refactor: simplify temperature conversion function usage (#1326)

* refactor: simplify temperature conversion function usage

Just make it a function on the temperature type enum.

* fix sysinfo variant

* simple test
This commit is contained in:
Clement Tsang 2023-11-20 08:08:24 +00:00 committed by GitHub
parent 10a37c263a
commit ab9331140a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 32 deletions

View File

@ -8,9 +8,7 @@ use crate::app::Filter;
use crate::app::layout_manager::UsedWidgets;
use crate::data_harvester::memory::MemHarvest;
use crate::data_harvester::temperature::{
convert_temp_unit, is_temp_filtered, TempHarvest, TemperatureType,
};
use crate::data_harvester::temperature::{is_temp_filtered, TempHarvest, TemperatureType};
pub static NVML_DATA: Lazy<Result<Nvml, NvmlError>> = Lazy::new(Nvml::init);
@ -52,8 +50,8 @@ pub fn get_nvidia_vecs(
}
if widgets_to_harvest.use_temp && is_temp_filtered(filter, &name) {
if let Ok(temperature) = device.temperature(TemperatureSensor::Gpu) {
let temperature = temperature as f32;
let temperature = convert_temp_unit(temperature, temp_type);
let temperature = temp_type.convert_temp_unit(temperature as f32);
temp_vec.push(TempHarvest {
name: name.clone(),
temperature,

View File

@ -29,19 +29,22 @@ pub enum TemperatureType {
Fahrenheit,
}
fn convert_celsius_to_kelvin(celsius: f32) -> f32 {
celsius + 273.15
}
impl TemperatureType {
/// Given a temperature in Celsius, covert it if necessary for a different unit.
pub fn convert_temp_unit(&self, temp_celsius: f32) -> f32 {
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
}
fn convert_celsius_to_fahrenheit(celsius: f32) -> f32 {
(celsius * (9.0 / 5.0)) + 32.0
}
pub fn convert_temp_unit(temp: f32, temp_type: &TemperatureType) -> f32 {
match temp_type {
TemperatureType::Celsius => temp,
TemperatureType::Kelvin => convert_celsius_to_kelvin(temp),
TemperatureType::Fahrenheit => convert_celsius_to_fahrenheit(temp),
match self {
TemperatureType::Celsius => temp_celsius,
TemperatureType::Kelvin => convert_celsius_to_kelvin(temp_celsius),
TemperatureType::Fahrenheit => convert_celsius_to_fahrenheit(temp_celsius),
}
}
}
@ -59,3 +62,23 @@ pub fn is_temp_filtered(filter: &Option<Filter>, text: &str) -> bool {
true
}
}
#[cfg(test)]
mod test {
use crate::app::data_harvester::temperature::TemperatureType;
#[test]
fn temp_conversions() {
const TEMP: f32 = 100.0;
assert_eq!(
TemperatureType::Celsius.convert_temp_unit(TEMP),
TEMP,
"celsius to celsius is the same"
);
assert_eq!(TemperatureType::Kelvin.convert_temp_unit(TEMP), 373.15);
assert_eq!(TemperatureType::Fahrenheit.convert_temp_unit(TEMP), 212.0);
}
}

View File

@ -9,7 +9,7 @@ use anyhow::Result;
use hashbrown::{HashMap, HashSet};
use super::{is_temp_filtered, TempHarvest, TemperatureType};
use crate::app::{data_harvester::temperature::convert_temp_unit, Filter};
use crate::app::Filter;
const EMPTY_NAME: &str = "Unknown";
@ -272,7 +272,7 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H
let name = finalize_name(hwmon_name, sensor_label, &sensor_name, &mut seen_names);
if is_temp_filtered(filter, &name) {
let temp = if should_read_temp {
let temp_celsius = if should_read_temp {
if let Ok(temp) = read_temp(&temp_path) {
temp
} else {
@ -284,7 +284,7 @@ fn hwmon_temperatures(temp_type: &TemperatureType, filter: &Option<Filter>) -> H
temperatures.push(TempHarvest {
name,
temperature: convert_temp_unit(temp, temp_type),
temperature: temp_type.convert_temp_unit(temp_celsius),
});
}
}
@ -324,12 +324,12 @@ fn add_thermal_zone_temperatures(
if let Some(name) = read_to_string_lossy(name_path) {
if is_temp_filtered(filter, &name) {
let temp_path = file_path.join("temp");
if let Ok(temp) = read_temp(&temp_path) {
if let Ok(temp_celsius) = read_temp(&temp_path) {
let name = counted_name(&mut seen_names, name);
temperatures.push(TempHarvest {
name,
temperature: convert_temp_unit(temp, temp_type),
temperature: temp_type.convert_temp_unit(temp_celsius),
});
}
}

View File

@ -2,10 +2,7 @@
use anyhow::Result;
use super::{
convert_celsius_to_fahrenheit, convert_celsius_to_kelvin, is_temp_filtered, TempHarvest,
TemperatureType,
};
use super::{is_temp_filtered, TempHarvest, TemperatureType};
use crate::app::Filter;
pub fn get_temperature_data(
@ -22,13 +19,7 @@ pub fn get_temperature_data(
if is_temp_filtered(filter, &name) {
temperature_vec.push(TempHarvest {
name,
temperature: match temp_type {
TemperatureType::Celsius => component.temperature(),
TemperatureType::Kelvin => convert_celsius_to_kelvin(component.temperature()),
TemperatureType::Fahrenheit => {
convert_celsius_to_fahrenheit(component.temperature())
}
},
temperature: temp_type.convert_temp_unit(component.temperature()),
});
}
}