sinf 0.1.0

Library for obtaining in-depth system information at both the hardware and software levels
Documentation
use crate::sys::utils::{get_nb_cpus, get_sys_value_by_name};
use crate::{Component, Error};

pub(crate) struct ComponentInner {
    id: Vec<u8>,
    component_id: String,
    label: String,
    temperature: Option<f32>,
    max: f32,
    pub(crate) updated: bool,
}

impl ComponentInner {
    pub(crate) fn new(id: Vec<u8>, temperature: f32, core: usize) -> Self {
        Self {
            id,
            component_id: format!("cpu_{}", core + 1),
            label: format!("CPU {}", core + 1),
            temperature: Some(temperature),
            max: temperature,
            updated: true,
        }
    }

    pub(crate) fn temperature(&self) -> Option<f32> {
        self.temperature
    }

    pub(crate) fn max(&self) -> Option<f32> {
        Some(self.max)
    }

    pub(crate) fn critical(&self) -> Option<f32> {
        None
    }

    pub(crate) fn id(&self) -> Option<&str> {
        Some(&self.component_id)
    }

    pub(crate) fn label(&self) -> &str {
        &self.label
    }

    pub(crate) fn refresh(&mut self) {
        self.temperature = read_temperature(&self.id);
        if let Some(temperature) = self.temperature
            && temperature > self.max
        {
            self.max = temperature;
        }
    }
}

fn read_temperature(id: &[u8]) -> Option<f32> {
    let mut temperature: libc::c_int = 0;
    if !unsafe { get_sys_value_by_name(id, &mut temperature) } {
        return None;
    }
    // Convert from Kelvin (tenths) to Celsius.
    Some((temperature - 2732) as f32 / 10.)
}

pub(crate) struct ComponentsInner {
    nb_cpus: usize,
    pub(crate) components: Vec<Component>,
}

impl ComponentsInner {
    pub(crate) fn new() -> Result<Self, Error> {
        let nb_cpus = unsafe { get_nb_cpus() };
        Ok(Self {
            nb_cpus,
            components: Vec::with_capacity(nb_cpus),
        })
    }

    pub(crate) fn list(&self) -> &[Component] {
        &self.components
    }

    pub(crate) fn list_mut(&mut self) -> &mut [Component] {
        &mut self.components
    }

    pub(crate) fn refresh(&mut self) {
        if self.components.len() != self.nb_cpus {
            for core in 0..self.nb_cpus {
                let id = format!("dev.cpu.{core}.temperature\0").into_bytes();
                if let Some(temperature) = read_temperature(&id) {
                    self.components.push(Component {
                        inner: ComponentInner::new(id, temperature, core),
                    });
                }
            }
        } else {
            for component in &mut self.components {
                component.refresh();
                component.inner.updated = true;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::ComponentInner;
    use crate::Component;

    #[test]
    fn test_components() {
        let component1 = Component {
            inner: ComponentInner::new(b"dev.cpu.0.temperature\0".to_vec(), 1.234, 0),
        };

        let component2 = Component {
            inner: ComponentInner::new(b"dev.cpu.1.temperature\0".to_vec(), 5.678, 1),
        };
        assert_eq!(component1.id(), Some("cpu_1"));
        assert_eq!(component1.label(), "CPU 1");
        assert_eq!(component1.temperature(), Some(1.234));

        assert_eq!(component2.id(), Some("cpu_2"));
        assert_eq!(component2.label(), "CPU 2");
        assert_eq!(component2.temperature(), Some(5.678));
    }
}