-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
307 lines (265 loc) · 10.2 KB
/
helpers.ts
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
// eslint-disable-next-line eslint-comments/disable-enable-pair
/* eslint-disable @typescript-eslint/indent */
import type {
GreenButtonContentType,
GreenButtonEntry,
GreenButtonEntryWithApplicationInformationContent,
GreenButtonEntryWithAuthorizationContent,
GreenButtonEntryWithBatchListContent,
GreenButtonEntryWithCustomerAccountContent,
GreenButtonEntryWithCustomerAgreementContent,
GreenButtonEntryWithCustomerContent,
GreenButtonEntryWithElectricPowerQualitySummaryContent,
GreenButtonEntryWithIntervalBlockContent,
GreenButtonEntryWithLocalTimeParametersContent,
GreenButtonEntryWithMeterContent,
GreenButtonEntryWithMeterReadingContent,
GreenButtonEntryWithReadingTypeContent,
GreenButtonEntryWithServiceLocationContent,
GreenButtonEntryWithServiceStatusContent,
GreenButtonEntryWithServiceSupplierContent,
GreenButtonEntryWithUsagePointContent,
GreenButtonEntryWithUsageSummaryContent,
GreenButtonJson,
GreenButtonLinks
} from './types/entryTypes.js'
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'ApplicationInformation'
): GreenButtonEntryWithApplicationInformationContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'Authorization'
): GreenButtonEntryWithAuthorizationContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'BatchList'
): GreenButtonEntryWithBatchListContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'Customer'
): GreenButtonEntryWithCustomerContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'CustomerAccount'
): GreenButtonEntryWithCustomerAccountContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'CustomerAgreement'
): GreenButtonEntryWithCustomerAgreementContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'ElectricPowerQualitySummary'
): GreenButtonEntryWithElectricPowerQualitySummaryContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'IntervalBlock'
): GreenButtonEntryWithIntervalBlockContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'LocalTimeParameters'
): GreenButtonEntryWithLocalTimeParametersContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'Meter'
): GreenButtonEntryWithMeterContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'MeterReading'
): GreenButtonEntryWithMeterReadingContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'ReadingType'
): GreenButtonEntryWithReadingTypeContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'ServiceLocation'
): GreenButtonEntryWithServiceLocationContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'ServiceStatus'
): GreenButtonEntryWithServiceStatusContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'ServiceSupplier'
): GreenButtonEntryWithServiceSupplierContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'UsagePoint'
): GreenButtonEntryWithUsagePointContent[]
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: 'UsageSummary'
): GreenButtonEntryWithUsageSummaryContent[]
/**
* Filters entries in a Green Button object to only include entries of a given content type.
* @param {GreenButtonJson} greenButtonJson Green Button object
* @param {GreenButtonContentType} contentType Content type
* @returns {GreenButtonEntry[]} A filtered list of Green Button entries
*/
export function getEntriesByContentType(
greenButtonJson: GreenButtonJson,
contentType: GreenButtonContentType
): GreenButtonEntry[] {
const entries: GreenButtonEntry[] = []
for (const entry of greenButtonJson.entries) {
if (Object.hasOwn(entry.content, contentType)) {
entries.push(entry)
}
}
return entries
}
/**
* Filters entries in a Green Button object to only include entries with a specific link value.
* @param {GreenButtonJson} greenButtonJson Green Button object
* @param {string} link The link to look for.
* @param {keyof GreenButtonLinks} relationship The link relationship (i.e. self, up, related)
* @returns {GreenButtonEntry[]} A filtered list of Green Button entries
*/
export function getEntriesByLink(
greenButtonJson: GreenButtonJson,
link: string,
relationship: keyof GreenButtonLinks
): GreenButtonEntry[] {
const entries: GreenButtonEntry[] = []
for (const entry of greenButtonJson.entries) {
if (
Object.hasOwn(entry.links, relationship) &&
((typeof entry.links[relationship] === 'string' &&
entry.links[relationship] === link) ||
entry.links[relationship]?.includes(link))
) {
entries.push(entry)
}
}
return entries
}
/**
* Finds the related MeterReading entry.
* @param {GreenButtonJson} greenButtonJson Green Button object
* @param {GreenButtonEntryWithIntervalBlockContent} entryWithIntervalBlock A Green Button entry that includes IntervalBlock content.
* @returns {GreenButtonEntryWithMeterReadingContent | undefined} A related Green button entry that includes MeterReading content.
*/
export function getMeterReadingEntryFromIntervalBlockEntry(
greenButtonJson: GreenButtonJson,
entryWithIntervalBlock: GreenButtonEntryWithIntervalBlockContent
): GreenButtonEntryWithMeterReadingContent | undefined {
const possibleMeterReadingEntries = getEntriesByLink(
greenButtonJson,
entryWithIntervalBlock.links.up ?? '',
'related'
)
const meterReadingEntries = possibleMeterReadingEntries.filter(
(possibleEntry) => {
return possibleEntry.content.MeterReading !== undefined
}
)
if (meterReadingEntries.length === 0) {
return undefined
}
return meterReadingEntries[0] as GreenButtonEntryWithMeterReadingContent
}
/**
* Finds the related ReadingType entry.
* @param {GreenButtonJson} greenButtonJson Green Button object
* @param {GreenButtonEntryWithMeterReadingContent} entryWithMeterReading A Green Button entry that includes MeterReading content.
* @returns {GreenButtonEntryWithReadingTypeContent | undefined} A related Green Button entry with ReadingType content.
*/
export function getReadingTypeEntryFromMeterReadingEntry(
greenButtonJson: GreenButtonJson,
entryWithMeterReading: GreenButtonEntryWithMeterReadingContent
): GreenButtonEntryWithReadingTypeContent | undefined {
for (const relatedLink of entryWithMeterReading.links.related ?? []) {
const possibleReadingTypeEntries = getEntriesByLink(
greenButtonJson,
relatedLink,
'self'
)
const readingTypeEntries = possibleReadingTypeEntries.filter(
(possibleEntry) => {
return possibleEntry.content.ReadingType !== undefined
}
)
if (readingTypeEntries.length > 0) {
return readingTypeEntries[0] as GreenButtonEntryWithReadingTypeContent
}
}
return undefined
}
/**
* Finds the related ReadingType entry.
* @param {GreenButtonJson} greenButtonJson Green Button object
* @param {GreenButtonEntryWithIntervalBlockContent} entryWithIntervalBlock A Green Button entry that includes IntervalBlock content.
* @returns {GreenButtonEntryWithReadingTypeContent | undefined} A related Green Button entry with ReadingType content.
*/
export function getReadingTypeEntryFromIntervalBlockEntry(
greenButtonJson: GreenButtonJson,
entryWithIntervalBlock: GreenButtonEntryWithIntervalBlockContent
): GreenButtonEntryWithReadingTypeContent | undefined {
const meterReadingEntry = getMeterReadingEntryFromIntervalBlockEntry(
greenButtonJson,
entryWithIntervalBlock
)
if (meterReadingEntry === undefined) {
return undefined
}
return getReadingTypeEntryFromMeterReadingEntry(
greenButtonJson,
meterReadingEntry
)
}
/**
* Finds the related UsagePoint entry.
* @param {GreenButtonJson} greenButtonJson Green Button object
* @param {GreenButtonEntryWithMeterReadingContent | GreenButtonEntryWithUsageSummaryContent} entry A Green Button entry that includes either MeterReading or UsageSummary content.
* @returns {GreenButtonEntryWithUsagePointContent | undefined} A related Green Button entry with UsagePoint content.
*/
export function getUsagePointEntryFromEntry(
greenButtonJson: GreenButtonJson,
entry:
| GreenButtonEntryWithMeterReadingContent
| GreenButtonEntryWithUsageSummaryContent
): GreenButtonEntryWithUsagePointContent | undefined {
const possibleUsagePointEntries = getEntriesByLink(
greenButtonJson,
entry.links.up ?? '',
'related'
)
const usagePointEntries = possibleUsagePointEntries.filter(
(possibleEntry) => {
return possibleEntry.content.UsagePoint !== undefined
}
)
if (usagePointEntries.length > 0) {
return usagePointEntries[0] as GreenButtonEntryWithUsagePointContent
}
return undefined
}
/**
* Finds the related UsagePoint entry.
* @param {GreenButtonJson} greenButtonJson Green Button object
* @param {GreenButtonEntryWithIntervalBlockContent} entryWithIntervalBlock A Green Button entry that includes IntervalBlock content.
* @returns {GreenButtonEntryWithUsagePointContent | undefined} A related Green Button entry with UsagePoint content.
*/
export function getUsagePointEntryFromIntervalBlockEntry(
greenButtonJson: GreenButtonJson,
entryWithIntervalBlock: GreenButtonEntryWithIntervalBlockContent
): GreenButtonEntryWithUsagePointContent | undefined {
const meterReadingEntry = getMeterReadingEntryFromIntervalBlockEntry(
greenButtonJson,
entryWithIntervalBlock
)
if (meterReadingEntry === undefined) {
return undefined
}
return getUsagePointEntryFromEntry(greenButtonJson, meterReadingEntry)
}
export default {
getEntriesByContentType,
getEntriesByLink,
getMeterReadingEntryFromIntervalBlockEntry,
getReadingTypeEntryFromMeterReadingEntry,
getReadingTypeEntryFromIntervalBlockEntry,
getUsagePointEntryFromEntry,
getUsagePointEntryFromIntervalBlockEntry
}