#![allow(
clippy::unwrap_used,
clippy::expect_used,
clippy::panic,
clippy::indexing_slicing,
clippy::float_cmp,
clippy::print_stdout
)]
use s2_kit::prelude::*;
use s2_kit::session::Analyzer;
use s2_kit::types::common::EnergyManagementRole::{Cem, Rm};
fn main() {
let now: Timestamp = "2024-01-01T12:00:00Z".parse().unwrap();
let traffic = [
(
Rm,
r#"{"message_type":"ResourceManagerDetails","message_id":"d1",
"resource_id":"battery-1","roles":[{"role":"ENERGY_STORAGE","commodity":"ELECTRICITY"}],
"instruction_processing_delay":500,
"available_control_types":["FILL_RATE_BASED_CONTROL"],
"provides_forecast":false,
"provides_power_measurement_types":["ELECTRIC.POWER.3_PHASE_SYMMETRIC"]}"#,
),
(
Cem,
r#"{"message_type":"SelectControlType","message_id":"c1",
"control_type":"FILL_RATE_BASED_CONTROL"}"#,
),
(
Rm,
r#"{"message_type":"FRBC.SystemDescription","message_id":"s1",
"valid_from":"2024-01-01T00:00:00Z",
"actuators":[{"id":"actuator1","supported_commodities":["ELECTRICITY"],
"operation_modes":[{"id":"om1","elements":[
{"fill_level_range":{"start_of_range":0,"end_of_range":100},
"fill_rate":{"start_of_range":0,"end_of_range":0.001},
"power_ranges":[{"start_of_range":0,"end_of_range":5000,
"commodity_quantity":"ELECTRIC.POWER.3_PHASE_SYMMETRIC"}]}],
"abnormal_condition_only":false}],
"transitions":[],"timers":[]}],
"storage":{"provides_leakage_behaviour":false,
"provides_fill_level_target_profile":false,"provides_usage_forecast":false,
"fill_level_range":{"start_of_range":0,"end_of_range":100}}}"#,
),
(
Rm,
r#"{"message_type":"FRBC.StorageStatus","message_id":"m2",
"present_fill_level":48.0,"acme_cell_temperature":31.4}"#,
),
(
Cem,
r#"{"message_type":"FRBC.Instruction","message_id":"m3","id":"i1",
"actuator_id":"actuator1","operation_mode":"om1","operation_mode_factor":1.3,
"execution_time":"2024-01-01T12:00:00Z","abnormal_condition":false}"#,
),
(
Cem,
r#"{"message_type":"FRBC.Instruction","message_id":"m4","id":"i2",
"actuator_id":"actuator9","operation_mode":"om1","operation_mode_factor":0.5,
"execution_time":"2024-01-01T12:00:00Z","abnormal_condition":false}"#,
),
(
Rm,
r#"{"message_type":"SelectControlType","message_id":"m5",
"control_type":"NOT_CONTROLABLE"}"#,
),
(Cem, r#"{"message_type":"FRBC.Surprise","message_id":"m6"}"#),
];
let mut analyzer = Analyzer::new();
let mut forwarded = 0;
let mut findings = 0;
for (sender, text) in traffic {
let observed = analyzer.observe(sender, text, now);
let who = if sender == Rm { "rm >" } else { "cem>" };
match &observed.message {
Some(message) => {
for violation in observed.report.violations() {
println!(" ! {violation}");
findings += 1;
}
println!("{who} {}", encode(message));
forwarded += 1;
}
None => {
for violation in observed.report.violations() {
println!(" ✗ {violation}");
findings += 1;
}
}
}
}
println!(
"\nforwarded {forwarded} of {} messages, {findings} finding(s)",
traffic.len()
);
println!(
"the conversation described {} actuator(s)",
analyzer
.registry()
.frbc
.as_ref()
.map_or(0, |d| d.actuators.len())
);
}