1use alloc::string::String;
15use alloc::vec::Vec;
16
17use bon::Builder;
18use serde::{Deserialize, Serialize};
19
20use super::common::{Commodity, PowerRange, Timer, Transition};
21use super::{Duration, Id, NumberRange, Timestamp};
22
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
25#[serde(deny_unknown_fields)]
26#[builder(on(String, into))]
27pub struct OperationMode {
28 #[serde(rename = "Id")]
33 pub id: Id,
34 #[serde(skip_serializing_if = "Option::is_none")]
36 pub diagnostic_label: Option<String>,
37 pub power_ranges: Vec<PowerRange>,
39 pub supply_range: NumberRange,
42 #[serde(skip_serializing_if = "Option::is_none")]
45 pub running_costs: Option<NumberRange>,
46 pub abnormal_condition_only: bool,
48}
49
50#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
52#[serde(deny_unknown_fields)]
53#[builder(on(String, into))]
54pub struct ActuatorDescription {
55 pub id: Id,
57 #[serde(skip_serializing_if = "Option::is_none")]
59 pub diagnostic_label: Option<String>,
60 #[serde(rename = "supported_commodites")]
64 pub supported_commodities: Vec<Commodity>,
65 pub operation_modes: Vec<OperationMode>,
67 pub transitions: Vec<Transition>,
69 pub timers: Vec<Timer>,
71}
72
73impl ActuatorDescription {
74 #[must_use]
76 pub fn operation_mode(&self, id: &Id) -> Option<&OperationMode> {
77 self.operation_modes.iter().find(|m| m.id == *id)
78 }
79
80 #[must_use]
82 pub fn timer(&self, id: &Id) -> Option<&Timer> {
83 self.timers.iter().find(|t| t.id == *id)
84 }
85
86 #[must_use]
88 pub fn transition(&self, from: &Id, to: &Id) -> Option<&Transition> {
89 self.transitions
90 .iter()
91 .find(|t| t.from == *from && t.to == *to)
92 }
93}
94
95#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Builder)]
97#[serde(deny_unknown_fields)]
98pub struct AverageDemandRateForecastElement {
99 pub duration: Duration,
101 #[serde(skip_serializing_if = "Option::is_none")]
103 pub demand_rate_upper_limit: Option<f64>,
104 #[serde(
106 rename = "demand_rate_upper_95PPR",
107 skip_serializing_if = "Option::is_none"
108 )]
109 pub demand_rate_upper_95ppr: Option<f64>,
110 #[serde(
112 rename = "demand_rate_upper_68PPR",
113 skip_serializing_if = "Option::is_none"
114 )]
115 pub demand_rate_upper_68ppr: Option<f64>,
116 pub demand_rate_expected: f64,
118 #[serde(
120 rename = "demand_rate_lower_68PPR",
121 skip_serializing_if = "Option::is_none"
122 )]
123 pub demand_rate_lower_68ppr: Option<f64>,
124 #[serde(
126 rename = "demand_rate_lower_95PPR",
127 skip_serializing_if = "Option::is_none"
128 )]
129 pub demand_rate_lower_95ppr: Option<f64>,
130 #[serde(skip_serializing_if = "Option::is_none")]
132 pub demand_rate_lower_limit: Option<f64>,
133}
134
135impl AverageDemandRateForecastElement {
136 #[must_use]
138 pub fn bands_ascending(&self) -> [(&'static str, Option<f64>); 7] {
139 [
140 ("demand_rate_lower_limit", self.demand_rate_lower_limit),
141 ("demand_rate_lower_95PPR", self.demand_rate_lower_95ppr),
142 ("demand_rate_lower_68PPR", self.demand_rate_lower_68ppr),
143 ("demand_rate_expected", Some(self.demand_rate_expected)),
144 ("demand_rate_upper_68PPR", self.demand_rate_upper_68ppr),
145 ("demand_rate_upper_95PPR", self.demand_rate_upper_95ppr),
146 ("demand_rate_upper_limit", self.demand_rate_upper_limit),
147 ]
148 }
149}
150
151#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
153#[serde(deny_unknown_fields)]
154pub struct SystemDescription {
155 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
157 pub message_id: Id,
158 pub valid_from: Timestamp,
160 pub actuators: Vec<ActuatorDescription>,
162 #[serde(skip_serializing_if = "Option::is_none")]
169 pub present_demand_rate: Option<NumberRange>,
170 pub provides_average_demand_rate_forecast: bool,
172}
173
174impl SystemDescription {
175 #[must_use]
177 pub fn actuator(&self, id: &Id) -> Option<&ActuatorDescription> {
178 self.actuators.iter().find(|a| a.id == *id)
179 }
180}
181
182#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
186#[serde(deny_unknown_fields)]
187pub struct PresentDemandStatus {
188 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
190 pub message_id: Id,
191 pub present_demand_rate: NumberRange,
193}
194
195#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
197#[serde(deny_unknown_fields)]
198pub struct ActuatorStatus {
199 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
201 pub message_id: Id,
202 pub actuator_id: Id,
204 pub active_operation_mode_id: Id,
206 pub operation_mode_factor: f64,
208 #[serde(skip_serializing_if = "Option::is_none")]
211 pub previous_operation_mode_id: Option<Id>,
212 #[serde(skip_serializing_if = "Option::is_none")]
214 pub transition_timestamp: Option<Timestamp>,
215}
216
217#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
219#[serde(deny_unknown_fields)]
220pub struct TimerStatus {
221 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
223 pub message_id: Id,
224 pub timer_id: Id,
226 pub actuator_id: Id,
228 pub finished_at: Timestamp,
230}
231
232#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
234#[serde(deny_unknown_fields)]
235pub struct AverageDemandRateForecast {
236 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
238 pub message_id: Id,
239 pub start_time: Timestamp,
241 pub elements: Vec<AverageDemandRateForecastElement>,
243}
244
245#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
247#[serde(deny_unknown_fields)]
248pub struct Instruction {
249 #[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
251 pub message_id: Id,
252 pub id: Id,
254 pub execution_time: Timestamp,
256 pub abnormal_condition: bool,
258 pub actuator_id: Id,
260 pub operation_mode_id: Id,
262 pub operation_mode_factor: f64,
264}
265
266#[cfg(test)]
267mod tests {
268 use super::*;
269 use crate::types::common::CommodityQuantity;
270
271 #[test]
272 fn the_two_misspellings_are_on_the_wire_and_not_in_rust() {
273 let mode = OperationMode {
274 id: Id::new_const("heatpump"),
275 diagnostic_label: None,
276 power_ranges: alloc::vec![PowerRange::exactly(
277 2000.0,
278 CommodityQuantity::ElectricPowerL1
279 )],
280 supply_range: NumberRange::new(0.0, 6000.0),
281 running_costs: None,
282 abnormal_condition_only: false,
283 };
284 let actuator = ActuatorDescription {
285 id: Id::new_const("a1"),
286 diagnostic_label: None,
287 supported_commodities: alloc::vec![Commodity::Electricity],
288 operation_modes: alloc::vec![mode],
289 transitions: Vec::new(),
290 timers: Vec::new(),
291 };
292 let json = serde_json::to_string(&actuator).unwrap();
293 assert!(json.contains("\"supported_commodites\""), "{json}");
294 assert!(json.contains("\"Id\":\"heatpump\""), "{json}");
295 assert!(!json.contains("\"supported_commodities\""));
296 let back: ActuatorDescription = serde_json::from_str(&json).unwrap();
297 assert_eq!(back, actuator);
298 }
299
300 #[test]
301 fn the_demand_rate_is_a_field_in_beta_and_a_message_in_one_zero() {
302 let one_zero = SystemDescription {
304 message_id: Id::new_const("m1"),
305 valid_from: Timestamp::UNIX_EPOCH,
306 actuators: Vec::new(),
307 present_demand_rate: None,
308 provides_average_demand_rate_forecast: false,
309 };
310 let json = serde_json::to_string(&one_zero).unwrap();
311 assert!(!json.contains("present_demand_rate"));
312
313 let beta = SystemDescription {
316 present_demand_rate: Some(NumberRange::new(0.0, 6000.0)),
317 ..one_zero
318 };
319 let json = serde_json::to_string(&beta).unwrap();
320 let demand_at = json.find("present_demand_rate").unwrap();
321 let actuators_at = json.find("actuators").unwrap();
322 let provides_at = json.find("provides_average").unwrap();
323 assert!(actuators_at < demand_at && demand_at < provides_at);
324 }
325}