s2-kit 0.4.0

The S2 energy flexibility standard (EN 50491-12-2) in Rust: the S2 JSON data model, a semantic validator, sans-IO Resource Manager and Customer Energy Manager session engines, and S2 Connect
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! The normative table of what may be sent when.
//!
//! `S2C §State of communication` gives it as a table with one row per state and one
//! column per role. This module is that table, and it is the only place the rule lives:
//! the session engines consult it rather than carrying a second copy.
//!
//! ```text
//! WebSocketConnected ──SelectControlType(ct)──▶ ControlTypeActivated(ct)
//!         ▲                                             │
//!         └──────── SelectControlType(NO_SELECTION) ─────┘
//! ```

use crate::message::MessageKind;
use crate::types::common::{ControlType, EnergyManagementRole};

/// Whether a message may be sent, and if not, why not.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Allowance {
    /// It may.
    Yes,
    /// Not in this state.
    WrongState,
    /// Not from this role.
    WrongRole,
}

impl Allowance {
    /// Whether the message is allowed.
    #[must_use]
    pub const fn is_allowed(self) -> bool {
        matches!(self, Allowance::Yes)
    }
}

/// Where a session has got to, as the state table sees it.
///
/// The standard's own table has two rows — `WebSocket Connected` and
/// `ControlType <X> activated` — because under S2 Connect the version is settled before
/// the socket opens and there is nothing before the first row. A bare-WebSocket session
/// has one more: `S2J messages/Handshake` is exchanged first, and until it is, the two
/// sides have not agreed which schema the next message is to be read against.
///
/// [`Negotiating`](Self::Negotiating) is that row. Leaving it out is what lets a
/// `PowerMeasurement` sent before the handshake — a message whose *version* nobody has
/// agreed — pass a check named after the state it is not in.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum Phase {
    /// Before the handshake has completed, on a session that has one.
    ///
    /// Only `Handshake`, `HandshakeResponse`, `SessionRequest` and `ReceptionStatus` may
    /// cross. A session under S2 Connect is never in this phase: session initiation
    /// already agreed the version, and the handshake messages "can not be sent".
    Negotiating,
    /// `WebSocketConnected`, the standard's first row.
    ///
    /// Also where a session sits after `NO_SELECTION` or `NOT_CONTROLABLE`, which are a
    /// deselection and a selection with nothing to instruct.
    #[default]
    Connected,
    /// `ControlTypeActivated`, the standard's per-control-type rows.
    Activated(ControlType),
}

impl Phase {
    /// The phase a control-type selection puts a session in.
    ///
    /// `NO_SELECTION` and `NOT_CONTROLABLE` are [`Connected`](Self::Connected): the first
    /// is a deselection, the second a legal selection after which nothing control-type
    /// specific may flow (docs `learn/examples/nocontrol`).
    #[must_use]
    pub fn selected(control_type: ControlType) -> Self {
        if control_type.is_controllable() {
            Phase::Activated(control_type)
        } else {
            Phase::Connected
        }
    }

    /// The control type that is active, if one that can be instructed is.
    #[must_use]
    pub const fn active_control_type(self) -> Option<ControlType> {
        match self {
            Phase::Activated(c) => Some(c),
            Phase::Negotiating | Phase::Connected => None,
        }
    }

    /// The row's name, for a diagnostic.
    #[must_use]
    pub fn label(self) -> &'static str {
        match self {
            Phase::Negotiating => "negotiating",
            Phase::Connected => "WebSocketConnected",
            Phase::Activated(c) => c.abbreviation().unwrap_or("WebSocketConnected"),
        }
    }
}

