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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
use bitflags::bitflags;
use sep2_common_derive::{
    SEIdentifiedObject, SEList, SEResource, SERespondableIdentifiedObject, SERespondableResource,
    SERespondableSubscribableIdentifiedObject, SEResponse, SESubscribableIdentifiedObject,
    SESubscribableList, SESubscribableResource,
};
use yaserde::{HexBinaryYaSerde, YaDeserialize, YaSerialize};

use super::{
    objects::EventStatusType,
    primitives::{HexBinary160, String32, Uint32},
    types::{MRIDType, SubscribableType, TimeType, VersionType},
};

use crate::traits::{
    SEIdentifiedObject, SEList, SEResource, SERespondableIdentifiedObject, SERespondableResource,
    SERespondableSubscribableIdentifiedObject, SEResponse, SESubscribableIdentifiedObject,
    SESubscribableList, SESubscribableResource, Validate,
};

/// A resource is an addressable unit of information, either a collection (List)
/// or instance of an object (identifiedObject, or simply, Resource)
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEResource)]
#[yaserde(rename = "Resource")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Resource {
    /// 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 Resource {}

/// The Response object is the generic response data
/// repository which is extended for specific function sets.
#[derive(
    Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEResponse, SEResource,
)]
#[yaserde(rename = "Response")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Response {
    /// The createdDateTime field contains the date and time when the
    /// acknowledgement/status occurred in the client. The client will provide
    /// the timestamp to ensure the proper time is captured in case the response
    /// is delayed in reaching the server (server receipt time would not be the
    /// same as the actual confirmation time). The time reported from the client
    /// should be relative to the time server indicated by the
    /// FunctionSetAssignment that also indicated the event resource; if no
    /// FunctionSetAssignment exists, the time of the server where the event
    /// resource was hosted.
    #[yaserde(rename = "createdDateTime")]
    pub created_date_time: Option<TimeType>,

    /// Contains the LFDI of the device providing the response.
    #[yaserde(rename = "endDeviceLFDI")]
    pub end_device_lfdi: HexBinary160,

    /// The status field contains the acknowledgement or status. Each event type
    /// (DRLC, DER, Price, or Text) can return different status information (e.g.
    /// an Acknowledge will be returned for a Price event where a DRLC event can
    /// return Event Received, Event Started, and Event Completed). The Status
    /// field value definitions are defined in Table 27: Response Types by
    /// Function Set.
    #[yaserde(rename = "status")]
    pub status: Option<ResponseStatus>,

    /// The subject field provides a method to match the response with the
    /// originating event. It is populated with the mRID of the original object.
    #[yaserde(rename = "subject")]
    pub subject: MRIDType,

    /// 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 PartialOrd for Response {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for Response {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Primary Key - createdDateTime (descending)
        match self
            .created_date_time
            .cmp(&other.created_date_time)
            .reverse()
        {
            core::cmp::Ordering::Equal => {}
            ord => return ord,
        }
        // Secondary Key - endDeviceLFDI (ascending)
        self.end_device_lfdi.cmp(&other.end_device_lfdi)
    }
}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
/// Defined in Table 27: Response Types by Function Set
/// Can be created from a [`EventStatusType`] for the purpose of reading Event resources
pub enum ResponseStatus {
    #[default]
    EventReceived = 1,
    EventStarted = 2,
    EventCompleted = 3,
    EventOptOut = 4,
    EventOptIn = 5,
    EventCancelled = 6,
    EventSuperseded = 7,
    EventPartialOptOut = 8,
    EventPartialOptIn = 9,
    EventCompletedNoUserParticipation = 10,
    EventAcknowledge = 11,
    EventNoDisplay = 12,
    EventAbortedServer = 13,
    EventAbortedProgram = 14,
    EventNotApplicable = 252,
    EventInvalid = 253,
    EventExpired = 254,
}

impl From<EventStatusType> for ResponseStatus {
    fn from(value: EventStatusType) -> Self {
        match value {
            EventStatusType::Scheduled => ResponseStatus::EventReceived,
            EventStatusType::Active => ResponseStatus::EventStarted,
            EventStatusType::Cancelled => ResponseStatus::EventCancelled,
            EventStatusType::CancelledRandom => ResponseStatus::EventCancelled,
            EventStatusType::Superseded => ResponseStatus::EventSuperseded,
        }
    }
}

impl Validate for Response {}

