use alloc::string::String;
use alloc::vec::Vec;
use bon::Builder;
use serde::{Deserialize, Serialize};
use super::common::{Commodity, PowerRange, Timer, Transition};
use super::{Duration, Id, NumberRange, Timestamp};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
#[builder(on(String, into))]
pub struct OperationMode {
#[serde(rename = "Id")]
pub id: Id,
#[serde(skip_serializing_if = "Option::is_none")]
pub diagnostic_label: Option<String>,
pub power_ranges: Vec<PowerRange>,
pub supply_range: NumberRange,
#[serde(skip_serializing_if = "Option::is_none")]
pub running_costs: Option<NumberRange>,
pub abnormal_condition_only: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
#[builder(on(String, into))]
pub struct ActuatorDescription {
pub id: Id,
#[serde(skip_serializing_if = "Option::is_none")]
pub diagnostic_label: Option<String>,
#[serde(rename = "supported_commodites")]
pub supported_commodities: Vec<Commodity>,
pub operation_modes: Vec<OperationMode>,
pub transitions: Vec<Transition>,
pub timers: Vec<Timer>,
}
impl ActuatorDescription {
#[must_use]
pub fn operation_mode(&self, id: &Id) -> Option<&OperationMode> {
self.operation_modes.iter().find(|m| m.id == *id)
}
#[must_use]
pub fn timer(&self, id: &Id) -> Option<&Timer> {
self.timers.iter().find(|t| t.id == *id)
}
#[must_use]
pub fn transition(&self, from: &Id, to: &Id) -> Option<&Transition> {
self.transitions
.iter()
.find(|t| t.from == *from && t.to == *to)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct AverageDemandRateForecastElement {
pub duration: Duration,
#[serde(skip_serializing_if = "Option::is_none")]
pub demand_rate_upper_limit: Option<f64>,
#[serde(
rename = "demand_rate_upper_95PPR",
skip_serializing_if = "Option::is_none"
)]
pub demand_rate_upper_95ppr: Option<f64>,
#[serde(
rename = "demand_rate_upper_68PPR",
skip_serializing_if = "Option::is_none"
)]
pub demand_rate_upper_68ppr: Option<f64>,
pub demand_rate_expected: f64,
#[serde(
rename = "demand_rate_lower_68PPR",
skip_serializing_if = "Option::is_none"
)]
pub demand_rate_lower_68ppr: Option<f64>,
#[serde(
rename = "demand_rate_lower_95PPR",
skip_serializing_if = "Option::is_none"
)]
pub demand_rate_lower_95ppr: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub demand_rate_lower_limit: Option<f64>,
}
impl AverageDemandRateForecastElement {
#[must_use]
pub fn bands_ascending(&self) -> [(&'static str, Option<f64>); 7] {
[
("demand_rate_lower_limit", self.demand_rate_lower_limit),
("demand_rate_lower_95PPR", self.demand_rate_lower_95ppr),
("demand_rate_lower_68PPR", self.demand_rate_lower_68ppr),
("demand_rate_expected", Some(self.demand_rate_expected)),
("demand_rate_upper_68PPR", self.demand_rate_upper_68ppr),
("demand_rate_upper_95PPR", self.demand_rate_upper_95ppr),
("demand_rate_upper_limit", self.demand_rate_upper_limit),
]
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct SystemDescription {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub valid_from: Timestamp,
pub actuators: Vec<ActuatorDescription>,
#[serde(skip_serializing_if = "Option::is_none")]
pub present_demand_rate: Option<NumberRange>,
pub provides_average_demand_rate_forecast: bool,
}
impl SystemDescription {
#[must_use]
pub fn actuator(&self, id: &Id) -> Option<&ActuatorDescription> {
self.actuators.iter().find(|a| a.id == *id)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct PresentDemandStatus {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub present_demand_rate: NumberRange,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct ActuatorStatus {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub actuator_id: Id,
pub active_operation_mode_id: Id,
pub operation_mode_factor: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub previous_operation_mode_id: Option<Id>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transition_timestamp: Option<Timestamp>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct TimerStatus {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub timer_id: Id,
pub actuator_id: Id,
pub finished_at: Timestamp,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct AverageDemandRateForecast {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub start_time: Timestamp,
pub elements: Vec<AverageDemandRateForecastElement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct Instruction {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub id: Id,
pub execution_time: Timestamp,
pub abnormal_condition: bool,
pub actuator_id: Id,
pub operation_mode_id: Id,
pub operation_mode_factor: f64,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::types::common::CommodityQuantity;
#[test]
fn the_two_misspellings_are_on_the_wire_and_not_in_rust() {
let mode = OperationMode {
id: Id::new_const("heatpump"),
diagnostic_label: None,
power_ranges: alloc::vec![PowerRange::exactly(
2000.0,
CommodityQuantity::ElectricPowerL1
)],
supply_range: NumberRange::new(0.0, 6000.0),
running_costs: None,
abnormal_condition_only: false,
};
let actuator = ActuatorDescription {
id: Id::new_const("a1"),
diagnostic_label: None,
supported_commodities: alloc::vec![Commodity::Electricity],
operation_modes: alloc::vec![mode],
transitions: Vec::new(),
timers: Vec::new(),
};
let json = serde_json::to_string(&actuator).unwrap();
assert!(json.contains("\"supported_commodites\""), "{json}");
assert!(json.contains("\"Id\":\"heatpump\""), "{json}");
assert!(!json.contains("\"supported_commodities\""));
let back: ActuatorDescription = serde_json::from_str(&json).unwrap();
assert_eq!(back, actuator);
}
#[test]
fn the_demand_rate_is_a_field_in_beta_and_a_message_in_one_zero() {
let one_zero = SystemDescription {
message_id: Id::new_const("m1"),
valid_from: Timestamp::UNIX_EPOCH,
actuators: Vec::new(),
present_demand_rate: None,
provides_average_demand_rate_forecast: false,
};
let json = serde_json::to_string(&one_zero).unwrap();
assert!(!json.contains("present_demand_rate"));
let beta = SystemDescription {
present_demand_rate: Some(NumberRange::new(0.0, 6000.0)),
..one_zero
};
let json = serde_json::to_string(&beta).unwrap();
let demand_at = json.find("present_demand_rate").unwrap();
let actuators_at = json.find("actuators").unwrap();
let provides_at = json.find("provides_average").unwrap();
assert!(actuators_at < demand_at && demand_at < provides_at);
}
}