monitoring kit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use anyhow::*;
use std::ffi::OsStr;
use std::fs::{File, read_to_string as read};
use std::io::{Read, Seek};
use std::ops::Deref;
use std::path::PathBuf;
#[derive(Debug)]
pub struct Hwmon(String, PathBuf, Option<File>);
impl Deref for Hwmon {
    type Target = str;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}
impl Hwmon {
    pub fn load(self) -> Result<Self> {
        File::open(&self.1)
            .map(|x| Self(self.0, self.1, Some(x)))
            .context("couldnt load hwmon")
    }
    #[implicit_fn::implicit_fn]
    pub fn read(&mut self) -> Result<f64> {
        let mut o = String::default();
        let f = self.2.as_mut().unwrap();
        f.seek(std::io::SeekFrom::Start(0))?;
        f.read_to_string(&mut o)?;
        o.trim()
            .parse::<f64>()
            .map(_ / 1000.0)
            .context("parsing hwmon")
    }
}
#[implicit_fn::implicit_fn]
pub fn hwmons() -> impl Iterator<Item = Hwmon> {
    std::fs::read_dir("/sys/class/hwmon/").into_iter().flat_map(
        _.into_iter()
            .filter_map(Result::ok)
            .map(_.path())
            .map(std::fs::read_dir)
            .filter_map(Result::ok)
            .flatten()
            .filter_map(Result::ok)
            .map(_.path())
            .filter(
                _.file_name()
                    .and_then(OsStr::to_str)
                    .is_some_and(|x| x.starts_with("temp") & x.ends_with("input")),
            )
            .filter_map(|f| {
                Some(Hwmon(
                    read(f.parent()?.join(format!("temp{}_label",
                    f.file_name()?.to_str()?.bytes().skip(4)
                        .take_while(u8::is_ascii_digit)
                        .fold(0, |acc, x| acc * 10 + (x - b'0') as u64))))
                    .ok()?
                    .trim()
                    .to_string(),
                    f,
                    None,
                ))
            }),
    )
}

#[test]
fn x() {
    dbg!(hwmons().collect::<Vec<_>>());
}