#![allow(unused_imports)]
use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
use crate::dm::meta::{EventPriority, Reporting};
use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
use crate::error::Result;
use crate::im::{AttributeId, ClusterId, CommandId, EventId};
use crate::tlv::{
ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
};
pub const ID: ClusterId = 0x0091;
pub const REVISION: u16 = 2;
pub const PICS: &str = "EEM";
pub mod feature {
pub const IMPORTED_ENERGY: u32 = 1 << 0;
pub const EXPORTED_ENERGY: u32 = 1 << 1;
pub const CUMULATIVE_ENERGY: u32 = 1 << 2;
pub const PERIODIC_ENERGY: u32 = 1 << 3;
pub const APPARENT_ENERGY: u32 = 1 << 4;
pub const REACTIVE_ENERGY: u32 = 1 << 5;
}
pub const FEATURES: &[Feature] = &[
Feature {
bit: 0,
code: "IMPE",
name: "ImportedEnergy",
conform: Conform::OPTIONAL,
},
Feature {
bit: 1,
code: "EXPE",
name: "ExportedEnergy",
conform: Conform::OPTIONAL,
},
Feature {
bit: 2,
code: "CUME",
name: "CumulativeEnergy",
conform: Conform::OPTIONAL,
},
Feature {
bit: 3,
code: "PERE",
name: "PeriodicEnergy",
conform: Conform::OPTIONAL,
},
Feature {
bit: 4,
code: "APPE",
name: "ApparentEnergy",
conform: Conform {
clauses: &[
Clause {
verdict: Conformance::Provisional,
when: Condition::Always,
},
Clause {
verdict: Conformance::Optional,
when: Condition::Always,
},
],
},
},
Feature {
bit: 5,
code: "REAE",
name: "ReactiveEnergy",
conform: Conform {
clauses: &[
Clause {
verdict: Conformance::Provisional,
when: Condition::Always,
},
Clause {
verdict: Conformance::Optional,
when: Condition::Always,
},
],
},
},
];
pub mod attribute {
use crate::im::AttributeId;
pub const ACCURACY: AttributeId = 0x00;
pub const CUMULATIVE_ENERGY_IMPORTED: AttributeId = 0x01;
pub const CUMULATIVE_ENERGY_EXPORTED: AttributeId = 0x02;
pub const PERIODIC_ENERGY_IMPORTED: AttributeId = 0x03;
pub const PERIODIC_ENERGY_EXPORTED: AttributeId = 0x04;
pub const CUMULATIVE_ENERGY_RESET: AttributeId = 0x05;
}
pub mod event {
use crate::im::EventId;
pub const CUMULATIVE_ENERGY_MEASURED: EventId = 0x00;
pub const PERIODIC_ENERGY_MEASURED: EventId = 0x01;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum MeasurementTypeEnum {
Unspecified = 0,
Voltage = 1,
ActiveCurrent = 2,
ReactiveCurrent = 3,
ApparentCurrent = 4,
ActivePower = 5,
ReactivePower = 6,
ApparentPower = 7,
RMSVoltage = 8,
RMSCurrent = 9,
RMSPower = 10,
Frequency = 11,
PowerFactor = 12,
NeutralCurrent = 13,
ElectricalEnergy = 14,
ReactiveEnergy = 15,
ApparentEnergy = 16,
}
impl MeasurementTypeEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Unspecified),
1 => Some(Self::Voltage),
2 => Some(Self::ActiveCurrent),
3 => Some(Self::ReactiveCurrent),
4 => Some(Self::ApparentCurrent),
5 => Some(Self::ActivePower),
6 => Some(Self::ReactivePower),
7 => Some(Self::ApparentPower),
8 => Some(Self::RMSVoltage),
9 => Some(Self::RMSCurrent),
10 => Some(Self::RMSPower),
11 => Some(Self::Frequency),
12 => Some(Self::PowerFactor),
13 => Some(Self::NeutralCurrent),
14 => Some(Self::ElectricalEnergy),
15 => Some(Self::ReactiveEnergy),
16 => Some(Self::ApparentEnergy),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for MeasurementTypeEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for MeasurementTypeEnum {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let value = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_value(value).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CumulativeEnergyResetStruct {
pub imported_reset_timestamp: Option<Nullable<u32>>,
pub exported_reset_timestamp: Option<Nullable<u32>>,
pub imported_reset_systime: Option<Nullable<u64>>,
pub exported_reset_systime: Option<Nullable<u64>>,
}
impl ToTlv for CumulativeEnergyResetStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
if let Some(value) = &self.imported_reset_timestamp {
value.to_tlv(w, Tag::Context(0))?;
}
if let Some(value) = &self.exported_reset_timestamp {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.imported_reset_systime {
value.to_tlv(w, Tag::Context(2))?;
}
if let Some(value) = &self.exported_reset_systime {
value.to_tlv(w, Tag::Context(3))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for CumulativeEnergyResetStruct {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut imported_reset_timestamp = None;
let mut exported_reset_timestamp = None;
let mut imported_reset_systime = None;
let mut exported_reset_systime = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => {
imported_reset_timestamp = Some(<Nullable<u32>>::from_tlv(reader, &field)?)
}
Some(1) => {
exported_reset_timestamp = Some(<Nullable<u32>>::from_tlv(reader, &field)?)
}
Some(2) => {
imported_reset_systime = Some(<Nullable<u64>>::from_tlv(reader, &field)?)
}
Some(3) => {
exported_reset_systime = Some(<Nullable<u64>>::from_tlv(reader, &field)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
imported_reset_timestamp,
exported_reset_timestamp,
imported_reset_systime,
exported_reset_systime,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct EnergyMeasurementStruct {
pub energy: i64,
pub start_timestamp: Option<u32>,
pub end_timestamp: Option<u32>,
pub start_systime: Option<u64>,
pub end_systime: Option<u64>,
pub apparent_energy: Option<i64>,
pub reactive_energy: Option<i64>,
}
impl ToTlv for EnergyMeasurementStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.energy.to_tlv(w, Tag::Context(0))?;
if let Some(value) = &self.start_timestamp {
value.to_tlv(w, Tag::Context(1))?;
}
if let Some(value) = &self.end_timestamp {
value.to_tlv(w, Tag::Context(2))?;
}
if let Some(value) = &self.start_systime {
value.to_tlv(w, Tag::Context(3))?;
}
if let Some(value) = &self.end_systime {
value.to_tlv(w, Tag::Context(4))?;
}
if let Some(value) = &self.apparent_energy {
value.to_tlv(w, Tag::Context(5))?;
}
if let Some(value) = &self.reactive_energy {
value.to_tlv(w, Tag::Context(6))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for EnergyMeasurementStruct {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut energy = None;
let mut start_timestamp = None;
let mut end_timestamp = None;
let mut start_systime = None;
let mut end_systime = None;
let mut apparent_energy = None;
let mut reactive_energy = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => energy = Some(<i64>::from_tlv(reader, &field)?),
Some(1) => start_timestamp = Some(<u32>::from_tlv(reader, &field)?),
Some(2) => end_timestamp = Some(<u32>::from_tlv(reader, &field)?),
Some(3) => start_systime = Some(<u64>::from_tlv(reader, &field)?),
Some(4) => end_systime = Some(<u64>::from_tlv(reader, &field)?),
Some(5) => apparent_energy = Some(<i64>::from_tlv(reader, &field)?),
Some(6) => reactive_energy = Some(<i64>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
energy: energy.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
start_timestamp,
end_timestamp,
start_systime,
end_systime,
apparent_energy,
reactive_energy,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MeasurementAccuracyRangeStruct {
pub range_min: i64,
pub range_max: i64,
pub percent_max: Option<u16>,
pub percent_min: Option<u16>,
pub percent_typical: Option<u16>,
pub fixed_max: Option<u64>,
pub fixed_min: Option<u64>,
pub fixed_typical: Option<u64>,
}
impl ToTlv for MeasurementAccuracyRangeStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.range_min.to_tlv(w, Tag::Context(0))?;
self.range_max.to_tlv(w, Tag::Context(1))?;
if let Some(value) = &self.percent_max {
value.to_tlv(w, Tag::Context(2))?;
}
if let Some(value) = &self.percent_min {
value.to_tlv(w, Tag::Context(3))?;
}
if let Some(value) = &self.percent_typical {
value.to_tlv(w, Tag::Context(4))?;
}
if let Some(value) = &self.fixed_max {
value.to_tlv(w, Tag::Context(5))?;
}
if let Some(value) = &self.fixed_min {
value.to_tlv(w, Tag::Context(6))?;
}
if let Some(value) = &self.fixed_typical {
value.to_tlv(w, Tag::Context(7))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for MeasurementAccuracyRangeStruct {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut range_min = None;
let mut range_max = None;
let mut percent_max = None;
let mut percent_min = None;
let mut percent_typical = None;
let mut fixed_max = None;
let mut fixed_min = None;
let mut fixed_typical = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => range_min = Some(<i64>::from_tlv(reader, &field)?),
Some(1) => range_max = Some(<i64>::from_tlv(reader, &field)?),
Some(2) => percent_max = Some(<u16>::from_tlv(reader, &field)?),
Some(3) => percent_min = Some(<u16>::from_tlv(reader, &field)?),
Some(4) => percent_typical = Some(<u16>::from_tlv(reader, &field)?),
Some(5) => fixed_max = Some(<u64>::from_tlv(reader, &field)?),
Some(6) => fixed_min = Some(<u64>::from_tlv(reader, &field)?),
Some(7) => fixed_typical = Some(<u64>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
range_min: range_min.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
range_max: range_max.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
percent_max,
percent_min,
percent_typical,
fixed_max,
fixed_min,
fixed_typical,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct MeasurementAccuracyStruct<'a> {
pub measurement_type: MeasurementTypeEnum,
pub measured: bool,
pub min_measured_value: i64,
pub max_measured_value: i64,
pub accuracy_ranges: TlvList<'a, MeasurementAccuracyRangeStruct>,
}
impl<'a> ToTlv for MeasurementAccuracyStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.measurement_type.to_tlv(w, Tag::Context(0))?;
self.measured.to_tlv(w, Tag::Context(1))?;
self.min_measured_value.to_tlv(w, Tag::Context(2))?;
self.max_measured_value.to_tlv(w, Tag::Context(3))?;
self.accuracy_ranges.to_tlv(w, Tag::Context(4))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for MeasurementAccuracyStruct<'a> {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
if element.value.container() != Some(ContainerKind::Structure) {
return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
}
let mut measurement_type = None;
let mut measured = None;
let mut min_measured_value = None;
let mut max_measured_value = None;
let mut accuracy_ranges = None;
loop {
let Some(field) = reader.next_element()? else {
return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
};
if field.value == Value::EndOfContainer {
break;
}
match field.tag.context() {
Some(0) => {
measurement_type = Some(<MeasurementTypeEnum>::from_tlv(reader, &field)?)
}
Some(1) => measured = Some(<bool>::from_tlv(reader, &field)?),
Some(2) => min_measured_value = Some(<i64>::from_tlv(reader, &field)?),
Some(3) => max_measured_value = Some(<i64>::from_tlv(reader, &field)?),
Some(4) => {
accuracy_ranges = Some(<TlvList<'a, MeasurementAccuracyRangeStruct>>::from_tlv(
reader, &field,
)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
measurement_type: measurement_type
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
measured: measured.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
min_measured_value: min_measured_value
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
max_measured_value: max_measured_value
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
accuracy_ranges: accuracy_ranges
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "Accuracy",
kind: "MeasurementAccuracyStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::FIXED,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "CumulativeEnergyImported",
kind: "EnergyMeasurementStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::Quieter,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(0), Condition::Feature(2)]),
}],
},
},
Attribute {
id: 0x0002,
name: "CumulativeEnergyExported",
kind: "EnergyMeasurementStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::Quieter,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(1), Condition::Feature(2)]),
}],
},
},
Attribute {
id: 0x0003,
name: "PeriodicEnergyImported",
kind: "EnergyMeasurementStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::Quieter,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(0), Condition::Feature(3)]),
}],
},
},
Attribute {
id: 0x0004,
name: "PeriodicEnergyExported",
kind: "EnergyMeasurementStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::Quieter,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::All(&[Condition::Feature(1), Condition::Feature(3)]),
}],
},
},
Attribute {
id: 0x0005,
name: "CumulativeEnergyReset",
kind: "CumulativeEnergyResetStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(2),
}],
},
},
];
pub const COMMANDS: &[Command] = &[];
pub const EVENTS: &[Event] = &[
Event {
id: 0x00,
name: "CumulativeEnergyMeasured",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(2),
}],
},
},
Event {
id: 0x01,
name: "PeriodicEnergyMeasured",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(3),
}],
},
},
];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Electrical Energy Measurement",
revision: REVISION,
pics: "EEM",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};