moving code into mtemp struct

This commit is contained in:
Jorge Bolois 2024-02-03 12:18:08 +01:00
parent bedc643bbf
commit ecc2d9f57b
3 changed files with 52 additions and 24 deletions

4
Cargo.lock generated
View File

@ -81,9 +81,9 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.152" version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13e3bf6590cbc649f4d1a3eefc9d5d6eb746f5200ffb04e5e142700b8faa56e7" checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]] [[package]]
name = "mtemp" name = "mtemp"

View File

@ -1,9 +1,12 @@
mod cli; mod cli;
mod lm_sensors; mod lm_sensors;
mod mtemp;
mod nvidia_smi; mod nvidia_smi;
mod sensor_names; mod sensor_names;
use cli::Arguments; use cli::Arguments;
use lm_sensors::{sensors_data, Sensor}; use lm_sensors::sensors_data;
use mtemp::Mtemp;
use nvidia_smi::temperature; use nvidia_smi::temperature;
use structopt::StructOpt; use structopt::StructOpt;
@ -18,38 +21,24 @@ fn main() {
} }
fn print_cpu_temp() { fn print_cpu_temp() {
let sensors_data = sensors_data(); if let Some(temp) = Mtemp::cpu() {
if let Ok(sensors) = sensors_data { println!("{}", temp);
print_chip_temp(sensors.cpu_chip());
} }
} }
fn print_disk_temp() { fn print_disk_temp() {
let sensors_data = sensors_data(); if let Some(temp) = Mtemp::disk() {
if let Ok(sensors) = sensors_data { println!("{}", temp)
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);
}
} }
} }
fn print_gpu_temp() { fn print_gpu_temp() {
let gpu_temp_data = temperature(); if let Some(temp) = Mtemp::gpu() {
if let Ok(temp) = gpu_temp_data {
println!("{}", temp) println!("{}", temp)
} }
} }
fn debug() { fn debug() {
let sensors_data = sensors_data(); println!("lm-sensors: {:?}", sensors_data());
println!("lm-sensors: {:?}", sensors_data); println!("nvidia-smi: {:?}", temperature());
let nvidia_smi_temp = temperature();
println!("nvidia-smi: {:?}", nvidia_smi_temp);
} }

39
src/mtemp.rs Normal file
View File

@ -0,0 +1,39 @@
use crate::{lm_sensors::sensors_data, nvidia_smi::temperature};
pub struct Mtemp {}
impl Mtemp {
pub fn cpu() -> Option<f64> {
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<f64> {
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<f64> {
if let Ok(temp) = temperature() {
Some(temp)
} else {
None
}
}
}