-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (140 loc) · 6.12 KB
/
index.js
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
import { atomToGreenButtonJson } from '@cityssm/green-button-parser';
import axios from 'axios';
import Debug from 'debug';
import { apiConfigurations } from './apiConfigurations.js';
import { formatDateTimeFiltersParameters } from './utilities.js';
const debug = Debug('green-button-subscriber');
export class GreenButtonSubscriber {
#configuration;
#token;
constructor(configuration) {
if (configuration !== undefined) {
this.setConfiguration(configuration);
}
}
setConfiguration(configuration) {
this.#configuration = configuration;
this.#token = undefined;
}
setUtilityApiConfiguration(apiToken, baseUrl = apiConfigurations.utilityAPI.baseUrl) {
this.setConfiguration({
baseUrl,
accessToken: apiToken
});
}
async #getAccessToken() {
if (this.#configuration.accessToken !== undefined) {
this.#token = {
access_token: this.#configuration.accessToken,
expires_in: Number.POSITIVE_INFINITY
};
return;
}
try {
const authorizeUrl = this.#configuration.oauthUrl ??
`${this.#configuration.baseUrl}oauth/authorize`;
debug(`Authorize URL: ${authorizeUrl}`);
const response = await axios.post(authorizeUrl, 'grant_type=client_credentials&scope=FB=36_40', {
headers: {
Accept: 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: `Basic ${Buffer.from(`${this.#configuration.clientId}:${this.#configuration.clientSecret}`).toString('base64')}`
}
});
this.#token = response.data;
debug('Access token obtained successfully.');
debug('Access Token:', this.#token);
}
catch (error) {
debug('Error getting access token:', error.response.data);
}
}
async #getEndpoint(apiEndpoint, getParameters = {}) {
if (this.#token === undefined ||
Date.now() >= this.#token.expires_in * 1000) {
debug('Token expired.');
await this.#getAccessToken();
}
const headers = {
Authorization: `Bearer ${this.#token?.access_token ?? ''}`
};
debug(`End Point: ${apiEndpoint}`);
const requestOptions = {
headers
};
if (getParameters !== undefined && Object.keys(getParameters).length > 0) {
requestOptions.params = getParameters;
}
try {
const response = await axios.get(apiEndpoint, requestOptions);
return {
data: response.data,
status: response.status
};
}
catch (error) {
debug('Error accessing API endpoint:', error.response.data);
}
return undefined;
}
async getGreenButtonHttpsLink(greenButtonHttpsLink, getParameters) {
const endpointResponse = await this.#getEndpoint(greenButtonHttpsLink, getParameters);
if (endpointResponse === undefined) {
return undefined;
}
let json;
if ((endpointResponse.data ?? '') !== '') {
json = await atomToGreenButtonJson(endpointResponse.data);
}
return {
status: endpointResponse.status,
json
};
}
async getGreenButtonEndpoint(greenButtonEndpoint, getParameters) {
return await this.getGreenButtonHttpsLink(`${this.#configuration.baseUrl}espi/1_1/resource${greenButtonEndpoint}`, getParameters);
}
async getAuthorizations() {
return await this.getGreenButtonEndpoint('/Authorization');
}
async getAuthorization(authorizationId) {
return await this.getGreenButtonEndpoint(`/Authorization/${authorizationId}`);
}
async getUsagePoints(authorizationId) {
return await this.getGreenButtonEndpoint(`/Subscription/${authorizationId}/UsagePoint`);
}
async getMeterReadings(authorizationId, meterId) {
return await this.getGreenButtonEndpoint(`/Subscription/${authorizationId}/UsagePoint/${meterId}/MeterReading`);
}
async getIntervalBlocks(authorizationId, meterId, readingId) {
return await this.getGreenButtonEndpoint(`/Subscription/${authorizationId}/UsagePoint/${meterId}/MeterReading/${readingId}/IntervalBlock`);
}
async getUsageSummaries(authorizationId, meterId) {
return await this.getGreenButtonEndpoint(`/Subscription/${authorizationId}/UsagePoint/${meterId}/UsageSummary`);
}
async getElectricPowerQualitySummaries(authorizationId, meterId) {
return await this.getGreenButtonEndpoint(`/Subscription/${authorizationId}/UsagePoint/${meterId}/ElectricPowerQualitySummary`);
}
async getCustomers(authorizationId) {
return await this.getGreenButtonEndpoint(`/RetailCustomer/${authorizationId}/Customer`);
}
async getCustomerAccounts(authorizationId, customerId) {
return await this.getGreenButtonEndpoint(`/RetailCustomer/${authorizationId}/Customer/${customerId}/CustomerAccount`);
}
async getCustomerAgreements(authorizationId, customerId, customerAccountId) {
return await this.getGreenButtonEndpoint(`/RetailCustomer/${authorizationId}/Customer/${customerId}/CustomerAccount/${customerAccountId}/CustomerAgreement`);
}
async getBatchSubscriptionsByAuthorization(authorizationId, dateTimeFilters) {
return await this.getGreenButtonEndpoint(`/Batch/Subscription/${authorizationId}`, formatDateTimeFiltersParameters(dateTimeFilters));
}
async getBatchSubscriptionsByMeter(authorizationId, meterId, dateTimeFilters) {
return await this.getGreenButtonEndpoint(`/Batch/Subscription/${authorizationId}/UsagePoint/${meterId}`, formatDateTimeFiltersParameters(dateTimeFilters));
}
async getServiceStatus() {
return await this.getGreenButtonEndpoint('/ReadServiceStatus');
}
async getApplicationInformation(appId) {
return await this.getGreenButtonEndpoint(`/ApplicationInformation/${appId}`);
}
}
export { helpers } from '@cityssm/green-button-parser';