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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
use crate::traits::{
    SEEvent, SEIdentifiedObject, SEList, SERandomizableEvent, SEResource, SERespondableResource,
    SERespondableSubscribableIdentifiedObject, SESubscribableIdentifiedObject, SESubscribableList,
    SESubscribableResource, Validate,
};
use sep2_common_derive::{
    SEEvent, SEIdentifiedObject, SEList, SERandomizableEvent, SEResource, SERespondableResource,
    SERespondableSubscribableIdentifiedObject, SESubscribableIdentifiedObject, SESubscribableList,
    SESubscribableResource,
};

use super::{
    identification::ResponseRequired,
    links::{
        ActiveDERControlListLink, AssociatedDERProgramListLink, AssociatedUsagePointLink,
        DERAvailabilityLink, DERCapabilityLink, DERControlListLink, DERCurveLink, DERCurveListLink,
        DERProgramLink, DERSettingsLink, DERStatusLink, DefaultDERControlLink,
    },
    objects::EventStatus,
    primitives::{Int16, Int32, String32, String6, Uint16, Uint32, Uint8},
    types::{
        DateTimeInterval, DeviceCategoryType, MRIDType, OneHourRangeType, Percent,
        PowerOfTenMultiplierType, PrimacyType, SignedPercent, SubscribableType, TimeType,
        VersionType,
    },
};
use bitflags::bitflags;
use yaserde::{HexBinaryYaSerde, YaDeserialize, YaSerialize};

#[cfg(test)]
use crate::serialize;

#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableIdentifiedObject,
    SEIdentifiedObject,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "DefaultDERControl")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DefaultDERControl {
    #[yaserde(rename = "DERControlBase")]
    pub der_control_base: DERControlBase,

    /// Enter service delay, in hundredths of a second. When present, this value
    /// SHALL update the value of the corresponding setting
    /// (DERSettings::setESDelay).
    #[yaserde(rename = "setESDelay")]
    pub set_es_delay: Option<Uint32>,

    /// Enter service frequency high. Specified in hundredths of Hz. When
    /// present, this value SHALL update the value of the corresponding setting
    /// (DERSettings::setESHighFreq).
    #[yaserde(rename = "setESHighFreq")]
    pub set_es_high_freq: Option<Uint16>,

    /// Enter service voltage high. Specified as an effective percent voltage,
    /// defined as (100% * (locally measured voltage - setVRefOfs) / setVRef), in
    /// hundredths of a percent. When present, this value SHALL update the value
    /// of the corresponding setting (DERSettings::setESHighVolt).
    #[yaserde(rename = "setESHighVolt")]
    pub set_es_high_volt: Option<Int16>,

    /// Enter service frequency low. Specified in hundredths of Hz. When present,
    /// this value SHALL update the value of the corresponding setting
    /// (DERSettings::setESLowFreq).
    #[yaserde(rename = "setESLowFreq")]
    pub set_es_low_freq: Option<Uint16>,

    /// Enter service voltage low. Specified as an effective percent voltage,
    /// defined as (100% * (locally measured voltage - setVRefOfs) / setVRef), in
    /// hundredths of a percent. When present, this value SHALL update the value
    /// of the corresponding setting (DERSettings::setESLowVolt).
    #[yaserde(rename = "setESLowVolt")]
    pub set_es_low_volt: Option<Int16>,

    /// Enter service ramp time, in hundredths of a second. When present, this
    /// value SHALL update the value of the corresponding setting
    /// (DERSettings::setESRampTms).
    #[yaserde(rename = "setESRampTms")]
    pub set_es_ramp_tms: Option<Uint32>,

    /// Enter service randomized delay, in hundredths of a second. When present,
    /// this value SHALL update the value of the corresponding setting
    /// (DERSettings::setESRandomDelay).
    #[yaserde(rename = "setESRandomDelay")]
    pub set_es_random_delay: Option<Uint32>,

    /// Set default rate of change (ramp rate) of active power output due to
    /// command or internal action, defined in %setWMax / second. Resolution is
    /// in hundredths of a percent/second. A value of 0 means there is no limit.
    /// Interpreted as a percentage change in output capability limit per second
    /// when used as a default ramp rate. When present, this value SHALL update
    /// the value of the corresponding setting (DERSettings::setGradW).
    #[yaserde(rename = "setGradW")]
    pub set_grad_w: Option<Uint16>,

    /// Set soft-start rate of change (soft-start ramp rate) of active power
    /// output due to command or internal action, defined in %setWMax / second.
    /// Resolution is in hundredths of a percent/second. A value of 0 means there
    /// is no limit. Interpreted as a percentage change in output capability
    /// limit per second when used as a ramp rate. When present, this value SHALL
    /// update the value of the corresponding setting
    /// (DERSettings::setSoftGradW).
    #[yaserde(rename = "setSoftGradW")]
    pub set_soft_grad_w: Option<Uint16>,

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

/// Type for Frequency-Droop (Frequency-Watt) operation.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "FreqDroopType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct FreqDroopType {
    /// Frequency droop dead band for over-frequency conditions. In thousandths
    /// of Hz.
    #[yaserde(rename = "dBOF")]
    pub d_bof: Uint32,

    /// Frequency droop dead band for under-frequency conditions. In thousandths
    /// of Hz.
    #[yaserde(rename = "dBUF")]
    pub d_buf: Uint32,

    /// Frequency droop per-unit frequency change for over-frequency conditions
    /// corresponding to 1 per-unit power output change. In thousandths,
    /// unitless.
    #[yaserde(rename = "kOF")]
    pub k_of: Uint16,

    /// Frequency droop per-unit frequency change for under-frequency conditions
    /// corresponding to 1 per-unit power output change. In thousandths,
    /// unitless.
    #[yaserde(rename = "kUF")]
    pub k_uf: Uint16,

    /// Open loop response time, the duration from a step change in control
    /// signal input until the output changes by 90% of its final change before
    /// any overshoot, in hundredths of a second. Resolution is 1/100 sec. A
    /// value of 0 is used to mean no limit.
    #[yaserde(rename = "openLoopTms")]
    pub open_loop_tms: Uint16,
}

impl Validate for FreqDroopType {}

#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "DER")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DER {
    #[yaserde(rename = "AssociatedDERProgramListLink")]
    pub associated_der_program_list_link: Option<AssociatedDERProgramListLink>,

    #[yaserde(rename = "AssociatedUsagePointLink")]
    pub associated_usage_point_link: Option<AssociatedUsagePointLink>,

    #[yaserde(rename = "CurrentDERProgramLink")]
    pub current_der_program_link: Option<DERProgramLink>,

    #[yaserde(rename = "DERAvailabilityLink")]
    pub der_availability_link: Option<DERAvailabilityLink>,

    #[yaserde(rename = "DERCapabilityLink")]
    pub der_capability_link: Option<DERCapabilityLink>,

    #[yaserde(rename = "DERSettingsLink")]
    pub der_settings_link: Option<DERSettingsLink>,

    #[yaserde(rename = "DERStatusLink")]
    pub der_status_link: Option<DERStatusLink>,

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

impl Ord for DER {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Primary Key - href (ascending)
        self.href.cmp(&other.href)
    }
}

impl Validate for DER {}

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

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

