use crate::message::MessageKind;
use crate::types::common::{ControlType, EnergyManagementRole};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Allowance {
Yes,
WrongState,
WrongRole,
}
impl Allowance {
#[must_use]
pub const fn is_allowed(self) -> bool {
matches!(self, Allowance::Yes)
}
}
#[must_use]
pub fn allowed(
active: Option<ControlType>,
sender: EnergyManagementRole,
kind: MessageKind,
) -> Allowance {
use MessageKind as K;
if let Some(expected) = kind.sender()
&& expected != sender
{
return Allowance::WrongRole;
}
let active = active.filter(|c| c.is_controllable());
#[allow(clippy::match_same_arms)]
match kind {
K::ReceptionStatus | K::SessionRequest => Allowance::Yes,
K::Handshake | K::HandshakeResponse => {
if active.is_none() {
Allowance::Yes
} else {
Allowance::WrongState
}
}
K::ResourceManagerDetails
| K::PowerMeasurement
| K::PowerForecast
| K::SelectControlType => Allowance::Yes,
K::InstructionStatusUpdate | K::RevokeObject => {
if active.is_some() {
Allowance::Yes
} else {
Allowance::WrongState
}
}
_ => match (kind.control_type(), active) {
(Some(needed), Some(current)) if needed == current => Allowance::Yes,
_ => Allowance::WrongState,
},
}
}
#[must_use]
pub fn allowed_kinds(
active: Option<ControlType>,
sender: EnergyManagementRole,
) -> alloc::vec::Vec<MessageKind> {
MessageKind::ALL
.iter()
.copied()
.filter(|k| allowed(active, sender, *k).is_allowed())
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use EnergyManagementRole::{Cem, Rm};
use alloc::vec::Vec;
fn names(active: Option<ControlType>, sender: EnergyManagementRole) -> Vec<&'static str> {
let mut v: Vec<_> = allowed_kinds(active, sender)
.into_iter()
.map(crate::message::MessageKind::as_str)
.collect();
v.sort_unstable();
v
}
#[test]
fn the_websocket_connected_row_is_the_standards_own() {
assert_eq!(
names(None, Cem),
alloc::vec![
"Handshake",
"HandshakeResponse",
"ReceptionStatus",
"SelectControlType",
"SessionRequest",
]
);
assert_eq!(
names(None, Rm),
alloc::vec![
"Handshake",
"PowerForecast",
"PowerMeasurement",
"ReceptionStatus",
"ResourceManagerDetails",
"SessionRequest",
]
);
}
#[test]
fn the_frbc_row_is_the_standards_own() {
assert_eq!(
names(Some(ControlType::FillRateBasedControl), Cem),
alloc::vec![
"FRBC.Instruction",
"ReceptionStatus",
"RevokeObject",
"SelectControlType",
"SessionRequest",
]
);
assert_eq!(
names(Some(ControlType::FillRateBasedControl), Rm),
alloc::vec![
"FRBC.ActuatorStatus",
"FRBC.FillLevelTargetProfile",
"FRBC.LeakageBehaviour",
"FRBC.StorageStatus",
"FRBC.SystemDescription",
"FRBC.TimerStatus",
"FRBC.UsageForecast",
"InstructionStatusUpdate",
"PowerForecast",
"PowerMeasurement",
"ReceptionStatus",
"ResourceManagerDetails",
"RevokeObject",
"SessionRequest",
]
);
}
#[test]
fn the_pebc_row_is_the_standards_own() {
assert_eq!(
names(Some(ControlType::PowerEnvelopeBasedControl), Cem),
alloc::vec![
"PEBC.Instruction",
"ReceptionStatus",
"RevokeObject",
"SelectControlType",
"SessionRequest",
]
);
assert_eq!(
names(Some(ControlType::PowerEnvelopeBasedControl), Rm),
alloc::vec![
"InstructionStatusUpdate",
"PEBC.EnergyConstraint",
"PEBC.PowerConstraints",
"PowerForecast",
"PowerMeasurement",
"ReceptionStatus",
"ResourceManagerDetails",
"RevokeObject",
"SessionRequest",
]
);
}
#[test]
fn selecting_not_controlable_leaves_the_session_where_it_was() {
assert_eq!(
names(Some(ControlType::NotControllable), Rm),
names(None, Rm)
);
assert_eq!(names(Some(ControlType::NoSelection), Rm), names(None, Rm));
}
#[test]
fn a_control_type_message_in_the_wrong_control_type_is_a_state_error() {
assert_eq!(
allowed(
Some(ControlType::PowerEnvelopeBasedControl),
Cem,
MessageKind::FrbcInstruction
),
Allowance::WrongState
);
assert_eq!(
allowed(None, Cem, MessageKind::FrbcInstruction),
Allowance::WrongState
);
}
#[test]
fn a_message_from_the_wrong_role_is_a_role_error() {
assert_eq!(
allowed(
Some(ControlType::FillRateBasedControl),
Rm,
MessageKind::FrbcInstruction
),
Allowance::WrongRole
);
assert_eq!(
allowed(None, Cem, MessageKind::ResourceManagerDetails),
Allowance::WrongRole
);
assert_eq!(
allowed(None, Rm, MessageKind::SelectControlType),
Allowance::WrongRole
);
}
#[test]
fn revocation_is_accepted_from_both_roles() {
for role in [Cem, Rm] {
assert_eq!(
allowed(
Some(ControlType::FillRateBasedControl),
role,
MessageKind::RevokeObject
),
Allowance::Yes
);
}
}
#[test]
fn instruction_statuses_need_something_to_be_about() {
assert_eq!(
allowed(None, Rm, MessageKind::InstructionStatusUpdate),
Allowance::WrongState
);
assert_eq!(
allowed(
Some(ControlType::OperationModeBasedControl),
Rm,
MessageKind::InstructionStatusUpdate
),
Allowance::Yes
);
}
#[test]
fn every_message_is_allowed_somewhere() {
for kind in MessageKind::ALL {
let reachable = [
None,
Some(ControlType::PowerEnvelopeBasedControl),
Some(ControlType::PowerProfileBasedControl),
Some(ControlType::OperationModeBasedControl),
Some(ControlType::FillRateBasedControl),
Some(ControlType::DemandDrivenBasedControl),
]
.into_iter()
.any(|active| {
[Cem, Rm]
.into_iter()
.any(|role| allowed(active, role, *kind).is_allowed())
});
assert!(reachable, "{kind} can never be sent");
}
}
}