/// Whether `sender` may send `kind` while the session is in `phase`.
#[must_use]
pub fn allowed(phase: Phase, sender: EnergyManagementRole, kind: MessageKind) -> Allowance {
    use MessageKind as K;

    if let Some(expected) = kind.sender()
        && expected != sender
    {
        return Allowance::WrongRole;
    }

    let active = phase.active_control_type();

    // The arms below are grouped by *reason*, not by outcome, so several share a body on
    // purpose: collapsing them would lose the row of the standard's table each belongs to.
    #[allow(clippy::match_same_arms)]
    match kind {
        // Never state-dependent: either side, at any point. A handshake has to be
        // answerable, and either side may give up at any moment.
        K::ReceptionStatus | K::SessionRequest => Allowance::Yes,

        // The handshake belongs to the start of a session, and only to it. Under S2
        // Connect it does not happen at all, which is a warning rather than a state error
        // (E9 / S2-STATE-003).
        K::Handshake | K::HandshakeResponse => match phase {
            Phase::Negotiating | Phase::Connected => Allowance::Yes,
            Phase::Activated(_) => Allowance::WrongState,
        },

        // Everything below needs an agreed version to be read against, so nothing but the
        // handshake itself crosses while one is still being agreed.
        _ if phase == Phase::Negotiating => Allowance::WrongState,

        // The RM's standing capabilities, and the CEM's ability to change its mind.
        // `S2C`: measurements and forecasts "can always be used, even if no Control Type
        // has been activated".
        K::ResourceManagerDetails
        | K::PowerMeasurement
        | K::PowerForecast
        | K::SelectControlType => Allowance::Yes,

        // Only meaningful once something can be instructed.
        K::InstructionStatusUpdate | K::RevokeObject => {
            if active.is_some() {
                Allowance::Yes
            } else {
                Allowance::WrongState
            }
        }

        // Everything else belongs to exactly one control type.
        _ => match (kind.control_type(), active) {
            (Some(needed), Some(current)) if needed == current => Allowance::Yes,
            _ => Allowance::WrongState,
        },
    }
}