#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "DERSettings")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DERSettings {
    /// Bitmap indicating the DER Controls enabled on the device. See
    /// DERControlType for values. If a control is supported (see
    /// DERCapability::modesSupported), but not enabled, the control will not be
    /// executed if encountered.
    #[yaserde(rename = "modesEnabled")]
    pub modes_enabled: Option<DERControlType>,

    /// Enter service delay, in hundredths of a second.
    #[yaserde(rename = "setESDelay")]
    pub set_es_delay: Option<Uint32>,

    /// Enter service frequency high. Specified in hundredths of Hz.
    #[yaserde(rename = "setESHighFreq")]
    pub set_es_high_freq: Option<Uint16>,

    /// Enter service voltage high. Specified as an effective percent voltage,
    /// defined as (100% * (locally measured voltage - setVRefOfs) / setVRef), in
    /// hundredths of a percent.
    #[yaserde(rename = "setESHighVolt")]
    pub set_es_high_volt: Option<Int16>,

    /// Enter service frequency low. Specified in hundredths of Hz.
    #[yaserde(rename = "setESLowFreq")]
    pub set_es_low_freq: Option<Uint16>,

    /// Enter service voltage low. Specified as an effective percent voltage,
    /// defined as (100% * (locally measured voltage - setVRefOfs) / setVRef), in
    /// hundredths of a percent.
    #[yaserde(rename = "setESLowVolt")]
    pub set_es_low_volt: Option<Int16>,

    /// Enter service ramp time, in hundredths of a second.
    #[yaserde(rename = "setESRampTms")]
    pub set_es_ramp_tms: Option<Uint32>,

    /// Enter service randomized delay, in hundredths of a second.
    #[yaserde(rename = "setESRandomDelay")]
    pub set_es_random_delay: Option<Uint32>,

    /// Set default rate of change (ramp rate) of active power output due to
    /// command or internal action, defined in %setWMax / second. Resolution is
    /// in hundredths of a percent/second. A value of 0 means there is no limit.
    /// Interpreted as a percentage change in output capability limit per second
    /// when used as a default ramp rate.
    #[yaserde(rename = "setGradW")]
    pub set_grad_w: Uint16,

    /// AC current maximum. Maximum AC current in RMS Amperes.
    #[yaserde(rename = "setMaxA")]
    pub set_max_a: Option<CurrentRMS>,

    /// Maximum usable energy storage capacity of the DER, in AmpHours. Note:
    /// this may be different from physical capability.
    #[yaserde(rename = "setMaxAh")]
    pub set_max_ah: Option<AmpereHour>,

    /// Apparent power charge maximum. Maximum apparent power the DER can absorb
    /// from the grid in Volt-Amperes. May differ from the apparent power maximum
    /// (setMaxVA).
    #[yaserde(rename = "setMaxChargeRateVA")]
    pub set_max_charge_rate_va: Option<ApparentPower>,

    /// Maximum rate of energy transfer received by the storage device, in Watts.
    /// Defaults to rtgMaxChargeRateW.
    #[yaserde(rename = "setMaxChargeRateW")]
    pub set_max_charge_rate_w: Option<ActivePower>,

    /// Apparent power discharge maximum. Maximum apparent power the DER can
    /// deliver to the grid in Volt-Amperes. May differ from the apparent power
    /// maximum (setMaxVA).
    #[yaserde(rename = "setMaxDischargeRateVA")]
    pub set_max_discharge_rate_va: Option<ApparentPower>,

    /// Maximum rate of energy transfer delivered by the storage device, in
    /// Watts. Defaults to rtgMaxDischargeRateW.
    #[yaserde(rename = "setMaxDischargeRateW")]
    pub set_max_discharge_rate_w: Option<ActivePower>,

    /// AC voltage maximum setting.
    #[yaserde(rename = "setMaxV")]
    pub set_max_v: Option<VoltageRMS>,

    /// Set limit for maximum apparent power capability of the DER (in VA).
    /// Defaults to rtgMaxVA.
    #[yaserde(rename = "setMaxVA")]
    pub set_max_va: Option<ApparentPower>,

    /// Set limit for maximum reactive power delivered by the DER (in var). SHALL
    /// be a positive value &lt;= rtgMaxVar (default).
    #[yaserde(rename = "setMaxVar")]
    pub set_max_var: Option<ReactivePower>,

    /// Set limit for maximum reactive power received by the DER (in var). If
    /// present, SHALL be a negative value &gt;= rtgMaxVarNeg (default). If
    /// absent, defaults to negative setMaxVar.
    #[yaserde(rename = "setMaxVarNeg")]
    pub set_max_var_neg: Option<ReactivePower>,

    /// Set limit for maximum active power capability of the DER (in W). Defaults
    /// to rtgMaxW.
    #[yaserde(rename = "setMaxW")]
    pub set_max_w: ActivePower,

    /// Maximum energy storage capacity of the DER, in WattHours. Note: this may
    /// be different from physical capability.
    #[yaserde(rename = "setMaxWh")]
    pub set_max_wh: Option<WattHour>,

    /// Set minimum Power Factor displacement limit of the DER when injecting
    /// reactive power (over-excited); SHALL be a positive value between 0.0
    /// (typically &gt; 0.7) and 1.0. SHALL be &gt;= rtgMinPFOverExcited
    /// (default).
    #[yaserde(rename = "setMinPFOverExcited")]
    pub set_min_pf_over_excited: Option<PowerFactor>,

    /// Set minimum Power Factor displacement limit of the DER when absorbing
    /// reactive power (under-excited); SHALL be a positive value between 0.0
    /// (typically &gt; 0.7) and 0.9999. If present, SHALL be &gt;=
    /// rtgMinPFUnderExcited (default). If absent, defaults to
    /// setMinPFOverExcited.
    #[yaserde(rename = "setMinPFUnderExcited")]
    pub set_min_pf_under_excited: Option<PowerFactor>,

    /// AC voltage minimum setting.
    #[yaserde(rename = "setMinV")]
    pub set_min_v: Option<VoltageRMS>,

    /// Set soft-start rate of change (soft-start ramp rate) of active power
    /// output due to command or internal action, defined in %setWMax / second.
    /// Resolution is in hundredths of a percent/second. A value of 0 means there
    /// is no limit. Interpreted as a percentage change in output capability
    /// limit per second when used as a ramp rate.
    #[yaserde(rename = "setSoftGradW")]
    pub set_soft_grad_w: Option<Uint16>,

    /// AC voltage nominal setting.
    #[yaserde(rename = "setVNom")]
    pub set_v_nom: Option<VoltageRMS>,

    /// The nominal AC voltage (RMS) at the utility's point of common coupling.
    #[yaserde(rename = "setVRef")]
    pub set_v_ref: Option<VoltageRMS>,

    /// The nominal AC voltage (RMS) offset between the DER's electrical
    /// connection point and the utility's point of common coupling.
    #[yaserde(rename = "setVRefOfs")]
    pub set_v_ref_ofs: Option<VoltageRMS>,

    /// Specifies the time at which the DER information was last updated.
    #[yaserde(rename = "updatedTime")]
    pub updated_time: TimeType,

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

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(rename = "DERType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum DERType {
    /// Not applicable / Unknown
    #[default]
    Unknown = 0,
    VirtualOrMixedDER = 1,
    ReciprocatingEngine = 2,
    FuelCell = 3,
    PhotovoltaicSystem = 4,
    HeatAndPower = 5,
    /// Other generation system
    OtherGeneration = 6,
    /// Other storage system
    OtherStorage = 80,
    ElectricVehicle = 81,
    EVSE = 82,
    CombinedPVAndStorage = 83,
    // ALL OTHERS RESERVED
}

impl Validate for DERType {}

#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "DERAvailability")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DERAvailability {
    /// Indicates number of seconds the DER will be able to deliver active power
    /// at the reservePercent level.
    #[yaserde(rename = "availabilityDuration")]
    pub availability_duration: Option<Uint32>,

    /// Indicates number of seconds the DER will be able to receive active power
    /// at the reserveChargePercent level.
    #[yaserde(rename = "maxChargeDuration")]
    pub max_charge_duration: Option<Uint32>,

    /// The timestamp when the DER availability was last updated.
    #[yaserde(rename = "readingTime")]
    pub reading_time: TimeType,

    /// Percent of continuous received active power (%setMaxChargeRateW) that is
    /// estimated to be available in reserve.
    #[yaserde(rename = "reserveChargePercent")]
    pub reserve_charge_percent: Option<Percent>,

    /// Percent of continuous delivered active power (%setMaxW) that is estimated
    /// to be available in reserve.
    #[yaserde(rename = "reservePercent")]
    pub reserve_percent: Option<Percent>,

    /// Estimated reserve reactive power, in var. Represents the lesser of
    /// received or delivered reactive power.
    #[yaserde(rename = "statVarAvail")]
    pub stat_var_avail: Option<ReactivePower>,

    /// Estimated reserve active power, in watts.
    #[yaserde(rename = "statWAvail")]
    pub stat_w_avail: Option<ActivePower>,

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

#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEResource)]
#[yaserde(rename = "DERCapability")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[cfg_attr(
    feature = "csip_aus",
    yaserde(namespace = "csipaus: https://csipaus.org/ns")
)]
pub struct DERCapability {
    /// Bitmap indicating the DER Controls implemented by the device. See
    /// DERControlType for values.
    #[yaserde(rename = "modesSupported")]
    pub modes_supported: DERControlType,

    /// Bitmap indicating the CSIP-AUS controls implemented
    #[cfg(feature = "csip_aus")]
    #[yaserde(rename = "doeModesSupported")]
    #[yaserde(prefix = "csipaus", namespace = "csipaus: https://csipaus.org/ns")]
    pub doe_modes_supported: DOEControlType,

    /// Abnormal operating performance category as defined by IEEE 1547-2018. One
    /// of:
    /// 0 - not specified
    /// 1 - Category I
    /// 2 - Category II
    /// 3 - Category III
    /// All other values reserved.
    #[yaserde(rename = "rtgAbnormalCategory")]
    pub rtg_abnormal_category: Option<AbnormalCategory>,

    /// Maximum continuous AC current capability of the DER, in Amperes (RMS).
    #[yaserde(rename = "rtgMaxA")]
    pub rtg_max_a: Option<CurrentRMS>,

    /// Usable energy storage capacity of the DER, in AmpHours.
    #[yaserde(rename = "rtgMaxAh")]
    pub rtg_max_ah: Option<AmpereHour>,

