node-red-contrib-octopus-intelligent 1.4.0 → 1.6.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +487 -408
- package/README.md +1004 -980
- package/examples/basic-monitoring.json +1 -1
- package/examples/ha-dashboard.yaml +602 -486
- package/examples/mqtt-integration.json +1 -1
- package/examples/node-red-dashboard-flow.json +1287 -0
- package/examples/set-preferences.json +1 -1
- package/lib/api-metrics.js +31 -31
- package/lib/auth.js +20 -20
- package/lib/categories/account.js +32 -32
- package/lib/categories/applicable-rates.js +127 -91
- package/lib/categories/completed-dispatches.js +35 -35
- package/lib/categories/electricity.js +146 -133
- package/lib/categories/flex-planned-dispatches.js +36 -36
- package/lib/categories/free-electricity.js +61 -61
- package/lib/categories/gas.js +95 -95
- package/lib/categories/home-mini.js +37 -37
- package/lib/categories/intelligent.js +155 -124
- package/lib/categories/octoplus.js +32 -32
- package/lib/categories/saving-sessions.js +64 -64
- package/lib/categories/wheel-of-fortune.js +49 -49
- package/lib/commands.js +47 -0
- package/lib/discovery-retry.js +13 -0
- package/lib/discovery.js +92 -85
- package/lib/graphql-errors.js +18 -0
- package/lib/graphql.js +44 -44
- package/lib/http-get-json.js +22 -0
- package/lib/payload.js +89 -77
- package/lib/poll-tick.js +44 -44
- package/lib/preferences.js +32 -0
- package/lib/scheduler.js +47 -47
- package/lib/sensor-definitions.js +219 -0
- package/lib/timezone.js +49 -49
- package/lib/update-check.js +38 -0
- package/octopus-intelligent.html +218 -205
- package/octopus-intelligent.js +1687 -1685
- package/package.json +54 -54
|
@@ -1,133 +1,146 @@
|
|
|
1
|
-
// lib/categories/electricity.js
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
const RATES_QUERY = `
|
|
5
|
-
query getElectricityRates($account: String!) {
|
|
6
|
-
account(accountNumber: $account) {
|
|
7
|
-
electricityAgreements {
|
|
8
|
-
validFrom validTo
|
|
9
|
-
tariff {
|
|
10
|
-
... on TariffType { productCode standingCharge }
|
|
11
|
-
... on StandardTariff { productCode standingCharge unitRate }
|
|
12
|
-
... on HalfHourlyTariff { productCode standingCharge }
|
|
13
|
-
... on DayNightTariff { productCode standingCharge }
|
|
14
|
-
... on FourRateEvTariff { productCode standingCharge }
|
|
15
|
-
... on PrepayTariff { productCode standingCharge unitRate }
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
}`;
|
|
20
|
-
|
|
21
|
-
const CONSUMPTION_QUERY = `
|
|
22
|
-
query getElectricityConsumption($account: String!, $startAt: DateTime!, $timezone: String!) {
|
|
23
|
-
account(accountNumber: $account) {
|
|
24
|
-
properties {
|
|
25
|
-
electricityMeterPoints {
|
|
26
|
-
mpan
|
|
27
|
-
meters {
|
|
28
|
-
consumption(startAt: $startAt, last: 1, grouping: DAY, timezone: $timezone) {
|
|
29
|
-
edges { node { startAt endAt value } }
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}`;
|
|
36
|
-
|
|
37
|
-
function buildRatesQuery(account) {
|
|
38
|
-
return { query: RATES_QUERY, variables: { account } };
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
function buildConsumptionQuery(account, timezone) {
|
|
42
|
-
const tz = timezone || 'UTC';
|
|
43
|
-
const startAt = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
|
|
44
|
-
return { query: CONSUMPTION_QUERY, variables: { account, startAt, timezone: tz } };
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
// Tariff productCodes containing "OUTGOING" are export agreements (e.g. AGILE-OUTGOING-19-05-13).
|
|
48
|
-
// We surface both import and export tariff metadata as separate fields.
|
|
49
|
-
function parseRatesResponse(data) {
|
|
50
|
-
const all = (data.account || {}).electricityAgreements || [];
|
|
51
|
-
const isExport = (a) => ((a.tariff || {}).productCode || '').includes('OUTGOING');
|
|
52
|
-
const importAgreements = all.filter(a => !isExport(a));
|
|
53
|
-
const exportAgreements = all.filter(isExport);
|
|
54
|
-
|
|
55
|
-
const fieldsFor = (prefix, agreements) => {
|
|
56
|
-
const latest = agreements[0];
|
|
57
|
-
const tariff = (latest && latest.tariff) || {};
|
|
58
|
-
// Kraken Float scalars can serialise as strings — coerce defensively
|
|
59
|
-
return {
|
|
60
|
-
[`${prefix}_unit_rate`]: tariff.unitRate != null ? parseFloat(tariff.unitRate) : null,
|
|
61
|
-
[`${prefix}_standing_charge`]: tariff.standingCharge != null ? parseFloat(tariff.standingCharge) : null,
|
|
62
|
-
[`${prefix}_tariff_code`]: tariff.productCode || null,
|
|
63
|
-
[`${prefix}_valid_from`]: (latest && latest.validFrom) || null,
|
|
64
|
-
[`${prefix}_valid_to`]: (latest && latest.validTo) || null,
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
const
|
|
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
|
-
|
|
1
|
+
// lib/categories/electricity.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const RATES_QUERY = `
|
|
5
|
+
query getElectricityRates($account: String!) {
|
|
6
|
+
account(accountNumber: $account) {
|
|
7
|
+
electricityAgreements {
|
|
8
|
+
validFrom validTo
|
|
9
|
+
tariff {
|
|
10
|
+
... on TariffType { productCode standingCharge }
|
|
11
|
+
... on StandardTariff { productCode standingCharge unitRate }
|
|
12
|
+
... on HalfHourlyTariff { productCode standingCharge }
|
|
13
|
+
... on DayNightTariff { productCode standingCharge dayRate nightRate }
|
|
14
|
+
... on FourRateEvTariff { productCode standingCharge dayRate nightRate evDevicePeakRate evDeviceOffPeakRate }
|
|
15
|
+
... on PrepayTariff { productCode standingCharge unitRate }
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}`;
|
|
20
|
+
|
|
21
|
+
const CONSUMPTION_QUERY = `
|
|
22
|
+
query getElectricityConsumption($account: String!, $startAt: DateTime!, $timezone: String!) {
|
|
23
|
+
account(accountNumber: $account) {
|
|
24
|
+
properties {
|
|
25
|
+
electricityMeterPoints {
|
|
26
|
+
mpan
|
|
27
|
+
meters {
|
|
28
|
+
consumption(startAt: $startAt, last: 1, grouping: DAY, timezone: $timezone) {
|
|
29
|
+
edges { node { startAt endAt value } }
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}`;
|
|
36
|
+
|
|
37
|
+
function buildRatesQuery(account) {
|
|
38
|
+
return { query: RATES_QUERY, variables: { account } };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function buildConsumptionQuery(account, timezone) {
|
|
42
|
+
const tz = timezone || 'UTC';
|
|
43
|
+
const startAt = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000).toISOString();
|
|
44
|
+
return { query: CONSUMPTION_QUERY, variables: { account, startAt, timezone: tz } };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Tariff productCodes containing "OUTGOING" are export agreements (e.g. AGILE-OUTGOING-19-05-13).
|
|
48
|
+
// We surface both import and export tariff metadata as separate fields.
|
|
49
|
+
function parseRatesResponse(data) {
|
|
50
|
+
const all = (data.account || {}).electricityAgreements || [];
|
|
51
|
+
const isExport = (a) => ((a.tariff || {}).productCode || '').includes('OUTGOING');
|
|
52
|
+
const importAgreements = all.filter(a => !isExport(a));
|
|
53
|
+
const exportAgreements = all.filter(isExport);
|
|
54
|
+
|
|
55
|
+
const fieldsFor = (prefix, agreements) => {
|
|
56
|
+
const latest = agreements[0];
|
|
57
|
+
const tariff = (latest && latest.tariff) || {};
|
|
58
|
+
// Kraken Float scalars can serialise as strings — coerce defensively
|
|
59
|
+
return {
|
|
60
|
+
[`${prefix}_unit_rate`]: tariff.unitRate != null ? parseFloat(tariff.unitRate) : null,
|
|
61
|
+
[`${prefix}_standing_charge`]: tariff.standingCharge != null ? parseFloat(tariff.standingCharge) : null,
|
|
62
|
+
[`${prefix}_tariff_code`]: tariff.productCode || null,
|
|
63
|
+
[`${prefix}_valid_from`]: (latest && latest.validFrom) || null,
|
|
64
|
+
[`${prefix}_valid_to`]: (latest && latest.validTo) || null,
|
|
65
|
+
// v1.5 rate-band fields — null on tariffs that don't expose them
|
|
66
|
+
[`${prefix}_day_rate`]: tariff.dayRate != null ? parseFloat(tariff.dayRate) : null,
|
|
67
|
+
[`${prefix}_night_rate`]: tariff.nightRate != null ? parseFloat(tariff.nightRate) : null,
|
|
68
|
+
[`${prefix}_ev_peak_rate`]: tariff.evDevicePeakRate != null ? parseFloat(tariff.evDevicePeakRate) : null,
|
|
69
|
+
[`${prefix}_ev_off_peak_rate`]: tariff.evDeviceOffPeakRate != null ? parseFloat(tariff.evDeviceOffPeakRate) : null,
|
|
70
|
+
};
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
return {
|
|
74
|
+
...fieldsFor('electricity', importAgreements),
|
|
75
|
+
...fieldsFor('electricity_export', exportAgreements),
|
|
76
|
+
electricity_rates_error: null
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// Picks the latest consumption edge for a given MPAN out of the query response.
|
|
81
|
+
function _findEntryForMpan(data, mpan) {
|
|
82
|
+
if (!mpan) return null;
|
|
83
|
+
const points = ((data.account || {}).properties || [])[0];
|
|
84
|
+
const meterPoints = points ? (points.electricityMeterPoints || []) : [];
|
|
85
|
+
const mp = meterPoints.find(p => p.mpan === mpan);
|
|
86
|
+
if (!mp) return null;
|
|
87
|
+
const meter = (mp.meters || [])[0];
|
|
88
|
+
const edges = ((meter && meter.consumption) || {}).edges || [];
|
|
89
|
+
return edges.length > 0 ? edges[edges.length - 1].node : null;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Parses both import and export consumption from one response, given both MPANs.
|
|
93
|
+
// Either MPAN may be null on accounts without that direction — the corresponding
|
|
94
|
+
// fields will be null without affecting the other direction.
|
|
95
|
+
function parseConsumptionResponse(data, importMpan, exportMpan) {
|
|
96
|
+
const importEntry = _findEntryForMpan(data, importMpan);
|
|
97
|
+
const exportEntry = _findEntryForMpan(data, exportMpan);
|
|
98
|
+
return {
|
|
99
|
+
electricity_consumption_kwh: importEntry ? parseFloat(importEntry.value) : null,
|
|
100
|
+
electricity_consumption_cost: null,
|
|
101
|
+
electricity_consumption_from: importEntry ? importEntry.startAt : null,
|
|
102
|
+
electricity_consumption_to: importEntry ? importEntry.endAt : null,
|
|
103
|
+
electricity_export_consumption_kwh: exportEntry ? parseFloat(exportEntry.value) : null,
|
|
104
|
+
electricity_export_consumption_from: exportEntry ? exportEntry.startAt : null,
|
|
105
|
+
electricity_export_consumption_to: exportEntry ? exportEntry.endAt : null,
|
|
106
|
+
electricity_consumption_error: null
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const defaultData = {
|
|
111
|
+
electricity_unit_rate: null,
|
|
112
|
+
electricity_standing_charge: null,
|
|
113
|
+
electricity_tariff_code: null,
|
|
114
|
+
electricity_valid_from: null,
|
|
115
|
+
electricity_valid_to: null,
|
|
116
|
+
electricity_day_rate: null,
|
|
117
|
+
electricity_night_rate: null,
|
|
118
|
+
electricity_ev_peak_rate: null,
|
|
119
|
+
electricity_ev_off_peak_rate: null,
|
|
120
|
+
electricity_export_unit_rate: null,
|
|
121
|
+
electricity_export_standing_charge: null,
|
|
122
|
+
electricity_export_tariff_code: null,
|
|
123
|
+
electricity_export_valid_from: null,
|
|
124
|
+
electricity_export_valid_to: null,
|
|
125
|
+
electricity_export_day_rate: null,
|
|
126
|
+
electricity_export_night_rate: null,
|
|
127
|
+
electricity_export_ev_peak_rate: null,
|
|
128
|
+
electricity_export_ev_off_peak_rate: null,
|
|
129
|
+
electricity_rates_error: null,
|
|
130
|
+
electricity_consumption_kwh: null,
|
|
131
|
+
electricity_consumption_cost: null,
|
|
132
|
+
electricity_consumption_from: null,
|
|
133
|
+
electricity_consumption_to: null,
|
|
134
|
+
electricity_export_consumption_kwh: null,
|
|
135
|
+
electricity_export_consumption_from: null,
|
|
136
|
+
electricity_export_consumption_to: null,
|
|
137
|
+
electricity_consumption_error: null
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
module.exports = {
|
|
141
|
+
buildRatesQuery,
|
|
142
|
+
buildConsumptionQuery,
|
|
143
|
+
parseRatesResponse,
|
|
144
|
+
parseConsumptionResponse,
|
|
145
|
+
defaultData
|
|
146
|
+
};
|
|
@@ -1,36 +1,36 @@
|
|
|
1
|
-
// lib/categories/flex-planned-dispatches.js
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
const QUERY = `
|
|
5
|
-
query getFlexPlannedDispatches($deviceId: String!) {
|
|
6
|
-
flexPlannedDispatches(deviceId: $deviceId) {
|
|
7
|
-
start end type energyAddedKwh
|
|
8
|
-
}
|
|
9
|
-
}`;
|
|
10
|
-
|
|
11
|
-
function buildQuery(deviceId) {
|
|
12
|
-
return { query: QUERY, variables: { deviceId } };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function parseResponse(data) {
|
|
16
|
-
const dispatches = data.flexPlannedDispatches || [];
|
|
17
|
-
return {
|
|
18
|
-
flex_planned_dispatches: dispatches.map(d => ({
|
|
19
|
-
start: d.start,
|
|
20
|
-
end: d.end,
|
|
21
|
-
type: d.type || null,
|
|
22
|
-
energy_kwh: d.energyAddedKwh !== null && d.energyAddedKwh !== undefined
|
|
23
|
-
? parseFloat(d.energyAddedKwh) : null
|
|
24
|
-
})),
|
|
25
|
-
flex_planned_dispatches_count: dispatches.length,
|
|
26
|
-
flex_planned_dispatches_error: null
|
|
27
|
-
};
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
const defaultData = {
|
|
31
|
-
flex_planned_dispatches: [],
|
|
32
|
-
flex_planned_dispatches_count: 0,
|
|
33
|
-
flex_planned_dispatches_error: null
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
module.exports = { buildQuery, parseResponse, defaultData };
|
|
1
|
+
// lib/categories/flex-planned-dispatches.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const QUERY = `
|
|
5
|
+
query getFlexPlannedDispatches($deviceId: String!) {
|
|
6
|
+
flexPlannedDispatches(deviceId: $deviceId) {
|
|
7
|
+
start end type energyAddedKwh
|
|
8
|
+
}
|
|
9
|
+
}`;
|
|
10
|
+
|
|
11
|
+
function buildQuery(deviceId) {
|
|
12
|
+
return { query: QUERY, variables: { deviceId } };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseResponse(data) {
|
|
16
|
+
const dispatches = data.flexPlannedDispatches || [];
|
|
17
|
+
return {
|
|
18
|
+
flex_planned_dispatches: dispatches.map(d => ({
|
|
19
|
+
start: d.start,
|
|
20
|
+
end: d.end,
|
|
21
|
+
type: d.type || null,
|
|
22
|
+
energy_kwh: d.energyAddedKwh !== null && d.energyAddedKwh !== undefined
|
|
23
|
+
? parseFloat(d.energyAddedKwh) : null
|
|
24
|
+
})),
|
|
25
|
+
flex_planned_dispatches_count: dispatches.length,
|
|
26
|
+
flex_planned_dispatches_error: null
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const defaultData = {
|
|
31
|
+
flex_planned_dispatches: [],
|
|
32
|
+
flex_planned_dispatches_count: 0,
|
|
33
|
+
flex_planned_dispatches_error: null
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -1,61 +1,61 @@
|
|
|
1
|
-
// lib/categories/free-electricity.js
|
|
2
|
-
// Detects upcoming and active "Free Electricity" sessions (0p/kWh windows).
|
|
3
|
-
// Requires account number and MPAN from account discovery.
|
|
4
|
-
'use strict';
|
|
5
|
-
|
|
6
|
-
const QUERY = `
|
|
7
|
-
query getFreeElectricity($account: String!, $mpan: String!) {
|
|
8
|
-
customerFlexibilityCampaignEvents(
|
|
9
|
-
accountNumber: $account,
|
|
10
|
-
supplyPointIdentifier: $mpan,
|
|
11
|
-
campaignSlug: "free_electricity",
|
|
12
|
-
first: 50
|
|
13
|
-
) {
|
|
14
|
-
edges {
|
|
15
|
-
node {
|
|
16
|
-
code
|
|
17
|
-
startAt
|
|
18
|
-
endAt
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}`;
|
|
23
|
-
|
|
24
|
-
function buildQuery(account, mpan) {
|
|
25
|
-
return { query: QUERY, variables: { account, mpan } };
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
function parseResponse(data) {
|
|
29
|
-
const now = new Date();
|
|
30
|
-
|
|
31
|
-
const edges = ((data.customerFlexibilityCampaignEvents || {}).edges) || [];
|
|
32
|
-
const sessions = edges.map(e => ({ startAt: e.node.startAt, endAt: e.node.endAt }));
|
|
33
|
-
|
|
34
|
-
// Filter to sessions not yet ended, sort by startAt ascending, take the first
|
|
35
|
-
const future = sessions
|
|
36
|
-
.filter(s => new Date(s.endAt) > now)
|
|
37
|
-
.sort((a, b) => new Date(a.startAt) - new Date(b.startAt));
|
|
38
|
-
|
|
39
|
-
const next = future[0] || null;
|
|
40
|
-
|
|
41
|
-
const active = next !== null && new Date(next.startAt) <= now && new Date(next.endAt) > now;
|
|
42
|
-
const available = next !== null;
|
|
43
|
-
|
|
44
|
-
return {
|
|
45
|
-
free_electricity_active: active,
|
|
46
|
-
free_electricity_available: available,
|
|
47
|
-
free_electricity_start: next ? next.startAt : null,
|
|
48
|
-
free_electricity_end: next ? next.endAt : null,
|
|
49
|
-
free_electricity_error: null
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
const defaultData = {
|
|
54
|
-
free_electricity_active: null,
|
|
55
|
-
free_electricity_available: null,
|
|
56
|
-
free_electricity_start: null,
|
|
57
|
-
free_electricity_end: null,
|
|
58
|
-
free_electricity_error: null
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
module.exports = { buildQuery, parseResponse, defaultData };
|
|
1
|
+
// lib/categories/free-electricity.js
|
|
2
|
+
// Detects upcoming and active "Free Electricity" sessions (0p/kWh windows).
|
|
3
|
+
// Requires account number and MPAN from account discovery.
|
|
4
|
+
'use strict';
|
|
5
|
+
|
|
6
|
+
const QUERY = `
|
|
7
|
+
query getFreeElectricity($account: String!, $mpan: String!) {
|
|
8
|
+
customerFlexibilityCampaignEvents(
|
|
9
|
+
accountNumber: $account,
|
|
10
|
+
supplyPointIdentifier: $mpan,
|
|
11
|
+
campaignSlug: "free_electricity",
|
|
12
|
+
first: 50
|
|
13
|
+
) {
|
|
14
|
+
edges {
|
|
15
|
+
node {
|
|
16
|
+
code
|
|
17
|
+
startAt
|
|
18
|
+
endAt
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}`;
|
|
23
|
+
|
|
24
|
+
function buildQuery(account, mpan) {
|
|
25
|
+
return { query: QUERY, variables: { account, mpan } };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function parseResponse(data) {
|
|
29
|
+
const now = new Date();
|
|
30
|
+
|
|
31
|
+
const edges = ((data.customerFlexibilityCampaignEvents || {}).edges) || [];
|
|
32
|
+
const sessions = edges.map(e => ({ startAt: e.node.startAt, endAt: e.node.endAt }));
|
|
33
|
+
|
|
34
|
+
// Filter to sessions not yet ended, sort by startAt ascending, take the first
|
|
35
|
+
const future = sessions
|
|
36
|
+
.filter(s => new Date(s.endAt) > now)
|
|
37
|
+
.sort((a, b) => new Date(a.startAt) - new Date(b.startAt));
|
|
38
|
+
|
|
39
|
+
const next = future[0] || null;
|
|
40
|
+
|
|
41
|
+
const active = next !== null && new Date(next.startAt) <= now && new Date(next.endAt) > now;
|
|
42
|
+
const available = next !== null;
|
|
43
|
+
|
|
44
|
+
return {
|
|
45
|
+
free_electricity_active: active,
|
|
46
|
+
free_electricity_available: available,
|
|
47
|
+
free_electricity_start: next ? next.startAt : null,
|
|
48
|
+
free_electricity_end: next ? next.endAt : null,
|
|
49
|
+
free_electricity_error: null
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const defaultData = {
|
|
54
|
+
free_electricity_active: null,
|
|
55
|
+
free_electricity_available: null,
|
|
56
|
+
free_electricity_start: null,
|
|
57
|
+
free_electricity_end: null,
|
|
58
|
+
free_electricity_error: null
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|