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
use crate::traits::{
    SEIdentifiedObject, SEList, SEResource, SESubscribableList, SESubscribableResource, Validate,
};
use sep2_common_derive::{
    SEIdentifiedObject, SEList, SEResource, SESubscribableList, SESubscribableResource,
};

use yaserde::{YaDeserialize, YaSerialize};

use super::{
    links::{
        AccountBalanceLink, ActiveCreditRegisterListLink, ActiveSupplyInterruptionOverrideListLink,
        CreditRegisterListLink, PrepayOperationStatusLink, SupplyInterruptionOverrideListLink,
        UsagePointLink,
    },
    metering::UsagePoint,
    primitives::{Int32, String32, Uint32},
    types::{
        CurrencyCode, DateTimeInterval, MRIDType, PowerOfTenMultiplierType, RealEnergy,
        SubscribableType, TimeType, VersionType,
    },
};

#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEResource)]
#[yaserde(rename = "AccountBalance")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct AccountBalance {
    /// AvailableCredit shows the balance of the sum of credits minus the sum of
    /// charges. In a Central Wallet mode this value may be passed down to the
    /// Prepayment server via an out-of-band mechanism. In Local or ESI modes,
    /// this value may be calculated based upon summation of CreditRegister
    /// transactions minus consumption charges calculated using Metering (and
    /// possibly Pricing) function set data. This value may be negative; for
    /// instance, if disconnection is prevented due to a Supply Interruption
    /// Override.
    #[yaserde(rename = "availableCredit")]
    pub available_credit: AccountingUnit,

    /// CreditStatus identifies whether the present value of availableCredit is
    /// considered OK, low, exhausted, or negative.
    #[yaserde(rename = "creditStatus")]
    pub credit_status: Option<CreditStatusType>,

    /// EmergencyCredit is the amount of credit still available for the given
    /// service or commodity prepayment instance. If both availableCredit and
    /// emergyCredit are exhausted, then service will typically be disconnected.
    #[yaserde(rename = "emergencyCredit")]
    pub emergency_credit: Option<AccountingUnit>,

    /// EmergencyCreditStatus identifies whether the present value of
    /// emergencyCredit is considered OK, low, exhausted, or negative.
    #[yaserde(rename = "emergencyCreditStatus")]
    pub emergency_credit_status: Option<CreditStatusType>,

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

/// Unit for accounting; use either 'energyUnit' or 'currencyUnit' to specify the
/// unit for 'value'.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "AccountingUnit")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct AccountingUnit {
    /// Unit of service.
    #[yaserde(rename = "energyUnit")]
    pub energy_unit: Option<RealEnergy>,

    /// Unit of currency.
    #[yaserde(rename = "monetaryUnit")]
    pub monetary_unit: CurrencyCode,

    /// Multiplier for the 'energyUnit' or 'monetaryUnit'.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value of the monetary aspect
    #[yaserde(rename = "value")]
    pub value: Int32,
}

impl Validate for AccountingUnit {}

#[derive(
    Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEIdentifiedObject, SEResource,
)]
#[yaserde(rename = "CreditRegister")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct CreditRegister {
    /// CreditAmount is the amount of credit being added by a particular
    /// CreditRegister transaction. Negative values indicate that credit is being
    /// subtracted.
    #[yaserde(rename = "creditAmount")]
    pub credit_amount: AccountingUnit,

    /// CreditType indicates whether the credit transaction applies to regular or
    /// emergency credit.
    #[yaserde(rename = "creditType")]
    pub credit_type: Option<CreditTypeType>,

    /// EffectiveTime identifies the time at which the credit transaction goes
    /// into effect. For credit addition transactions, this is typically the
    /// moment at which the transaction takes place. For credit subtraction
    /// transactions, (e.g., non-fuel debt recovery transactions initiated from a
    /// back-haul or ESI) this may be a future time at which credit is deducted.
    #[yaserde(rename = "effectiveTime")]
    pub effective_time: TimeType,

    /// Token is security data that authenticates the legitimacy of the
    /// transaction. The details of this token are not defined by IEEE 2030.5.
    /// How a Prepayment server handles this field is left as vendor specific
    /// implementation or will be defined by one or more other standards.
    #[yaserde(rename = "token")]
    pub token: String32,

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

