use super::*;
use crate::hwmon::*;
use crate::units::{Accuracy, Power, Raw};
use crate::{Parseable, ParsingResult};
#[cfg(feature = "writable")]
use std::convert::TryFrom;
pub trait PowerSensor: SensorBase {
fn read_accuracy(&self) -> Result<Accuracy> {
let raw = self.read_raw(SensorSubFunctionType::Accuracy)?;
Accuracy::from_raw(&raw).map_err(Error::from)
}
fn read_cap(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::Cap)?;
Power::from_raw(&raw).map_err(Error::from)
}
fn read_cap_max(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::CapMax)?;
Power::from_raw(&raw).map_err(Error::from)
}
fn read_cap_min(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::CapMin)?;
Power::from_raw(&raw).map_err(Error::from)
}
fn read_cap_hyst(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::CapHyst)?;
Power::from_raw(&raw).map_err(Error::from)
}
fn read_average_interval(&self) -> Result<Duration> {
let raw = self.read_raw(SensorSubFunctionType::AverageInterval)?;
Duration::from_raw(&raw).map_err(Error::from)
}
fn read_average_interval_max(&self) -> Result<Duration> {
let raw = self.read_raw(SensorSubFunctionType::AverageIntervalMax)?;
Duration::from_raw(&raw).map_err(Error::from)
}
fn read_average_interval_min(&self) -> Result<Duration> {
let raw = self.read_raw(SensorSubFunctionType::AverageIntervalMin)?;
Duration::from_raw(&raw).map_err(Error::from)
}
fn read_average_highest(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageHighest)?;
Power::from_raw(&raw).map_err(Error::from)
}
fn read_average_lowest(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageLowest)?;
Power::from_raw(&raw).map_err(Error::from)
}
fn read_average_max(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageMax)?;
Power::from_raw(&raw).map_err(Error::from)
}
fn read_average_min(&self) -> Result<Power> {
let raw = self.read_raw(SensorSubFunctionType::AverageMin)?;
Power::from_raw(&raw).map_err(Error::from)
}
#[cfg(feature = "writable")]
fn write_cap(&self, cap: Power) -> Result<()>
where
Self: WritableSensorBase,
{
self.write_raw(SensorSubFunctionType::Cap, &cap.to_raw())
}
#[cfg(feature = "writable")]
fn write_cap_hyst(&self, cap_hyst: Power) -> Result<()>
where
Self: WritableSensorBase,
{
self.write_raw(SensorSubFunctionType::CapHyst, &cap_hyst.to_raw())
}
#[cfg(feature = "writable")]
fn write_average_interval(&self, interval: Duration) -> Result<()>
where
Self: WritableSensorBase,
{
self.write_raw(SensorSubFunctionType::AverageInterval, &interval.to_raw())
}
}
impl<S: PowerSensor> Sensor<Power> for S {}
impl<S: PowerSensor> Max<Power> for S {}
impl<S: PowerSensor> Crit<Power> for S {}
impl<S: PowerSensor> Average<Power> for S {}
impl<S: PowerSensor> Highest<Power> for S {}
impl<S: PowerSensor> Lowest<Power> for S {}
#[derive(Debug, Clone)]
pub struct ReadOnlyPower {
hwmon_path: PathBuf,
index: u16,
}
impl SensorBase for ReadOnlyPower {
fn base(&self) -> &'static str {
"power"
}
fn index(&self) -> u16 {
self.index
}
fn hwmon_path(&self) -> &Path {
self.hwmon_path.as_path()
}
}
impl Parseable for ReadOnlyPower {
type Parent = ReadOnlyHwmon;
fn parse(parent: &Self::Parent, index: u16) -> ParsingResult<Self> {
let power = Self {
hwmon_path: parent.path().to_path_buf(),
index,
};
inspect_sensor(power)
}
}
impl PowerSensor for ReadOnlyPower {}
#[cfg(feature = "writable")]
impl From<ReadWritePower> for ReadOnlyPower {
fn from(write_power: ReadWritePower) -> ReadOnlyPower {
ReadOnlyPower {
hwmon_path: write_power.hwmon_path,
index: write_power.index,
}
}
}
#[cfg(feature = "writable")]
#[derive(Debug, Clone)]
pub struct ReadWritePower {
hwmon_path: PathBuf,
index: u16,
}
#[cfg(feature = "writable")]
impl SensorBase for ReadWritePower {
fn base(&self) -> &'static str {
"power"
}
fn index(&self) -> u16 {
self.index
}
fn hwmon_path(&self) -> &Path {
self.hwmon_path.as_path()
}
}
#[cfg(feature = "writable")]
impl Parseable for ReadWritePower {
type Parent = ReadWriteHwmon;
fn parse(parent: &Self::Parent, index: u16) -> ParsingResult<Self> {
let power = Self {
hwmon_path: parent.path().to_path_buf(),
index,
};
inspect_sensor(power)
}
}
#[cfg(feature = "writable")]
impl PowerSensor for ReadWritePower {}
#[cfg(feature = "writable")]
impl WritableSensorBase for ReadWritePower {}
#[cfg(feature = "writable")]
impl TryFrom<ReadOnlyPower> for ReadWritePower {
type Error = Error;
fn try_from(value: ReadOnlyPower) -> std::result::Result<Self, Self::Error> {
let read_write = ReadWritePower {
hwmon_path: value.hwmon_path,
index: value.index,
};
if read_write.supported_write_sub_functions().is_empty() {
return Err(Error::InsufficientRights {
path: read_write.hwmon_path.join(format!(
"{}{}",
read_write.base(),
read_write.index(),
)),
});
}
Ok(read_write)
}
}