#![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 = 0x00A0;
pub const REVISION: u16 = 1;
pub const PICS: &str = "EGC";
pub mod feature {
pub const FORECASTING: u32 = 1 << 0;
}
pub const FEATURES: &[Feature] = &[Feature {
bit: 0,
code: "FORE",
name: "Forecasting",
conform: Conform::OPTIONAL,
}];
pub mod attribute {
use crate::im::AttributeId;
pub const LOCAL_GENERATION_AVAILABLE: AttributeId = 0x00;
pub const CURRENT_CONDITIONS: AttributeId = 0x01;
pub const FORECAST_CONDITIONS: AttributeId = 0x02;
}
pub mod event {
use crate::im::EventId;
pub const CURRENT_CONDITIONS_CHANGED: EventId = 0x00;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(u8)]
pub enum ThreeLevelEnum {
Low = 0,
Medium = 1,
High = 2,
}
impl ThreeLevelEnum {
#[must_use]
pub const fn from_value(value: u8) -> Option<Self> {
match value {
0 => Some(Self::Low),
1 => Some(Self::Medium),
2 => Some(Self::High),
_ => None,
}
}
#[must_use]
pub const fn value(self) -> u8 {
self as u8
}
}
impl ToTlv for ThreeLevelEnum {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.value().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for ThreeLevelEnum {
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 ElectricalGridConditionsStruct {
pub period_start: u32,
pub period_end: Nullable<u32>,
pub grid_carbon_intensity: i16,
pub grid_carbon_level: ThreeLevelEnum,
pub local_carbon_intensity: i16,
pub local_carbon_level: ThreeLevelEnum,
}
impl ToTlv for ElectricalGridConditionsStruct {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.period_start.to_tlv(w, Tag::Context(0))?;
self.period_end.to_tlv(w, Tag::Context(1))?;
self.grid_carbon_intensity.to_tlv(w, Tag::Context(2))?;
self.grid_carbon_level.to_tlv(w, Tag::Context(3))?;
self.local_carbon_intensity.to_tlv(w, Tag::Context(4))?;
self.local_carbon_level.to_tlv(w, Tag::Context(5))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for ElectricalGridConditionsStruct {
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 period_start = None;
let mut period_end = None;
let mut grid_carbon_intensity = None;
let mut grid_carbon_level = None;
let mut local_carbon_intensity = None;
let mut local_carbon_level = 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) => period_start = Some(<u32>::from_tlv(reader, &field)?),
Some(1) => period_end = Some(<Nullable<u32>>::from_tlv(reader, &field)?),
Some(2) => grid_carbon_intensity = Some(<i16>::from_tlv(reader, &field)?),
Some(3) => grid_carbon_level = Some(<ThreeLevelEnum>::from_tlv(reader, &field)?),
Some(4) => local_carbon_intensity = Some(<i16>::from_tlv(reader, &field)?),
Some(5) => local_carbon_level = Some(<ThreeLevelEnum>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
period_start: period_start.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
period_end: period_end.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
grid_carbon_intensity: grid_carbon_intensity
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
grid_carbon_level: grid_carbon_level
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
local_carbon_intensity: local_carbon_intensity
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
local_carbon_level: local_carbon_level
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "LocalGenerationAvailable",
kind: "bool",
access: Access::read_write_with(Privilege::View, Privilege::Operate),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "CurrentConditions",
kind: "ElectricalGridConditionsStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0002,
name: "ForecastConditions",
kind: "list[ElectricalGridConditionsStruct]",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Feature(0),
}],
},
},
];
pub const COMMANDS: &[Command] = &[];
pub const EVENTS: &[Event] = &[Event {
id: 0x00,
name: "CurrentConditionsChanged",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform::OPTIONAL,
}];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Electrical Grid Conditions",
revision: REVISION,
pics: "EGC",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};