    /// Maximum apparent power charge rating in Volt-Amperes. May differ from the
    /// maximum apparent power rating.
    #[yaserde(rename = "rtgMaxChargeRateVA")]
    pub rtg_max_charge_rate_va: Option<ApparentPower>,

    /// Maximum rate of energy transfer received by the storage DER, in Watts.
    #[yaserde(rename = "rtgMaxChargeRateW")]
    pub rtg_max_charge_rate_w: Option<ActivePower>,

    /// Maximum apparent power discharge rating in Volt-Amperes. May differ from
    /// the maximum apparent power rating.
    #[yaserde(rename = "rtgMaxDischargeRateVA")]
    pub rtg_max_discharge_rate_va: Option<ApparentPower>,

    /// Maximum rate of energy transfer delivered by the storage DER, in Watts.
    /// Required for combined generation/storage DERs (e.g. DERType == 83).
    #[yaserde(rename = "rtgMaxDischargeRateW")]
    pub rtg_max_discharge_rate_w: Option<ActivePower>,

    /// AC voltage maximum rating.
    #[yaserde(rename = "rtgMaxV")]
    pub rtg_max_v: Option<VoltageRMS>,

    /// Maximum continuous apparent power output capability of the DER, in VA.
    #[yaserde(rename = "rtgMaxVA")]
    pub rtg_max_va: Option<ApparentPower>,

    /// Maximum continuous reactive power delivered by the DER, in var.
    #[yaserde(rename = "rtgMaxVar")]
    pub rtg_max_var: Option<ReactivePower>,

    /// Maximum continuous reactive power received by the DER, in var. If absent,
    /// defaults to negative rtgMaxVar.
    #[yaserde(rename = "rtgMaxVarNeg")]
    pub rtg_max_var_neg: Option<ReactivePower>,

    /// Maximum continuous active power output capability of the DER, in watts.
    /// Represents combined generation plus storage output if DERType == 83.
    #[yaserde(rename = "rtgMaxW")]
    pub rtg_max_w: ActivePower,

    /// Maximum energy storage capacity of the DER, in WattHours.
    #[yaserde(rename = "rtgMaxWh")]
    pub rtg_max_wh: Option<WattHour>,

    /// Minimum Power Factor displacement capability of the DER when injecting
    /// reactive power (over-excited); SHALL be a positive value between 0.0
    /// (typically &gt; 0.7) and 1.0. If absent, defaults to unity.
    #[yaserde(rename = "rtgMinPFOverExcited")]
    pub rtg_min_pf_over_excited: Option<PowerFactor>,

    /// Minimum Power Factor displacement capability of the DER when absorbing
    /// reactive power (under-excited); SHALL be a positive value between 0.0
    /// (typically &gt; 0.7) and 0.9999. If absent, defaults to
    /// rtgMinPFOverExcited.
    #[yaserde(rename = "rtgMinPFUnderExcited")]
    pub rtg_min_pf_under_excited: Option<PowerFactor>,

    /// AC voltage minimum rating.
    #[yaserde(rename = "rtgMinV")]
    pub rtg_min_v: Option<VoltageRMS>,

    /// Normal operating performance category as defined by IEEE 1547-2018. One
    /// of:
    /// 0 - not specified
    /// 1 - Category A
    /// 2 - Category B
    /// All other values reserved.
    #[yaserde(rename = "rtgNormalCategory")]
    pub rtg_normal_category: Option<NormalCategory>,

    /// Specified over-excited power factor.
    #[yaserde(rename = "rtgOverExcitedPF")]
    pub rtg_over_excited_pf: Option<PowerFactor>,

    /// Active power rating in Watts at specified over-excited power factor
    /// (rtgOverExcitedPF). If present, rtgOverExcitedPF SHALL be present.
    #[yaserde(rename = "rtgOverExcitedW")]
    pub rtg_over_excited_w: Option<ActivePower>,

    /// Reactive susceptance that remains connected to the Area EPS in the cease
    /// to energize and trip state.
    #[yaserde(rename = "rtgReactiveSusceptance")]
    pub rtg_reactive_susceptance: Option<ReactiveSusceptance>,

    /// Specified under-excited power factor.
    #[yaserde(rename = "rtgUnderExcitedPF")]
    pub rtg_under_excited_pf: Option<PowerFactor>,

    /// Active power rating in Watts at specified under-excited power factor
    /// (rtgUnderExcitedPF). If present, rtgUnderExcitedPF SHALL be present.
    #[yaserde(rename = "rtgUnderExcitedW")]
    pub rtg_under_excited_w: Option<ActivePower>,

    /// AC voltage nominal rating.
    #[yaserde(rename = "rtgVNom")]
    pub rtg_v_nom: Option<VoltageRMS>,

    /// Type of DER; see DERType object
    #[yaserde(rename = "type")]
    pub _type: DERType,

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

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum AbnormalCategory {
    #[default]
    NotSpecified = 0,
    CategoryOne = 1,
    CategoryTwo = 2,
    CategoryThree = 3,
}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum NormalCategory {
    #[default]
    NotSpecified = 0,
    CategoryA = 1,
    CategoryB = 2,
}

impl Validate for DERCapability {}

/// Distributed Energy Resource (DER) control values.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "DERControlBase")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[cfg_attr(
    feature = "csip_aus",
    yaserde(namespace = "csipaus: https://csipaus.org/ns")
)]
pub struct DERControlBase {
    /// Set DER as connected (true) or disconnected (false). Used in conjunction
    /// with ramp rate when re-connecting. Implies galvanic isolation.
    #[yaserde(rename = "opModConnect")]
    pub op_mod_connect: Option<bool>,

    /// Set DER as energized (true) or de-energized (false). Used in conjunction
    /// with ramp rate when re-energizing.
    #[yaserde(rename = "opModEnergize")]
    pub op_mod_energize: Option<bool>,

    /// The opModFixedPFAbsorbW function specifies a requested fixed Power Factor
    /// (PF) setting for when active power is being absorbed. The actual
    /// displacement SHALL be within the limits established by
    /// setMinPFOverExcited and setMinPFUnderExcited. If issued simultaneously
    /// with other reactive power controls (e.g. opModFixedVar) the control
    /// resulting in least var magnitude SHOULD take precedence.
    #[yaserde(rename = "opModFixedPFAbsorbW")]
    pub op_mod_fixed_pf_absorb_w: Option<PowerFactorWithExcitation>,

    /// The opModFixedPFInjectW function specifies a requested fixed Power Factor
    /// (PF) setting for when active power is being injected. The actual
    /// displacement SHALL be within the limits established by
    /// setMinPFOverExcited and setMinPFUnderExcited. If issued simultaneously
    /// with other reactive power controls (e.g. opModFixedVar) the control
    /// resulting in least var magnitude SHOULD take precedence.
    #[yaserde(rename = "opModFixedPFInjectW")]
    pub op_mod_fixed_pf_inject_w: Option<PowerFactorWithExcitation>,

    /// The opModFixedVar function specifies the delivered or received reactive
    /// power setpoint. The context for the setpoint value is determined by
    /// refType and SHALL be one of %setMaxW, %setMaxVar, or %statVarAvail. If
    /// issued simultaneously with other reactive power controls (e.g.
    /// opModFixedPFInjectW) the control resulting in least var magnitude SHOULD
    /// take precedence.
    #[yaserde(rename = "opModFixedVar")]
    pub op_mod_fixed_var: Option<FixedVar>,

    /// The opModFixedW function specifies a requested charge or discharge mode
    /// setpoint, in %setMaxChargeRateW if negative value or %setMaxW or
    /// %setMaxDischargeRateW if positive value (in hundredths).
    #[yaserde(rename = "opModFixedW")]
    pub op_mod_fixed_w: Option<SignedPercent>,

    /// Specifies a frequency-watt operation. This operation limits active power
    /// generation or consumption when the line frequency deviates from nominal
    /// by a specified amount.
    #[yaserde(rename = "opModFreqDroop")]
    pub op_mod_freq_droop: Option<FreqDroopType>,

