node-red-contrib-octopus-intelligent 1.5.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 -442
- package/README.md +1004 -980
- package/examples/basic-monitoring.json +1 -1
- package/examples/ha-dashboard.yaml +602 -599
- package/examples/mqtt-integration.json +1 -1
- package/examples/node-red-dashboard-flow.json +1287 -1199
- 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 -127
- package/lib/categories/completed-dispatches.js +35 -35
- package/lib/categories/electricity.js +146 -146
- 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 -22
- package/lib/payload.js +89 -88
- 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 -38
- package/octopus-intelligent.html +218 -205
- package/octopus-intelligent.js +1687 -1807
- package/package.json +54 -54
|
@@ -1 +1 @@
|
|
|
1
|
-
[{"id":"1","type":"inject","name":"Set 85% by 07:30","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"topic":"","payload":"{\"set_limit\":85,\"set_time\":\"07:30\"}","payloadType":"json","x":150,"y":140,"wires":[["2"]]},{"id":"2","type":"octopus-intelligent","name":"Octopus Intelligent","accountNumber":"A-XXXXXXXX","refreshInterval":"5","enableMqtt":true,"broker":"mqtt-broker","x":380,"y":140,"wires":[["3"]]},{"id":"3","type":"debug","name":"Confirmation","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":140,"wires":[]}]
|
|
1
|
+
[{"id":"1","type":"inject","name":"Set 85% by 07:30","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"topic":"","payload":"{\"set_limit\":85,\"set_time\":\"07:30\"}","payloadType":"json","x":150,"y":140,"wires":[["2"]]},{"id":"2","type":"octopus-intelligent","name":"Octopus Intelligent","accountNumber":"A-XXXXXXXX","refreshInterval":"5","enableMqtt":true,"broker":"mqtt-broker","x":380,"y":140,"wires":[["3"]]},{"id":"3","type":"debug","name":"Confirmation","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":610,"y":140,"wires":[]}]
|
package/lib/api-metrics.js
CHANGED
|
@@ -1,31 +1,31 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const HOURLY_LIMIT = 50000;
|
|
3
|
-
const ONE_HOUR_MS = 60 * 60 * 1000;
|
|
4
|
-
|
|
5
|
-
function createApiMetrics() {
|
|
6
|
-
const history = [];
|
|
7
|
-
|
|
8
|
-
function recordPoll(complexity) {
|
|
9
|
-
history.push({ timestamp: Date.now(), complexity: complexity || 0 });
|
|
10
|
-
pruneOld();
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
function pruneOld() {
|
|
14
|
-
const cutoff = Date.now() - ONE_HOUR_MS;
|
|
15
|
-
while (history.length > 0 && history[0].timestamp < cutoff) history.shift();
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function getMetrics() {
|
|
19
|
-
pruneOld();
|
|
20
|
-
const total = history.reduce((sum, e) => sum + e.complexity, 0);
|
|
21
|
-
return {
|
|
22
|
-
requests_last_hour: history.length,
|
|
23
|
-
complexity_last_hour: total,
|
|
24
|
-
complexity_percent: ((total / HOURLY_LIMIT) * 100).toFixed(1)
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return { recordPoll, getMetrics, _history: history };
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
module.exports = { createApiMetrics };
|
|
1
|
+
'use strict';
|
|
2
|
+
const HOURLY_LIMIT = 50000;
|
|
3
|
+
const ONE_HOUR_MS = 60 * 60 * 1000;
|
|
4
|
+
|
|
5
|
+
function createApiMetrics() {
|
|
6
|
+
const history = [];
|
|
7
|
+
|
|
8
|
+
function recordPoll(complexity) {
|
|
9
|
+
history.push({ timestamp: Date.now(), complexity: complexity || 0 });
|
|
10
|
+
pruneOld();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function pruneOld() {
|
|
14
|
+
const cutoff = Date.now() - ONE_HOUR_MS;
|
|
15
|
+
while (history.length > 0 && history[0].timestamp < cutoff) history.shift();
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function getMetrics() {
|
|
19
|
+
pruneOld();
|
|
20
|
+
const total = history.reduce((sum, e) => sum + e.complexity, 0);
|
|
21
|
+
return {
|
|
22
|
+
requests_last_hour: history.length,
|
|
23
|
+
complexity_last_hour: total,
|
|
24
|
+
complexity_percent: ((total / HOURLY_LIMIT) * 100).toFixed(1)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
return { recordPoll, getMetrics, _history: history };
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = { createApiMetrics };
|
package/lib/auth.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
const { graphqlPost } = require('./graphql');
|
|
3
|
-
|
|
4
|
-
async function obtainToken(apiKey) {
|
|
5
|
-
const response = await graphqlPost({
|
|
6
|
-
query: `mutation obtainToken($input: ObtainJSONWebTokenInput!) {
|
|
7
|
-
obtainKrakenToken(input: $input) { token }
|
|
8
|
-
}`,
|
|
9
|
-
variables: { input: { APIKey: apiKey } }
|
|
10
|
-
});
|
|
11
|
-
if (response.data.errors) {
|
|
12
|
-
throw new Error(`Auth failed: ${JSON.stringify(response.data.errors)}`);
|
|
13
|
-
}
|
|
14
|
-
if (!response.data.data || !response.data.data.obtainKrakenToken) {
|
|
15
|
-
throw new Error('Auth response missing token data');
|
|
16
|
-
}
|
|
17
|
-
return response.data.data.obtainKrakenToken.token;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
module.exports = { obtainToken };
|
|
1
|
+
'use strict';
|
|
2
|
+
const { graphqlPost } = require('./graphql');
|
|
3
|
+
|
|
4
|
+
async function obtainToken(apiKey) {
|
|
5
|
+
const response = await graphqlPost({
|
|
6
|
+
query: `mutation obtainToken($input: ObtainJSONWebTokenInput!) {
|
|
7
|
+
obtainKrakenToken(input: $input) { token }
|
|
8
|
+
}`,
|
|
9
|
+
variables: { input: { APIKey: apiKey } }
|
|
10
|
+
});
|
|
11
|
+
if (response.data.errors) {
|
|
12
|
+
throw new Error(`Auth failed: ${JSON.stringify(response.data.errors)}`);
|
|
13
|
+
}
|
|
14
|
+
if (!response.data.data || !response.data.data.obtainKrakenToken) {
|
|
15
|
+
throw new Error('Auth response missing token data');
|
|
16
|
+
}
|
|
17
|
+
return response.data.data.obtainKrakenToken.token;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
module.exports = { obtainToken };
|
|
@@ -1,32 +1,32 @@
|
|
|
1
|
-
// lib/categories/account.js
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
const QUERY = `
|
|
5
|
-
query getAccountBalance($account: String!) {
|
|
6
|
-
account(accountNumber: $account) {
|
|
7
|
-
balance
|
|
8
|
-
}
|
|
9
|
-
}`;
|
|
10
|
-
|
|
11
|
-
function buildQuery(account) {
|
|
12
|
-
return { query: QUERY, variables: { account } };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function parseResponse(data) {
|
|
16
|
-
// Kraken numeric scalars can serialise as strings — coerce defensively
|
|
17
|
-
const raw = ((data.account || {}).balance) ?? null;
|
|
18
|
-
const balancePence = raw !== null ? parseFloat(raw) : null;
|
|
19
|
-
return {
|
|
20
|
-
account_balance_pence: balancePence,
|
|
21
|
-
account_balance_pounds: balancePence !== null ? parseFloat((balancePence / 100).toFixed(2)) : null,
|
|
22
|
-
account_error: null
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const defaultData = {
|
|
27
|
-
account_balance_pence: null,
|
|
28
|
-
account_balance_pounds: null,
|
|
29
|
-
account_error: null
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
module.exports = { buildQuery, parseResponse, defaultData };
|
|
1
|
+
// lib/categories/account.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const QUERY = `
|
|
5
|
+
query getAccountBalance($account: String!) {
|
|
6
|
+
account(accountNumber: $account) {
|
|
7
|
+
balance
|
|
8
|
+
}
|
|
9
|
+
}`;
|
|
10
|
+
|
|
11
|
+
function buildQuery(account) {
|
|
12
|
+
return { query: QUERY, variables: { account } };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseResponse(data) {
|
|
16
|
+
// Kraken numeric scalars can serialise as strings — coerce defensively
|
|
17
|
+
const raw = ((data.account || {}).balance) ?? null;
|
|
18
|
+
const balancePence = raw !== null ? parseFloat(raw) : null;
|
|
19
|
+
return {
|
|
20
|
+
account_balance_pence: balancePence,
|
|
21
|
+
account_balance_pounds: balancePence !== null ? parseFloat((balancePence / 100).toFixed(2)) : null,
|
|
22
|
+
account_error: null
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const defaultData = {
|
|
27
|
+
account_balance_pence: null,
|
|
28
|
+
account_balance_pounds: null,
|
|
29
|
+
account_error: null
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -1,127 +1,127 @@
|
|
|
1
|
-
// lib/categories/applicable-rates.js
|
|
2
|
-
// Half-hourly time-of-use rates for a given electricity supply point.
|
|
3
|
-
// Requires MPAN (or MPRN). Returns 30-min slots for the next 24h.
|
|
4
|
-
//
|
|
5
|
-
// Same parser handles import and export — pass { fieldPrefix } to parseResponse
|
|
6
|
-
// and exportFields() to swap the output naming to electricity_export_rate_*.
|
|
7
|
-
'use strict';
|
|
8
|
-
|
|
9
|
-
const QUERY = `
|
|
10
|
-
query getApplicableRates($account: String!, $mpxn: String!, $startAt: DateTime!, $endAt: DateTime!) {
|
|
11
|
-
applicableRates(accountNumber: $account, mpxn: $mpxn, startAt: $startAt, endAt: $endAt, first: 48) {
|
|
12
|
-
edges { node { validFrom validTo value } }
|
|
13
|
-
}
|
|
14
|
-
}`;
|
|
15
|
-
|
|
16
|
-
function buildQuery(account, mpan) {
|
|
17
|
-
const startAt = new Date().toISOString();
|
|
18
|
-
const endAt = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString();
|
|
19
|
-
return { query: QUERY, variables: { account, mpxn: mpan, startAt, endAt } };
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function _stats(slots) {
|
|
23
|
-
const values = slots
|
|
24
|
-
.map(s => s.value_pence_per_kwh)
|
|
25
|
-
.filter(v => v !== null && v !== undefined);
|
|
26
|
-
if (values.length === 0) {
|
|
27
|
-
return { min: null, max: null, median: null, avg: null };
|
|
28
|
-
}
|
|
29
|
-
const sorted = values.slice().sort((a, b) => a - b);
|
|
30
|
-
const min = sorted[0];
|
|
31
|
-
const max = sorted[sorted.length - 1];
|
|
32
|
-
const sum = values.reduce((a, v) => a + v, 0);
|
|
33
|
-
const avg = parseFloat((sum / values.length).toFixed(4));
|
|
34
|
-
let median;
|
|
35
|
-
if (sorted.length % 2 === 1) {
|
|
36
|
-
median = sorted[Math.floor(sorted.length / 2)];
|
|
37
|
-
} else {
|
|
38
|
-
const mid = sorted.length / 2;
|
|
39
|
-
median = parseFloat(((sorted[mid - 1] + sorted[mid]) / 2).toFixed(4));
|
|
40
|
-
}
|
|
41
|
-
return { min, max, median, avg };
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function _emit(prefix, slots, current, prev, next, stats) {
|
|
45
|
-
const pence = current ? current.value_pence_per_kwh : null;
|
|
46
|
-
const prevPence = prev ? prev.value_pence_per_kwh : null;
|
|
47
|
-
const nextPence = next ? next.value_pence_per_kwh : null;
|
|
48
|
-
const out = {};
|
|
49
|
-
out[`${prefix}`] = slots;
|
|
50
|
-
out[`${prefix}_count`] = slots.length;
|
|
51
|
-
out[`${prefix}_current_pence`] = pence;
|
|
52
|
-
out[`${prefix}_current_gbp`] = pence !== null ? parseFloat((pence / 100).toFixed(4)) : null;
|
|
53
|
-
out[`${prefix}_prev_pence`] = prevPence;
|
|
54
|
-
out[`${prefix}_prev_gbp`] = prevPence !== null ? parseFloat((prevPence / 100).toFixed(4)) : null;
|
|
55
|
-
out[`${prefix}_prev_to`] = prev ? prev.valid_to : null;
|
|
56
|
-
out[`${prefix}_next_pence`] = nextPence;
|
|
57
|
-
out[`${prefix}_next_gbp`] = nextPence !== null ? parseFloat((nextPence / 100).toFixed(4)) : null;
|
|
58
|
-
out[`${prefix}_next_from`] = next ? next.valid_from : null;
|
|
59
|
-
out[`${prefix}_error`] = null;
|
|
60
|
-
// v1.5 stats
|
|
61
|
-
out[`${prefix}_min_pence`] = stats.min;
|
|
62
|
-
out[`${prefix}_max_pence`] = stats.max;
|
|
63
|
-
out[`${prefix}_median_pence`] = stats.median;
|
|
64
|
-
out[`${prefix}_avg_pence`] = stats.avg;
|
|
65
|
-
return out;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
function parseResponse(data, options) {
|
|
69
|
-
const prefix = (options && options.fieldPrefix) || 'applicable_rates';
|
|
70
|
-
const edges = ((data.applicableRates || {}).edges) || [];
|
|
71
|
-
const slots = edges.map(e => ({
|
|
72
|
-
valid_from: e.node.validFrom,
|
|
73
|
-
valid_to: e.node.validTo,
|
|
74
|
-
value_pence_per_kwh: e.node.value !== null ? parseFloat(e.node.value) : null
|
|
75
|
-
}));
|
|
76
|
-
|
|
77
|
-
slots.sort((a, b) => new Date(a.valid_from) - new Date(b.valid_from));
|
|
78
|
-
|
|
79
|
-
const now = Date.now();
|
|
80
|
-
const currentIdx = slots.findIndex(s => {
|
|
81
|
-
const from = new Date(s.valid_from).getTime();
|
|
82
|
-
const to = new Date(s.valid_to).getTime();
|
|
83
|
-
return now >= from && now < to;
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
const current = currentIdx !== -1 ? slots[currentIdx] : null;
|
|
87
|
-
const prev = currentIdx > 0 ? slots[currentIdx - 1] : null;
|
|
88
|
-
const next = (currentIdx !== -1 && currentIdx < slots.length - 1) ? slots[currentIdx + 1] : null;
|
|
89
|
-
|
|
90
|
-
const stats = _stats(slots);
|
|
91
|
-
return _emit(prefix, slots, current, prev, next, stats);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
const defaultData = {
|
|
95
|
-
applicable_rates: [],
|
|
96
|
-
applicable_rates_count: 0,
|
|
97
|
-
applicable_rates_current_pence: null,
|
|
98
|
-
applicable_rates_current_gbp: null,
|
|
99
|
-
applicable_rates_prev_pence: null,
|
|
100
|
-
applicable_rates_prev_gbp: null,
|
|
101
|
-
applicable_rates_prev_to: null,
|
|
102
|
-
applicable_rates_next_pence: null,
|
|
103
|
-
applicable_rates_next_gbp: null,
|
|
104
|
-
applicable_rates_next_from: null,
|
|
105
|
-
applicable_rates_error: null,
|
|
106
|
-
applicable_rates_min_pence: null,
|
|
107
|
-
applicable_rates_max_pence: null,
|
|
108
|
-
applicable_rates_median_pence: null,
|
|
109
|
-
applicable_rates_avg_pence: null,
|
|
110
|
-
electricity_export_rate: [],
|
|
111
|
-
electricity_export_rate_count: 0,
|
|
112
|
-
electricity_export_rate_current_pence: null,
|
|
113
|
-
electricity_export_rate_current_gbp: null,
|
|
114
|
-
electricity_export_rate_prev_pence: null,
|
|
115
|
-
electricity_export_rate_prev_gbp: null,
|
|
116
|
-
electricity_export_rate_prev_to: null,
|
|
117
|
-
electricity_export_rate_next_pence: null,
|
|
118
|
-
electricity_export_rate_next_gbp: null,
|
|
119
|
-
electricity_export_rate_next_from: null,
|
|
120
|
-
electricity_export_rate_error: null,
|
|
121
|
-
electricity_export_rate_min_pence: null,
|
|
122
|
-
electricity_export_rate_max_pence: null,
|
|
123
|
-
electricity_export_rate_median_pence: null,
|
|
124
|
-
electricity_export_rate_avg_pence: null
|
|
125
|
-
};
|
|
126
|
-
|
|
127
|
-
module.exports = { buildQuery, parseResponse, defaultData };
|
|
1
|
+
// lib/categories/applicable-rates.js
|
|
2
|
+
// Half-hourly time-of-use rates for a given electricity supply point.
|
|
3
|
+
// Requires MPAN (or MPRN). Returns 30-min slots for the next 24h.
|
|
4
|
+
//
|
|
5
|
+
// Same parser handles import and export — pass { fieldPrefix } to parseResponse
|
|
6
|
+
// and exportFields() to swap the output naming to electricity_export_rate_*.
|
|
7
|
+
'use strict';
|
|
8
|
+
|
|
9
|
+
const QUERY = `
|
|
10
|
+
query getApplicableRates($account: String!, $mpxn: String!, $startAt: DateTime!, $endAt: DateTime!) {
|
|
11
|
+
applicableRates(accountNumber: $account, mpxn: $mpxn, startAt: $startAt, endAt: $endAt, first: 48) {
|
|
12
|
+
edges { node { validFrom validTo value } }
|
|
13
|
+
}
|
|
14
|
+
}`;
|
|
15
|
+
|
|
16
|
+
function buildQuery(account, mpan) {
|
|
17
|
+
const startAt = new Date().toISOString();
|
|
18
|
+
const endAt = new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString();
|
|
19
|
+
return { query: QUERY, variables: { account, mpxn: mpan, startAt, endAt } };
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function _stats(slots) {
|
|
23
|
+
const values = slots
|
|
24
|
+
.map(s => s.value_pence_per_kwh)
|
|
25
|
+
.filter(v => v !== null && v !== undefined);
|
|
26
|
+
if (values.length === 0) {
|
|
27
|
+
return { min: null, max: null, median: null, avg: null };
|
|
28
|
+
}
|
|
29
|
+
const sorted = values.slice().sort((a, b) => a - b);
|
|
30
|
+
const min = sorted[0];
|
|
31
|
+
const max = sorted[sorted.length - 1];
|
|
32
|
+
const sum = values.reduce((a, v) => a + v, 0);
|
|
33
|
+
const avg = parseFloat((sum / values.length).toFixed(4));
|
|
34
|
+
let median;
|
|
35
|
+
if (sorted.length % 2 === 1) {
|
|
36
|
+
median = sorted[Math.floor(sorted.length / 2)];
|
|
37
|
+
} else {
|
|
38
|
+
const mid = sorted.length / 2;
|
|
39
|
+
median = parseFloat(((sorted[mid - 1] + sorted[mid]) / 2).toFixed(4));
|
|
40
|
+
}
|
|
41
|
+
return { min, max, median, avg };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function _emit(prefix, slots, current, prev, next, stats) {
|
|
45
|
+
const pence = current ? current.value_pence_per_kwh : null;
|
|
46
|
+
const prevPence = prev ? prev.value_pence_per_kwh : null;
|
|
47
|
+
const nextPence = next ? next.value_pence_per_kwh : null;
|
|
48
|
+
const out = {};
|
|
49
|
+
out[`${prefix}`] = slots;
|
|
50
|
+
out[`${prefix}_count`] = slots.length;
|
|
51
|
+
out[`${prefix}_current_pence`] = pence;
|
|
52
|
+
out[`${prefix}_current_gbp`] = pence !== null ? parseFloat((pence / 100).toFixed(4)) : null;
|
|
53
|
+
out[`${prefix}_prev_pence`] = prevPence;
|
|
54
|
+
out[`${prefix}_prev_gbp`] = prevPence !== null ? parseFloat((prevPence / 100).toFixed(4)) : null;
|
|
55
|
+
out[`${prefix}_prev_to`] = prev ? prev.valid_to : null;
|
|
56
|
+
out[`${prefix}_next_pence`] = nextPence;
|
|
57
|
+
out[`${prefix}_next_gbp`] = nextPence !== null ? parseFloat((nextPence / 100).toFixed(4)) : null;
|
|
58
|
+
out[`${prefix}_next_from`] = next ? next.valid_from : null;
|
|
59
|
+
out[`${prefix}_error`] = null;
|
|
60
|
+
// v1.5 stats
|
|
61
|
+
out[`${prefix}_min_pence`] = stats.min;
|
|
62
|
+
out[`${prefix}_max_pence`] = stats.max;
|
|
63
|
+
out[`${prefix}_median_pence`] = stats.median;
|
|
64
|
+
out[`${prefix}_avg_pence`] = stats.avg;
|
|
65
|
+
return out;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function parseResponse(data, options) {
|
|
69
|
+
const prefix = (options && options.fieldPrefix) || 'applicable_rates';
|
|
70
|
+
const edges = ((data.applicableRates || {}).edges) || [];
|
|
71
|
+
const slots = edges.map(e => ({
|
|
72
|
+
valid_from: e.node.validFrom,
|
|
73
|
+
valid_to: e.node.validTo,
|
|
74
|
+
value_pence_per_kwh: e.node.value !== null ? parseFloat(e.node.value) : null
|
|
75
|
+
}));
|
|
76
|
+
|
|
77
|
+
slots.sort((a, b) => new Date(a.valid_from) - new Date(b.valid_from));
|
|
78
|
+
|
|
79
|
+
const now = Date.now();
|
|
80
|
+
const currentIdx = slots.findIndex(s => {
|
|
81
|
+
const from = new Date(s.valid_from).getTime();
|
|
82
|
+
const to = new Date(s.valid_to).getTime();
|
|
83
|
+
return now >= from && now < to;
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const current = currentIdx !== -1 ? slots[currentIdx] : null;
|
|
87
|
+
const prev = currentIdx > 0 ? slots[currentIdx - 1] : null;
|
|
88
|
+
const next = (currentIdx !== -1 && currentIdx < slots.length - 1) ? slots[currentIdx + 1] : null;
|
|
89
|
+
|
|
90
|
+
const stats = _stats(slots);
|
|
91
|
+
return _emit(prefix, slots, current, prev, next, stats);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const defaultData = {
|
|
95
|
+
applicable_rates: [],
|
|
96
|
+
applicable_rates_count: 0,
|
|
97
|
+
applicable_rates_current_pence: null,
|
|
98
|
+
applicable_rates_current_gbp: null,
|
|
99
|
+
applicable_rates_prev_pence: null,
|
|
100
|
+
applicable_rates_prev_gbp: null,
|
|
101
|
+
applicable_rates_prev_to: null,
|
|
102
|
+
applicable_rates_next_pence: null,
|
|
103
|
+
applicable_rates_next_gbp: null,
|
|
104
|
+
applicable_rates_next_from: null,
|
|
105
|
+
applicable_rates_error: null,
|
|
106
|
+
applicable_rates_min_pence: null,
|
|
107
|
+
applicable_rates_max_pence: null,
|
|
108
|
+
applicable_rates_median_pence: null,
|
|
109
|
+
applicable_rates_avg_pence: null,
|
|
110
|
+
electricity_export_rate: [],
|
|
111
|
+
electricity_export_rate_count: 0,
|
|
112
|
+
electricity_export_rate_current_pence: null,
|
|
113
|
+
electricity_export_rate_current_gbp: null,
|
|
114
|
+
electricity_export_rate_prev_pence: null,
|
|
115
|
+
electricity_export_rate_prev_gbp: null,
|
|
116
|
+
electricity_export_rate_prev_to: null,
|
|
117
|
+
electricity_export_rate_next_pence: null,
|
|
118
|
+
electricity_export_rate_next_gbp: null,
|
|
119
|
+
electricity_export_rate_next_from: null,
|
|
120
|
+
electricity_export_rate_error: null,
|
|
121
|
+
electricity_export_rate_min_pence: null,
|
|
122
|
+
electricity_export_rate_max_pence: null,
|
|
123
|
+
electricity_export_rate_median_pence: null,
|
|
124
|
+
electricity_export_rate_avg_pence: null
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
// lib/categories/completed-dispatches.js
|
|
2
|
-
'use strict';
|
|
3
|
-
|
|
4
|
-
const QUERY = `
|
|
5
|
-
query getCompletedDispatches($account: String!) {
|
|
6
|
-
completedDispatches(accountNumber: $account) {
|
|
7
|
-
start end delta meta { source location }
|
|
8
|
-
}
|
|
9
|
-
}`;
|
|
10
|
-
|
|
11
|
-
function buildQuery(account) {
|
|
12
|
-
return { query: QUERY, variables: { account } };
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function parseResponse(data) {
|
|
16
|
-
const dispatches = data.completedDispatches || [];
|
|
17
|
-
return {
|
|
18
|
-
completed_dispatches: dispatches.map(d => ({
|
|
19
|
-
start: d.start,
|
|
20
|
-
end: d.end,
|
|
21
|
-
delta_kwh: d.delta !== null && d.delta !== undefined ? parseFloat(d.delta) : null,
|
|
22
|
-
source: (d.meta || {}).source || null
|
|
23
|
-
})),
|
|
24
|
-
completed_dispatches_count: dispatches.length,
|
|
25
|
-
completed_dispatches_error: null
|
|
26
|
-
};
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
const defaultData = {
|
|
30
|
-
completed_dispatches: [],
|
|
31
|
-
completed_dispatches_count: 0,
|
|
32
|
-
completed_dispatches_error: null
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
module.exports = { buildQuery, parseResponse, defaultData };
|
|
1
|
+
// lib/categories/completed-dispatches.js
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const QUERY = `
|
|
5
|
+
query getCompletedDispatches($account: String!) {
|
|
6
|
+
completedDispatches(accountNumber: $account) {
|
|
7
|
+
start end delta meta { source location }
|
|
8
|
+
}
|
|
9
|
+
}`;
|
|
10
|
+
|
|
11
|
+
function buildQuery(account) {
|
|
12
|
+
return { query: QUERY, variables: { account } };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function parseResponse(data) {
|
|
16
|
+
const dispatches = data.completedDispatches || [];
|
|
17
|
+
return {
|
|
18
|
+
completed_dispatches: dispatches.map(d => ({
|
|
19
|
+
start: d.start,
|
|
20
|
+
end: d.end,
|
|
21
|
+
delta_kwh: d.delta !== null && d.delta !== undefined ? parseFloat(d.delta) : null,
|
|
22
|
+
source: (d.meta || {}).source || null
|
|
23
|
+
})),
|
|
24
|
+
completed_dispatches_count: dispatches.length,
|
|
25
|
+
completed_dispatches_error: null
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const defaultData = {
|
|
30
|
+
completed_dispatches: [],
|
|
31
|
+
completed_dispatches_count: 0,
|
|
32
|
+
completed_dispatches_error: null
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
module.exports = { buildQuery, parseResponse, defaultData };
|