1#![allow(unused_imports)]
15
16use crate::dm::conformance::{Clause, Condition, Conform, Conformance};
17use crate::dm::meta::{EventPriority, Reporting};
18use crate::dm::spec::{Attribute, Cluster, Command, Event, Feature};
19use crate::dm::{Access, AccessQualities, AttributeQualities, Privilege};
20use crate::error::Result;
21use crate::im::{AttributeId, ClusterId, CommandId, EventId};
22use crate::tlv::{
23 ContainerKind, Element, FromTlv, Nullable, Tag, TlvList, TlvReader, TlvWriter, ToTlv, Value,
24};
25
26pub const ID: ClusterId = 0x0095;
28
29pub const REVISION: u16 = 4;
32
33pub const PICS: &str = "SEPR";
35
36pub mod feature {
41 pub const FORECASTING: u32 = 1 << 0;
45}
46
47pub const FEATURES: &[Feature] = &[Feature {
49 bit: 0,
50 code: "FORE",
51 name: "Forecasting",
52 conform: Conform::OPTIONAL,
53}];
54
55pub mod attribute {
57 use crate::im::AttributeId;
58
59 pub const TARIFF_UNIT: AttributeId = 0x00;
61 pub const CURRENCY: AttributeId = 0x01;
63 pub const CURRENT_PRICE: AttributeId = 0x02;
65 pub const PRICE_FORECAST: AttributeId = 0x03;
67}
68
69pub mod command {
71 use crate::im::CommandId;
72
73 pub const GET_DETAILED_PRICE_REQUEST: CommandId = 0x00;
75 pub const GET_DETAILED_PRICE_RESPONSE: CommandId = 0x01;
77 pub const GET_DETAILED_FORECAST_REQUEST: CommandId = 0x02;
79 pub const GET_DETAILED_FORECAST_RESPONSE: CommandId = 0x03;
81}
82
83pub mod event {
85 use crate::im::EventId;
86
87 pub const PRICE_CHANGE: EventId = 0x00;
89}
90
91bitflags::bitflags! {
92 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
94 pub struct CommodityPriceDetailBitmap: u8 {
95 const DESCRIPTION = 1 << 0;
97 const COMPONENTS = 1 << 1;
99 }
100}
101
102impl ToTlv for CommodityPriceDetailBitmap {
103 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
104 self.bits().to_tlv(w, tag)
105 }
106}
107
108impl<'a> FromTlv<'a> for CommodityPriceDetailBitmap {
109 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
110 let bits = <u8 as FromTlv<'a>>::from_tlv(reader, element)?;
111 Self::from_bits(bits).ok_or(crate::Error::new(crate::ErrorCode::TlvOutOfRange))
112 }
113}
114
115#[derive(Debug, Clone, Copy, PartialEq)]
117pub struct CommodityPriceComponentStruct<'a> {
118 pub price: i64,
120 pub source: crate::clusters::generated::globals::TariffPriceTypeEnum,
122 pub description: Option<&'a str>,
124 pub tariff_component_id: Option<u32>,
126}
127
128impl<'a> ToTlv for CommodityPriceComponentStruct<'a> {
129 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
130 w.start_structure(tag)?;
131 self.price.to_tlv(w, Tag::Context(0))?;
132 self.source.to_tlv(w, Tag::Context(1))?;
133 if let Some(value) = &self.description {
134 value.to_tlv(w, Tag::Context(2))?;
135 }
136 if let Some(value) = &self.tariff_component_id {
137 value.to_tlv(w, Tag::Context(3))?;
138 }
139 w.end_container()
140 }
141}
142
143impl<'a> FromTlv<'a> for CommodityPriceComponentStruct<'a> {
144 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
145 if element.value.container() != Some(ContainerKind::Structure) {
146 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
147 }
148 let mut price = None;
149 let mut source = None;
150 let mut description = None;
151 let mut tariff_component_id = None;
152 loop {
153 let Some(field) = reader.next_element()? else {
154 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
155 };
156 if field.value == Value::EndOfContainer {
157 break;
158 }
159 match field.tag.context() {
160 Some(0) => price = Some(<i64>::from_tlv(reader, &field)?),
161 Some(1) => {
162 source = Some(
163 <crate::clusters::generated::globals::TariffPriceTypeEnum>::from_tlv(
164 reader, &field,
165 )?,
166 )
167 }
168 Some(2) => description = Some(<&'a str>::from_tlv(reader, &field)?),
169 Some(3) => tariff_component_id = Some(<u32>::from_tlv(reader, &field)?),
170 _ => reader.skip_value(&field)?,
174 }
175 }
176 Ok(Self {
177 price: price.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
178 source: source.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
179 description,
180 tariff_component_id,
181 })
182 }
183}
184
185#[derive(Debug, Clone, Copy, PartialEq)]
187pub struct CommodityPriceStruct<'a> {
188 pub period_start: u32,
190 pub period_end: Nullable<u32>,
192 pub price: Option<i64>,
194 pub price_level: Option<i16>,
196 pub description: Option<&'a str>,
198 pub components: Option<TlvList<'a, CommodityPriceComponentStruct<'a>>>,
200}
201
202impl<'a> ToTlv for CommodityPriceStruct<'a> {
203 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
204 w.start_structure(tag)?;
205 self.period_start.to_tlv(w, Tag::Context(0))?;
206 self.period_end.to_tlv(w, Tag::Context(1))?;
207 if let Some(value) = &self.price {
208 value.to_tlv(w, Tag::Context(2))?;
209 }
210 if let Some(value) = &self.price_level {
211 value.to_tlv(w, Tag::Context(3))?;
212 }
213 if let Some(value) = &self.description {
214 value.to_tlv(w, Tag::Context(4))?;
215 }
216 if let Some(value) = &self.components {
217 value.to_tlv(w, Tag::Context(5))?;
218 }
219 w.end_container()
220 }
221}
222
223impl<'a> FromTlv<'a> for CommodityPriceStruct<'a> {
224 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
225 if element.value.container() != Some(ContainerKind::Structure) {
226 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
227 }
228 let mut period_start = None;
229 let mut period_end = None;
230 let mut price = None;
231 let mut price_level = None;
232 let mut description = None;
233 let mut components = None;
234 loop {
235 let Some(field) = reader.next_element()? else {
236 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
237 };
238 if field.value == Value::EndOfContainer {
239 break;
240 }
241 match field.tag.context() {
242 Some(0) => period_start = Some(<u32>::from_tlv(reader, &field)?),
243 Some(1) => period_end = Some(<Nullable<u32>>::from_tlv(reader, &field)?),
244 Some(2) => price = Some(<i64>::from_tlv(reader, &field)?),
245 Some(3) => price_level = Some(<i16>::from_tlv(reader, &field)?),
246 Some(4) => description = Some(<&'a str>::from_tlv(reader, &field)?),
247 Some(5) => {
248 components = Some(<TlvList<'a, CommodityPriceComponentStruct<'a>>>::from_tlv(
249 reader, &field,
250 )?)
251 }
252 _ => reader.skip_value(&field)?,
256 }
257 }
258 Ok(Self {
259 period_start: period_start.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
260 period_end: period_end.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
261 price,
262 price_level,
263 description,
264 components,
265 })
266 }
267}
268
269#[derive(Debug, Clone, Copy, PartialEq)]
271pub struct GetDetailedPriceRequestFields {
272 pub details: CommodityPriceDetailBitmap,
274}
275
276impl ToTlv for GetDetailedPriceRequestFields {
277 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
278 w.start_structure(tag)?;
279 self.details.to_tlv(w, Tag::Context(0))?;
280 w.end_container()
281 }
282}
283
284impl<'a> FromTlv<'a> for GetDetailedPriceRequestFields {
285 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
286 if element.value.container() != Some(ContainerKind::Structure) {
287 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
288 }
289 let mut details = None;
290 loop {
291 let Some(field) = reader.next_element()? else {
292 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
293 };
294 if field.value == Value::EndOfContainer {
295 break;
296 }
297 match field.tag.context() {
298 Some(0) => details = Some(<CommodityPriceDetailBitmap>::from_tlv(reader, &field)?),
299 _ => reader.skip_value(&field)?,
303 }
304 }
305 Ok(Self {
306 details: details.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
307 })
308 }
309}
310
311#[derive(Debug, Clone, Copy, PartialEq)]
313pub struct GetDetailedPriceResponseFields<'a> {
314 pub current_price: Nullable<CommodityPriceStruct<'a>>,
316}
317
318impl<'a> ToTlv for GetDetailedPriceResponseFields<'a> {
319 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
320 w.start_structure(tag)?;
321 self.current_price.to_tlv(w, Tag::Context(0))?;
322 w.end_container()
323 }
324}
325
326impl<'a> FromTlv<'a> for GetDetailedPriceResponseFields<'a> {
327 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
328 if element.value.container() != Some(ContainerKind::Structure) {
329 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
330 }
331 let mut current_price = None;
332 loop {
333 let Some(field) = reader.next_element()? else {
334 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
335 };
336 if field.value == Value::EndOfContainer {
337 break;
338 }
339 match field.tag.context() {
340 Some(0) => {
341 current_price = Some(<Nullable<CommodityPriceStruct<'a>>>::from_tlv(
342 reader, &field,
343 )?)
344 }
345 _ => reader.skip_value(&field)?,
349 }
350 }
351 Ok(Self {
352 current_price: current_price.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
353 })
354 }
355}
356
357#[derive(Debug, Clone, Copy, PartialEq)]
359pub struct GetDetailedForecastRequestFields {
360 pub details: CommodityPriceDetailBitmap,
362}
363
364impl ToTlv for GetDetailedForecastRequestFields {
365 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
366 w.start_structure(tag)?;
367 self.details.to_tlv(w, Tag::Context(0))?;
368 w.end_container()
369 }
370}
371
372impl<'a> FromTlv<'a> for GetDetailedForecastRequestFields {
373 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
374 if element.value.container() != Some(ContainerKind::Structure) {
375 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
376 }
377 let mut details = None;
378 loop {
379 let Some(field) = reader.next_element()? else {
380 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
381 };
382 if field.value == Value::EndOfContainer {
383 break;
384 }
385 match field.tag.context() {
386 Some(0) => details = Some(<CommodityPriceDetailBitmap>::from_tlv(reader, &field)?),
387 _ => reader.skip_value(&field)?,
391 }
392 }
393 Ok(Self {
394 details: details.ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
395 })
396 }
397}
398
399#[derive(Debug, Clone, Copy, PartialEq)]
401pub struct GetDetailedForecastResponseFields<'a> {
402 pub price_forecast: TlvList<'a, CommodityPriceStruct<'a>>,
404}
405
406impl<'a> ToTlv for GetDetailedForecastResponseFields<'a> {
407 fn to_tlv(&self, w: &mut TlvWriter<'_>, tag: Tag) -> Result<()> {
408 w.start_structure(tag)?;
409 self.price_forecast.to_tlv(w, Tag::Context(0))?;
410 w.end_container()
411 }
412}
413
414impl<'a> FromTlv<'a> for GetDetailedForecastResponseFields<'a> {
415 fn from_tlv(reader: &mut TlvReader<'a>, element: &Element<'a>) -> Result<Self> {
416 if element.value.container() != Some(ContainerKind::Structure) {
417 return Err(crate::Error::new(crate::ErrorCode::TlvWrongType));
418 }
419 let mut price_forecast = None;
420 loop {
421 let Some(field) = reader.next_element()? else {
422 return Err(crate::Error::new(crate::ErrorCode::TlvTruncated));
423 };
424 if field.value == Value::EndOfContainer {
425 break;
426 }
427 match field.tag.context() {
428 Some(0) => {
429 price_forecast = Some(<TlvList<'a, CommodityPriceStruct<'a>>>::from_tlv(
430 reader, &field,
431 )?)
432 }
433 _ => reader.skip_value(&field)?,
437 }
438 }
439 Ok(Self {
440 price_forecast: price_forecast
441 .ok_or(crate::Error::new(crate::ErrorCode::TlvNotFound))?,
442 })
443 }
444}
445
446pub const ATTRIBUTES: &[Attribute] = &[
448 Attribute {
449 id: 0x0000,
450 name: "TariffUnit",
451 kind: "TariffUnitEnum",
452 access: Access::read_only(Privilege::View),
453 qualities: AttributeQualities::empty(),
454 reporting: Reporting::OnChange,
455 conform: Conform::MANDATORY,
456 },
457 Attribute {
458 id: 0x0001,
459 name: "Currency",
460 kind: "CurrencyStruct",
461 access: Access::read_only(Privilege::View),
462 qualities: AttributeQualities::NULLABLE,
463 reporting: Reporting::OnChange,
464 conform: Conform::MANDATORY,
465 },
466 Attribute {
467 id: 0x0002,
468 name: "CurrentPrice",
469 kind: "CommodityPriceStruct",
470 access: Access::read_only(Privilege::View),
471 qualities: AttributeQualities::NULLABLE,
472 reporting: Reporting::OnChange,
473 conform: Conform::MANDATORY,
474 },
475 Attribute {
476 id: 0x0003,
477 name: "PriceForecast",
478 kind: "list[CommodityPriceStruct]",
479 access: Access::read_only(Privilege::View),
480 qualities: AttributeQualities::empty(),
481 reporting: Reporting::OnChange,
482 conform: Conform {
483 clauses: &[Clause {
484 verdict: Conformance::Mandatory,
485 when: Condition::Feature(0),
486 }],
487 },
488 },
489];
490
491pub const COMMANDS: &[Command] = &[
493 Command {
494 id: 0x00,
495 name: "GetDetailedPriceRequest",
496 to_server: true,
497 response: Some(0x01),
498 access: Access::invoke(Privilege::Operate),
499 conform: Conform::OPTIONAL,
500 },
501 Command {
502 id: 0x01,
503 name: "GetDetailedPriceResponse",
504 to_server: false,
505 response: None,
506 access: Access::invoke(Privilege::Operate),
507 conform: Conform {
508 clauses: &[Clause {
509 verdict: Conformance::Mandatory,
510 when: Condition::Command(0x00),
511 }],
512 },
513 },
514 Command {
515 id: 0x02,
516 name: "GetDetailedForecastRequest",
517 to_server: true,
518 response: Some(0x03),
519 access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
520 conform: Conform {
521 clauses: &[Clause {
522 verdict: Conformance::Optional,
523 when: Condition::Feature(0),
524 }],
525 },
526 },
527 Command {
528 id: 0x03,
529 name: "GetDetailedForecastResponse",
530 to_server: false,
531 response: None,
532 access: Access::invoke(Privilege::Operate).with_qualities(AccessQualities::LARGE),
533 conform: Conform {
534 clauses: &[Clause {
535 verdict: Conformance::Mandatory,
536 when: Condition::Command(0x02),
537 }],
538 },
539 },
540];
541
542pub const EVENTS: &[Event] = &[Event {
544 id: 0x00,
545 name: "PriceChange",
546 priority: EventPriority::Info,
547 access: Access::read_only(Privilege::View),
548 conform: Conform::OPTIONAL,
549}];
550
551pub const CLUSTER: Cluster = Cluster {
553 id: ID,
554 name: "Commodity Price",
555 revision: REVISION,
556 pics: "SEPR",
557 derived_from: None,
558 features: FEATURES,
559 attributes: ATTRIBUTES,
560 commands: COMMANDS,
561 events: EVENTS,
562};