    /// Specify DERCurveLink for curveType == 0. The Frequency-Watt function
    /// limits active power generation or consumption when the line frequency
    /// deviates from nominal by a specified amount. The Frequency-Watt curve is
    /// specified as an array of Frequency-Watt pairs that are interpolated into
    /// a piecewise linear function with hysteresis. The x value of each pair
    /// specifies a frequency in Hz. The y value specifies a corresponding active
    /// power output in %setMaxW.
    #[yaserde(rename = "opModFreqWatt")]
    pub op_mod_freq_watt: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 1. The High Frequency Ride-Through
    /// (HFRT) function is specified by one or two duration-frequency curves that
    /// define the operating region under high frequency conditions. Each HFRT
    /// curve is specified by an array of duration-frequency pairs that will be
    /// interpolated into a piecewise linear function that defines an operating
    /// region. The x value of each pair specifies a duration (time at a given
    /// frequency in seconds). The y value of each pair specifies a frequency, in
    /// Hz. This control specifies the "may trip" region.
    #[yaserde(rename = "opModHFRTMayTrip")]
    pub op_mod_hfrt_may_trip: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 2. The High Frequency Ride-Through
    /// (HFRT) function is specified by a duration-frequency curve that defines
    /// the operating region under high frequency conditions. Each HFRT curve is
    /// specified by an array of duration-frequency pairs that will be
    /// interpolated into a piecewise linear function that defines an operating
    /// region. The x value of each pair specifies a duration (time at a given
    /// frequency in seconds). The y value of each pair specifies a frequency, in
    /// Hz. This control specifies the "must trip" region.
    #[yaserde(rename = "opModHFRTMustTrip")]
    pub op_mod_hfrt_must_trip: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 3. The High Voltage Ride-Through
    /// (HVRT) function is specified by one, two, or three duration-volt curves
    /// that define the operating region under high voltage conditions. Each HVRT
    /// curve is specified by an array of duration-volt pairs that will be
    /// interpolated into a piecewise linear function that defines an operating
    /// region. The x value of each pair specifies a duration (time at a given
    /// voltage in seconds). The y value of each pair specifies an effective
    /// percentage voltage, defined as ((locally measured voltage - setVRefOfs /
    /// setVRef). This control specifies the "may trip" region.
    #[yaserde(rename = "opModHVRTMayTrip")]
    pub op_mod_hvrt_may_trip: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 4. The High Voltage Ride-Through
    /// (HVRT) function is specified by duration-volt curves that define the
    /// operating region under high voltage conditions. Each HVRT curve is
    /// specified by an array of duration-volt pairs that will be interpolated
    /// into a piecewise linear function that defines an operating region. The x
    /// value of each pair specifies a duration (time at a given voltage in
    /// seconds). The y value of each pair specifies an effective percent
    /// voltage, defined as ((locally measured voltage - setVRefOfs) / setVRef).
    /// This control specifies the "momentary cessation" region.
    #[yaserde(rename = "opModHVRTMomentaryCessation")]
    pub op_mod_hvrt_momentary_cessation: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 5. The High Voltage Ride-Through
    /// (HVRT) function is specified by duration-volt curves that define the
    /// operating region under high voltage conditions. Each HVRT curve is
    /// specified by an array of duration-volt pairs that will be interpolated
    /// into a piecewise linear function that defines an operating region. The x
    /// value of each pair specifies a duration (time at a given voltage in
    /// seconds). The y value of each pair specifies an effective percent
    /// voltage, defined as ((locally measured voltage - setVRefOfs) / setVRef).
    /// This control specifies the "must trip" region.
    #[yaserde(rename = "opModHVRTMustTrip")]
    pub op_mod_hvrt_must_trip: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 6. The Low Frequency Ride-Through
    /// (LFRT) function is specified by one or two duration-frequency curves that
    /// define the operating region under low frequency conditions. Each LFRT
    /// curve is specified by an array of duration-frequency pairs that will be
    /// interpolated into a piecewise linear function that defines an operating
    /// region. The x value of each pair specifies a duration (time at a given
    /// frequency in seconds). The y value of each pair specifies a frequency, in
    /// Hz. This control specifies the "may trip" region.
    #[yaserde(rename = "opModLFRTMayTrip")]
    pub op_mod_lfrt_may_trip: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 7. The Low Frequency Ride-Through
    /// (LFRT) function is specified by a duration-frequency curve that defines
    /// the operating region under low frequency conditions. Each LFRT curve is
    /// specified by an array of duration-frequency pairs that will be
    /// interpolated into a piecewise linear function that defines an operating
    /// region. The x value of each pair specifies a duration (time at a given
    /// frequency in seconds). The y value of each pair specifies a frequency, in
    /// Hz. This control specifies the "must trip" region.
    #[yaserde(rename = "opModLFRTMustTrip")]
    pub op_mod_lfrt_must_trip: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 8. The Low Voltage Ride-Through
    /// (LVRT) function is specified by one, two, or three duration-volt curves
    /// that define the operating region under low voltage conditions. Each LVRT
    /// curve is specified by an array of duration-volt pairs that will be
    /// interpolated into a piecewise linear function that defines an operating
    /// region. The x value of each pair specifies a duration (time at a given
    /// voltage in seconds). The y value of each pair specifies an effective
    /// percent voltage, defined as ((locally measured voltage - setVRefOfs) /
    /// setVRef). This control specifies the "may trip" region.
    #[yaserde(rename = "opModLVRTMayTrip")]
    pub op_mod_lvrt_may_trip: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 9. The Low Voltage Ride-Through
    /// (LVRT) function is specified by duration-volt curves that define the
    /// operating region under low voltage conditions. Each LVRT curve is
    /// specified by an array of duration-volt pairs that will be interpolated
    /// into a piecewise linear function that defines an operating region. The x
    /// value of each pair specifies a duration (time at a given voltage in
    /// seconds). The y value of each pair specifies an effective percent
    /// voltage, defined as ((locally measured voltage - setVRefOfs) / setVRef).
    /// This control specifies the "momentary cessation" region.
    #[yaserde(rename = "opModLVRTMomentaryCessation")]
    pub op_mod_lvrt_momentary_cessation: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 10. The Low Voltage Ride-Through
    /// (LVRT) function is specified by duration-volt curves that define the
    /// operating region under low voltage conditions. Each LVRT curve is
    /// specified by an array of duration-volt pairs that will be interpolated
    /// into a piecewise linear function that defines an operating region. The x
    /// value of each pair specifies a duration (time at a given voltage in
    /// seconds). The y value of each pair specifies an effective percent
    /// voltage, defined as ((locally measured voltage - setVRefOfs) / setVRef).
    /// This control specifies the "must trip" region.
    #[yaserde(rename = "opModLVRTMustTrip")]
    pub op_mod_lvrt_must_trip: Option<DERCurveLink>,

    /// The opModMaxLimW function sets the maximum active power generation level
    /// at the electrical coupling point as a percentage of set capacity
    /// (%setMaxW, in hundredths). This limitation may be met e.g. by reducing PV
    /// output or by using excess PV output to charge associated storage.
    #[yaserde(rename = "opModMaxLimW")]
    pub op_mod_max_lim_w: Option<Percent>,

    /// Target reactive power, in var. This control is likely to be more useful
    /// for aggregators, as individual DERs may not be able to maintain a target
    /// setting.
    #[yaserde(rename = "opModTargetVar")]
    pub op_mod_target_var: Option<ReactivePower>,

    /// Target output power, in Watts. This control is likely to be more useful
    /// for aggregators, as individual DERs may not be able to maintain a target
    /// setting.
    #[yaserde(rename = "opModTargetW")]
    pub op_mod_target_w: Option<ActivePower>,

    /// Specify DERCurveLink for curveType == 11. The static volt-var function
    /// provides over- or under-excited var compensation as a function of
    /// measured voltage. The volt-var curve is specified as an array of volt-var
    /// pairs that are interpolated into a piecewise linear function with
    /// hysteresis. The x value of each pair specifies an effective percent
    /// voltage, defined as ((locally measured voltage - setVRefOfs) / setVRef)
    /// and SHOULD support a domain of at least 0 - 135. If VRef is present in
    /// DERCurve, then the x value of each pair is additionally multiplied by
    /// (VRef / 10000). The y value specifies a target var output interpreted as
    /// a signed percentage (-100 to 100). The meaning of the y value is
    /// determined by yRefType and must be one of %setMaxW, %setMaxVar, or
    /// %statVarAvail.
    #[yaserde(rename = "opModVoltVar")]
    pub op_mod_volt_var: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 12. The Volt-Watt reduces active
    /// power output as a function of measured voltage. The Volt-Watt curve is
    /// specified as an array of Volt-Watt pairs that are interpolated into a
    /// piecewise linear function with hysteresis. The x value of each pair
    /// specifies an effective percent voltage, defined as ((locally measured
    /// voltage - setVRefOfs) / setVRef) and SHOULD support a domain of at least
    /// 0 - 135. The y value specifies an active power output interpreted as a
    /// percentage (0 - 100). The meaning of the y value is determined by
    /// yRefType and must be one of %setMaxW or %statWAvail.
    #[yaserde(rename = "opModVoltWatt")]
    pub op_mod_volt_watt: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 13. The Watt-PF function varies
    /// Power Factor (PF) as a function of delivered active power. The Watt-PF
    /// curve is specified as an array of Watt-PF coordinates that are
    /// interpolated into a piecewise linear function with hysteresis. The x
    /// value of each pair specifies a watt setting in %setMaxW, (0 - 100). The
    /// PF output setting is a signed displacement in y value (PF sign SHALL be
    /// interpreted according to the EEI convention, where unity PF is considered
    /// unsigned). These settings are not expected to be updated very often
    /// during the life of the installation, therefore only a single curve is
    /// required. If issued simultaneously with other reactive power controls
    /// (e.g. opModFixedPFInjectW) the control resulting in least var magnitude
    /// SHOULD take precedence.
    #[yaserde(rename = "opModWattPF")]
    pub op_mod_watt_pf: Option<DERCurveLink>,

