use alloc::string::String;
use alloc::vec::Vec;
use bon::Builder;
use serde::{Deserialize, Serialize};
use super::{Duration, Id, ProtocolVersion, Timestamp};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
#[non_exhaustive]
pub enum Commodity {
Gas,
Heat,
Electricity,
Oil,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum CommodityQuantity {
#[serde(rename = "ELECTRIC.POWER.L1")]
ElectricPowerL1,
#[serde(rename = "ELECTRIC.POWER.L2")]
ElectricPowerL2,
#[serde(rename = "ELECTRIC.POWER.L3")]
ElectricPowerL3,
#[serde(rename = "ELECTRIC.POWER.3_PHASE_SYMMETRIC")]
ElectricPower3PhaseSymmetric,
#[serde(rename = "NATURAL_GAS.FLOW_RATE")]
NaturalGasFlowRate,
#[serde(rename = "HYDROGEN.FLOW_RATE")]
HydrogenFlowRate,
#[serde(rename = "HEAT.TEMPERATURE")]
HeatTemperature,
#[serde(rename = "HEAT.FLOW_RATE")]
HeatFlowRate,
#[serde(rename = "HEAT.THERMAL_POWER")]
HeatThermalPower,
#[serde(rename = "OIL.FLOW_RATE")]
OilFlowRate,
}
impl CommodityQuantity {
#[must_use]
pub const fn commodity(self) -> Commodity {
match self {
Self::ElectricPowerL1
| Self::ElectricPowerL2
| Self::ElectricPowerL3
| Self::ElectricPower3PhaseSymmetric => Commodity::Electricity,
Self::NaturalGasFlowRate | Self::HydrogenFlowRate => Commodity::Gas,
Self::HeatTemperature | Self::HeatFlowRate | Self::HeatThermalPower => Commodity::Heat,
Self::OilFlowRate => Commodity::Oil,
}
}
#[must_use]
pub const fn is_per_phase_electric(self) -> bool {
matches!(
self,
Self::ElectricPowerL1 | Self::ElectricPowerL2 | Self::ElectricPowerL3
)
}
#[must_use]
pub const fn is_power(self) -> bool {
!matches!(self, Self::HeatTemperature)
}
#[must_use]
pub const fn unit(self) -> &'static str {
match self {
Self::ElectricPowerL1
| Self::ElectricPowerL2
| Self::ElectricPowerL3
| Self::ElectricPower3PhaseSymmetric
| Self::HeatThermalPower => "W",
Self::NaturalGasFlowRate | Self::HeatFlowRate => "l/s",
Self::HydrogenFlowRate => "g/s",
Self::HeatTemperature => "°C",
Self::OilFlowRate => "l/h",
}
}
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::ElectricPowerL1 => "ELECTRIC.POWER.L1",
Self::ElectricPowerL2 => "ELECTRIC.POWER.L2",
Self::ElectricPowerL3 => "ELECTRIC.POWER.L3",
Self::ElectricPower3PhaseSymmetric => "ELECTRIC.POWER.3_PHASE_SYMMETRIC",
Self::NaturalGasFlowRate => "NATURAL_GAS.FLOW_RATE",
Self::HydrogenFlowRate => "HYDROGEN.FLOW_RATE",
Self::HeatTemperature => "HEAT.TEMPERATURE",
Self::HeatFlowRate => "HEAT.FLOW_RATE",
Self::HeatThermalPower => "HEAT.THERMAL_POWER",
Self::OilFlowRate => "OIL.FLOW_RATE",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum ControlType {
PowerEnvelopeBasedControl,
PowerProfileBasedControl,
OperationModeBasedControl,
FillRateBasedControl,
DemandDrivenBasedControl,
#[serde(rename = "NOT_CONTROLABLE")]
NotControllable,
NoSelection,
}
impl ControlType {
#[must_use]
pub const fn is_controllable(self) -> bool {
matches!(
self,
Self::PowerEnvelopeBasedControl
| Self::PowerProfileBasedControl
| Self::OperationModeBasedControl
| Self::FillRateBasedControl
| Self::DemandDrivenBasedControl
)
}
#[must_use]
pub const fn uses_operation_modes(self) -> bool {
matches!(
self,
Self::OperationModeBasedControl
| Self::FillRateBasedControl
| Self::DemandDrivenBasedControl
)
}
#[must_use]
pub const fn abbreviation(self) -> Option<&'static str> {
match self {
Self::PowerEnvelopeBasedControl => Some("PEBC"),
Self::PowerProfileBasedControl => Some("PPBC"),
Self::OperationModeBasedControl => Some("OMBC"),
Self::FillRateBasedControl => Some("FRBC"),
Self::DemandDrivenBasedControl => Some("DDBC"),
Self::NotControllable | Self::NoSelection => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
#[non_exhaustive]
#[allow(missing_docs)] pub enum Currency {
Aed,
Ang,
Aud,
Che,
Chf,
Chw,
Eur,
Gbp,
Lbp,
Lkr,
Lrd,
Lsl,
Lyd,
Mad,
Mdl,
Mga,
Mkd,
Mmk,
Mnt,
Mop,
Mro,
Mur,
Mvr,
Mwk,
Mxn,
Mxv,
Myr,
Mzn,
Nad,
Ngn,
Nio,
Nok,
Npr,
Nzd,
Omr,
Pab,
Pen,
Pgk,
Php,
Pkr,
Pln,
Pyg,
Qar,
Ron,
Rsd,
Rub,
Rwf,
Sar,
Sbd,
Scr,
Sdg,
Sek,
Sgd,
Shp,
Sll,
Sos,
Srd,
Ssp,
Std,
Syp,
Szl,
Thb,
Tjs,
Tmt,
Tnd,
Top,
Try,
Ttd,
Twd,
Tzs,
Uah,
Ugx,
Usd,
Usn,
Uyi,
Uyu,
Uzs,
Vef,
Vnd,
Vuv,
Wst,
Xag,
Xau,
Xba,
Xbb,
Xbc,
Xbd,
Xcd,
Xof,
Xpd,
Xpf,
Xpt,
Xsu,
Xts,
Xua,
Xxx,
Yer,
Zar,
Zmw,
Zwl,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum EnergyManagementRole {
Cem,
Rm,
}
impl EnergyManagementRole {
#[must_use]
pub const fn peer(self) -> Self {
match self {
Self::Cem => Self::Rm,
Self::Rm => Self::Cem,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum InstructionStatus {
New,
Accepted,
Rejected,
Revoked,
Started,
Succeeded,
Aborted,
}
impl InstructionStatus {
#[must_use]
pub const fn is_terminal(self) -> bool {
matches!(
self,
Self::Rejected | Self::Revoked | Self::Succeeded | Self::Aborted
)
}
#[must_use]
pub const fn progress_rank(self) -> u8 {
match self {
Self::New => 0,
Self::Accepted => 1,
Self::Started => 2,
Self::Succeeded | Self::Rejected | Self::Revoked | Self::Aborted => 3,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[non_exhaustive]
pub enum ReceptionStatusValues {
InvalidData,
InvalidMessage,
InvalidContent,
TemporaryError,
PermanentError,
Ok,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Consequence {
Proceed,
Ignored,
Retry,
Disconnect,
}
impl ReceptionStatusValues {
#[must_use]
pub const fn consequence(self) -> Consequence {
match self {
Self::Ok => Consequence::Proceed,
Self::InvalidData | Self::InvalidMessage | Self::InvalidContent => Consequence::Ignored,
Self::TemporaryError => Consequence::Retry,
Self::PermanentError => Consequence::Disconnect,
}
}
#[must_use]
pub const fn is_error(self) -> bool {
!matches!(self, Self::Ok)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[non_exhaustive]
pub enum RevokableObjects {
#[serde(rename = "PEBC.PowerConstraints")]
PebcPowerConstraints,
#[serde(rename = "PEBC.EnergyConstraint")]
PebcEnergyConstraint,
#[serde(rename = "PEBC.Instruction")]
PebcInstruction,
#[serde(rename = "PPBC.PowerProfileDefinition")]
PpbcPowerProfileDefinition,
#[serde(rename = "PPBC.ScheduleInstruction")]
PpbcScheduleInstruction,
#[serde(rename = "PPBC.StartInterruptionInstruction")]
PpbcStartInterruptionInstruction,
#[serde(rename = "PPBC.EndInterruptionInstruction")]
PpbcEndInterruptionInstruction,
#[serde(rename = "OMBC.SystemDescription")]
OmbcSystemDescription,
#[serde(rename = "OMBC.Instruction")]
OmbcInstruction,
#[serde(rename = "FRBC.SystemDescription")]
FrbcSystemDescription,
#[serde(rename = "FRBC.Instruction")]
FrbcInstruction,
#[serde(rename = "DDBC.SystemDescription")]
DdbcSystemDescription,
#[serde(rename = "DDBC.Instruction")]
DdbcInstruction,
}
impl RevokableObjects {
#[must_use]
pub const fn owner(self) -> EnergyManagementRole {
match self {
Self::PebcInstruction
| Self::PpbcScheduleInstruction
| Self::PpbcStartInterruptionInstruction
| Self::PpbcEndInterruptionInstruction
| Self::OmbcInstruction
| Self::FrbcInstruction
| Self::DdbcInstruction => EnergyManagementRole::Cem,
Self::PebcPowerConstraints
| Self::PebcEnergyConstraint
| Self::PpbcPowerProfileDefinition
| Self::OmbcSystemDescription
| Self::FrbcSystemDescription
| Self::DdbcSystemDescription => EnergyManagementRole::Rm,
}
}
#[must_use]
pub const fn control_type(self) -> ControlType {
match self {
Self::PebcPowerConstraints | Self::PebcEnergyConstraint | Self::PebcInstruction => {
ControlType::PowerEnvelopeBasedControl
}
Self::PpbcPowerProfileDefinition
| Self::PpbcScheduleInstruction
| Self::PpbcStartInterruptionInstruction
| Self::PpbcEndInterruptionInstruction => ControlType::PowerProfileBasedControl,
Self::OmbcSystemDescription | Self::OmbcInstruction => {
ControlType::OperationModeBasedControl
}
Self::FrbcSystemDescription | Self::FrbcInstruction => {
ControlType::FillRateBasedControl
}
Self::DdbcSystemDescription | Self::DdbcInstruction => {
ControlType::DemandDrivenBasedControl
}
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RoleType {
EnergyProducer,
EnergyConsumer,
EnergyStorage,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum SessionRequestType {
Reconnect,
Terminate,
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NumberRange {
pub start_of_range: f64,
pub end_of_range: f64,
}
impl NumberRange {
#[must_use]
pub const fn new(start: f64, end: f64) -> Self {
Self {
start_of_range: start,
end_of_range: end,
}
}
#[must_use]
pub const fn exactly(value: f64) -> Self {
Self::new(value, value)
}
#[must_use]
pub fn contains(self, value: f64) -> bool {
let (lo, hi) = self.ordered();
value >= lo && value <= hi
}
#[must_use]
pub fn ordered(self) -> (f64, f64) {
if self.start_of_range <= self.end_of_range {
(self.start_of_range, self.end_of_range)
} else {
(self.end_of_range, self.start_of_range)
}
}
#[must_use]
pub fn span(self) -> f64 {
self.end_of_range - self.start_of_range
}
#[must_use]
#[allow(
clippy::float_cmp,
reason = "0 and 1 are the endpoints, not approximations"
)]
pub fn at_factor(self, factor: f64) -> f64 {
if factor == 0.0 {
self.start_of_range
} else if factor == 1.0 {
self.end_of_range
} else {
self.start_of_range + self.span() * factor
}
}
#[must_use]
pub fn factor_of(self, value: f64) -> Option<f64> {
let span = self.span();
if span == 0.0 {
None
} else {
Some((value - self.start_of_range) / span)
}
}
#[must_use]
pub fn is_finite(self) -> bool {
self.start_of_range.is_finite() && self.end_of_range.is_finite()
}
#[must_use]
pub fn overlaps(self, other: Self) -> bool {
let (a_lo, a_hi) = self.ordered();
let (b_lo, b_hi) = other.ordered();
a_lo < b_hi && b_lo < a_hi
}
}
impl From<core::ops::Range<f64>> for NumberRange {
fn from(r: core::ops::Range<f64>) -> Self {
Self::new(r.start, r.end)
}
}
impl From<NumberRange> for core::ops::Range<f64> {
fn from(r: NumberRange) -> Self {
r.start_of_range..r.end_of_range
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PowerRange {
pub start_of_range: f64,
pub end_of_range: f64,
pub commodity_quantity: CommodityQuantity,
}
impl PowerRange {
#[must_use]
pub const fn new(start: f64, end: f64, commodity_quantity: CommodityQuantity) -> Self {
Self {
start_of_range: start,
end_of_range: end,
commodity_quantity,
}
}
#[must_use]
pub const fn exactly(value: f64, commodity_quantity: CommodityQuantity) -> Self {
Self::new(value, value, commodity_quantity)
}
#[must_use]
pub const fn range(self) -> NumberRange {
NumberRange::new(self.start_of_range, self.end_of_range)
}
#[must_use]
pub fn at_factor(self, factor: f64) -> f64 {
self.range().at_factor(factor)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PowerValue {
pub commodity_quantity: CommodityQuantity,
pub value: f64,
}
impl PowerValue {
#[must_use]
pub const fn new(commodity_quantity: CommodityQuantity, value: f64) -> Self {
Self {
commodity_quantity,
value,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct PowerForecastValue {
#[serde(skip_serializing_if = "Option::is_none")]
pub value_upper_limit: Option<f64>,
#[serde(rename = "value_upper_95PPR", skip_serializing_if = "Option::is_none")]
pub value_upper_95ppr: Option<f64>,
#[serde(rename = "value_upper_68PPR", skip_serializing_if = "Option::is_none")]
pub value_upper_68ppr: Option<f64>,
pub value_expected: f64,
#[serde(rename = "value_lower_68PPR", skip_serializing_if = "Option::is_none")]
pub value_lower_68ppr: Option<f64>,
#[serde(rename = "value_lower_95PPR", skip_serializing_if = "Option::is_none")]
pub value_lower_95ppr: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub value_lower_limit: Option<f64>,
pub commodity_quantity: CommodityQuantity,
}
impl PowerForecastValue {
#[must_use]
pub const fn expected(value: f64, commodity_quantity: CommodityQuantity) -> Self {
Self {
value_upper_limit: None,
value_upper_95ppr: None,
value_upper_68ppr: None,
value_expected: value,
value_lower_68ppr: None,
value_lower_95ppr: None,
value_lower_limit: None,
commodity_quantity,
}
}
#[must_use]
pub fn bands_ascending(&self) -> [(&'static str, Option<f64>); 7] {
[
("value_lower_limit", self.value_lower_limit),
("value_lower_95PPR", self.value_lower_95ppr),
("value_lower_68PPR", self.value_lower_68ppr),
("value_expected", Some(self.value_expected)),
("value_upper_68PPR", self.value_upper_68ppr),
("value_upper_95PPR", self.value_upper_95ppr),
("value_upper_limit", self.value_upper_limit),
]
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PowerForecastElement {
pub duration: Duration,
pub power_values: Vec<PowerForecastValue>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Role {
pub role: RoleType,
pub commodity: Commodity,
}
impl Role {
#[must_use]
pub const fn new(role: RoleType, commodity: Commodity) -> Self {
Self { role, commodity }
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
#[builder(on(String, into))]
pub struct Timer {
pub id: Id,
#[serde(skip_serializing_if = "Option::is_none")]
pub diagnostic_label: Option<String>,
pub duration: Duration,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct Transition {
pub id: Id,
pub from: Id,
pub to: Id,
pub start_timers: Vec<Id>,
pub blocking_timers: Vec<Id>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transition_costs: Option<f64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub transition_duration: Option<Duration>,
pub abnormal_condition_only: bool,
}
impl Transition {
#[must_use]
pub fn simple(id: Id, from: Id, to: Id) -> Self {
Self {
id,
from,
to,
start_timers: Vec::new(),
blocking_timers: Vec::new(),
transition_costs: None,
transition_duration: None,
abnormal_condition_only: false,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct Handshake {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub role: EnergyManagementRole,
#[serde(skip_serializing_if = "Option::is_none")]
pub supported_protocol_versions: Option<Vec<ProtocolVersion>>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct HandshakeResponse {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub selected_protocol_version: ProtocolVersion,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
#[builder(on(String, into))]
pub struct ResourceManagerDetails {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub resource_id: Id,
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
pub roles: Vec<Role>,
#[serde(skip_serializing_if = "Option::is_none")]
pub manufacturer: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub model: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub serial_number: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub firmware_version: Option<String>,
pub instruction_processing_delay: Duration,
pub available_control_types: Vec<ControlType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
pub provides_forecast: bool,
pub provides_power_measurement_types: Vec<CommodityQuantity>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct SelectControlType {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub control_type: ControlType,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
#[builder(on(String, into))]
pub struct SessionRequest {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub request: SessionRequestType,
#[serde(skip_serializing_if = "Option::is_none")]
pub diagnostic_label: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
#[builder(on(String, into))]
pub struct ReceptionStatus {
pub subject_message_id: Id,
pub status: ReceptionStatusValues,
#[serde(skip_serializing_if = "Option::is_none")]
pub diagnostic_label: Option<String>,
}
impl ReceptionStatus {
#[must_use]
pub fn ok(subject: Id) -> Self {
Self {
subject_message_id: subject,
status: ReceptionStatusValues::Ok,
diagnostic_label: None,
}
}
#[must_use]
pub fn error(
subject: Id,
status: ReceptionStatusValues,
diagnostic: impl Into<String>,
) -> Self {
Self {
subject_message_id: subject,
status,
diagnostic_label: Some(diagnostic.into()),
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct InstructionStatusUpdate {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub instruction_id: Id,
pub status_type: InstructionStatus,
pub timestamp: Timestamp,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct PowerMeasurement {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub measurement_timestamp: Timestamp,
pub values: Vec<PowerValue>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct PowerForecast {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub start_time: Timestamp,
pub elements: Vec<PowerForecastElement>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Builder)]
#[serde(deny_unknown_fields)]
pub struct RevokeObject {
#[cfg_attr(feature = "uuid", builder(default = Id::generate()))]
pub message_id: Id,
pub object_type: RevokableObjects,
pub object_id: Id,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn control_type_wire_spellings_are_the_standards_own() {
for (value, wire) in [
(
ControlType::PowerEnvelopeBasedControl,
"\"POWER_ENVELOPE_BASED_CONTROL\"",
),
(
ControlType::PowerProfileBasedControl,
"\"POWER_PROFILE_BASED_CONTROL\"",
),
(
ControlType::OperationModeBasedControl,
"\"OPERATION_MODE_BASED_CONTROL\"",
),
(
ControlType::FillRateBasedControl,
"\"FILL_RATE_BASED_CONTROL\"",
),
(
ControlType::DemandDrivenBasedControl,
"\"DEMAND_DRIVEN_BASED_CONTROL\"",
),
(ControlType::NotControllable, "\"NOT_CONTROLABLE\""),
(ControlType::NoSelection, "\"NO_SELECTION\""),
] {
assert_eq!(serde_json::to_string(&value).unwrap(), wire);
assert_eq!(serde_json::from_str::<ControlType>(wire).unwrap(), value);
}
assert!(serde_json::from_str::<ControlType>("\"NOT_CONTROLLABLE\"").is_err());
}
#[test]
fn commodity_quantity_wire_spellings() {
for (value, wire) in [
(CommodityQuantity::ElectricPowerL1, "\"ELECTRIC.POWER.L1\""),
(
CommodityQuantity::ElectricPower3PhaseSymmetric,
"\"ELECTRIC.POWER.3_PHASE_SYMMETRIC\"",
),
(
CommodityQuantity::NaturalGasFlowRate,
"\"NATURAL_GAS.FLOW_RATE\"",
),
(
CommodityQuantity::HeatThermalPower,
"\"HEAT.THERMAL_POWER\"",
),
(CommodityQuantity::OilFlowRate, "\"OIL.FLOW_RATE\""),
] {
assert_eq!(serde_json::to_string(&value).unwrap(), wire);
assert_eq!(
serde_json::from_str::<CommodityQuantity>(wire).unwrap(),
value
);
}
}
#[test]
fn revokable_objects_keep_their_dots() {
assert_eq!(
serde_json::to_string(&RevokableObjects::FrbcInstruction).unwrap(),
"\"FRBC.Instruction\""
);
assert_eq!(
RevokableObjects::FrbcInstruction.owner(),
EnergyManagementRole::Cem
);
assert_eq!(
RevokableObjects::FrbcSystemDescription.owner(),
EnergyManagementRole::Rm
);
}
#[test]
fn currency_codes_are_uppercase() {
assert_eq!(serde_json::to_string(&Currency::Eur).unwrap(), "\"EUR\"");
assert_eq!(
serde_json::from_str::<Currency>("\"XXX\"").unwrap(),
Currency::Xxx
);
}
#[test]
fn reception_status_consequences_are_the_normative_ones() {
use Consequence as C;
use ReceptionStatusValues as R;
assert_eq!(R::Ok.consequence(), C::Proceed);
assert_eq!(R::InvalidData.consequence(), C::Ignored);
assert_eq!(R::InvalidMessage.consequence(), C::Ignored);
assert_eq!(R::InvalidContent.consequence(), C::Ignored);
assert_eq!(R::TemporaryError.consequence(), C::Retry);
assert_eq!(R::PermanentError.consequence(), C::Disconnect);
}
#[test]
fn factor_interpolation_matches_the_documented_formula() {
let r = NumberRange::new(1000.0, 2500.0);
assert!((r.at_factor(0.5) - 1750.0).abs() < 1e-9);
assert!((r.at_factor(0.0) - 1000.0).abs() < 1e-9);
assert!((r.at_factor(1.0) - 2500.0).abs() < 1e-9);
assert!((r.factor_of(1750.0).unwrap() - 0.5).abs() < 1e-9);
assert_eq!(NumberRange::exactly(0.0).factor_of(0.0), None);
assert!((NumberRange::exactly(7.0).at_factor(0.3) - 7.0).abs() < 1e-12);
}
#[test]
fn unknown_fields_are_refused() {
let json = r#"{"start_of_range":0,"end_of_range":1,"extra":true}"#;
assert!(serde_json::from_str::<NumberRange>(json).is_err());
}
#[test]
fn absent_optionals_are_absent_rather_than_null() {
let v = PowerForecastValue::expected(-3450.1, CommodityQuantity::ElectricPowerL1);
let json = serde_json::to_string(&v).unwrap();
assert_eq!(
json,
r#"{"value_expected":-3450.1,"commodity_quantity":"ELECTRIC.POWER.L1"}"#
);
}
#[test]
fn percentile_bands_keep_their_capitalised_wire_names() {
let json = r#"{
"value_upper_limit": -3400.0, "value_upper_95PPR": -3440.0,
"value_upper_68PPR": -3445.0, "value_expected": -3450.1,
"value_lower_68PPR": -3455.0, "value_lower_95PPR": -3460.0,
"value_lower_limit": -3500.0, "commodity_quantity": "ELECTRIC.POWER.L1"
}"#;
let v: PowerForecastValue = serde_json::from_str(json).unwrap();
assert_eq!(v.value_upper_95ppr, Some(-3440.0));
assert_eq!(v.value_lower_68ppr, Some(-3455.0));
let back = serde_json::to_string(&v).unwrap();
assert!(back.contains("\"value_upper_95PPR\":-3440.0"));
}
}