impl Ord for CreditRegister {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Primary Key - effectiveTime (descending)
        match self.effective_time.cmp(&other.effective_time).reverse() {
            core::cmp::Ordering::Equal => {}
            ord => return ord,
        }
        // Secondary Key - mRID (descending)
        self.mrid.cmp(&other.mrid).reverse()
    }
}

impl Validate for CreditRegister {}

#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEList, SEResource)]
#[yaserde(rename = "CreditRegisterList")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct CreditRegisterList {
    #[yaserde(rename = "CreditRegister")]
    pub credit_register: Vec<CreditRegister>,

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

#[derive(
    Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEIdentifiedObject, SEResource,
)]
#[yaserde(rename = "Prepayment")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct Prepayment {
    #[yaserde(rename = "AccountBalanceLink")]
    pub account_balance_link: AccountBalanceLink,

    #[yaserde(rename = "ActiveCreditRegisterListLink")]
    pub active_credit_register_list_link: Option<ActiveCreditRegisterListLink>,

    #[yaserde(rename = "ActiveSupplyInterruptionOverrideListLink")]
    pub active_supply_interruption_override_list_link:
        Option<ActiveSupplyInterruptionOverrideListLink>,

    /// CreditExpiryLevel is the set point for availableCredit at which the
    /// service level may be changed. The typical value for this attribute is 0,
    /// regardless of whether the account balance is measured in a monetary or
    /// commodity basis. The units for this attribute SHALL match the units used
    /// for availableCredit.
    #[yaserde(rename = "creditExpiryLevel")]
    pub credit_expiry_level: Option<AccountingUnit>,

    #[yaserde(rename = "CreditRegisterListLink")]
    pub credit_register_list_link: CreditRegisterListLink,

    /// LowCreditWarningLevel is the set point for availableCredit at which the
    /// creditStatus attribute in the AccountBalance resource SHALL indicate that
    /// available credit is low. The units for this attribute SHALL match the
    /// units used for availableCredit. Typically, this value is set by the
    /// service provider.
    #[yaserde(rename = "lowCreditWarningLevel")]
    pub low_credit_warning_level: Option<AccountingUnit>,

    /// LowEmergencyCreditWarningLevel is the set point for emergencyCredit at
    /// which the creditStatus attribute in the AccountBalance resource SHALL
    /// indicate that emergencycredit is low. The units for this attribute SHALL
    /// match the units used for availableCredit. Typically, this value is set by
    /// the service provider.
    #[yaserde(rename = "lowEmergencyCreditWarningLevel")]
    pub low_emergency_credit_warning_level: Option<AccountingUnit>,

    /// PrepayMode specifies whether the given Prepayment instance is operating
    /// in Credit, Central Wallet, ESI, or Local prepayment mode. The Credit mode
    /// indicates that prepayment is not presently in effect. The other modes are
    /// described in the Overview Section above.
    #[yaserde(rename = "prepayMode")]
    pub prepay_mode: PrepayModeType,

    #[yaserde(rename = "PrepayOperationStatusLink")]
    pub prepay_operation_status_link: PrepayOperationStatusLink,

    #[yaserde(rename = "SupplyInterruptionOverrideListLink")]
    pub supply_interruption_override_list_link: SupplyInterruptionOverrideListLink,

    #[yaserde(rename = "UsagePoint")]
    pub usage_point: Vec<UsagePoint>,

    #[yaserde(rename = "UsagePointLink")]
    pub usage_point_link: Option<UsagePointLink>,

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

impl Ord for Prepayment {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Primary Key - mRID (descending)
        self.mrid.cmp(&other.mrid).reverse()
    }
}

impl Validate for Prepayment {}

