#[cfg(feature = "sync")]
pub mod sync_sensors;
#[cfg(feature = "async")]
pub mod async_sensors;
mod error;
mod subfunction_type;
use std::path::{Path, PathBuf};
pub use error::Error;
pub use subfunction_type::SensorSubFunctionType;
pub trait Sensor {
fn static_base() -> &'static str where Self: Sized;
fn base(&self) -> &'static str;
fn index(&self) -> u16;
fn hwmon_path(&self) -> &Path;
fn supported_read_sub_functions(&self) -> Vec<SensorSubFunctionType> {
SensorSubFunctionType::read_list()
.filter(|&s| {
std::fs::OpenOptions::new()
.read(true)
.open(self.subfunction_path(s))
.is_ok()
})
.collect()
}
fn subfunction_path(&self, sub_type: SensorSubFunctionType) -> PathBuf {
self.hwmon_path().join(format!(
"{}{}{}",
self.base(),
self.index(),
sub_type.to_suffix()
))
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SensorState {
pub(crate) states: std::collections::HashMap<SensorSubFunctionType, String>,
}
impl SensorState {
pub fn sub_types(&self) -> impl Iterator<Item = SensorSubFunctionType> + '_ {
self.states.keys().copied()
}
pub fn value(&self, sub_type: SensorSubFunctionType) -> Option<&str> {
self.states.get(&sub_type).map(String::as_str)
}
}