    /// Specify DERCurveLink for curveType == 14. The Watt-Var function varies
    /// vars as a function of delivered active power. The Watt-Var curve is
    /// specified as an array of Watt-Var pairs that are interpolated into a
    /// piecewise linear function with hysteresis. The x value of each pair
    /// specifies a watt setting in %setMaxW, (0-100). The y value specifies a
    /// target var output interpreted as a signed percentage (-100 to 100). The
    /// meaning of the y value is determined by yRefType and must be one of
    /// %setMaxW, %setMaxVar, or %statVarAvail.
    #[yaserde(rename = "opModWattVar")]
    pub op_mod_watt_var: Option<DERCurveLink>,

    /// This is the constraint on the imported active power at the connection point.
    #[cfg(feature = "csip_aus")]
    #[yaserde(rename = "opModImpLimW")]
    #[yaserde(prefix = "csipaus", namespace = "csipaus: https://csipaus.org/ns")]
    pub op_mod_imp_lim_w: Option<ActivePower>,

    /// This is the constraint on the exported active power at the connection point.
    #[cfg(feature = "csip_aus")]
    #[yaserde(rename = "opModExpLimW")]
    #[yaserde(prefix = "csipaus", namespace = "csipaus: https://csipaus.org/ns")]
    pub op_mod_exp_lim_w: Option<ActivePower>,

    /// This is a constraint on the maxium allowable discharge rate, in Watts,
    /// specifically for a single physical device (or aggregation of devices,
    /// excluding uncontrolled devices) such as an EV charge station.
    #[cfg(feature = "csip_aus")]
    #[yaserde(rename = "opModGenLimW")]
    #[yaserde(prefix = "csipaus", namespace = "csipaus: https://csipaus.org/ns")]
    pub op_mod_gen_lim_w: Option<ActivePower>,

    /// This is a constraint on the maximum allowable charge rate, in Watts,
    /// specifically for a single physical device (or aggregation of devices,
    /// excluding uncontrolled devices) such as an EV charge station.
    #[cfg(feature = "csip_aus")]
    #[yaserde(rename = "opModLoadLimW")]
    #[yaserde(prefix = "csipaus", namespace = "csipaus: https://csipaus.org/ns")]
    pub op_mod_load_lim_w: Option<ActivePower>,

    /// Requested ramp time, in hundredths of a second, for the device to
    /// transition from the current DERControl mode setting(s) to the new mode
    /// setting(s). If absent, use default ramp rate (setGradW). Resolution is
    /// 1/100 sec.
    #[yaserde(rename = "rampTms")]
    pub ramp_tms: Option<Uint16>,
}

impl DERControlBase {
    /// Determine if two DERControlBase instances target the same set of controls
    /// by whether they contain the same optional fields.
    pub fn same_target(&self, other: &Self) -> bool {
        // TODO: Could replace this by having a bitflag of the
        // options in the struct but would require manual serde impl
        #[cfg(feature = "csip_aus")]
        let extensions = {
            self.op_mod_imp_lim_w.is_some() == other.op_mod_imp_lim_w.is_some()
                && self.op_mod_exp_lim_w.is_some() == other.op_mod_exp_lim_w.is_some()
                && self.op_mod_gen_lim_w.is_some() == other.op_mod_gen_lim_w.is_some()
                && self.op_mod_load_lim_w.is_some() == other.op_mod_load_lim_w.is_some()
        };
        #[cfg(not(feature = "csip_aus"))]
        let extensions = true;
        self.op_mod_connect.is_some() == other.op_mod_connect.is_some()
            && self.op_mod_energize.is_some() == other.op_mod_energize.is_some()
            && self.op_mod_fixed_pf_absorb_w.is_some() == other.op_mod_fixed_pf_absorb_w.is_some()
            && self.op_mod_fixed_pf_inject_w.is_some() == other.op_mod_fixed_pf_inject_w.is_some()
            && self.op_mod_fixed_var.is_some() == other.op_mod_fixed_var.is_some()
            && self.op_mod_fixed_w.is_some() == other.op_mod_fixed_w.is_some()
            && self.op_mod_freq_droop.is_some() == other.op_mod_freq_droop.is_some()
            && self.op_mod_freq_watt.is_some() == other.op_mod_freq_watt.is_some()
            && self.op_mod_hfrt_may_trip.is_some() == other.op_mod_hfrt_may_trip.is_some()
            && self.op_mod_hfrt_must_trip.is_some() == other.op_mod_hfrt_must_trip.is_some()
            && self.op_mod_hvrt_may_trip.is_some() == other.op_mod_hvrt_may_trip.is_some()
            && self.op_mod_hvrt_momentary_cessation.is_some()
                == other.op_mod_hvrt_momentary_cessation.is_some()
            && self.op_mod_hvrt_must_trip.is_some() == other.op_mod_hvrt_must_trip.is_some()
            && self.op_mod_lfrt_may_trip.is_some() == other.op_mod_lfrt_may_trip.is_some()
            && self.op_mod_lfrt_must_trip.is_some() == other.op_mod_lfrt_must_trip.is_some()
            && self.op_mod_lvrt_may_trip.is_some() == other.op_mod_lvrt_may_trip.is_some()
            && self.op_mod_lvrt_momentary_cessation.is_some()
                == other.op_mod_lvrt_momentary_cessation.is_some()
            && self.op_mod_lvrt_must_trip.is_some() == other.op_mod_lvrt_must_trip.is_some()
            && self.op_mod_max_lim_w.is_some() == other.op_mod_max_lim_w.is_some()
            && self.op_mod_target_var.is_some() == other.op_mod_target_var.is_some()
            && self.op_mod_target_w.is_some() == other.op_mod_target_w.is_some()
            && self.op_mod_volt_var.is_some() == other.op_mod_volt_var.is_some()
            && self.op_mod_volt_watt.is_some() == other.op_mod_volt_watt.is_some()
            && self.op_mod_watt_pf.is_some() == other.op_mod_watt_pf.is_some()
            && self.op_mod_watt_var.is_some() == other.op_mod_watt_var.is_some()
            && self.ramp_tms.is_some() == other.ramp_tms.is_some()
            && extensions
    }
}

impl Validate for DERControlBase {}

/// Distributed Energy Resource (DER) time/event-based control.
#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SERandomizableEvent,
    SEEvent,
    SERespondableSubscribableIdentifiedObject,
    SEIdentifiedObject,
    SESubscribableResource,
    SERespondableResource,
    SEResource,
)]
#[yaserde(rename = "DERControl")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DERControl {
    #[yaserde(rename = "DERControlBase")]
    pub der_control_base: DERControlBase,

    /// Specifies the bitmap indicating the categories of devices that SHOULD
    /// respond. Devices SHOULD ignore events that do not indicate their device
    /// category. If not present, all devices SHOULD respond.
    #[yaserde(rename = "deviceCategory")]
    pub device_category: Option<DeviceCategoryType>,

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

impl Ord for DERControl {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        // Primary Key - primacy (ascending)
        match self.interval.start.cmp(&other.interval.start) {
            core::cmp::Ordering::Equal => {}
            ord => return ord,
        }
        // Secondary Key - creationTime (descending)
        match self.creation_time.cmp(&other.creation_time).reverse() {
            core::cmp::Ordering::Equal => {}
            ord => return ord,
        }
        // Tertiary Key - mRID (descending)
        self.mrid.cmp(&other.mrid).reverse()
    }
}

impl Validate for DERControl {}

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

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

