node-red-contrib-octopus-intelligent 1.3.3 → 1.5.0
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 +51 -0
- package/examples/ha-dashboard.yaml +159 -46
- package/examples/node-red-dashboard-flow.json +1199 -0
- package/lib/categories/applicable-rates.js +39 -3
- package/lib/categories/electricity.js +15 -2
- package/lib/http-get-json.js +22 -0
- package/lib/payload.js +12 -1
- package/lib/poll-tick.js +44 -0
- package/lib/scheduler.js +17 -3
- package/lib/update-check.js +38 -0
- package/octopus-intelligent.js +348 -379
- package/package.json +1 -1
|
@@ -19,7 +19,29 @@ function buildQuery(account, mpan) {
|
|
|
19
19
|
return { query: QUERY, variables: { account, mpxn: mpan, startAt, endAt } };
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
function
|
|
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) {
|
|
23
45
|
const pence = current ? current.value_pence_per_kwh : null;
|
|
24
46
|
const prevPence = prev ? prev.value_pence_per_kwh : null;
|
|
25
47
|
const nextPence = next ? next.value_pence_per_kwh : null;
|
|
@@ -35,6 +57,11 @@ function _emit(prefix, slots, current, prev, next) {
|
|
|
35
57
|
out[`${prefix}_next_gbp`] = nextPence !== null ? parseFloat((nextPence / 100).toFixed(4)) : null;
|
|
36
58
|
out[`${prefix}_next_from`] = next ? next.valid_from : null;
|
|
37
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;
|
|
38
65
|
return out;
|
|
39
66
|
}
|
|
40
67
|
|
|
@@ -60,7 +87,8 @@ function parseResponse(data, options) {
|
|
|
60
87
|
const prev = currentIdx > 0 ? slots[currentIdx - 1] : null;
|
|
61
88
|
const next = (currentIdx !== -1 && currentIdx < slots.length - 1) ? slots[currentIdx + 1] : null;
|
|
62
89
|
|
|
63
|
-
|
|
90
|
+
const stats = _stats(slots);
|
|
91
|
+
return _emit(prefix, slots, current, prev, next, stats);
|
|
64
92
|
}
|
|
65
93
|
|
|
66
94
|
const defaultData = {
|
|
@@ -75,6 +103,10 @@ const defaultData = {
|
|
|
75
103
|
applicable_rates_next_gbp: null,
|
|
76
104
|
applicable_rates_next_from: null,
|
|
77
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,
|
|
78
110
|
electricity_export_rate: [],
|
|
79
111
|
electricity_export_rate_count: 0,
|
|
80
112
|
electricity_export_rate_current_pence: null,
|
|
@@ -85,7 +117,11 @@ const defaultData = {
|
|
|
85
117
|
electricity_export_rate_next_pence: null,
|
|
86
118
|
electricity_export_rate_next_gbp: null,
|
|
87
119
|
electricity_export_rate_next_from: null,
|
|
88
|
-
electricity_export_rate_error: 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
|
|
89
125
|
};
|
|
90
126
|
|
|
91
127
|
module.exports = { buildQuery, parseResponse, defaultData };
|
|
@@ -10,8 +10,8 @@ query getElectricityRates($account: String!) {
|
|
|
10
10
|
... on TariffType { productCode standingCharge }
|
|
11
11
|
... on StandardTariff { productCode standingCharge unitRate }
|
|
12
12
|
... on HalfHourlyTariff { productCode standingCharge }
|
|
13
|
-
... on DayNightTariff { productCode standingCharge }
|
|
14
|
-
... on FourRateEvTariff { productCode standingCharge }
|
|
13
|
+
... on DayNightTariff { productCode standingCharge dayRate nightRate }
|
|
14
|
+
... on FourRateEvTariff { productCode standingCharge dayRate nightRate evDevicePeakRate evDeviceOffPeakRate }
|
|
15
15
|
... on PrepayTariff { productCode standingCharge unitRate }
|
|
16
16
|
}
|
|
17
17
|
}
|
|
@@ -62,6 +62,11 @@ function parseRatesResponse(data) {
|
|
|
62
62
|
[`${prefix}_tariff_code`]: tariff.productCode || null,
|
|
63
63
|
[`${prefix}_valid_from`]: (latest && latest.validFrom) || null,
|
|
64
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,
|
|
65
70
|
};
|
|
66
71
|
};
|
|
67
72
|
|
|
@@ -108,11 +113,19 @@ const defaultData = {
|
|
|
108
113
|
electricity_tariff_code: null,
|
|
109
114
|
electricity_valid_from: null,
|
|
110
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,
|
|
111
120
|
electricity_export_unit_rate: null,
|
|
112
121
|
electricity_export_standing_charge: null,
|
|
113
122
|
electricity_export_tariff_code: null,
|
|
114
123
|
electricity_export_valid_from: null,
|
|
115
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,
|
|
116
129
|
electricity_rates_error: null,
|
|
117
130
|
electricity_consumption_kwh: null,
|
|
118
131
|
electricity_consumption_cost: null,
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
const https = require('https');
|
|
3
|
+
|
|
4
|
+
function httpGetJson(url) {
|
|
5
|
+
return new Promise((resolve, reject) => {
|
|
6
|
+
const req = https.get(url, res => {
|
|
7
|
+
if (res.statusCode < 200 || res.statusCode >= 300) {
|
|
8
|
+
res.resume();
|
|
9
|
+
return reject(new Error(`HTTP ${res.statusCode}`));
|
|
10
|
+
}
|
|
11
|
+
let body = '';
|
|
12
|
+
res.on('data', chunk => { body += chunk; });
|
|
13
|
+
res.on('end', () => {
|
|
14
|
+
try { resolve(JSON.parse(body)); }
|
|
15
|
+
catch (e) { reject(new Error(`JSON parse: ${e.message}`)); }
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
req.on('error', reject);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = httpGetJson;
|
package/lib/payload.js
CHANGED
|
@@ -70,7 +70,18 @@ function buildDefaultPayload(state) {
|
|
|
70
70
|
applicable_rates_current_pence: null, applicable_rates_error: null,
|
|
71
71
|
account_balance_pence: null, account_balance_pounds: null, account_error: null,
|
|
72
72
|
octoplus_enrolled: null, octoplus_enrollment_status: null,
|
|
73
|
-
octoplus_loyalty_points_user: null, octoplus_error: null
|
|
73
|
+
octoplus_loyalty_points_user: null, octoplus_error: null,
|
|
74
|
+
// v1.5 — tariff rate-band fields (electricity)
|
|
75
|
+
electricity_day_rate: null, electricity_night_rate: null,
|
|
76
|
+
electricity_ev_peak_rate: null, electricity_ev_off_peak_rate: null,
|
|
77
|
+
// v1.5 — 24h schedule stats
|
|
78
|
+
applicable_rates_min_pence: null, applicable_rates_max_pence: null,
|
|
79
|
+
applicable_rates_median_pence: null, applicable_rates_avg_pence: null,
|
|
80
|
+
electricity_export_rate_min_pence: null, electricity_export_rate_max_pence: null,
|
|
81
|
+
electricity_export_rate_median_pence: null, electricity_export_rate_avg_pence: null,
|
|
82
|
+
// v1.5 — update check
|
|
83
|
+
installed_version: null, latest_version: null,
|
|
84
|
+
update_available: false, update_check_at: null, update_check_error: null
|
|
74
85
|
};
|
|
75
86
|
}
|
|
76
87
|
|
package/lib/poll-tick.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
function simplePoll(queryFn, parseFn, graphqlPost) {
|
|
4
|
+
return async function poll(token) {
|
|
5
|
+
const { query, variables, hostname } = queryFn();
|
|
6
|
+
const response = await graphqlPost({ query, variables }, token, hostname);
|
|
7
|
+
if (response.data.errors) throw new Error(JSON.stringify(response.data.errors));
|
|
8
|
+
if (!response.data.data) throw new Error('Response missing data');
|
|
9
|
+
return parseFn(response.data.data);
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const { isDue } = require('./scheduler');
|
|
14
|
+
const { mergePayload } = require('./payload');
|
|
15
|
+
|
|
16
|
+
async function runTick({ categories, state, getToken, now = Date.now }) {
|
|
17
|
+
const due = categories.filter((c) => c.enabled && isDue(c));
|
|
18
|
+
if (due.length === 0) return { state, emitted: false };
|
|
19
|
+
let token;
|
|
20
|
+
try {
|
|
21
|
+
token = await getToken();
|
|
22
|
+
} catch (e) {
|
|
23
|
+
let newState = state;
|
|
24
|
+
for (const cat of due) {
|
|
25
|
+
newState = mergePayload(newState, { [`${cat.id}_error`]: `Auth failed: ${e.message}` });
|
|
26
|
+
cat.lastPolled = now();
|
|
27
|
+
}
|
|
28
|
+
return { state: newState, emitted: true };
|
|
29
|
+
}
|
|
30
|
+
let newState = state;
|
|
31
|
+
for (const cat of due) {
|
|
32
|
+
try {
|
|
33
|
+
const partial = await cat.poll(token);
|
|
34
|
+
newState = mergePayload(newState, partial);
|
|
35
|
+
newState = mergePayload(newState, { [`${cat.id}_error`]: null });
|
|
36
|
+
} catch (e) {
|
|
37
|
+
newState = mergePayload(newState, { [`${cat.id}_error`]: e.message });
|
|
38
|
+
}
|
|
39
|
+
cat.lastPolled = now();
|
|
40
|
+
}
|
|
41
|
+
return { state: newState, emitted: true };
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
module.exports = { simplePoll, runTick };
|
package/lib/scheduler.js
CHANGED
|
@@ -12,14 +12,28 @@ function alignedStart(nowMs) {
|
|
|
12
12
|
return ms === 0 ? 1000 : 1000 - ms;
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
function wrapWithInFlightGuard(fn) {
|
|
16
|
+
let inFlight = false;
|
|
17
|
+
return async function guarded(...args) {
|
|
18
|
+
if (inFlight) return;
|
|
19
|
+
inFlight = true;
|
|
20
|
+
try {
|
|
21
|
+
return await fn(...args);
|
|
22
|
+
} finally {
|
|
23
|
+
inFlight = false;
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
15
28
|
function createScheduler(onTick) {
|
|
29
|
+
const guarded = wrapWithInFlightGuard(onTick);
|
|
16
30
|
let handle = null;
|
|
17
31
|
|
|
18
32
|
function start() {
|
|
19
33
|
const delay = alignedStart(Date.now());
|
|
20
34
|
setTimeout(() => {
|
|
21
|
-
|
|
22
|
-
handle = setInterval(
|
|
35
|
+
guarded();
|
|
36
|
+
handle = setInterval(guarded, 1000);
|
|
23
37
|
}, delay);
|
|
24
38
|
}
|
|
25
39
|
|
|
@@ -30,4 +44,4 @@ function createScheduler(onTick) {
|
|
|
30
44
|
return { start, stop };
|
|
31
45
|
}
|
|
32
46
|
|
|
33
|
-
module.exports = { isDue, alignedStart, createScheduler };
|
|
47
|
+
module.exports = { isDue, alignedStart, createScheduler, wrapWithInFlightGuard };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const NPM_LATEST_URL = 'https://registry.npmjs.org/node-red-contrib-octopus-intelligent/latest';
|
|
4
|
+
|
|
5
|
+
function compareSemver(a, b) {
|
|
6
|
+
// Strict numeric X.Y.Z with optional pre-release suffix.
|
|
7
|
+
// Pre-release (1.5.0-dev.1) is OLDER than the base release (1.5.0).
|
|
8
|
+
const parse = v => {
|
|
9
|
+
const m = String(v).match(/^(\d+)\.(\d+)\.(\d+)(-.+)?$/);
|
|
10
|
+
if (!m) return null;
|
|
11
|
+
return { major: +m[1], minor: +m[2], patch: +m[3], pre: m[4] || null };
|
|
12
|
+
};
|
|
13
|
+
const pa = parse(a), pb = parse(b);
|
|
14
|
+
if (!pa || !pb) return 0;
|
|
15
|
+
if (pa.major !== pb.major) return pa.major - pb.major;
|
|
16
|
+
if (pa.minor !== pb.minor) return pa.minor - pb.minor;
|
|
17
|
+
if (pa.patch !== pb.patch) return pa.patch - pb.patch;
|
|
18
|
+
if (pa.pre && !pb.pre) return -1;
|
|
19
|
+
if (!pa.pre && pb.pre) return 1;
|
|
20
|
+
return 0;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
async function checkUpdate(httpGetJson, installedVersion) {
|
|
24
|
+
const data = await httpGetJson(NPM_LATEST_URL);
|
|
25
|
+
const latest = data && data.version;
|
|
26
|
+
if (typeof latest !== 'string' || !/^\d+\.\d+\.\d+/.test(latest)) {
|
|
27
|
+
throw new Error('npm registry response missing version');
|
|
28
|
+
}
|
|
29
|
+
return {
|
|
30
|
+
installed_version: installedVersion,
|
|
31
|
+
latest_version: latest,
|
|
32
|
+
update_available: compareSemver(latest, installedVersion) > 0,
|
|
33
|
+
update_check_at: new Date().toISOString(),
|
|
34
|
+
update_check_error: null,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
module.exports = { checkUpdate, compareSemver, NPM_LATEST_URL };
|