1use alloc::string::String;
9use alloc::vec::Vec;
10
11use bon::Builder;
12use serde::{Deserialize, Serialize};
13
14use super::{Duration, Id, ProtocolVersion, Timestamp};
15
16#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
24#[serde(rename_all = "UPPERCASE")]
25#[non_exhaustive]
26pub enum Commodity {
27 Gas,
29 Heat,
31 Electricity,
33 Oil,
35}
36
37#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
43#[non_exhaustive]
44pub enum CommodityQuantity {
45 #[serde(rename = "ELECTRIC.POWER.L1")]
47 ElectricPowerL1,
48 #[serde(rename = "ELECTRIC.POWER.L2")]
50 ElectricPowerL2,
51 #[serde(rename = "ELECTRIC.POWER.L3")]
53 ElectricPowerL3,
54 #[serde(rename = "ELECTRIC.POWER.3_PHASE_SYMMETRIC")]
56 ElectricPower3PhaseSymmetric,
57 #[serde(rename = "NATURAL_GAS.FLOW_RATE")]
59 NaturalGasFlowRate,
60 #[serde(rename = "HYDROGEN.FLOW_RATE")]
62 HydrogenFlowRate,
63 #[serde(rename = "HEAT.TEMPERATURE")]
65 HeatTemperature,
66 #[serde(rename = "HEAT.FLOW_RATE")]
68 HeatFlowRate,
69 #[serde(rename = "HEAT.THERMAL_POWER")]
71 HeatThermalPower,
72 #[serde(rename = "OIL.FLOW_RATE")]
74 OilFlowRate,
75}
76
77impl CommodityQuantity {
78 #[must_use]
80 pub const fn commodity(self) -> Commodity {
81 match self {
82 Self::ElectricPowerL1
83 | Self::ElectricPowerL2
84 | Self::ElectricPowerL3
85 | Self::ElectricPower3PhaseSymmetric => Commodity::Electricity,
86 Self::NaturalGasFlowRate | Self::HydrogenFlowRate => Commodity::Gas,
87 Self::HeatTemperature | Self::HeatFlowRate | Self::HeatThermalPower => Commodity::Heat,
88 Self::OilFlowRate => Commodity::Oil,
89 }
90 }
91
92 #[must_use]
94 pub const fn is_per_phase_electric(self) -> bool {
95 matches!(
96 self,
97 Self::ElectricPowerL1 | Self::ElectricPowerL2 | Self::ElectricPowerL3
98 )
99 }
100
101 #[must_use]
106 pub const fn is_power(self) -> bool {
107 !matches!(self, Self::HeatTemperature)
108 }
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
118#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
119#[non_exhaustive]
120pub enum ControlType {
121 PowerEnvelopeBasedControl,
124 PowerProfileBasedControl,
127 OperationModeBasedControl,
130 FillRateBasedControl,
133 DemandDrivenBasedControl,
136 #[serde(rename = "NOT_CONTROLABLE")]
141 NotControllable,
142 NoSelection,
144}
145
146impl ControlType {
147 #[must_use]
150 pub const fn is_controllable(self) -> bool {
151 matches!(
152 self,
153 Self::PowerEnvelopeBasedControl
154 | Self::PowerProfileBasedControl
155 | Self::OperationModeBasedControl
156 | Self::FillRateBasedControl
157 | Self::DemandDrivenBasedControl
158 )
159 }
160
161 #[must_use]
166 pub const fn uses_operation_modes(self) -> bool {
167 matches!(
168 self,
169 Self::OperationModeBasedControl
170 | Self::FillRateBasedControl
171 | Self::DemandDrivenBasedControl
172 )
173 }
174
175 #[must_use]
177 pub const fn abbreviation(self) -> Option<&'static str> {
178 match self {
179 Self::PowerEnvelopeBasedControl => Some("PEBC"),
180 Self::PowerProfileBasedControl => Some("PPBC"),
181 Self::OperationModeBasedControl => Some("OMBC"),
182 Self::FillRateBasedControl => Some("FRBC"),
183 Self::DemandDrivenBasedControl => Some("DDBC"),
184 Self::NotControllable | Self::NoSelection => None,
185 }
186 }
187}
188
189#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
195#[serde(rename_all = "UPPERCASE")]
196#[non_exhaustive]
197#[allow(missing_docs)] pub enum Currency {
199 Aed,
200 Ang,
201 Aud,
202 Che,
203 Chf,
204 Chw,
205 Eur,
206 Gbp,
207 Lbp,
208 Lkr,
209 Lrd,
210 Lsl,
211 Lyd,
212 Mad,
213 Mdl,
214 Mga,
215 Mkd,
216 Mmk,
217 Mnt,
218 Mop,
219 Mro,
220 Mur,
221 Mvr,
222 Mwk,
223 Mxn,
224 Mxv,
225 Myr,
226 Mzn,
227 Nad,
228 Ngn,
229 Nio,
230 Nok,
231 Npr,
232 Nzd,
233 Omr,
234 Pab,
235 Pen,
236 Pgk,
237 Php,
238 Pkr,
239 Pln,
240 Pyg,
241 Qar,
242 Ron,
243 Rsd,
244 Rub,
245 Rwf,
246 Sar,
247 Sbd,
248 Scr,
249 Sdg,
250 Sek,
251 Sgd,
252 Shp,
253 Sll,
254 Sos,
255 Srd,
256 Ssp,
257 Std,
258 Syp,
259 Szl,
260 Thb,
261 Tjs,
262 Tmt,
263 Tnd,
264 Top,
265 Try,
266 Ttd,
267 Twd,
268 Tzs,
269 Uah,
270 Ugx,
271 Usd,
272 Usn,
273 Uyi,
274 Uyu,
275 Uzs,
276 Vef,
277 Vnd,
278 Vuv,
279 Wst,
280 Xag,
281 Xau,
282 Xba,
283 Xbb,
284 Xbc,
285 Xbd,
286 Xcd,
287 Xof,
288 Xpd,
289 Xpf,
290 Xpt,
291 Xsu,
292 Xts,
293 Xua,
294 Xxx,
295 Yer,
296 Zar,
297 Zmw,
298 Zwl,
299}
300
301#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
303#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
304pub enum EnergyManagementRole {
305 Cem,
307 Rm,
309}
310
311impl EnergyManagementRole {
312 #[must_use]
314 pub const fn peer(self) -> Self {
315 match self {
316 Self::Cem => Self::Rm,
317 Self::Rm => Self::Cem,
318 }
319 }
320}
321
322#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
328#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
329#[non_exhaustive]
330pub enum InstructionStatus {
331 New,
333 Accepted,
335 Rejected,
337 Revoked,
339 Started,
341 Succeeded,
343 Aborted,
345}
346
347impl InstructionStatus {
348 #[must_use]
350 pub const fn is_terminal(self) -> bool {
351 matches!(
352 self,
353 Self::Rejected | Self::Revoked | Self::Succeeded | Self::Aborted
354 )
355 }
356
357 #[must_use]
359 pub const fn progress_rank(self) -> u8 {
360 match self {
361 Self::New => 0,
362 Self::Accepted => 1,
363 Self::Started => 2,
364 Self::Succeeded | Self::Rejected | Self::Revoked | Self::Aborted => 3,
365 }
366 }
367}
368
369#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
374#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
375#[non_exhaustive]
376pub enum ReceptionStatusValues {
377 InvalidData,
379 InvalidMessage,
381 InvalidContent,
384 TemporaryError,
386 PermanentError,
388 Ok,
390}
391
392#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
394pub enum Consequence {
395 Proceed,
397 Ignored,
399 Retry,
401 Disconnect,
403}
404
405impl ReceptionStatusValues {
406 #[must_use]
408 pub const fn consequence(self) -> Consequence {
409 match self {
410 Self::Ok => Consequence::Proceed,
411 Self::InvalidData | Self::InvalidMessage | Self::InvalidContent => Consequence::Ignored,
412 Self::TemporaryError => Consequence::Retry,
413 Self::PermanentError => Consequence::Disconnect,
414 }
415 }
416
417 #[must_use]
419 pub const fn is_error(self) -> bool {
420 !matches!(self, Self::Ok)
421 }
422}
423
424#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
426#[non_exhaustive]
427pub enum RevokableObjects {
428 #[serde(rename = "PEBC.PowerConstraints")]
430 PebcPowerConstraints,
431 #[serde(rename = "PEBC.EnergyConstraint")]
433 PebcEnergyConstraint,
434 #[serde(rename = "PEBC.Instruction")]
436 PebcInstruction,
437 #[serde(rename = "PPBC.PowerProfileDefinition")]
439 PpbcPowerProfileDefinition,
440 #[serde(rename = "PPBC.ScheduleInstruction")]
442 PpbcScheduleInstruction,
443 #[serde(rename = "PPBC.StartInterruptionInstruction")]
445 PpbcStartInterruptionInstruction,
446 #[serde(rename = "PPBC.EndInterruptionInstruction")]
448 PpbcEndInterruptionInstruction,
449 #[serde(rename = "OMBC.SystemDescription")]
451 OmbcSystemDescription,
452 #[serde(rename = "OMBC.Instruction")]
454 OmbcInstruction,
455 #[serde(rename = "FRBC.SystemDescription")]
457 FrbcSystemDescription,
458 #[serde(rename = "FRBC.Instruction")]
460 FrbcInstruction,
461 #[serde(rename = "DDBC.SystemDescription")]
463 DdbcSystemDescription,
464 #[serde(rename = "DDBC.Instruction")]
466 DdbcInstruction,
467}
468
469impl RevokableObjects {
470 #[must_use]
476 pub const fn owner(self) -> EnergyManagementRole {
477 match self {
478 Self::PebcInstruction
479 | Self::PpbcScheduleInstruction
480 | Self::PpbcStartInterruptionInstruction
481 | Self::PpbcEndInterruptionInstruction
482 | Self::OmbcInstruction
483 | Self::FrbcInstruction
484 | Self::DdbcInstruction => EnergyManagementRole::Cem,
485 Self::PebcPowerConstraints
486 | Self::PebcEnergyConstraint
487 | Self::PpbcPowerProfileDefinition
488 | Self::OmbcSystemDescription
489 | Self::FrbcSystemDescription
490 | Self::DdbcSystemDescription => EnergyManagementRole::Rm,
491 }
492 }
493
494 #[must_use]
496 pub const fn control_type(self) -> ControlType {
497 match self {
498 Self::PebcPowerConstraints | Self::PebcEnergyConstraint | Self::PebcInstruction => {
499 ControlType::PowerEnvelopeBasedControl
500 }
501 Self::PpbcPowerProfileDefinition
502 | Self::PpbcScheduleInstruction
503 | Self::PpbcStartInterruptionInstruction
504 | Self::PpbcEndInterruptionInstruction => ControlType::PowerProfileBasedControl,
505 Self::OmbcSystemDescription | Self::OmbcInstruction => {
506 ControlType::OperationModeBasedControl
507 }
508 Self::FrbcSystemDescription | Self::FrbcInstruction => {
509 ControlType::FillRateBasedControl
510 }
511 Self::DdbcSystemDescription | Self::DdbcInstruction => {
512 ControlType::DemandDrivenBasedControl
513 }
514 }
515 }
516}
517
518#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
520#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
521pub enum RoleType {
522 EnergyProducer,
524 EnergyConsumer,
526 EnergyStorage,
528}
529
530#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
532#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
533pub enum SessionRequestType {
534 Reconnect,
537 Terminate,
540}
541
542#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
553#[serde(deny_unknown_fields)]
554pub struct NumberRange {
555 pub start_of_range: f64,
557 pub end_of_range: f64,
559}
560
561impl NumberRange {
562 #[must_use]
564 pub const fn new(start: f64, end: f64) -> Self {
565 Self {
566 start_of_range: start,
567 end_of_range: end,
568 }
569 }
570
571 #[must_use]
573 pub const fn exactly(value: f64) -> Self {
574 Self::new(value, value)
575 }
576
577 #[must_use]
579 pub fn contains(self, value: f64) -> bool {
580 let (lo, hi) = self.ordered();
581 value >= lo && value <= hi
582 }
583
584 #[must_use]
586 pub fn ordered(self) -> (f64, f64) {
587 if self.start_of_range <= self.end_of_range {
588 (self.start_of_range, self.end_of_range)
589 } else {
590 (self.end_of_range, self.start_of_range)
591 }
592 }
593
594 #[must_use]
596 pub fn span(self) -> f64 {
597 self.end_of_range - self.start_of_range
598 }
599
600 #[must_use]
608 #[allow(
609 clippy::float_cmp,
610 reason = "0 and 1 are the endpoints, not approximations"
611 )]
612 pub fn at_factor(self, factor: f64) -> f64 {
613 if factor == 0.0 {
614 self.start_of_range
615 } else if factor == 1.0 {
616 self.end_of_range
617 } else {
618 self.start_of_range + self.span() * factor
619 }
620 }
621
622 #[must_use]
627 pub fn factor_of(self, value: f64) -> Option<f64> {
628 let span = self.span();
629 if span == 0.0 {
630 None
631 } else {
632 Some((value - self.start_of_range) / span)
633 }
634 }
635
636 #[must_use]
638 pub fn is_finite(self) -> bool {
639 self.start_of_range.is_finite() && self.end_of_range.is_finite()
640 }
641
642 #[must_use]
644 pub fn overlaps(self, other: Self) -> bool {
645 let (a_lo, a_hi) = self.ordered();
646 let (b_lo, b_hi) = other.ordered();
647 a_lo < b_hi && b_lo < a_hi
648 }
649}
650
651impl From<core::ops::Range<f64>> for NumberRange {
652 fn from(r: core::ops::Range<f64>) -> Self {
653 Self::new(r.start, r.end)
654 }
655}
656
657impl From<NumberRange> for core::ops::Range<f64> {
658 fn from(r: NumberRange) -> Self {
659 r.start_of_range..r.end_of_range
660 }
661}
662
663#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
668#[serde(deny_unknown_fields)]
669pub struct PowerRange {
670 pub start_of_range: f64,
672 pub end_of_range: f64,
674 pub commodity_quantity: CommodityQuantity,
676}
677
678impl PowerRange {
679 #[must_use]
681 pub const fn new(start: f64, end: f64, commodity_quantity: CommodityQuantity) -> Self {
682 Self {
683 start_of_range: start,
684 end_of_range: end,
685 commodity_quantity,
686 }
687 }
688
689 #[must_use]
691 pub const fn exactly(value: f64, commodity_quantity: CommodityQuantity) -> Self {
692 Self::new(value, value, commodity_quantity)
693 }
694
695 #[must_use]
697 pub const fn range(self) -> NumberRange {
698 NumberRange::new(self.start_of_range, self.end_of_range)
699 }
700
701 #[must_use]
703 pub fn at_factor(self, factor: f64) -> f64 {
704 self.range().at_factor(factor)
705 }
706}
707
708#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
710#[serde(deny_unknown_fields)]
711pub struct PowerValue {
712 pub commodity_quantity: CommodityQuantity,
714 pub value: f64,
716}
717
718impl PowerValue {
719 #[must_use]
721 pub const fn new(commodity_quantity: CommodityQuantity, value: f64) -> Self {
722 Self {
723 commodity_quantity,
724 value,
725 }
726 }
727}
728
729#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Builder)]
735#[serde(deny_unknown_fields)]
736pub struct PowerForecastValue {
737 #[serde(skip_serializing_if = "Option::is_none")]
739 pub value_upper_limit: Option<f64>,
740 #[serde(rename = "value_upper_95PPR", skip_serializing_if = "Option::is_none")]
742 pub value_upper_95ppr: Option<f64>,
743 #[serde(rename = "value_upper_68PPR", skip_serializing_if = "Option::is_none")]
745 pub value_upper_68ppr: Option<f64>,
746 pub value_expected: f64,
748 #[serde(rename = "value_lower_68PPR", skip_serializing_if = "Option::is_none")]
750 pub value_lower_68ppr: Option<f64>,
751 #[serde(rename = "value_lower_95PPR", skip_serializing_if = "Option::is_none")]
753 pub value_lower_95ppr: Option<f64>,
754 #[serde(skip_serializing_if = "Option::is_none")]
756 pub value_lower_limit: Option<f64>,
757 pub commodity_quantity: CommodityQuantity,
759}
760
761impl PowerForecastValue {
762 #[must_use]
764 pub const fn expected(value: f64, commodity_quantity: CommodityQuantity) -> Self {
765 Self {
766 value_upper_limit: None,
767 value_upper_95ppr: None,
768 value_upper_68ppr: None,
769 value_expected: value,
770 value_lower_68ppr: None,
771 value_lower_95ppr: None,
772 value_lower_limit: None,
773 commodity_quantity,
774 }
775 }
776
777 #[must_use]
781 pub fn bands_ascending(&self) -> [(&'static str, Option<f64>); 7] {
782 [
783 ("value_lower_limit", self.value_lower_limit),
784 ("value_lower_95PPR", self.value_lower_95ppr),
785 ("value_lower_68PPR", self.value_lower_68ppr),
786 ("value_expected", Some(self.value_expected)),
787 ("value_upper_68PPR", self.value_upper_68ppr),
788 ("value_upper_95PPR", self.value_upper_95ppr),
789 ("value_upper_limit", self.value_upper_limit),
790 ]
791 }
792}
793
794#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
796#[serde(deny_unknown_fields)]
797pub struct PowerForecastElement {
798 pub duration: Duration,
800 pub power_values: Vec<PowerForecastValue>,
802}
803
804#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
806#[serde(deny_unknown_fields)]
807pub struct Role {
808 pub role: RoleType,
810 pub commodity: Commodity,
812}
813
814impl Role {
815 #[must_use]
817 pub const fn new(role: RoleType, commodity: Commodity) -> Self {
818 Self { role, commodity }
819 }
820}
821
822#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
824#[serde(deny_unknown_fields)]
825#[builder(on(String, into))]
826pub struct Timer {
827 pub id: Id,
829 #[serde(skip_serializing_if = "Option::is_none")]
831 pub diagnostic_label: Option<String>,
832 pub duration: Duration,
834}
835
836#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
838#[serde(deny_unknown_fields)]
839pub struct Transition {
840 pub id: Id,
842 pub from: Id,
844 pub to: Id,
846 pub start_timers: Vec<Id>,
848 pub blocking_timers: Vec<Id>,
850 #[serde(skip_serializing_if = "Option::is_none")]
852 pub transition_costs: Option<f64>,
853 #[serde(skip_serializing_if = "Option::is_none")]
856 pub transition_duration: Option<Duration>,
857 pub abnormal_condition_only: bool,
859}
860
861impl Transition {
862 #[must_use]
864 pub fn simple(id: Id, from: Id, to: Id) -> Self {
865 Self {
866 id,
867 from,
868 to,
869 start_timers: Vec::new(),
870 blocking_timers: Vec::new(),
871 transition_costs: None,
872 transition_duration: None,
873 abnormal_condition_only: false,
874 }
875 }
876}
877
878#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
888#[serde(deny_unknown_fields)]
889pub struct Handshake {
890 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
892 pub message_id: Id,
893 pub role: EnergyManagementRole,
895 #[serde(skip_serializing_if = "Option::is_none")]
898 pub supported_protocol_versions: Option<Vec<ProtocolVersion>>,
899}
900
901#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
903#[serde(deny_unknown_fields)]
904pub struct HandshakeResponse {
905 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
907 pub message_id: Id,
908 pub selected_protocol_version: ProtocolVersion,
910}
911
912#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
914#[serde(deny_unknown_fields)]
915#[builder(on(String, into))]
916pub struct ResourceManagerDetails {
917 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
919 pub message_id: Id,
920 pub resource_id: Id,
922 #[serde(skip_serializing_if = "Option::is_none")]
924 pub name: Option<String>,
925 pub roles: Vec<Role>,
927 #[serde(skip_serializing_if = "Option::is_none")]
929 pub manufacturer: Option<String>,
930 #[serde(skip_serializing_if = "Option::is_none")]
932 pub model: Option<String>,
933 #[serde(skip_serializing_if = "Option::is_none")]
935 pub serial_number: Option<String>,
936 #[serde(skip_serializing_if = "Option::is_none")]
938 pub firmware_version: Option<String>,
939 pub instruction_processing_delay: Duration,
941 pub available_control_types: Vec<ControlType>,
943 #[serde(skip_serializing_if = "Option::is_none")]
945 pub currency: Option<Currency>,
946 pub provides_forecast: bool,
948 pub provides_power_measurement_types: Vec<CommodityQuantity>,
950}
951
952#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
954#[serde(deny_unknown_fields)]
955pub struct SelectControlType {
956 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
958 pub message_id: Id,
959 pub control_type: ControlType,
961}
962
963#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
965#[serde(deny_unknown_fields)]
966#[builder(on(String, into))]
967pub struct SessionRequest {
968 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
970 pub message_id: Id,
971 pub request: SessionRequestType,
973 #[serde(skip_serializing_if = "Option::is_none")]
975 pub diagnostic_label: Option<String>,
976}
977
978#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
983#[serde(deny_unknown_fields)]
984#[builder(on(String, into))]
985pub struct ReceptionStatus {
986 pub subject_message_id: Id,
988 pub status: ReceptionStatusValues,
990 #[serde(skip_serializing_if = "Option::is_none")]
994 pub diagnostic_label: Option<String>,
995}
996
997impl ReceptionStatus {
998 #[must_use]
1000 pub fn ok(subject: Id) -> Self {
1001 Self {
1002 subject_message_id: subject,
1003 status: ReceptionStatusValues::Ok,
1004 diagnostic_label: None,
1005 }
1006 }
1007
1008 #[must_use]
1010 pub fn error(
1011 subject: Id,
1012 status: ReceptionStatusValues,
1013 diagnostic: impl Into<String>,
1014 ) -> Self {
1015 Self {
1016 subject_message_id: subject,
1017 status,
1018 diagnostic_label: Some(diagnostic.into()),
1019 }
1020 }
1021}
1022
1023#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
1028#[serde(deny_unknown_fields)]
1029pub struct InstructionStatusUpdate {
1030 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
1032 pub message_id: Id,
1033 pub instruction_id: Id,
1035 pub status_type: InstructionStatus,
1037 pub timestamp: Timestamp,
1039}
1040
1041#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
1043#[serde(deny_unknown_fields)]
1044pub struct PowerMeasurement {
1045 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
1047 pub message_id: Id,
1048 pub measurement_timestamp: Timestamp,
1050 pub values: Vec<PowerValue>,
1052}
1053
1054#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
1056#[serde(deny_unknown_fields)]
1057pub struct PowerForecast {
1058 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
1060 pub message_id: Id,
1061 pub start_time: Timestamp,
1063 pub elements: Vec<PowerForecastElement>,
1065}
1066
1067#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
1069#[serde(deny_unknown_fields)]
1070pub struct RevokeObject {
1071 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
1073 pub message_id: Id,
1074 pub object_type: RevokableObjects,
1076 pub object_id: Id,
1078}
1079
1080#[cfg(test)]
1081mod tests {
1082 use super::*;
1083
1084 #[test]
1085 fn control_type_wire_spellings_are_the_standards_own() {
1086 for (value, wire) in [
1088 (
1089 ControlType::PowerEnvelopeBasedControl,
1090 "\"POWER_ENVELOPE_BASED_CONTROL\"",
1091 ),
1092 (
1093 ControlType::PowerProfileBasedControl,
1094 "\"POWER_PROFILE_BASED_CONTROL\"",
1095 ),
1096 (
1097 ControlType::OperationModeBasedControl,
1098 "\"OPERATION_MODE_BASED_CONTROL\"",
1099 ),
1100 (
1101 ControlType::FillRateBasedControl,
1102 "\"FILL_RATE_BASED_CONTROL\"",
1103 ),
1104 (
1105 ControlType::DemandDrivenBasedControl,
1106 "\"DEMAND_DRIVEN_BASED_CONTROL\"",
1107 ),
1108 (ControlType::NotControllable, "\"NOT_CONTROLABLE\""),
1109 (ControlType::NoSelection, "\"NO_SELECTION\""),
1110 ] {
1111 assert_eq!(serde_json::to_string(&value).unwrap(), wire);
1112 assert_eq!(serde_json::from_str::<ControlType>(wire).unwrap(), value);
1113 }
1114 assert!(serde_json::from_str::<ControlType>("\"NOT_CONTROLLABLE\"").is_err());
1116 }
1117
1118 #[test]
1119 fn commodity_quantity_wire_spellings() {
1120 for (value, wire) in [
1121 (CommodityQuantity::ElectricPowerL1, "\"ELECTRIC.POWER.L1\""),
1122 (
1123 CommodityQuantity::ElectricPower3PhaseSymmetric,
1124 "\"ELECTRIC.POWER.3_PHASE_SYMMETRIC\"",
1125 ),
1126 (
1127 CommodityQuantity::NaturalGasFlowRate,
1128 "\"NATURAL_GAS.FLOW_RATE\"",
1129 ),
1130 (
1131 CommodityQuantity::HeatThermalPower,
1132 "\"HEAT.THERMAL_POWER\"",
1133 ),
1134 (CommodityQuantity::OilFlowRate, "\"OIL.FLOW_RATE\""),
1135 ] {
1136 assert_eq!(serde_json::to_string(&value).unwrap(), wire);
1137 assert_eq!(
1138 serde_json::from_str::<CommodityQuantity>(wire).unwrap(),
1139 value
1140 );
1141 }
1142 }
1143
1144 #[test]
1145 fn revokable_objects_keep_their_dots() {
1146 assert_eq!(
1147 serde_json::to_string(&RevokableObjects::FrbcInstruction).unwrap(),
1148 "\"FRBC.Instruction\""
1149 );
1150 assert_eq!(
1152 RevokableObjects::FrbcInstruction.owner(),
1153 EnergyManagementRole::Cem
1154 );
1155 assert_eq!(
1156 RevokableObjects::FrbcSystemDescription.owner(),
1157 EnergyManagementRole::Rm
1158 );
1159 }
1160
1161 #[test]
1162 fn currency_codes_are_uppercase() {
1163 assert_eq!(serde_json::to_string(&Currency::Eur).unwrap(), "\"EUR\"");
1164 assert_eq!(
1165 serde_json::from_str::<Currency>("\"XXX\"").unwrap(),
1166 Currency::Xxx
1167 );
1168 }
1169
1170 #[test]
1171 fn reception_status_consequences_are_the_normative_ones() {
1172 use Consequence as C;
1173 use ReceptionStatusValues as R;
1174 assert_eq!(R::Ok.consequence(), C::Proceed);
1175 assert_eq!(R::InvalidData.consequence(), C::Ignored);
1176 assert_eq!(R::InvalidMessage.consequence(), C::Ignored);
1177 assert_eq!(R::InvalidContent.consequence(), C::Ignored);
1178 assert_eq!(R::TemporaryError.consequence(), C::Retry);
1179 assert_eq!(R::PermanentError.consequence(), C::Disconnect);
1180 }
1181
1182 #[test]
1183 fn factor_interpolation_matches_the_documented_formula() {
1184 let r = NumberRange::new(1000.0, 2500.0);
1187 assert!((r.at_factor(0.5) - 1750.0).abs() < 1e-9);
1188 assert!((r.at_factor(0.0) - 1000.0).abs() < 1e-9);
1189 assert!((r.at_factor(1.0) - 2500.0).abs() < 1e-9);
1190 assert!((r.factor_of(1750.0).unwrap() - 0.5).abs() < 1e-9);
1191 assert_eq!(NumberRange::exactly(0.0).factor_of(0.0), None);
1194 assert!((NumberRange::exactly(7.0).at_factor(0.3) - 7.0).abs() < 1e-12);
1195 }
1196
1197 #[test]
1198 fn unknown_fields_are_refused() {
1199 let json = r#"{"start_of_range":0,"end_of_range":1,"extra":true}"#;
1200 assert!(serde_json::from_str::<NumberRange>(json).is_err());
1201 }
1202
1203 #[test]
1204 fn absent_optionals_are_absent_rather_than_null() {
1205 let v = PowerForecastValue::expected(-3450.1, CommodityQuantity::ElectricPowerL1);
1206 let json = serde_json::to_string(&v).unwrap();
1207 assert_eq!(
1208 json,
1209 r#"{"value_expected":-3450.1,"commodity_quantity":"ELECTRIC.POWER.L1"}"#
1210 );
1211 }
1212
1213 #[test]
1214 fn percentile_bands_keep_their_capitalised_wire_names() {
1215 let json = r#"{
1216 "value_upper_limit": -3400.0, "value_upper_95PPR": -3440.0,
1217 "value_upper_68PPR": -3445.0, "value_expected": -3450.1,
1218 "value_lower_68PPR": -3455.0, "value_lower_95PPR": -3460.0,
1219 "value_lower_limit": -3500.0, "commodity_quantity": "ELECTRIC.POWER.L1"
1220 }"#;
1221 let v: PowerForecastValue = serde_json::from_str(json).unwrap();
1222 assert_eq!(v.value_upper_95ppr, Some(-3440.0));
1223 assert_eq!(v.value_lower_68ppr, Some(-3455.0));
1224 let back = serde_json::to_string(&v).unwrap();
1225 assert!(back.contains("\"value_upper_95PPR\":-3440.0"));
1226 }
1227}