bitflags! {
    /// Control modes supported by the DER.  Bit positions SHALL be defined as follows:
    ///
    /// 0 - Charge mode
    ///
    /// 1 - Discharge mode
    ///
    /// 2 - opModConnect (Connect / Disconnect - implies galvanic isolation)
    ///
    /// 3 - opModEnergize (Energize / De-Energize)
    ///
    /// 4 - opModFixedPFAbsorbW (Fixed Power Factor Setpoint when absorbing active power)
    ///
    /// 5 - opModFixedPFInjectW (Fixed Power Factor Setpoint when injecting active power)
    ///
    /// 6 - opModFixedVar (Reactive Power Setpoint)
    ///
    /// 7 - opModFixedW (Charge / Discharge Setpoint)
    ///
    /// 8 - opModFreqDroop (Frequency-Watt Parameterized Mode)
    ///
    /// 9 - opModFreqWatt (Frequency-Watt Curve Mode)
    ///
    /// 10 - opModHFRTMayTrip (High Frequency Ride Through, May Trip Mode)
    ///
    /// 11 - opModHFRTMustTrip (High Frequency Ride Through, Must Trip Mode)
    ///
    /// 12 - opModHVRTMayTrip (High Voltage Ride Through, May Trip Mode)
    ///
    /// 13 - opModHVRTMomentaryCessation (High Voltage Ride Through, Momentary Cessation Mode)
    ///
    /// 14 - opModHVRTMustTrip (High Voltage Ride Through, Must Trip Mode)
    ///
    /// 15 - opModLFRTMayTrip (Low Frequency Ride Through, May Trip Mode)
    ///
    /// 16 - opModLFRTMustTrip (Low Frequency Ride Through, Must Trip Mode)
    ///
    /// 17 - opModLVRTMayTrip (Low Voltage Ride Through, May Trip Mode)
    ///
    /// 18 - opModLVRTMomentaryCessation (Low Voltage Ride Through, Momentary Cessation Mode)
    ///
    /// 19 - opModLVRTMustTrip (Low Voltage Ride Through, Must Trip Mode)
    ///
    /// 20 - opModMaxLimW (Maximum Active Power)
    ///
    /// 21 - opModTargetVar (Target Reactive Power)
    ///
    /// 22 - opModTargetW (Target Active Power)
    ///
    /// 23 - opModVoltVar (Volt-Var Mode)
    ///
    /// 24 - opModVoltWatt (Volt-Watt Mode)
    ///
    /// 25 - opModWattPF (Watt-PowerFactor Mode)
    ///
    /// 26 - opModWattVar (Watt-Var Mode)
    ///
    /// All other values reserved.
    ///
    #[derive(Default, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, HexBinaryYaSerde)]
    pub struct DERControlType: u32 { // HexBinary32
        const ChargeMode = 1;
        const DischargeMode = 2;
        const OpModConnect = 4;
        const OpModEnergize = 8;
        const OpModFixedPFAbsorbW = 16;
        const OpModFixedPFInjectW = 32;
        const OpModFixedVar = 64;
        const OpModFixedW = 128;
        const OpModFreqDroop = 256;
        const OpModFreqWatt = 512;
        const OpModHFRTMayTrip = 1024;
        const OpModHFRTMustTrip = 2048;
        const OpModHVRTMayTrip = 4096;
        const OpModHVRTMomentaryCessation = 8192;
        const OpModHVRTMustTrip = 16384;
        const OpModLFRTMayTrip = 32768;
        const OpModLFRTMustTrip = 65536;
        const OpModLVRTMayTrip = 131072;
        const OpModLVRTMomentaryCessation = 262144;
        const OpModLVRTMustTrip = 524288;
        const OpModMaxLimW = 1048576;
        const OpModTargetVar = 2097152;
        const OpModTargetW = 4194304;
        const OpModVoltVar = 8388608;
        const OpModVoltWatt = 16777216;
        const OpModWattPF = 33554432;
        const OpModWattVar = 67108864;
    }
}

impl Validate for DERControlType {}

#[cfg(feature = "csip_aus")]
bitflags! {
    #[derive(Default, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, HexBinaryYaSerde)]
    pub struct DOEControlType: u16 { // HexBinary 16
        const OpModExpLimW = 1;
        const OpModImpLimW = 2;
        const OpModGenLimW = 4;
        const OpModLoadLimW = 8;
    }
}

#[derive(
    Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize, SEIdentifiedObject, SEResource,
)]
#[yaserde(rename = "DERCurve")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DERCurve {
    /// If the curveType is opModVoltVar, then this field MAY be present. If the
    /// curveType is not opModVoltVar, then this field SHALL NOT be present.
    /// Enable/disable autonomous vRef adjustment. When enabled, the Volt-Var
    /// curve characteristic SHALL be adjusted autonomously as vRef changes and
    /// autonomousVRefTimeConstant SHALL be present. If a DER is able to support
    /// Volt-Var mode but is unable to support autonomous vRef adjustment, then
    /// the DER SHALL execute the curve without autonomous vRef adjustment. If
    /// not specified, then the value is false.
    #[yaserde(rename = "autonomousVRefEnable")]
    pub autonomous_v_ref_enable: Option<bool>,

    /// If the curveType is opModVoltVar, then this field MAY be present. If the
    /// curveType is not opModVoltVar, then this field SHALL NOT be present.
    /// Adjustment range for vRef time constant, in hundredths of a second.
    #[yaserde(rename = "autonomousVRefTimeConstant")]
    pub autonomous_v_ref_time_constant: Option<Uint32>,

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

    /// Data point values for defining a curve or schedule
    /// Max size: 10
    /// TODO: Validate this max
    #[yaserde(rename = "CurveData")]
    pub curve_data: Vec<CurveData>,

    /// Specifies the associated curve-based control mode.
    #[yaserde(rename = "curveType")]
    pub curve_type: DERCurveType,

    /// Open loop response time, the time to ramp up to 90% of the new target in
    /// response to the change in voltage, in hundredths of a second. Resolution
    /// is 1/100 sec. A value of 0 is used to mean no limit. When not present,
    /// the device SHOULD follow its default behavior.
    #[yaserde(rename = "openLoopTms")]
    pub open_loop_tms: Option<Uint16>,

    /// Decreasing ramp rate, interpreted as a percentage change in output
    /// capability limit per second (e.g. %setMaxW / sec). Resolution is in
    /// hundredths of a percent/second. A value of 0 means there is no limit. If
    /// absent, ramp rate defaults to setGradW.
    #[yaserde(rename = "rampDecTms")]
    pub ramp_dec_tms: Option<Uint16>,

    /// Increasing ramp rate, interpreted as a percentage change in output
    /// capability limit per second (e.g. %setMaxW / sec). Resolution is in
    /// hundredths of a percent/second. A value of 0 means there is no limit. If
    /// absent, ramp rate defaults to rampDecTms.
    #[yaserde(rename = "rampIncTms")]
    pub ramp_inc_tms: Option<Uint16>,

    /// The configuration parameter for a low-pass filter, PT1 is a time, in
    /// hundredths of a second, in which the filter will settle to 95% of a step
    /// change in the input value. Resolution is 1/100 sec.
    #[yaserde(rename = "rampPT1Tms")]
    pub ramp_pt1_tms: Option<Uint16>,

    /// If the curveType is opModVoltVar, then this field MAY be present. If the
    /// curveType is not opModVoltVar, then this field SHALL NOT be present. The
    /// nominal AC voltage (RMS) adjustment to the voltage curve points for
    /// Volt-Var curves.
    #[yaserde(rename = "vRef")]
    pub v_ref: Option<Percent>,

    /// Exponent for X-axis value.
    #[yaserde(rename = "xMultiplier")]
    pub x_multiplier: PowerOfTenMultiplierType,

    /// Exponent for Y-axis value.
    #[yaserde(rename = "yMultiplier")]
    pub y_multiplier: PowerOfTenMultiplierType,

    /// The Y-axis units context.
    #[yaserde(rename = "yRefType")]
    pub y_ref_type: DERUnitRefType,

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

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

impl Validate for DERCurve {}

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

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

/// Data point values for defining a curve or schedule
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "CurveData")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct CurveData {
    /// If yvalue is Power Factor, then this field SHALL be present. If yvalue is
    /// not Power Factor, then this field SHALL NOT be present.
    /// True when DER is absorbing reactive power (under-excited), false
    /// when DER is injecting reactive power (over-excited).
    #[yaserde(rename = "excitation")]
    pub excitation: Option<bool>,

    /// The data value of the X-axis (independent) variable, depending on the
    /// curve type. See definitions in DERControlBase for further information.
    #[yaserde(rename = "xvalue")]
    pub xvalue: Int32,

    /// The data value of the Y-axis (dependent) variable, depending on the curve
    /// type. See definitions in DERControlBase for further information. If
    /// yvalue is Power Factor, the excitation field SHALL be present and yvalue
    /// SHALL be a positive value. If yvalue is not Power Factor, the excitation
    /// field SHALL NOT be present.
    #[yaserde(rename = "yvalue")]
    pub yvalue: Int32,
}

impl Validate for CurveData {}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(rename = "DERCurveType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum DERCurveType {
    #[default]
    OpModFreqWatt = 0, // (Frequency-Watt Curve Mode)
    OpModHfrtmayTrip = 1,  // (High Frequency Ride Through, May Trip Mode)
    OpModHfrtmustTrip = 2, // High Frequency Ride Through, Must Trip Mode)
    OpModHvrtmayTrip = 3,  // (High Voltage Ride Through, May Trip Mode)
    OpModHvrtmomentaryCessation = 4, // (High Voltage Ride Through, Momentary Cessation Mode)
    OpModHvrtmustTrip = 5, // (High Voltage Ride Through, Must Trip Mode)
    OpModLfrtmayTrip = 6,  // (Low Frequency Ride Through, May Trip Mode)
    OpModLfrtmustTrip = 7, // (Low Frequency Ride Through, Must Trip Mode)
    OpModLvrtmayTrip = 8,  // (Low Voltage Ride Through, May Trip Mode)
    OpModLvrtmomentaryCessation = 9, // (Low Voltage Ride Through, Momentary Cessation Mode)
    OpModLvrtmustTrip = 10, // (Low Voltage Ride Through, Must Trip Mode)
    OpModVoltVar = 11,     // (Volt-Var Mode)
    OpModVoltWatt = 12,    // (Volt-Watt Mode)
    OpModWattPf = 13,      // (Watt-PowerFactor Mode)
    OpModWattVar = 14,     // (Watt-Var Mode)
}

