diff --git a/Cargo.lock b/Cargo.lock index d9b2268..f52a5a3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -81,9 +81,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.152" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "mtemp" diff --git a/src/main.rs b/src/main.rs index aea7065..da2d287 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,12 @@ mod cli; mod lm_sensors; +mod mtemp; mod nvidia_smi; mod sensor_names; + use cli::Arguments; -use lm_sensors::{sensors_data, Sensor}; +use lm_sensors::sensors_data; +use mtemp::Mtemp; use nvidia_smi::temperature; use structopt::StructOpt; @@ -18,38 +21,24 @@ fn main() { } fn print_cpu_temp() { - let sensors_data = sensors_data(); - if let Ok(sensors) = sensors_data { - print_chip_temp(sensors.cpu_chip()); + if let Some(temp) = Mtemp::cpu() { + println!("{}", temp); } } fn print_disk_temp() { - let sensors_data = sensors_data(); - if let Ok(sensors) = sensors_data { - print_chip_temp(sensors.disk_chip()); - } -} - -fn print_chip_temp(chip: Option<&Sensor>) { - if let Some(chip) = chip { - if let Some(temp) = chip.temp() { - println!("{}", temp); - } + if let Some(temp) = Mtemp::disk() { + println!("{}", temp) } } fn print_gpu_temp() { - let gpu_temp_data = temperature(); - if let Ok(temp) = gpu_temp_data { + if let Some(temp) = Mtemp::gpu() { println!("{}", temp) } } fn debug() { - let sensors_data = sensors_data(); - println!("lm-sensors: {:?}", sensors_data); - - let nvidia_smi_temp = temperature(); - println!("nvidia-smi: {:?}", nvidia_smi_temp); + println!("lm-sensors: {:?}", sensors_data()); + println!("nvidia-smi: {:?}", temperature()); } diff --git a/src/mtemp.rs b/src/mtemp.rs new file mode 100644 index 0000000..a0675a3 --- /dev/null +++ b/src/mtemp.rs @@ -0,0 +1,39 @@ +use crate::{lm_sensors::sensors_data, nvidia_smi::temperature}; + +pub struct Mtemp {} + +impl Mtemp { + pub fn cpu() -> Option { + let sensors_data = sensors_data(); + if sensors_data.is_err() { + return None; + } + + if let Some(chip) = sensors_data.unwrap().cpu_chip() { + chip.temp() + } else { + None + } + } + + pub fn disk() -> Option { + let sensors_data = sensors_data(); + if sensors_data.is_err() { + return None; + } + + if let Some(chip) = sensors_data.unwrap().disk_chip() { + chip.temp() + } else { + None + } + } + + pub fn gpu() -> Option { + if let Ok(temp) = temperature() { + Some(temp) + } else { + None + } + } +}