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
use std::fmt::Display;

use sep2_common_derive::{
    SEEvent, SEIdentifiedObject, SEResource, SERespondableResource,
    SERespondableSubscribableIdentifiedObject, SESubscribableResource,
};

use crate::traits::{
    SEEvent, SEIdentifiedObject, SEResource, SERespondableResource,
    SERespondableSubscribableIdentifiedObject, SESubscribableResource, Validate,
};

use yaserde::{YaDeserialize, YaSerialize};

use super::{
    identification::ResponseRequired,
    primitives::{String192, String32, Uint16},
    types::{
        DateTimeInterval, MRIDType, OneHourRangeType, SubscribableType, TimeType, VersionType,
    },
};

/// Current status information relevant to a specific object. The Status object
/// is used to indicate the current status of an Event. Devices can read the
/// containing resource (e.g. TextMessage) to get the most up to date status of
/// the event. Devices can also subscribe to a specific resource instance to get
/// updates when any of its attributes change, including the Status object.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "EventStatus")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct EventStatus {
    /// Field representing the current status type.
    /// 0 = Scheduled
    /// This status indicates that the event has been scheduled and the event has
    /// not yet started. The server SHALL set the event to this status when the
    /// event is first scheduled and persist until the event has become active or
    /// has been cancelled. For events with a start time less than or equal to
    /// the current time, this status SHALL never be indicated, the event SHALL
    /// start with a status of “Active”.
    /// 1 = Active
    /// This status indicates that the event is currently active. The server
    /// SHALL set the event to this status when the event reaches its earliest
    /// Effective Start Time.
    /// 2 = Cancelled
    /// When events are cancelled, the Status.dateTime attribute SHALL be set to
    /// the time the cancellation occurred, which cannot be in the future. The
    /// server is responsible for maintaining the cancelled event in its
    /// collection for the duration of the original event, or until the server
    /// has run out of space and needs to store a new event. Client devices SHALL
    /// be aware of Cancelled events, determine if the Cancelled event applies to
    /// them, and cancel the event immediately if applicable.
    /// 3 = Cancelled with Randomization
    /// The server is responsible for maintaining the cancelled event in its
    /// collection for the duration of the Effective Scheduled Period. Client
    /// devices SHALL be aware of Cancelled with Randomization events, determine
    /// if the Cancelled event applies to them, and cancel the event immediately,
    /// using the larger of (absolute value of randomizeStart) and (absolute
    /// value of randomizeDuration) as the end randomization, in seconds. This
    /// Status.type SHALL NOT be used with "regular" Events, only with
    /// specializations of RandomizableEvent.
    /// 4 = Superseded
    /// Events marked as Superseded by servers are events that may have been
    /// replaced by new events from the same program that target the exact same
    /// set of deviceCategory's (if applicable) AND DERControl controls (e.g.,
    /// opModTargetW) (if applicable) and overlap for a given period of time.
    /// Servers SHALL mark an event as Superseded at the earliest Effective Start
    /// Time of the overlapping event. Servers are responsible for maintaining
    /// the Superseded event in their collection for the duration of the
    /// Effective Scheduled Period.
    /// Client devices encountering a Superseded event SHALL terminate execution
    /// of the event immediately and commence execution of the new event
    /// immediately, unless the current time is within the start randomization
    /// window of the superseded event, in which case the client SHALL obey the
    /// start randomization of the new event. This Status.type SHALL NOT be used
    /// with TextMessage, since multiple text messages can be active.
    /// All other values reserved.
    #[yaserde(rename = "currentStatus")]
    pub current_status: EventStatusType,

    /// The dateTime attribute will provide a timestamp of when the current
    /// status was defined. dateTime MUST be set to the time at which the status
    /// change occurred, not a time in the future or past.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// Set to true by a server of this event if there are events that overlap
    /// this event in time and also overlap in some, but not all,
    /// deviceCategory's (if applicable) AND DERControl controls (e.g.,
    /// opModTargetW) (if applicable) in the same function set instance.
    #[yaserde(rename = "potentiallySuperseded")]
    pub potentially_superseded: bool,

    /// Indicates the time that the potentiallySuperseded flag was set.
    #[yaserde(rename = "potentiallySupersededTime")]
    pub potentially_superseded_time: Option<TimeType>,

    /// The Reason attribute allows a Service provider to provide a textual
    /// explanation of the status.
    #[yaserde(rename = "reason")]
    pub reason: Option<String192>,
}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum EventStatusType {
    #[default]
    Scheduled,
    Active,
    Cancelled,
    CancelledRandom,
    Superseded,
}