impl Validate for DERCurveType {}

#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableIdentifiedObject,
    SEIdentifiedObject,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "DERProgram")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DERProgram {
    #[yaserde(rename = "ActiveDERControlListLink")]
    pub active_der_control_list_link: Option<ActiveDERControlListLink>,

    #[yaserde(rename = "DefaultDERControlLink")]
    pub default_der_control_link: Option<DefaultDERControlLink>,

    #[yaserde(rename = "DERControlListLink")]
    pub der_control_list_link: Option<DERControlListLink>,

    #[yaserde(rename = "DERCurveListLink")]
    pub der_curve_list_link: Option<DERCurveListLink>,

    /// Indicates the relative primacy of the provider of this Program.
    #[yaserde(rename = "primacy")]
    pub primacy: PrimacyType,

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

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

impl Validate for DERProgram {}

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

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

#[derive(
    Default,
    PartialEq,
    Eq,
    Debug,
    Clone,
    YaSerialize,
    YaDeserialize,
    SESubscribableResource,
    SEResource,
)]
#[yaserde(rename = "DERStatus")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct DERStatus {
    /// Bitmap indicating the status of DER alarms (see DER LogEvents for more
    /// details).
    /// 0 - DER_FAULT_OVER_CURRENT
    /// 1 - DER_FAULT_OVER_VOLTAGE
    /// 2 - DER_FAULT_UNDER_VOLTAGE
    /// 3 - DER_FAULT_OVER_FREQUENCY
    /// 4 - DER_FAULT_UNDER_FREQUENCY
    /// 5 - DER_FAULT_VOLTAGE_IMBALANCE
    /// 6 - DER_FAULT_CURRENT_IMBALANCE
    /// 7 - DER_FAULT_EMERGENCY_LOCAL
    /// 8 - DER_FAULT_EMERGENCY_REMOTE
    /// 9 - DER_FAULT_LOW_POWER_INPUT
    /// 10 - DER_FAULT_PHASE_ROTATION
    /// 11-31 - Reserved
    #[yaserde(rename = "alarmStatus")]
    pub alarm_status: Option<DERAlarmStatus>,

    /// Connect/status value for generator DER.
    /// See ConnectStatusType for values.
    #[yaserde(rename = "genConnectStatus")]
    pub gen_connect_status: Option<ConnectStatusType>,

    /// DER InverterStatus/value.
    /// See InverterStatusType for values.
    #[yaserde(rename = "inverterStatus")]
    pub inverter_status: Option<InverterStatusType>,

    /// The local control mode status.
    /// See LocalControlModeStatusType for values.
    #[yaserde(rename = "localControlModeStatus")]
    pub local_control_mode_status: Option<LocalControlModeStatusType>,

    /// Manufacturer status code.
    #[yaserde(rename = "manufacturerStatus")]
    pub manufacturer_status: Option<ManufacturerStatusType>,

    /// Operational mode currently in use.
    /// See OperationalModeStatusType for values.
    #[yaserde(rename = "operationalModeStatus")]
    pub operational_mode_status: Option<OperationalModeStatusType>,

    /// The timestamp when the current status was last updated.
    #[yaserde(rename = "readingTime")]
    pub reading_time: TimeType,

    /// State of charge status.
    /// See StateOfChargeStatusType for values.
    #[yaserde(rename = "stateOfChargeStatus")]
    pub state_of_charge_status: Option<StateOfChargeStatusType>,

    /// Storage mode status.
    /// See StorageModeStatusType for values.
    #[yaserde(rename = "storageModeStatus")]
    pub storage_mode_status: Option<StorageModeStatusType>,

    /// Connect/status value for storage DER.
    /// See ConnectStatusType for values.
    #[yaserde(rename = "storConnectStatus")]
    pub stor_connect_status: Option<ConnectStatusType>,

    /// 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>,
}

bitflags! {
    #[derive(Default, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, HexBinaryYaSerde)]
    pub struct DERAlarmStatus: u32 { // HexBinary32
        const DER_FAULT_OVER_CURRENT = 1;
        const DER_FAULT_OVER_VOLTAGE = 2;
        const DER_FAULT_UNDER_VOLTAGE = 4;
        const DER_FAULT_OVER_FREQUENCY = 8;
        const DER_FAULT_UNDER_FREQUENCY = 16;
        const DER_FAULT_VOLTAGE_IMBALANCE = 32;
        const DER_FAULT_CURRENT_IMBALANCE = 64;
        const DER_FAULT_EMERGENCY_LOCAL = 128;
        const DER_FAULT_EMERGENCY_REMOTE = 256;
        const DER_FAULT_LOW_POWER_INPUT = 512;
        const DER_FAULT_PHASE_ROTATION = 1024;
    }
}

impl Validate for DERStatus {}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(rename = "DERUnitRefType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum DERUnitRefType {
    /// Specifies context for interpreting percent values:
    #[default]
    NotApplicable = 0,
    SetMaxW = 1,
    SetMaxVar = 2,
    StatVarAvail = 3,
    SetEffectiveV = 4,
    SetMaxChargeRateW = 5,
    SetMaxDischargeRateW = 6,
    StatWAvail = 7,
    // 8-255 Reserved
}

impl Validate for DERUnitRefType {}

/// Average flow of charge through a conductor.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "CurrentRMS")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct CurrentRMS {
    /// Specifies exponent of value.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in amperes RMS (uom 5)
    #[yaserde(rename = "value")]
    pub value: Uint16,
}

impl Validate for CurrentRMS {}

/// Abstract type for specifying a fixed-point value without a given unit of
/// measure.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "FixedPointType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct FixedPointType {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Dimensionless value
    #[yaserde(rename = "value")]
    pub value: Int16,
}

impl Validate for FixedPointType {}

/// Abstract type for specifying an unsigned fixed-point value without a given
/// unit of measure.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "UnsignedFixedPointType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct UnsignedFixedPointType {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Dimensionless value
    #[yaserde(rename = "value")]
    pub value: Uint16,
}

impl Validate for UnsignedFixedPointType {}

/// The active (real) power P (in W) is the product of root-mean-square (RMS)
/// voltage, RMS current, and cos(theta) where theta is the phase angle of
/// current relative to voltage. It is the primary measure of the rate of flow of
/// energy.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ActivePower")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ActivePower {
    /// Specifies exponent for uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in watts (uom 38)
    #[yaserde(rename = "value")]
    pub value: Int16,
}

impl Validate for ActivePower {}

/// Available electric charge
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "AmpereHour")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct AmpereHour {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in ampere-hours (uom 106)
    #[yaserde(rename = "value")]
    pub value: Uint16,
}

impl Validate for AmpereHour {}

/// The apparent power S (in VA) is the product of root mean square (RMS) voltage
/// and RMS current.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ApparentPower")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ApparentPower {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in volt-amperes (uom 61)
    #[yaserde(rename = "value")]
    pub value: Uint16,
}

impl Validate for ApparentPower {}

/// The reactive power Q (in var) is the product of root mean square (RMS)
/// voltage, RMS current, and sin(theta) where theta is the phase angle of
/// current relative to voltage.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ReactivePower")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ReactivePower {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in volt-amperes reactive (var) (uom 63)
    #[yaserde(rename = "value")]
    pub value: Int16,
}

impl Validate for ReactivePower {}

/// Reactive susceptance
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ReactiveSusceptance")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ReactiveSusceptance {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in siemens (uom 53)
    #[yaserde(rename = "value")]
    pub value: Uint16,
}

impl Validate for ReactiveSusceptance {}

/// Specifies a setpoint for Displacement Power Factor, the ratio between
/// apparent and active powers at the fundamental frequency (e.g. 60 Hz).
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "PowerFactor")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct PowerFactor {
    /// Significand of an unsigned value of cos(theta) between 0 and 1.0. E.g. a
    /// value of 0.95 may be specified as a displacement of 950 and a multiplier
    /// of -3.
    #[yaserde(rename = "displacement")]
    pub displacement: Uint16,

    /// Specifies exponent of 'displacement'.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,
}

impl Validate for PowerFactor {}