/// Container to hold a collection of object instances or references.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEList, SEResource)]
#[yaserde(rename = "List")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct List {
    /// This field is OOS since it is different in every child type
    /// but is required for our SEList trait implementation
    #[yaserde(skip_serializing)]
    pub contents: Vec<String>,

    /// The number specifying "all" of the items in the list. Required on a
    /// response to a GET, ignored otherwise.
    #[yaserde(attribute, rename = "all")]
    pub all: Uint32,

    /// Indicates the number of items in this page of results.
    #[yaserde(attribute, rename = "results")]
    pub results: Uint32,

    /// 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 List {}

/// Links provide a reference, via URI, to another resource.
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "Link")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Link {
    /// A URI reference.
    #[yaserde(attribute, rename = "href")]
    pub href: String,
}

impl Validate for Link {}

/// ListLinks provide a reference, via URI, to a List.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ListLink")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ListLink {
    /// Indicates the total number of items in the referenced list. This
    /// attribute SHALL be present if the href is a local or relative URI. This
    /// attribute SHOULD NOT be present if the href is a remote or absolute URI,
    /// as the server may be unaware of changes to the value.
    #[yaserde(attribute, rename = "all")]
    pub all: Option<Uint32>,

    /// A URI reference.
    #[yaserde(attribute, rename = "href")]
    pub href: String,
}
impl Validate for ListLink {}

/// This is a root class to provide common naming attributes for all classes needing naming attributes
#[derive(
    Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEIdentifiedObject, SEResource,
)]
#[yaserde(rename = "IdentifiedObject")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct IdentifiedObject {
    /// 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>,

    /// 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 IdentifiedObject {}

/// A Resource to which a Response can be requested.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SERespondableResource,
    SEResource,
)]
#[yaserde(rename = "RespondableResource")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct RespondableResource {
    /// 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>,
}

bitflags! {
    #[derive(Default, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, HexBinaryYaSerde)]
    pub struct ResponseRequired: u8 { // HexBinary8
        const MessageReceived = 1;
        const SpecificResponse = 2;
        const ResponseRequired = 4;
    }
}

impl Validate for RespondableResource {}

/// An IdentifiedObject to which a Response can be requested.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SERespondableIdentifiedObject,
    SEIdentifiedObject,
    SERespondableResource,
    SEResource,
)]
#[yaserde(rename = "RespondableIdentifiedObject")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct RespondableIdentifiedObject {
    /// 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>,

    /// 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 RespondableIdentifiedObject {}

/// An IdentifiedObject to which a Response can be requested.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SERespondableSubscribableIdentifiedObject,
    SESubscribableResource,
    SEIdentifiedObject,
    SERespondableResource,
    SEResource,
)]
#[yaserde(rename = "RespondableSubscribableIdentifiedObject")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct RespondableSubscribableIdentifiedObject {
    /// 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 RespondableSubscribableIdentifiedObject {}

/// A Resource to which a Subscription can be requested.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "SubscribableResource")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct SubscribableResource {
    /// 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 resource address (URI). Required in a response to a
    /// GET, ignored otherwise.
    #[yaserde(attribute, rename = "href")]
    pub href: Option<String>,
}

impl Validate for SubscribableResource {}

/// A List to which a Subscription can be requested.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableList,
    SEList,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "SubscribableList")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct SubscribableList {
    /// This field is OOS since it is different in every child type
    /// but is required for our SEList trait implementation
    #[yaserde(skip_serializing)]
    pub contents: Vec<String>,

    /// The number specifying "all" of the items in the list. Required on GET,
    /// ignored otherwise.
    #[yaserde(attribute, rename = "all")]
    pub all: Uint32,

    /// Indicates the number of items in this page of results.
    #[yaserde(attribute, rename = "results")]
    pub results: Uint32,

    /// 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 resource address (URI). Required in a response to a
    /// GET, ignored otherwise.
    #[yaserde(attribute, rename = "href")]
    pub href: Option<String>,
}

impl Validate for SubscribableList {}

/// An IdentifiedObject to which a Response can be requested.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableIdentifiedObject,
    SEIdentifiedObject,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "SubscribableIdentifiedObject")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct SubscribableIdentifiedObject {
    /// 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 resource address (URI). Required in a response to a
    /// GET, ignored otherwise.
    #[yaserde(attribute, rename = "href")]
    pub href: Option<String>,
}

impl Validate for SubscribableIdentifiedObject {}