#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableList,
    SEList,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "PrepaymentList")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct PrepaymentList {
    #[yaserde(rename = "Prepayment")]
    pub prepayment: Vec<Prepayment>,

    /// The default polling rate for this function set (this resource and all
    /// resources below), in seconds. If not specified, a default of 900 seconds
    /// (15 minutes) is used. It is RECOMMENDED a client poll the resources of
    /// this function set every pollRate seconds.
    #[yaserde(attribute, rename = "pollRate")]
    pub poll_rate: Option<Uint32>,

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

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(rename = "PrepayModeType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum PrepayModeType {
    #[default]
    CentralWallet = 0,
    ESI = 1,
    Local = 2,
    Credit = 3,
    // 4-255 RESERVED
}

impl Validate for PrepayModeType {}

#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEResource)]
#[yaserde(rename = "PrepayOperationStatus")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct PrepayOperationStatus {
    /// CreditTypeChange is used to define a pending change of creditTypeInUse,
    /// which will activate at a specified time.
    #[yaserde(rename = "creditTypeChange")]
    pub credit_type_change: Option<CreditTypeChange>,

    /// CreditTypeInUse identifies whether the present mode of operation is
    /// consuming regular credit or emergency credit.
    #[yaserde(rename = "creditTypeInUse")]
    pub credit_type_in_use: Option<CreditTypeType>,

    /// ServiceChange is used to define a pending change of serviceStatus, which
    /// will activate at a specified time.
    #[yaserde(rename = "serviceChange")]
    pub service_change: Option<ServiceChange>,

    /// ServiceStatus identifies whether the service is connected or
    /// disconnected, or armed for connection or disconnection.
    #[yaserde(rename = "serviceStatus")]
    pub service_status: ServiceStatusType,

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

/// Specifies a change to the service status.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ServiceChange")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ServiceChange {
    /// The new service status, to take effect at the time specified by startTime
    #[yaserde(rename = "newStatus")]
    pub new_status: ServiceStatusType,

    /// The date/time when the change is to take effect.
    #[yaserde(rename = "startTime")]
    pub start_time: TimeType,
}

impl Validate for ServiceChange {}

#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEResource)]
#[yaserde(rename = "SupplyInterruptionOverride")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct SupplyInterruptionOverride {
    /// The description is a human readable text describing or naming the object.
    #[yaserde(rename = "description")]
    pub description: Option<String32>,

    /// Interval defines the period of time during which supply should not be
    /// interrupted.
    #[yaserde(rename = "interval")]
    pub interval: DateTimeInterval,

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

impl Ord for SupplyInterruptionOverride {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Primary Key - interval.start (ascending)
        match self.interval.start.cmp(&other.interval.start) {
            core::cmp::Ordering::Equal => {}
            ord => return ord,
        }
        // Secondary Key - interval.duration (ascending)
        self.interval.duration.cmp(&other.interval.duration)
    }
}

impl Validate for SupplyInterruptionOverride {}

#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEList, SEResource)]
#[yaserde(rename = "SupplyInterruptionOverrideList")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct SupplyInterruptionOverrideList {
    #[yaserde(rename = "SupplyInterruptionOverride")]
    pub supply_interruption_override: Vec<SupplyInterruptionOverride>,

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

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(rename = "CreditStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum CreditStatusType {
    #[default]
    Ok = 0,
    Low = 1,
    Exhausted = 2,
    Negative = 3,
    // 4-255 RESERVED
}

impl Validate for CreditStatusType {}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(rename = "CreditTypeType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum CreditTypeType {
    #[default]
    Regular = 0,
    Emergency = 1,
    RegularThenEmergency = 2,
    EmergencyThenRegular = 3,
    // 4-255 RESERVED
}

impl Validate for CreditTypeType {}

/// Specifies a change to the credit type.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "CreditTypeChange")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct CreditTypeChange {
    /// The new credit type, to take effect at the time specified by startTime
    #[yaserde(rename = "newType")]
    pub new_type: CreditTypeType,

    /// The date/time when the change is to take effect.
    #[yaserde(rename = "startTime")]
    pub start_time: TimeType,
}

impl Validate for CreditTypeChange {}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ServiceStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum ServiceStatusType {
    #[default]
    Connected = 0,
    Disconnected = 1,
    ArmedForConnect = 2,
    ArmedForDisconnect = 3,
    NoContactor = 4,
    LoadLimited = 5,
    // 6-255 RESERVED
}

impl Validate for ServiceStatusType {}