use core::fmt;
use crate::{GetListResponse, Obis, Quantity};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum StatusWord {
Edl,
Mme,
}
impl StatusWord {
const fn mask(self) -> u64 {
match self {
Self::Edl => 1 << 5,
Self::Mme => 1 << 11,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Direction {
Import,
Export,
}
impl fmt::Display for Direction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::Import => "import (+A)",
Self::Export => "export (−A)",
})
}
}
impl GetListResponse<'_> {
pub fn direction(&self, layout: StatusWord) -> Option<Direction> {
let status = [Obis::ENERGY_IMPORT, Obis::ENERGY_EXPORT]
.into_iter()
.find_map(|obis| self.get(obis).and_then(|e| e.status))?;
Some(if status & layout.mask() == 0 { Direction::Import } else { Direction::Export })
}
pub fn signed_power(&self, layout: StatusWord) -> Option<Quantity> {
let power = self.quantity(Obis::POWER)?;
Some(match self.direction(layout) {
Some(Direction::Export) if power.mantissa > 0 => Quantity { mantissa: -power.mantissa, ..power },
_ => power,
})
}
}