impl Validate for EventStatus {}

/// An Event indicates information that applies to a particular period of time. Events SHALL be executed relative to the time of the server, as described in the Time function set section 11.1.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SEEvent,
    SERespondableSubscribableIdentifiedObject,
    SEIdentifiedObject,
    SESubscribableResource,
    SERespondableResource,
    SEResource,
)]
#[yaserde(rename = "Event")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Event {
    /// The time at which the Event was created.
    #[yaserde(rename = "creationTime")]
    pub creation_time: TimeType,

    #[yaserde(rename = "EventStatus")]
    pub event_status: EventStatus,

    /// The period during which the Event applies.
    #[yaserde(rename = "interval")]
    pub interval: DateTimeInterval,

    /// The global identifier of the object.
    #[yaserde(rename = "mRID")]
    pub mrid: MRIDType,

    /// The description is a human readable text describing or naming the object.
    #[yaserde(rename = "description")]
    pub description: Option<String32>,

    /// Contains the version number of the object. See the type definition for
    /// details.
    #[yaserde(rename = "version")]
    pub version: Option<VersionType>,

    /// Indicates whether or not subscriptions are supported for this resource,
    /// and whether or not conditional (thresholds) are supported. If not
    /// specified, is "not subscribable" (0).
    #[yaserde(attribute, rename = "subscribable")]
    pub subscribable: Option<SubscribableType>,

    /// A reference to the response resource address (URI). Required on a
    /// response to a GET if responseRequired is "true".
    #[yaserde(attribute, rename = "replyTo")]
    pub reply_to: Option<String>,

    /// Indicates whether or not a response is required upon receipt, creation or
    /// update of this resource. Responses shall be posted to the collection
    /// specified in "replyTo".
    /// If the resource has a deviceCategory field, devices that match one or
    /// more of the device types indicated in deviceCategory SHALL respond
    /// according to the rules listed below. If the category does not match, the
    /// device SHALL NOT respond. If the resource does not have a deviceCategory
    /// field, a device receiving the resource SHALL respond according to the
    /// rules listed below.
    /// Value encoded as hex according to the following bit assignments, any
    /// combination is possible.
    /// See Table 27 for the list of appropriate Response status codes to be sent
    /// for these purposes.
    /// 0 - End device shall indicate that message was received
    /// 1 - End device shall indicate specific response.
    /// 2 - End user / customer response is required.
    /// All other values reserved.
    #[yaserde(attribute, rename = "responseRequired")]
    pub response_required: Option<ResponseRequired>,

    /// A reference to the resource address (URI). Required in a response to a
    /// GET, ignored otherwise.
    #[yaserde(attribute, rename = "href")]
    pub href: Option<String>,
}

impl Validate for Event {}

/// Contains information about the nature of an error if a request could not be
/// completed successfully.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "Error")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Error {
    /// Contains the number of seconds the client SHOULD wait before retrying the
    /// request.
    #[yaserde(rename = "maxRetryDuration")]
    pub max_retry_duration: Option<Uint16>,

    /// Code indicating the reason for failure.
    /// 0 - Invalid request format
    /// 1 - Invalid request values (e.g. invalid threshold values)
    /// 2 - Resource limit reached
    /// 3 - Conditional subscription field not supported
    /// 4 - Maximum request frequency exceeded
    /// All other values reserved
    #[yaserde(rename = "reasonCode")]
    pub reason_code: ErrorReason,
}

impl Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.max_retry_duration {
            Some(d) => write!(
                f,
                "Request Error - Max Retry Duration: {} - Reason: {}",
                d, self.reason_code
            ),
            None => write!(f, "Request Error - Reason: {}", self.reason_code),
        }
    }
}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u16)]
pub enum ErrorReason {
    #[default]
    InvalidRequestFormat = 0,
    InvalidRequestValues = 1,
    ResourceLimitReached = 2,
    SubscriptionNotSupported = 3,
    MaximumRequestFrequency = 4,
}