/// Specifies a setpoint for Displacement Power Factor, the ratio between
/// apparent and active powers at the fundamental frequency (e.g. 60 Hz) and
/// includes an excitation flag.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "PowerFactorWithExcitation")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct PowerFactorWithExcitation {
    /// Significand of an unsigned value of cos(theta) between 0 and 1.0. E.g. a
    /// value of 0.95 may be specified as a displacement of 950 and a multiplier
    /// of -3.
    #[yaserde(rename = "displacement")]
    pub displacement: Uint16,

    /// True when DER is absorbing reactive power (under-excited), false
    /// when DER is injecting reactive power (over-excited).
    #[yaserde(rename = "excitation")]
    pub excitation: bool,

    /// Specifies exponent of 'displacement'.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,
}

impl Validate for PowerFactorWithExcitation {}

/// Specifies a signed setpoint for reactive power.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "FixedVar")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct FixedVar {
    /// Indicates whether to interpret 'value' as %setMaxVar or %statVarAvail.
    #[yaserde(rename = "refType")]
    pub ref_type: DERUnitRefType,

    /// Specify a signed setpoint for reactive power in % (see 'refType' for
    /// context).
    #[yaserde(rename = "value")]
    pub value: SignedPercent,
}

impl Validate for FixedVar {}

/// Active (real) energy
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "WattHour")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct WattHour {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in watt-hours (uom 72)
    #[yaserde(rename = "value")]
    pub value: Uint16,
}

impl Validate for WattHour {}

/// Average electric potential difference between two points.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "VoltageRMS")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct VoltageRMS {
    /// Specifies exponent of uom.
    #[yaserde(rename = "multiplier")]
    pub multiplier: PowerOfTenMultiplierType,

    /// Value in volts RMS (uom 29)
    #[yaserde(rename = "value")]
    pub value: Uint16,
}

impl Validate for VoltageRMS {}

/// DER ConnectStatus value (bitmap):
///
/// 0 - Connected
///
/// 1 - Available
///
/// 2 - Operating
///
/// 3 - Test
///
/// 4 - Fault / Error
///
/// All other values reserved.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ConnectStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ConnectStatusType {
    /// The date and time at which the state applied.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// The value indicating the state.
    #[yaserde(rename = "value")]
    pub value: ConnectStatusValue,
}

bitflags! {
    #[derive(Default, PartialEq, PartialOrd, Eq, Ord, Clone, Copy, Debug, HexBinaryYaSerde)]
    pub struct ConnectStatusValue: u8 { // HexBinary8
        const Connected = 1;
        const Available = 2;
        const Operating = 4;
        const Test = 8;
        const Error = 16;
    }
}

impl Validate for ConnectStatusType {}

/// DER InverterStatus value:
/// 0 - N/A
/// 1 - off
/// 2 - sleeping (auto-shutdown) or DER is at low output power/voltage
/// 3 - starting up or ON but not producing power
/// 4 - tracking MPPT power point
/// 5 - forced power reduction/derating
/// 6 - shutting down
/// 7 - one or more faults exist
/// 8 - standby (service on unit) - DER may be at high output voltage/power
/// 9 - test mode
/// 10 - as defined in manufacturer status
/// All other values reserved.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "InverterStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct InverterStatusType {
    /// The date and time at which the state applied.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// The value indicating the state.
    #[yaserde(rename = "value")]
    pub value: InverterStatusValue,
}

impl Validate for InverterStatusType {}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum InverterStatusValue {
    #[default]
    NotApplicable = 0,
    Off = 1,
    SleepingOrLowPower = 2,
    StartingUpOrNoProduction = 3,
    TrackingMPPTPP = 4,
    ForcedPowerReduction = 5,
    ShuttingDown = 6,
    FaultsExist = 7,
    Standby = 8,
    TestMode = 9,
    ManufacturerStatus = 10,
}

/// DER LocalControlModeStatus/value:
/// 0 – local control
/// 1 – remote control
/// All other values reserved.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "LocalControlModeStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct LocalControlModeStatusType {
    /// The date and time at which the state applied.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// The value indicating the state.
    #[yaserde(rename = "value")]
    pub value: Uint8,
}

impl Validate for LocalControlModeStatusType {}

/// DER ManufacturerStatus/value: String data type
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "ManufacturerStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct ManufacturerStatusType {
    /// The date and time at which the state applied.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// The value indicating the state.
    #[yaserde(rename = "value")]
    pub value: String6,
}

impl Validate for ManufacturerStatusType {}

/// DER OperationalModeStatus value:
/// 0 - Not applicable / Unknown
/// 1 - Off
/// 2 - Operational mode
/// 3 - Test mode
/// All other values reserved.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "OperationalModeStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct OperationalModeStatusType {
    /// The date and time at which the state applied.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// The value indicating the state.
    #[yaserde(rename = "value")]
    pub value: OperationalModeStatusValue,
}

impl Validate for OperationalModeStatusType {}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum OperationalModeStatusValue {
    #[default]
    NotApplicable = 0,
    Off = 1,
    Operational = 2,
    Test = 3,
}

/// DER StateOfChargeStatus value: Percent data type
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "StateOfChargeStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct StateOfChargeStatusType {
    /// The date and time at which the state applied.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// The value indicating the state.
    #[yaserde(rename = "value")]
    pub value: Percent,
}

impl Validate for StateOfChargeStatusType {}

/// DER StorageModeStatus value:
/// 0 – storage charging
/// 1 – storage discharging
/// 2 – storage holding
/// All other values reserved.
#[derive(Default, PartialEq, Eq, Debug, Clone, YaSerialize, YaDeserialize)]
#[yaserde(rename = "StorageModeStatusType")]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
pub struct StorageModeStatusType {
    /// The date and time at which the state applied.
    #[yaserde(rename = "dateTime")]
    pub date_time: TimeType,

    /// The value indicating the state.
    #[yaserde(rename = "value")]
    pub value: StorageModeStatusValue,
}

#[derive(Default, PartialEq, Eq, Debug, Clone, Copy, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "urn:ieee:std:2030.5:ns")]
#[repr(u8)]
pub enum StorageModeStatusValue {
    #[default]
    StorageCharging = 0,
    StorageDischarging = 1,
    StorageHolding = 2,
}

impl Validate for StorageModeStatusType {}

#[cfg(not(feature = "csip_aus"))]
#[test]
fn dercap_no_csip_aus() {
    let expected = r#"<DERCapability xmlns="urn:ieee:std:2030.5:ns">
  <modesSupported>0</modesSupported>
  <rtgMaxW>
    <multiplier>0</multiplier>
    <value>0</value>
  </rtgMaxW>
  <type>0</type>
</DERCapability>"#;
    let dcap = DERCapability::default();
    let out = serialize(&dcap).unwrap();
    assert_eq!(expected, out);
}

#[cfg(feature = "csip_aus")]
#[test]
fn csip_aus_dercap() {
    let expected = r#"<DERCapability xmlns="urn:ieee:std:2030.5:ns" xmlns:csipaus="https://csipaus.org/ns">
  <modesSupported>0</modesSupported>
  <csipaus:doeModesSupported>0</csipaus:doeModesSupported>
  <rtgMaxW>
    <multiplier>0</multiplier>
    <value>0</value>
  </rtgMaxW>
  <type>0</type>
</DERCapability>"#;
    let dcap = DERCapability::default();
    let out = serialize(&dcap).unwrap();
    assert_eq!(expected, out);
}

#[cfg(not(feature = "csip_aus"))]
#[test]
fn dercontrolbase_no_csip_aus() {
    let expected = r#"<DERControlBase xmlns="urn:ieee:std:2030.5:ns" />"#;
    let dercb = DERControlBase::default();
    let out = serialize(&dercb).unwrap();
    assert_eq!(expected, out);
}

#[cfg(feature = "csip_aus")]
#[test]
fn csip_aus_dercontrolbase() {
    let expected = r#"<DERControlBase xmlns="urn:ieee:std:2030.5:ns" xmlns:csipaus="https://csipaus.org/ns">
  <csipaus:opModImpLimW>
    <multiplier>0</multiplier>
    <value>0</value>
  </csipaus:opModImpLimW>
  <csipaus:opModExpLimW>
    <multiplier>0</multiplier>
    <value>0</value>
  </csipaus:opModExpLimW>
  <csipaus:opModGenLimW>
    <multiplier>0</multiplier>
    <value>0</value>
  </csipaus:opModGenLimW>
  <csipaus:opModLoadLimW>
    <multiplier>0</multiplier>
    <value>0</value>
  </csipaus:opModLoadLimW>
</DERControlBase>"#;
    let mut dercb = DERControlBase::default();
    dercb.op_mod_imp_lim_w = Some(Default::default());
    dercb.op_mod_exp_lim_w = Some(Default::default());
    dercb.op_mod_gen_lim_w = Some(Default::default());
    dercb.op_mod_load_lim_w = Some(Default::default());
    let out = serialize(&dercb).unwrap();
    assert_eq!(expected, out);
}