/// Every message the role may send in the phase, for documentation and tests.
#[must_use]
pub fn allowed_kinds(phase: Phase, sender: EnergyManagementRole) -> alloc::vec::Vec<MessageKind> {
    MessageKind::ALL
        .iter()
        .copied()
        .filter(|k| allowed(phase, sender, *k).is_allowed())
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use EnergyManagementRole::{Cem, Rm};
    use alloc::vec::Vec;

    fn names(phase: Phase, sender: EnergyManagementRole) -> Vec<&'static str> {
        let mut v: Vec<_> = allowed_kinds(phase, sender)
            .into_iter()
            .map(crate::message::MessageKind::as_str)
            .collect();
        v.sort_unstable();
        v
    }

    #[test]
    fn the_websocket_connected_row_is_the_standards_own() {
        // `S2C §State of communication`, row "WebSocket Connected". The handshake
        // messages are added because a bare-WebSocket session exchanges them here.
        assert_eq!(
            names(Phase::Connected, Cem),
            alloc::vec![
                "Handshake",
                "HandshakeResponse",
                "ReceptionStatus",
                "SelectControlType",
                "SessionRequest",
            ]
        );
        assert_eq!(
            names(Phase::Connected, Rm),
            alloc::vec![
                "Handshake",
                "PowerForecast",
                "PowerMeasurement",
                "ReceptionStatus",
                "ResourceManagerDetails",
                "SessionRequest",
            ]
        );
    }

    #[test]
    fn nothing_but_the_handshake_crosses_while_the_version_is_still_being_agreed() {
        // The row the standard's table does not have, because under S2 Connect there is
        // nothing before `WebSocketConnected`. A bare-WebSocket session does have one,
        // and a message sent in it is a message whose schema version nobody has agreed.
        assert_eq!(
            names(Phase::Negotiating, Rm),
            alloc::vec!["Handshake", "ReceptionStatus", "SessionRequest"]
        );
        assert_eq!(
            names(Phase::Negotiating, Cem),
            alloc::vec![
                "Handshake",
                "HandshakeResponse",
                "ReceptionStatus",
                "SessionRequest",
            ]
        );
        // Including the RM's standing capabilities, which are unrestricted *afterwards*.
        assert_eq!(
            allowed(Phase::Negotiating, Rm, MessageKind::PowerMeasurement),
            Allowance::WrongState
        );
        assert_eq!(
            allowed(Phase::Negotiating, Rm, MessageKind::ResourceManagerDetails),
            Allowance::WrongState
        );
        assert_eq!(
            allowed(Phase::Negotiating, Cem, MessageKind::SelectControlType),
            Allowance::WrongState
        );
    }

    #[test]
    fn the_handshake_is_over_once_a_control_type_is_active() {
        assert_eq!(
            allowed(
                Phase::Activated(ControlType::FillRateBasedControl),
                Rm,
                MessageKind::Handshake
            ),
            Allowance::WrongState
        );
    }

    #[test]
    fn the_frbc_row_is_the_standards_own() {
        let frbc = Phase::Activated(ControlType::FillRateBasedControl);
        assert_eq!(
            names(frbc, Cem),
            alloc::vec![
                "FRBC.Instruction",
                "ReceptionStatus",
                "RevokeObject",
                "SelectControlType",
                "SessionRequest",
            ]
        );
        assert_eq!(
            names(frbc, 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() {
        let pebc = Phase::Activated(ControlType::PowerEnvelopeBasedControl);
        assert_eq!(
            names(pebc, Cem),
            alloc::vec![
                "PEBC.Instruction",
                "ReceptionStatus",
                "RevokeObject",
                "SelectControlType",
                "SessionRequest",
            ]
        );
        assert_eq!(
            names(pebc, 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() {
        // "NOT_CONTROLABLE" is a real selection, but nothing control-type specific may
        // follow it: docs `learn/examples/nocontrol`.
        assert_eq!(
            Phase::selected(ControlType::NotControllable),
            Phase::Connected
        );
        assert_eq!(Phase::selected(ControlType::NoSelection), Phase::Connected);
        assert_eq!(
            Phase::selected(ControlType::FillRateBasedControl),
            Phase::Activated(ControlType::FillRateBasedControl)
        );
    }

    #[test]
    fn a_control_type_message_in_the_wrong_control_type_is_a_state_error() {
        assert_eq!(
            allowed(
                Phase::Activated(ControlType::PowerEnvelopeBasedControl),
                Cem,
                MessageKind::FrbcInstruction
            ),
            Allowance::WrongState
        );
        assert_eq!(
            allowed(Phase::Connected, Cem, MessageKind::FrbcInstruction),
            Allowance::WrongState
        );
    }

    #[test]
    fn a_message_from_the_wrong_role_is_a_role_error() {
        // An RM does not instruct, and a CEM does not describe itself as a resource.
        // The role is checked before the phase, so it is the answer even where both are
        // wrong: a peer told "not in this state" would try again later for ever.
        assert_eq!(
            allowed(
                Phase::Activated(ControlType::FillRateBasedControl),
                Rm,
                MessageKind::FrbcInstruction
            ),
            Allowance::WrongRole
        );
        assert_eq!(
            allowed(Phase::Connected, Cem, MessageKind::ResourceManagerDetails),
            Allowance::WrongRole
        );
        assert_eq!(
            allowed(Phase::Connected, Rm, MessageKind::SelectControlType),
            Allowance::WrongRole
        );
        assert_eq!(
            allowed(Phase::Negotiating, Cem, MessageKind::ResourceManagerDetails),
            Allowance::WrongRole
        );
    }

    #[test]
    fn revocation_is_accepted_from_both_roles() {
        // E6: the state table lists RevokeObject only for the RM, but RevokableObjects
        // contains every instruction type, which only a CEM sends. Refusing the CEM's
        // revocation would make most of that enum unreachable.
        for role in [Cem, Rm] {
            assert_eq!(
                allowed(
                    Phase::Activated(ControlType::FillRateBasedControl),
                    role,
                    MessageKind::RevokeObject
                ),
                Allowance::Yes
            );
        }
    }

    #[test]
    fn instruction_statuses_need_something_to_be_about() {
        assert_eq!(
            allowed(Phase::Connected, Rm, MessageKind::InstructionStatusUpdate),
            Allowance::WrongState
        );
        assert_eq!(
            allowed(
                Phase::Activated(ControlType::OperationModeBasedControl),
                Rm,
                MessageKind::InstructionStatusUpdate
            ),
            Allowance::Yes
        );
    }

    #[test]
    fn every_message_is_allowed_somewhere() {
        // A message no phase and no role can send would be a hole in the table.
        for kind in MessageKind::ALL {
            let reachable = [
                Phase::Negotiating,
                Phase::Connected,
                Phase::Activated(ControlType::PowerEnvelopeBasedControl),
                Phase::Activated(ControlType::PowerProfileBasedControl),
                Phase::Activated(ControlType::OperationModeBasedControl),
                Phase::Activated(ControlType::FillRateBasedControl),
                Phase::Activated(ControlType::DemandDrivenBasedControl),
            ]
            .into_iter()
            .any(|phase| {
                [Cem, Rm]
                    .into_iter()
                    .any(|role| allowed(phase, role, *kind).is_allowed())
            });
            assert!(reachable, "{kind} can never be sent");
        }
    }
}