impl Display for ErrorReason {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorReason::InvalidRequestFormat => write!(f, "Invalid request format"),
            ErrorReason::InvalidRequestValues => {
                write!(f, "Invalid request values (e.g. invalid threshold values)")
            }
            ErrorReason::ResourceLimitReached => write!(f, "Resource limit reached"),
            ErrorReason::SubscriptionNotSupported => {
                write!(f, "Conditional subscription field not supported")
            }
            ErrorReason::MaximumRequestFrequency => write!(f, "Maximum request frequency exceeded"),
        }
    }
}
impl Validate for Error {}

/// An Event that can indicate time ranges over which the start time
/// and duration SHALL be randomized.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SEEvent,
    SERespondableSubscribableIdentifiedObject,
    SEIdentifiedObject,
    SESubscribableResource,
    SERespondableResource,
    SEResource,
)]
#[yaserde(rename = "RandomizableEvent")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct RandomizableEvent {
    /// Number of seconds boundary inside which a random value must be selected
    /// to be applied to the associated interval duration, to avoid sudden
    /// synchronized demand changes. If related to price level changes, sign may
    /// be ignored. Valid range is -3600 to 3600. If not specified, 0 is the
    /// default.
    #[yaserde(rename = "randomizeDuration")]
    pub randomize_duration: Option<OneHourRangeType>,

    /// Number of seconds boundary inside which a random value must be selected
    /// to be applied to the associated interval start time, to avoid sudden
    /// synchronized demand changes. If related to price level changes, sign may
    /// be ignored. Valid range is -3600 to 3600. If not specified, 0 is the
    /// default.
    #[yaserde(rename = "randomizeStart")]
    pub randomize_start: Option<OneHourRangeType>,

    /// The time at which the Event was created.
    #[yaserde(rename = "creationTime")]
    pub creation_time: TimeType,

    #[yaserde(rename = "EventStatus")]
    pub event_status: EventStatus,

    /// The period during which the Event applies.
    #[yaserde(rename = "interval")]
    pub interval: DateTimeInterval,

    /// The global identifier of the object.
    #[yaserde(rename = "mRID")]
    pub mrid: MRIDType,

    /// The description is a human readable text describing or naming the object.
    #[yaserde(rename = "description")]
    pub description: Option<String32>,

    /// Contains the version number of the object. See the type definition for
    /// details.
    #[yaserde(rename = "version")]
    pub version: Option<VersionType>,

    /// Indicates whether or not subscriptions are supported for this resource,
    /// and whether or not conditional (thresholds) are supported. If not
    /// specified, is "not subscribable" (0).
    #[yaserde(attribute, rename = "subscribable")]
    pub subscribable: Option<SubscribableType>,

    /// A reference to the response resource address (URI). Required on a
    /// response to a GET if responseRequired is "true".
    #[yaserde(attribute, rename = "replyTo")]
    pub reply_to: Option<String>,

    /// Indicates whether or not a response is required upon receipt, creation or
    /// update of this resource. Responses shall be posted to the collection
    /// specified in "replyTo".
    /// If the resource has a deviceCategory field, devices that match one or
    /// more of the device types indicated in deviceCategory SHALL respond
    /// according to the rules listed below. If the category does not match, the
    /// device SHALL NOT respond. If the resource does not have a deviceCategory
    /// field, a device receiving the resource SHALL respond according to the
    /// rules listed below.
    /// Value encoded as hex according to the following bit assignments, any
    /// combination is possible.
    /// See Table 27 for the list of appropriate Response status codes to be sent
    /// for these purposes.
    /// 0 - End device shall indicate that message was received
    /// 1 - End device shall indicate specific response.
    /// 2 - End user / customer response is required.
    /// All other values reserved.
    #[yaserde(attribute, rename = "responseRequired")]
    pub response_required: Option<ResponseRequired>,

    /// A reference to the resource address (URI). Required in a response to a
    /// GET, ignored otherwise.
    #[yaserde(attribute, rename = "href")]
    pub href: Option<String>,
}

impl Validate for RandomizableEvent {}