#![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 = 0x0095;
pub const REVISION: u16 = 4;
pub const PICS: &str = "SEPR";
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 TARIFF_UNIT: AttributeId = 0x00;
pub const CURRENCY: AttributeId = 0x01;
pub const CURRENT_PRICE: AttributeId = 0x02;
pub const PRICE_FORECAST: AttributeId = 0x03;
}
pub mod command {
use crate::im::CommandId;
pub const GET_DETAILED_PRICE_REQUEST: CommandId = 0x00;
pub const GET_DETAILED_PRICE_RESPONSE: CommandId = 0x01;
pub const GET_DETAILED_FORECAST_REQUEST: CommandId = 0x02;
pub const GET_DETAILED_FORECAST_RESPONSE: CommandId = 0x03;
}
pub mod event {
use crate::im::EventId;
pub const PRICE_CHANGE: EventId = 0x00;
}
bitflags::bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct CommodityPriceDetailBitmap: u8 {
const DESCRIPTION = 1 << 0;
const COMPONENTS = 1 << 1;
}
}
impl ToTlv for CommodityPriceDetailBitmap {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
self.bits().to_tlv(w, tag)
}
}
impl<'a> FromTlv<'a> for CommodityPriceDetailBitmap {
fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CommodityPriceComponentStruct<'a> {
pub price: i64,
pub source: crate::clusters::generated::globals::TariffPriceTypeEnum,
pub description: Option<&'a str>,
pub tariff_component_id: Option<u32>,
}
impl<'a> ToTlv for CommodityPriceComponentStruct<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.price.to_tlv(w, Tag::Context(0))?;
self.source.to_tlv(w, Tag::Context(1))?;
if let Some(value) = &self.description {
value.to_tlv(w, Tag::Context(2))?;
}
if let Some(value) = &self.tariff_component_id {
value.to_tlv(w, Tag::Context(3))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for CommodityPriceComponentStruct<'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 price = None;
let mut source = None;
let mut description = None;
let mut tariff_component_id = 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) => price = Some(<i64>::from_tlv(reader, &field)?),
Some(1) => {
source = Some(
<crate::clusters::generated::globals::TariffPriceTypeEnum>::from_tlv(
reader, &field,
)?,
)
}
Some(2) => description = Some(<&'a str>::from_tlv(reader, &field)?),
Some(3) => tariff_component_id = Some(<u32>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
price: price.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
source: source.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
description,
tariff_component_id,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CommodityPriceStruct<'a> {
pub period_start: u32,
pub period_end: Nullable<u32>,
pub price: Option<i64>,
pub price_level: Option<i16>,
pub description: Option<&'a str>,
pub components: Option<TlvList<'a, CommodityPriceComponentStruct<'a>>>,
}
impl<'a> ToTlv for CommodityPriceStruct<'a> {
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))?;
if let Some(value) = &self.price {
value.to_tlv(w, Tag::Context(2))?;
}
if let Some(value) = &self.price_level {
value.to_tlv(w, Tag::Context(3))?;
}
if let Some(value) = &self.description {
value.to_tlv(w, Tag::Context(4))?;
}
if let Some(value) = &self.components {
value.to_tlv(w, Tag::Context(5))?;
}
w.end_container()
}
}
impl<'a> FromTlv<'a> for CommodityPriceStruct<'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 period_start = None;
let mut period_end = None;
let mut price = None;
let mut price_level = None;
let mut description = None;
let mut components = 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) => price = Some(<i64>::from_tlv(reader, &field)?),
Some(3) => price_level = Some(<i16>::from_tlv(reader, &field)?),
Some(4) => description = Some(<&'a str>::from_tlv(reader, &field)?),
Some(5) => {
components = Some(<TlvList<'a, CommodityPriceComponentStruct<'a>>>::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))?,
price,
price_level,
description,
components,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetDetailedPriceRequestFields {
pub details: CommodityPriceDetailBitmap,
}
impl ToTlv for GetDetailedPriceRequestFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.details.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for GetDetailedPriceRequestFields {
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 details = 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) => details = Some(<CommodityPriceDetailBitmap>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
details: details.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetDetailedPriceResponseFields<'a> {
pub current_price: Nullable<CommodityPriceStruct<'a>>,
}
impl<'a> ToTlv for GetDetailedPriceResponseFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.current_price.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for GetDetailedPriceResponseFields<'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 current_price = 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) => {
current_price = Some(<Nullable<CommodityPriceStruct<'a>>>::from_tlv(
reader, &field,
)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
current_price: current_price.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetDetailedForecastRequestFields {
pub details: CommodityPriceDetailBitmap,
}
impl ToTlv for GetDetailedForecastRequestFields {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.details.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for GetDetailedForecastRequestFields {
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 details = 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) => details = Some(<CommodityPriceDetailBitmap>::from_tlv(reader, &field)?),
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
details: details.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GetDetailedForecastResponseFields<'a> {
pub price_forecast: TlvList<'a, CommodityPriceStruct<'a>>,
}
impl<'a> ToTlv for GetDetailedForecastResponseFields<'a> {
fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
w.start_structure(tag)?;
self.price_forecast.to_tlv(w, Tag::Context(0))?;
w.end_container()
}
}
impl<'a> FromTlv<'a> for GetDetailedForecastResponseFields<'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 price_forecast = 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) => {
price_forecast = Some(<TlvList<'a, CommodityPriceStruct<'a>>>::from_tlv(
reader, &field,
)?)
}
_ => reader.skip_value(&field)?,
}
}
Ok(Self {
price_forecast: price_forecast
.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
})
}
}
pub const ATTRIBUTES: &[Attribute] = &[
Attribute {
id: 0x0000,
name: "TariffUnit",
kind: "TariffUnitEnum",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::empty(),
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0001,
name: "Currency",
kind: "CurrencyStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0002,
name: "CurrentPrice",
kind: "CommodityPriceStruct",
access: Access::read_only(Privilege::View),
qualities: AttributeQualities::NULLABLE,
reporting: Reporting::OnChange,
conform: Conform::MANDATORY,
},
Attribute {
id: 0x0003,
name: "PriceForecast",
kind: "list[CommodityPriceStruct]",
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] = &[
Command {
id: 0x00,
name: "GetDetailedPriceRequest",
to_server: true,
response: Some(0x01),
access: Access::invoke(Privilege::Operate),
conform: Conform::OPTIONAL,
},
Command {
id: 0x01,
name: "GetDetailedPriceResponse",
to_server: false,
response: None,
access: Access::invoke(Privilege::Operate),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Command(0x00),
}],
},
},
Command {
id: 0x02,
name: "GetDetailedForecastRequest",
to_server: true,
response: Some(0x03),
access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Optional,
when: Condition::Feature(0),
}],
},
},
Command {
id: 0x03,
name: "GetDetailedForecastResponse",
to_server: false,
response: None,
access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
conform: Conform {
clauses: &[Clause {
verdict: Conformance::Mandatory,
when: Condition::Command(0x02),
}],
},
},
];
pub const EVENTS: &[Event] = &[Event {
id: 0x00,
name: "PriceChange",
priority: EventPriority::Info,
access: Access::read_only(Privilege::View),
conform: Conform::OPTIONAL,
}];
pub const CLUSTER: Cluster = Cluster {
id: ID,
name: "Commodity Price",
revision: REVISION,
pics: "SEPR",
derived_from: None,
features: FEATURES,
attributes: ATTRIBUTES,
commands: COMMANDS,
events: EVENTS,
};