From 31836c60673b5697eb8bc01b3c010bca8aaf7217 Mon Sep 17 00:00:00 2001 From: xml Date: Mon, 15 Apr 2024 09:55:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0hwmon?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/hwmon.rs | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 15 ++++++++ 2 files changed, 118 insertions(+) create mode 100644 src/hwmon.rs diff --git a/src/hwmon.rs b/src/hwmon.rs new file mode 100644 index 0000000..60b5499 --- /dev/null +++ b/src/hwmon.rs @@ -0,0 +1,103 @@ +// /sys/class/hwmon/hwmon +use std::collections::HashMap; + +static ROOTPATH: &str = "/sys/class/hwmon"; + +#[derive(Clone, Debug)] +pub struct Hwmon { + pub node: String, + pub name: String, +} + +impl Hwmon { + pub fn new(path: &str) -> std::io::Result { + let path = std::path::Path::new(path); + let node = path.file_name().unwrap().to_str().unwrap().to_string(); + let name = crate::utils::read_line(&path.join("name").to_str().unwrap().to_string())?; + + Ok(Hwmon{ + node, + name, + }) + } + + pub fn fans(&self) -> HashMap { + let mut node_vals :HashMap= HashMap::new(); + for entry in walkdir::WalkDir::new(format!("{}/{}", ROOTPATH, self.node)) + .sort_by_file_name() + .max_depth(1) + .into_iter() + .filter_map(Result::ok) + .filter(|e| (e.file_type().is_file() || e.file_type().is_symlink())) { + let re = regex::Regex::new(r"^fan\d{1,3}_input$").unwrap(); + if re.is_match(entry.path().file_name().unwrap().to_str().unwrap()) { + if let Ok(raw_val) = crate::utils::read_line(entry.path().to_str().unwrap()) { + if let Ok(val) = raw_val.parse::() { + let key = entry.path().file_name().unwrap().to_str().unwrap().to_string(); + node_vals.insert(key, val); + } + } + } + } + + node_vals + } + + pub fn temps(&self) -> HashMap { + let mut node_vals: HashMap = HashMap::new(); + for entry in walkdir::WalkDir::new(format!("{}/{}", ROOTPATH, self.node)) + .sort_by_file_name() + .max_depth(1) + .into_iter() + .filter_map(Result::ok) + .filter(|e| (e.file_type().is_file() || e.file_type().is_symlink())) { + //println!("path: {:#?}", entry.path()); + let re = regex::Regex::new(r"^temp(?\d{1,3})_input$").unwrap(); + if let Some(caps) = re.captures(entry.path().file_name().unwrap().to_str().unwrap()) { + let num = caps["num"].to_string(); + + + let val = if let Ok(raw_val) = crate::utils::read_line(entry.path().to_str().unwrap()) { + if let Ok(val) = raw_val.parse::() { + val + } else { + continue; + } + } else { + continue; + }; + + let key = if let Ok(val) = crate::utils::read_line(&format!("{}/{}/temp{}_label", ROOTPATH, self.node, num)) { + val + } else { + format!("temp{}_label", num) + }; + node_vals.insert(key, val); + } + } + + node_vals + } +} + +pub fn enumerate() -> Vec { + let mut hwmons = Vec::new(); + for entry in walkdir::WalkDir::new(ROOTPATH) + .sort_by_file_name() + .max_depth(1) + .into_iter() + .filter_map(Result::ok) + .filter(|e| (e.file_type().is_dir() || e.file_type().is_symlink())){ + if entry.path().to_str().unwrap() != ROOTPATH { + //println!("node: {:#?}", entry.path()); + let re = regex::Regex::new(r"^hwmon\d{1,3}$").unwrap(); + if re.is_match(entry.path().file_name().unwrap().to_str().unwrap().trim()) { + if let Ok(hwmon) = Hwmon::new(entry.path().to_str().unwrap().trim()) { + hwmons.push(hwmon) + } + } + } + } + + hwmons +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 9f1a011..51f5314 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ mod visualization; mod cpu; mod battery; mod thermal; +mod hwmon; use walkdir; use regex; @@ -287,6 +288,20 @@ fn main() -> std::io::Result<()> { println!("{:#?}", bats); let thermals = thermal::enumerate(); println!("{:#?}", thermals); + let hwmons = hwmon::enumerate(); + for hwmon in hwmons { + let fans = hwmon.fans(); + let temps = hwmon.temps(); + if !fans.is_empty() || !temps.is_empty() { + println!("name: {}", hwmon.name); + if !fans.is_empty() { + println!("{:#?}", hwmon.fans()); + } + if !temps.is_empty() { + println!("{:#?}", hwmon.temps()); + } + } + } }, Command::Collect { time } => { let mut collectors: Vec> = Vec::new();