node-red-contrib-ccu 4.7.1 → 4.8.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/.mocharc.json +4 -0
- package/CHANGELOG.md +68 -0
- package/README.md +1 -1
- package/nodes/ccu-connection.html +1 -1
- package/nodes/ccu-connection.js +281 -227
- package/nodes/lib/cachefile.js +105 -0
- package/nodes/lib/subscriptions.js +333 -0
- package/package.json +1 -1
package/.mocharc.json
ADDED
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,74 @@ Notable changes to node-red-contrib-ccu. Format follows
|
|
|
5
5
|
user-visible symptom and the cause, not the commit list (the release notes
|
|
6
6
|
append commits automatically).
|
|
7
7
|
|
|
8
|
+
## 4.8.0 (2026-10-29)
|
|
9
|
+
|
|
10
|
+
Less memory: the connection's own bookkeeping no longer grows with the
|
|
11
|
+
number of datapoints, and the paramset descriptions in memory are those of
|
|
12
|
+
the device types the system has. On a large system (a few thousand
|
|
13
|
+
datapoints, a few hundred subscriptions from value nodes, RedMatic-HomeKit
|
|
14
|
+
and RedMatic-Matter) that is on the order of 100 MB of live heap less, and
|
|
15
|
+
a deploy no longer adds to it. It is a reduction, not the whole of a large
|
|
16
|
+
Node-RED's memory: flows and the other nodes keep what they keep.
|
|
17
|
+
|
|
18
|
+
### Changed
|
|
19
|
+
|
|
20
|
+
- **Datapoint subscriptions are dispatched through an index.** Until now the
|
|
21
|
+
connection remembered, for every datapoint, which subscriptions had matched
|
|
22
|
+
it and which had not - two sets of ids per datapoint name, so the memory
|
|
23
|
+
grew with datapoints × subscriptions (3,500 datapoints × 536 subscriptions
|
|
24
|
+
were about 70 MB of live heap on a CCU3), a subscription with `cache` on
|
|
25
|
+
walked every cached value at deploy time to fill them all, and - because
|
|
26
|
+
the replay and the event path stored the id in two different forms - a
|
|
27
|
+
redeploy left the old entries behind and grew the sets again. Now a
|
|
28
|
+
subscription is indexed by the most exact attribute of its filter (the
|
|
29
|
+
datapoint name, the channel, the device, ...), an event is tested only
|
|
30
|
+
against the subscriptions that can match it, and unsubscribe removes
|
|
31
|
+
exactly one entry. What a subscription receives, and in which order, is
|
|
32
|
+
unchanged (a randomised equivalence test against the old dispatch guards
|
|
33
|
+
it). In the benchmark (`test/bench/subscriptions.js`, 12,000 datapoints,
|
|
34
|
+
1,000 subscriptions) the heap goes from 481 MB to 3 MB, a deploy from
|
|
35
|
+
6.5 s to 40 ms, an event from 28 µs to 8 µs.
|
|
36
|
+
- **Paramset descriptions in memory only for the device types present.**
|
|
37
|
+
`paramsets.json` - the shipped 14 MB plus what a system fetched - was held
|
|
38
|
+
in memory whole: some 2,800 type/version keys, 16-20 MB of heap, where a
|
|
39
|
+
system uses the few dozen types it has (40 types are 3 MB). The file on
|
|
40
|
+
disk keeps every description in its format; the memory holds the ones the
|
|
41
|
+
device table needs, loaded at start and, for a device that arrives later,
|
|
42
|
+
from the file before the interface is asked. A fetched description is
|
|
43
|
+
merged into the file. The editor's device data is correspondingly smaller.
|
|
44
|
+
- An obsolete `ccu_paramsets_v2.json` (the format of 2019) still lying in a
|
|
45
|
+
userDir is removed at start, once, with a log line.
|
|
46
|
+
|
|
47
|
+
## 4.7.2 (2026-10-29)
|
|
48
|
+
|
|
49
|
+
### Fixed
|
|
50
|
+
|
|
51
|
+
- **On openccu-lite the metadata file was written once per interface at every
|
|
52
|
+
start** (`metadata saved to …/ccu_<host>.json`, four lines within a second
|
|
53
|
+
on a system with four interfaces), although nothing had changed. The
|
|
54
|
+
system's stream hands the connection every interface's whole device list
|
|
55
|
+
when it connects, in the shape of a `newDevices` call, and the connection
|
|
56
|
+
saved whenever that list was not empty - on a CCU `newDevices` only ever
|
|
57
|
+
names devices the connection did not know. The file is now written only
|
|
58
|
+
when a device is new or its data changed, a burst of changes is one write
|
|
59
|
+
(and one log line), and the cache files (metadata, names, values) are
|
|
60
|
+
written atomically - a temporary file and a rename - so a power cut while
|
|
61
|
+
writing leaves the old file, not a truncated one.
|
|
62
|
+
- **On openccu-lite the start log's `Interfaces:` line listed ReGaHSS**, right
|
|
63
|
+
after the line saying the system has no ReGaHSS; it leaves it out now (the
|
|
64
|
+
mqtt node's per-interface counters as well). Log and dialog texts say "the
|
|
65
|
+
system" where they said "the box".
|
|
66
|
+
|
|
67
|
+
### Changed
|
|
68
|
+
|
|
69
|
+
- The README names RedMatic 9.15.1 as the version whose token makes lite-rpc
|
|
70
|
+
the default on the system (not 9.13).
|
|
71
|
+
- The release workflow's flow-library step no longer trusts the library's
|
|
72
|
+
"already at latest version" while its page still shows the previous
|
|
73
|
+
release; it asks again after 2, 5, 10 and 20 minutes and warns only after
|
|
74
|
+
that.
|
|
75
|
+
|
|
8
76
|
## 4.7.1 (2026-10-28)
|
|
9
77
|
|
|
10
78
|
### Fixed
|
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ start on openccu-lite, the log says so, and a choice made in the node is never c
|
|
|
47
47
|
CCU-Jack keep their own connections.
|
|
48
48
|
|
|
49
49
|
- Node-RED **on the system** (RedMatic from its addon catalogue): lite-rpc with RedMatic's own token, which the system
|
|
50
|
-
mints for it from RedMatic's manifest (RedMatic 9.
|
|
50
|
+
mints for it from RedMatic's manifest (RedMatic 9.15.1 or later); the names come with the system's read-only token.
|
|
51
51
|
Nothing to configure. An older RedMatic has no such token and connects the CCU way, the log says so.
|
|
52
52
|
- Node-RED **elsewhere**: an API token of the system goes into the connection node's **openccu-lite token** field -
|
|
53
53
|
for lite-rpc it needs the `rpc:*` scope of what the flow does (`rpc:read` for events and reads, `rpc:operate` for
|
|
@@ -329,6 +329,6 @@
|
|
|
329
329
|
<div class="form-row">
|
|
330
330
|
<label for="node-config-input-metaPort"><i class="icon-tag"></i> openccu-lite port</label>
|
|
331
331
|
<input type="text" id="node-config-input-metaPort" placeholder="80 / 443">
|
|
332
|
-
<small>Only needed when the
|
|
332
|
+
<small>Only needed when the system's web server is not on the default port.</small>
|
|
333
333
|
</div>
|
|
334
334
|
</script>
|
package/nodes/ccu-connection.js
CHANGED
|
@@ -19,6 +19,8 @@ const {Rega} = require('homematic-rega'); // ES module - require(esm) needs Node
|
|
|
19
19
|
const metaProvider = require('./lib/metaprovider.js');
|
|
20
20
|
const {InitRetry, retryDelay, RETRY_INTERVAL} = require('./lib/initretry.js');
|
|
21
21
|
const lite = require('./lib/lite.js');
|
|
22
|
+
const {writeJsonAtomic, DebouncedSave} = require('./lib/cachefile.js');
|
|
23
|
+
const {SubscriptionIndex, matches: filterMatches} = require('./lib/subscriptions.js');
|
|
22
24
|
const xmlrpc = require('homematic-xmlrpc');
|
|
23
25
|
const binrpc = require('binrpc');
|
|
24
26
|
|
|
@@ -718,13 +720,19 @@ module.exports = function (RED) {
|
|
|
718
720
|
this.paramsQueue = [];
|
|
719
721
|
|
|
720
722
|
this.paramsetFile = path.join(RED.settings.userDir || path.join(__dirname, '..'), 'paramsets.json');
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
this.
|
|
725
|
-
|
|
726
|
-
this.
|
|
727
|
-
this.
|
|
723
|
+
// task 28: the descriptions in memory are those of the device types present (and
|
|
724
|
+
// whatever was fetched); the file on disk keeps them all. Fetched since the last
|
|
725
|
+
// save, merged into the file by saveParamsets():
|
|
726
|
+
this.paramsetNew = {};
|
|
727
|
+
// names the file does not have, so a miss does not parse it again
|
|
728
|
+
this.paramsetFileMissing = new Set();
|
|
729
|
+
this.paramsetFilePass = null;
|
|
730
|
+
this.paramsetDescriptions = {};
|
|
731
|
+
|
|
732
|
+
// task 27: the datapoint subscriptions, indexed by their exact key - memory grows with
|
|
733
|
+
// the subscriptions, not with datapoints x subscriptions as the per-datapoint
|
|
734
|
+
// whitelists/blacklists did
|
|
735
|
+
this.subscriptions = new SubscriptionIndex();
|
|
728
736
|
|
|
729
737
|
this.sysvarCallbacks = {};
|
|
730
738
|
this.idSysvarCallback = 0;
|
|
@@ -781,8 +789,13 @@ module.exports = function (RED) {
|
|
|
781
789
|
RED.settings.userDir || path.join(__dirname, '..'),
|
|
782
790
|
'ccu_lite_' + this.host + '.json',
|
|
783
791
|
);
|
|
792
|
+
// task 24: the device lists of all interfaces arrive within a second at start -
|
|
793
|
+
// one write of the metadata file for the burst, flushed at close
|
|
794
|
+
this.metadataSave = new DebouncedSave(() => this.writeMetadata(), 1000);
|
|
784
795
|
|
|
785
796
|
this.loadMetadata();
|
|
797
|
+
// after the metadata: the descriptions of the device table's types are what is loaded
|
|
798
|
+
this.loadParamsets();
|
|
786
799
|
this.loadRegadata();
|
|
787
800
|
this.loadValues();
|
|
788
801
|
this.pruneCaches();
|
|
@@ -983,7 +996,7 @@ module.exports = function (RED) {
|
|
|
983
996
|
this.logger.debug(
|
|
984
997
|
'stats rpc rx: ' + JSON.stringify(this.rxCounters) + ' tx: ' + JSON.stringify(this.txCounters),
|
|
985
998
|
);
|
|
986
|
-
this.logger.debug('stats rpc subscribers ' +
|
|
999
|
+
this.logger.debug('stats rpc subscribers ' + this.subscriptions.size);
|
|
987
1000
|
this.logger.debug(
|
|
988
1001
|
'stats rega subscribers ' +
|
|
989
1002
|
(Object.keys(this.programCallbacks).length + Object.keys(this.sysvarCallbacks).length),
|
|
@@ -1130,11 +1143,30 @@ module.exports = function (RED) {
|
|
|
1130
1143
|
* @returns {Promise<any>}
|
|
1131
1144
|
*/
|
|
1132
1145
|
saveMetadata() {
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1146
|
+
// task 24: a burst (every interface's device list at start) is one write and one line
|
|
1147
|
+
this.metadataSave.request();
|
|
1148
|
+
return Promise.resolve();
|
|
1149
|
+
}
|
|
1150
|
+
|
|
1151
|
+
/** writes the metadata file now (the debounce's write, and the flush at close) */
|
|
1152
|
+
writeMetadata() {
|
|
1153
|
+
this.writeCache(this.metadataFile, 'metadata', this.metadata);
|
|
1154
|
+
}
|
|
1155
|
+
|
|
1156
|
+
/**
|
|
1157
|
+
* task 24: one of the cache files, written atomically; a failure is logged, never thrown
|
|
1158
|
+
* (the caches are a convenience, the connection works without them)
|
|
1159
|
+
* @param {string} file
|
|
1160
|
+
* @param {string} what for the log line
|
|
1161
|
+
* @param {*} data
|
|
1162
|
+
*/
|
|
1163
|
+
writeCache(file, what, data) {
|
|
1164
|
+
try {
|
|
1165
|
+
writeJsonAtomic(file, data);
|
|
1166
|
+
this.logger.info(what + ' saved to', file);
|
|
1167
|
+
} catch (error) {
|
|
1168
|
+
this.logger.error(what + ' not saved to ' + file + ': ' + error.message);
|
|
1169
|
+
}
|
|
1138
1170
|
}
|
|
1139
1171
|
|
|
1140
1172
|
/**
|
|
@@ -1142,25 +1174,19 @@ module.exports = function (RED) {
|
|
|
1142
1174
|
* @returns {Promise<any>}
|
|
1143
1175
|
*/
|
|
1144
1176
|
saveRegadata() {
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
groups: this.groups,
|
|
1157
|
-
sysvar: this.sysvar,
|
|
1158
|
-
program: this.program,
|
|
1159
|
-
}),
|
|
1160
|
-
);
|
|
1161
|
-
this.logger.info('regadata saved to', this.regadataFile);
|
|
1162
|
-
resolve();
|
|
1177
|
+
this.writeCache(this.regadataFile, 'regadata', {
|
|
1178
|
+
channelNames: this.channelNames,
|
|
1179
|
+
regaIdChannel: this.regaIdChannel,
|
|
1180
|
+
regaChannels: this.regaChannels,
|
|
1181
|
+
channelRooms: this.channelRooms,
|
|
1182
|
+
channelFunctions: this.channelFunctions,
|
|
1183
|
+
rooms: this.rooms,
|
|
1184
|
+
functions: this.functions,
|
|
1185
|
+
groups: this.groups,
|
|
1186
|
+
sysvar: this.sysvar,
|
|
1187
|
+
program: this.program,
|
|
1163
1188
|
});
|
|
1189
|
+
return Promise.resolve();
|
|
1164
1190
|
}
|
|
1165
1191
|
|
|
1166
1192
|
/**
|
|
@@ -1168,16 +1194,8 @@ module.exports = function (RED) {
|
|
|
1168
1194
|
* @returns {Promise<any>}
|
|
1169
1195
|
*/
|
|
1170
1196
|
saveValues() {
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
this.valuesFile,
|
|
1174
|
-
JSON.stringify({
|
|
1175
|
-
values: this.values,
|
|
1176
|
-
}),
|
|
1177
|
-
);
|
|
1178
|
-
this.logger.info('values saved to', this.valuesFile);
|
|
1179
|
-
resolve();
|
|
1180
|
-
});
|
|
1197
|
+
this.writeCache(this.valuesFile, 'values', {values: this.values});
|
|
1198
|
+
return Promise.resolve();
|
|
1181
1199
|
}
|
|
1182
1200
|
|
|
1183
1201
|
/**
|
|
@@ -1282,44 +1300,180 @@ module.exports = function (RED) {
|
|
|
1282
1300
|
}
|
|
1283
1301
|
|
|
1284
1302
|
/**
|
|
1285
|
-
*
|
|
1303
|
+
* Merges the descriptions fetched since the last save into the file on disk. The file
|
|
1304
|
+
* holds every description ever known (the vendored ones and the fetched ones, in the
|
|
1305
|
+
* format other consumers and the editor read); the memory holds only the present types,
|
|
1306
|
+
* so the file is read, merged and written back atomically (task 28).
|
|
1286
1307
|
* @returns {Promise<any>}
|
|
1287
1308
|
*/
|
|
1288
1309
|
saveParamsets() {
|
|
1289
1310
|
return new Promise((resolve) => {
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1311
|
+
const names = Object.keys(this.paramsetNew);
|
|
1312
|
+
if (names.length === 0) {
|
|
1313
|
+
resolve();
|
|
1314
|
+
return;
|
|
1315
|
+
}
|
|
1316
|
+
|
|
1317
|
+
try {
|
|
1318
|
+
const all = this.readParamsetFile() || {};
|
|
1319
|
+
for (const name of names) {
|
|
1320
|
+
all[name] = this.paramsetNew[name];
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
writeJsonAtomic(this.paramsetFile, all, ' ');
|
|
1324
|
+
this.paramsetNew = {};
|
|
1325
|
+
this.logger.info(
|
|
1326
|
+
'paramsets saved to',
|
|
1327
|
+
this.paramsetFile,
|
|
1328
|
+
names.length,
|
|
1329
|
+
'new,',
|
|
1330
|
+
Object.keys(all).length,
|
|
1331
|
+
'total',
|
|
1332
|
+
);
|
|
1333
|
+
} catch (error) {
|
|
1334
|
+
this.logger.error('error saving paramsets ' + error.message);
|
|
1335
|
+
}
|
|
1336
|
+
|
|
1296
1337
|
resolve();
|
|
1297
1338
|
});
|
|
1298
1339
|
}
|
|
1299
1340
|
|
|
1300
1341
|
/**
|
|
1301
|
-
*
|
|
1342
|
+
* Parses the paramsets file - the userDir's, or the vendored one when the userDir has none
|
|
1343
|
+
* yet (then it is copied there, as before, so the format on disk stays the same).
|
|
1344
|
+
* @returns {object|null} every description the file holds, or null when there is none
|
|
1345
|
+
*/
|
|
1346
|
+
readParamsetFile() {
|
|
1347
|
+
if (!fs.existsSync(this.paramsetFile)) {
|
|
1348
|
+
const vendored = path.join(__dirname, '..', 'paramsets.json');
|
|
1349
|
+
try {
|
|
1350
|
+
fs.copyFileSync(vendored, this.paramsetFile);
|
|
1351
|
+
this.logger.info('paramsets copied from', vendored, 'to', this.paramsetFile);
|
|
1352
|
+
} catch (error) {
|
|
1353
|
+
this.logger.error('error copying paramsets ' + error.message);
|
|
1354
|
+
}
|
|
1355
|
+
}
|
|
1356
|
+
|
|
1357
|
+
try {
|
|
1358
|
+
return JSON.parse(fs.readFileSync(this.paramsetFile));
|
|
1359
|
+
} catch (error) {
|
|
1360
|
+
if (error.code === 'ENOENT') {
|
|
1361
|
+
this.logger.info('paramsets new empty');
|
|
1362
|
+
} else {
|
|
1363
|
+
this.logger.error('error loading paramsets ' + error.message);
|
|
1364
|
+
}
|
|
1365
|
+
|
|
1366
|
+
return null;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
|
|
1370
|
+
/**
|
|
1371
|
+
* The paramset names the device table needs: every PARAMSETS entry of every device and
|
|
1372
|
+
* channel of every interface.
|
|
1373
|
+
* @returns {Set<string>}
|
|
1374
|
+
*/
|
|
1375
|
+
neededParamsetNames() {
|
|
1376
|
+
const names = new Set();
|
|
1377
|
+
const tables = (this.metadata && this.metadata.devices) || {};
|
|
1378
|
+
for (const iface of Object.keys(tables)) {
|
|
1379
|
+
for (const address of Object.keys(tables[iface])) {
|
|
1380
|
+
const device = tables[iface][address];
|
|
1381
|
+
if (device && Array.isArray(device.PARAMSETS)) {
|
|
1382
|
+
for (const paramset of device.PARAMSETS) {
|
|
1383
|
+
const name = this.paramsetName(iface, device, paramset);
|
|
1384
|
+
if (name) {
|
|
1385
|
+
names.add(name);
|
|
1386
|
+
}
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
|
|
1392
|
+
return names;
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
/**
|
|
1396
|
+
* Takes the descriptions the device table needs (and `extra`) from the file into memory;
|
|
1397
|
+
* one parse of the file per call. A name the file does not have is remembered, so the
|
|
1398
|
+
* queue asks the interface for it without another parse.
|
|
1399
|
+
* @param {Iterable<string>} [extra] further names
|
|
1400
|
+
* @returns {number} how many descriptions came in
|
|
1401
|
+
*/
|
|
1402
|
+
paramsetsFromFile(extra) {
|
|
1403
|
+
const needed = this.neededParamsetNames();
|
|
1404
|
+
for (const name of extra || []) {
|
|
1405
|
+
needed.add(name);
|
|
1406
|
+
}
|
|
1407
|
+
|
|
1408
|
+
const all = this.readParamsetFile();
|
|
1409
|
+
let loaded = 0;
|
|
1410
|
+
for (const name of needed) {
|
|
1411
|
+
if (this.paramsetDescriptions[name]) {
|
|
1412
|
+
continue;
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
if (all && all[name]) {
|
|
1416
|
+
this.paramsetDescriptions[name] = all[name];
|
|
1417
|
+
this.paramsetFileMissing.delete(name);
|
|
1418
|
+
loaded += 1;
|
|
1419
|
+
} else {
|
|
1420
|
+
this.paramsetFileMissing.add(name);
|
|
1421
|
+
}
|
|
1422
|
+
}
|
|
1423
|
+
|
|
1424
|
+
this.logger.info(
|
|
1425
|
+
'paramsets loaded from',
|
|
1426
|
+
this.paramsetFile,
|
|
1427
|
+
loaded,
|
|
1428
|
+
'of',
|
|
1429
|
+
all ? Object.keys(all).length : 0,
|
|
1430
|
+
'(' + Object.keys(this.paramsetDescriptions).length + ' in memory for the device types present)',
|
|
1431
|
+
);
|
|
1432
|
+
return loaded;
|
|
1433
|
+
}
|
|
1434
|
+
|
|
1435
|
+
/**
|
|
1436
|
+
* One file pass on the next turn of the event loop, however often it is asked for until then.
|
|
1437
|
+
*/
|
|
1438
|
+
paramsetFilePassSoon() {
|
|
1439
|
+
if (this.paramsetFilePass) {
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
this.paramsetFilePass = setImmediate(() => {
|
|
1444
|
+
this.paramsetFilePass = null;
|
|
1445
|
+
try {
|
|
1446
|
+
this.paramsetsFromFile();
|
|
1447
|
+
} catch (error) {
|
|
1448
|
+
this.logger.error('error loading paramsets ' + error.message);
|
|
1449
|
+
}
|
|
1450
|
+
});
|
|
1451
|
+
}
|
|
1452
|
+
|
|
1453
|
+
/**
|
|
1454
|
+
* At start: the descriptions of the cached device table's types from the file. An obsolete
|
|
1455
|
+
* `ccu_paramsets_v2.json` (the format of 2019) still lying in the userDir is removed.
|
|
1302
1456
|
* @returns {Promise<any>}
|
|
1303
1457
|
*/
|
|
1304
1458
|
loadParamsets() {
|
|
1305
1459
|
return new Promise((resolve) => {
|
|
1306
|
-
const
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
this.logger.info('paramsets new empty');
|
|
1312
|
-
this.paramsetDescriptions = {};
|
|
1460
|
+
const obsolete = path.join(path.dirname(this.paramsetFile), 'ccu_paramsets_v2.json');
|
|
1461
|
+
try {
|
|
1462
|
+
if (fs.existsSync(obsolete)) {
|
|
1463
|
+
fs.unlinkSync(obsolete);
|
|
1464
|
+
this.logger.info('removed the obsolete', obsolete);
|
|
1313
1465
|
}
|
|
1314
|
-
}
|
|
1466
|
+
} catch (error) {
|
|
1467
|
+
this.logger.warn('could not remove ' + obsolete + ': ' + error.message);
|
|
1468
|
+
}
|
|
1315
1469
|
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
load(path.join(__dirname, '..', 'paramsets.json'));
|
|
1321
|
-
this.saveParamsets().then(resolve);
|
|
1470
|
+
try {
|
|
1471
|
+
this.paramsetsFromFile();
|
|
1472
|
+
} catch (error) {
|
|
1473
|
+
this.logger.error('error loading paramsets ' + error.message);
|
|
1322
1474
|
}
|
|
1475
|
+
|
|
1476
|
+
resolve();
|
|
1323
1477
|
});
|
|
1324
1478
|
}
|
|
1325
1479
|
|
|
@@ -1352,7 +1506,11 @@ module.exports = function (RED) {
|
|
|
1352
1506
|
});
|
|
1353
1507
|
Object.keys(this.liveness).forEach((iface) => this.stopLiveness(iface));
|
|
1354
1508
|
clearTimeout(this.valuesPruneTimer);
|
|
1509
|
+
clearTimeout(this.getParamsetTimeout);
|
|
1510
|
+
clearImmediate(this.paramsetFilePass);
|
|
1511
|
+
this.paramsetFilePass = null;
|
|
1355
1512
|
|
|
1513
|
+
this.metadataSave.flush();
|
|
1356
1514
|
this.saveRegadata();
|
|
1357
1515
|
this.saveValues();
|
|
1358
1516
|
|
|
@@ -1491,7 +1649,7 @@ module.exports = function (RED) {
|
|
|
1491
1649
|
const line =
|
|
1492
1650
|
'meta api detection inconclusive (' +
|
|
1493
1651
|
outcome.reason +
|
|
1494
|
-
') - names come from the ReGaHSS until the
|
|
1652
|
+
') - names come from the ReGaHSS until the system answers, detecting again in ' +
|
|
1495
1653
|
delay / 1000 +
|
|
1496
1654
|
' s';
|
|
1497
1655
|
if (this.metaRedetectAttempt === 1) {
|
|
@@ -1568,6 +1726,8 @@ module.exports = function (RED) {
|
|
|
1568
1726
|
delete this.ifaceStatus.ReGaHSS;
|
|
1569
1727
|
delete this.ifaceWaiting.ReGaHSS;
|
|
1570
1728
|
this.ifaceStatus[META_IFACE] = null;
|
|
1729
|
+
// task 25: the system has no ReGaHSS - the "Interfaces:" line and the counters leave it out
|
|
1730
|
+
this.enabledIfaces = this.enabledIfaces.filter((iface) => iface !== 'ReGaHSS');
|
|
1571
1731
|
|
|
1572
1732
|
this.logger.info(
|
|
1573
1733
|
'openccu-lite detected (' +
|
|
@@ -1577,12 +1737,12 @@ module.exports = function (RED) {
|
|
|
1577
1737
|
') - names, rooms and functions come from its metadata api',
|
|
1578
1738
|
);
|
|
1579
1739
|
this.logger.info(
|
|
1580
|
-
'this
|
|
1740
|
+
'this system has no ReGaHSS: system variables, programs and HM-Script are not available (sysvar, program, poll and script nodes stay idle)',
|
|
1581
1741
|
);
|
|
1582
1742
|
|
|
1583
1743
|
if (info.version > 1) {
|
|
1584
1744
|
this.logger.warn(
|
|
1585
|
-
'the
|
|
1745
|
+
'the system speaks metadata api version ' +
|
|
1586
1746
|
info.version +
|
|
1587
1747
|
', this version of node-red-contrib-ccu implements version 1',
|
|
1588
1748
|
);
|
|
@@ -1590,7 +1750,7 @@ module.exports = function (RED) {
|
|
|
1590
1750
|
|
|
1591
1751
|
if (!this.metaToken) {
|
|
1592
1752
|
this.logger.warn(
|
|
1593
|
-
'no metadata api token: create one on the
|
|
1753
|
+
'no metadata api token: create one on the system (Users page) and enter it in the connection node, or run on the system where ' +
|
|
1594
1754
|
metaProvider.LOCAL_TOKEN_FILE +
|
|
1595
1755
|
' is readable. Without it the nodes work with addresses only.',
|
|
1596
1756
|
);
|
|
@@ -1631,10 +1791,13 @@ module.exports = function (RED) {
|
|
|
1631
1791
|
delete this.ifaceStatus[META_IFACE];
|
|
1632
1792
|
if (this.ifaceTypes.ReGaHSS.enabled) {
|
|
1633
1793
|
this.ifaceStatus.ReGaHSS = null;
|
|
1794
|
+
if (!this.enabledIfaces.includes('ReGaHSS')) {
|
|
1795
|
+
this.enabledIfaces.unshift('ReGaHSS');
|
|
1796
|
+
}
|
|
1634
1797
|
}
|
|
1635
1798
|
|
|
1636
1799
|
this.pushStatus();
|
|
1637
|
-
this.logger.warn('this
|
|
1800
|
+
this.logger.warn('this system no longer answers as openccu-lite - detecting again');
|
|
1638
1801
|
this.detectMetaOutcome().then((outcome) => {
|
|
1639
1802
|
if (outcome.info) {
|
|
1640
1803
|
return this.startMeta(outcome.info);
|
|
@@ -1717,7 +1880,7 @@ module.exports = function (RED) {
|
|
|
1717
1880
|
* @returns {Error}
|
|
1718
1881
|
*/
|
|
1719
1882
|
regaMissingError(feature) {
|
|
1720
|
-
return new Error(feature + ' are not available on this
|
|
1883
|
+
return new Error(feature + ' are not available on this system (openccu-lite has no ReGaHSS)');
|
|
1721
1884
|
}
|
|
1722
1885
|
|
|
1723
1886
|
/**
|
|
@@ -3334,6 +3497,12 @@ module.exports = function (RED) {
|
|
|
3334
3497
|
if (device && device.PARAMSETS) {
|
|
3335
3498
|
device.PARAMSETS.forEach((paramset) => {
|
|
3336
3499
|
const name = this.paramsetName(iface, device, paramset);
|
|
3500
|
+
if (name && !this.paramsetDescriptions[name] && !this.paramsetFileMissing.has(name)) {
|
|
3501
|
+
// task 28: the file may have it - one pass after this burst of pushes (a
|
|
3502
|
+
// device list arrives as one callback), not one parse per device
|
|
3503
|
+
this.paramsetFilePassSoon();
|
|
3504
|
+
}
|
|
3505
|
+
|
|
3337
3506
|
if (
|
|
3338
3507
|
!this.paramsetDescriptions[name] &&
|
|
3339
3508
|
// B-36: once per description key in the queue, and never again
|
|
@@ -3370,6 +3539,17 @@ module.exports = function (RED) {
|
|
|
3370
3539
|
if (item) {
|
|
3371
3540
|
const {iface, name, address, paramset} = item;
|
|
3372
3541
|
|
|
3542
|
+
if (!this.paramsetDescriptions[name] && name && !this.paramsetFileMissing.has(name)) {
|
|
3543
|
+
// task 28: the file first - one parse takes every name the device table
|
|
3544
|
+
// needs by now, the rest is asked of the interface
|
|
3545
|
+
try {
|
|
3546
|
+
this.paramsetsFromFile([name]);
|
|
3547
|
+
} catch (error) {
|
|
3548
|
+
this.logger.error('error loading paramsets ' + error.message);
|
|
3549
|
+
this.paramsetFileMissing.add(name);
|
|
3550
|
+
}
|
|
3551
|
+
}
|
|
3552
|
+
|
|
3373
3553
|
if (this.paramsetDescriptions[name]) {
|
|
3374
3554
|
//this.logger.trace('paramset', name, 'already known');
|
|
3375
3555
|
this.paramsetPending = false;
|
|
@@ -3382,6 +3562,8 @@ module.exports = function (RED) {
|
|
|
3382
3562
|
this.newParamsetDescriptionCount += 1;
|
|
3383
3563
|
this.newParamsetDescription = true;
|
|
3384
3564
|
this.paramsetDescriptions[name] = res;
|
|
3565
|
+
this.paramsetNew[name] = res;
|
|
3566
|
+
this.paramsetFileMissing.delete(name);
|
|
3385
3567
|
if (this.newParamsetDescriptionCount >= 30) {
|
|
3386
3568
|
this.newParamsetDescription = false;
|
|
3387
3569
|
this.newParamsetDescriptionCount = 0;
|
|
@@ -3456,9 +3638,12 @@ module.exports = function (RED) {
|
|
|
3456
3638
|
}
|
|
3457
3639
|
|
|
3458
3640
|
/**
|
|
3459
|
-
*
|
|
3641
|
+
* Takes a device description into the device table.
|
|
3460
3642
|
* @param iface
|
|
3461
3643
|
* @param device
|
|
3644
|
+
* @returns {boolean} whether the table changed: the device is new, or its data changed
|
|
3645
|
+
* (a description equal to the cached one, as the lite-rpc stream's snapshot sends at
|
|
3646
|
+
* every connect, changes nothing - task 24)
|
|
3462
3647
|
*/
|
|
3463
3648
|
newDevice(iface, device) {
|
|
3464
3649
|
if (!this.metadata.devices[iface]) {
|
|
@@ -3497,7 +3682,8 @@ module.exports = function (RED) {
|
|
|
3497
3682
|
this.logger.debug('newDevice', iface, device.ADDRESS);
|
|
3498
3683
|
}
|
|
3499
3684
|
|
|
3500
|
-
|
|
3685
|
+
const changed = !known || deviceChanged(known, device);
|
|
3686
|
+
if (changed) {
|
|
3501
3687
|
// B-36: a device added anew (re-paired, or its data changed) is asked again
|
|
3502
3688
|
this.forgetParamsetFailures(iface, device.ADDRESS);
|
|
3503
3689
|
}
|
|
@@ -3522,6 +3708,7 @@ module.exports = function (RED) {
|
|
|
3522
3708
|
}
|
|
3523
3709
|
|
|
3524
3710
|
this.paramsetQueuePush(iface, device);
|
|
3711
|
+
return changed;
|
|
3525
3712
|
}
|
|
3526
3713
|
|
|
3527
3714
|
/**
|
|
@@ -3860,8 +4047,9 @@ module.exports = function (RED) {
|
|
|
3860
4047
|
if (iface && Array.isArray(devices)) {
|
|
3861
4048
|
devices.forEach((device) => {
|
|
3862
4049
|
if (device && typeof device.ADDRESS === 'string' && device.TYPE) {
|
|
3863
|
-
this.newDevice(iface, device)
|
|
3864
|
-
|
|
4050
|
+
if (this.newDevice(iface, device)) {
|
|
4051
|
+
changed = true;
|
|
4052
|
+
}
|
|
3865
4053
|
} else {
|
|
3866
4054
|
this.logger.warn(
|
|
3867
4055
|
'newDevices',
|
|
@@ -4161,27 +4349,18 @@ module.exports = function (RED) {
|
|
|
4161
4349
|
}
|
|
4162
4350
|
}
|
|
4163
4351
|
|
|
4164
|
-
const id = this.
|
|
4165
|
-
this.idCallback += 1;
|
|
4166
|
-
|
|
4167
|
-
//this.logger.trace('subscribe', id, JSON.stringify(filter));
|
|
4168
|
-
this.callbacks[id] = {filter, callback};
|
|
4352
|
+
const id = this.subscriptions.add(filter, callback);
|
|
4169
4353
|
|
|
4170
4354
|
if (filter.cache && this.cachedValuesReceived) {
|
|
4171
|
-
|
|
4172
|
-
|
|
4355
|
+
// the replay goes through the index: one lookup for a filter that names its
|
|
4356
|
+
// datapoint, a walk over the cached values for the others (as before, but
|
|
4357
|
+
// nothing is remembered per datapoint any more)
|
|
4358
|
+
for (const cached of SubscriptionIndex.replayCandidates(filter, this.values)) {
|
|
4359
|
+
const message = {...cached};
|
|
4173
4360
|
message.cache = true;
|
|
4174
4361
|
message.change = false;
|
|
4175
|
-
if (!this.callbackBlacklists[message.datapointName]) {
|
|
4176
|
-
this.callbackBlacklists[message.datapointName] = new Set();
|
|
4177
|
-
}
|
|
4178
|
-
|
|
4179
|
-
if (!this.callbackWhitelists[message.datapointName]) {
|
|
4180
|
-
this.callbackWhitelists[message.datapointName] = new Set();
|
|
4181
|
-
}
|
|
4182
|
-
|
|
4183
4362
|
this.callCallback(message, id);
|
|
4184
|
-
}
|
|
4363
|
+
}
|
|
4185
4364
|
}
|
|
4186
4365
|
|
|
4187
4366
|
return id;
|
|
@@ -4193,18 +4372,8 @@ module.exports = function (RED) {
|
|
|
4193
4372
|
* @returns {boolean}
|
|
4194
4373
|
*/
|
|
4195
4374
|
unsubscribe(id) {
|
|
4196
|
-
if (this.
|
|
4375
|
+
if (this.subscriptions.remove(id)) {
|
|
4197
4376
|
this.logger.trace('unsubscribe', id);
|
|
4198
|
-
delete this.callbacks[id];
|
|
4199
|
-
|
|
4200
|
-
Object.keys(this.callbackBlacklists).forEach((dp) => {
|
|
4201
|
-
this.callbackBlacklists[dp].delete(id);
|
|
4202
|
-
});
|
|
4203
|
-
|
|
4204
|
-
Object.keys(this.callbackWhitelists).forEach((dp) => {
|
|
4205
|
-
this.callbackWhitelists[dp].delete(id);
|
|
4206
|
-
});
|
|
4207
|
-
|
|
4208
4377
|
return true;
|
|
4209
4378
|
}
|
|
4210
4379
|
|
|
@@ -4340,145 +4509,30 @@ module.exports = function (RED) {
|
|
|
4340
4509
|
* @returns {boolean}
|
|
4341
4510
|
*/
|
|
4342
4511
|
callCallback(message, id) {
|
|
4343
|
-
const
|
|
4344
|
-
|
|
4345
|
-
|
|
4346
|
-
let match = true;
|
|
4347
|
-
let matchCache;
|
|
4348
|
-
let matchChange;
|
|
4349
|
-
let matchStable;
|
|
4350
|
-
|
|
4351
|
-
// Checked before the attribute loop below: that loop may break
|
|
4352
|
-
// early for a whitelisted datapoint, and this decision has to be
|
|
4353
|
-
// made for every message regardless of key order. Only present in
|
|
4354
|
-
// the filter when a node opted in, so existing subscriptions keep
|
|
4355
|
-
// receiving values the ReGa could not timestamp (#96).
|
|
4356
|
-
if (filter && filter.uncertain === false && message.uncertain) {
|
|
4512
|
+
const subscription = this.subscriptions.get(id);
|
|
4513
|
+
if (!subscription) {
|
|
4357
4514
|
return false;
|
|
4358
4515
|
}
|
|
4359
4516
|
|
|
4360
|
-
|
|
4361
|
-
|
|
4362
|
-
|
|
4363
|
-
for (let i = 0, {length} = arrayAttr; match && i < length; i++) {
|
|
4364
|
-
const attr = arrayAttr[i];
|
|
4365
|
-
|
|
4366
|
-
if (attr === 'cache') {
|
|
4367
|
-
// if filter.cache==false - Drop messages with msg.cache==true
|
|
4368
|
-
if (!filter.cache && message.cache) {
|
|
4369
|
-
//this.logger.trace('cb mismatch cache ' + id + ' ' + filter.cache + ' ' + msg.cache);
|
|
4370
|
-
return false;
|
|
4371
|
-
}
|
|
4372
|
-
|
|
4373
|
-
matchCache = true;
|
|
4374
|
-
continue;
|
|
4375
|
-
}
|
|
4376
|
-
|
|
4377
|
-
if (attr === 'change') {
|
|
4378
|
-
// if filter.change==true - Drop messages with msg.change==false - except msg.cache==true && filter.cache==true
|
|
4379
|
-
if (filter.change && !message.change && !(filter.cache && message.cache)) {
|
|
4380
|
-
//this.logger.trace('cb mismatch change ' + id + ' ' + filter.change + ' ' + msg.change + ' ' + msg.cache);
|
|
4381
|
-
return false;
|
|
4382
|
-
}
|
|
4383
|
-
|
|
4384
|
-
matchChange = true;
|
|
4385
|
-
continue;
|
|
4386
|
-
}
|
|
4387
|
-
|
|
4388
|
-
if (attr === 'stable') {
|
|
4389
|
-
// if filter.stable==true - Drop messages with msg.stable==false
|
|
4390
|
-
if (filter.stable && !message.stable) {
|
|
4391
|
-
//this.logger.trace('cb mismatch stable ' + id + ' ' + filter.stable + ' ' + msg.stable);
|
|
4392
|
-
return false;
|
|
4393
|
-
}
|
|
4394
|
-
|
|
4395
|
-
matchStable = true;
|
|
4396
|
-
continue;
|
|
4397
|
-
}
|
|
4398
|
-
|
|
4399
|
-
if (attr === 'uncertain') {
|
|
4400
|
-
// already decided above, and not a value to compare
|
|
4401
|
-
continue;
|
|
4402
|
-
}
|
|
4403
|
-
|
|
4404
|
-
if (this.callbackWhitelists[message.datapointName].has(id)) {
|
|
4405
|
-
if (matchCache && matchChange && matchStable) {
|
|
4406
|
-
break;
|
|
4407
|
-
}
|
|
4408
|
-
|
|
4409
|
-
continue;
|
|
4410
|
-
}
|
|
4411
|
-
|
|
4412
|
-
if (filter[attr] === '') {
|
|
4413
|
-
// TODO rethink
|
|
4414
|
-
continue;
|
|
4415
|
-
}
|
|
4416
|
-
|
|
4417
|
-
if (attr === 'channelIndex' && typeof filter[attr] !== 'undefined') {
|
|
4418
|
-
filter[attr] = Number.parseInt(filter[attr], 10);
|
|
4419
|
-
}
|
|
4420
|
-
|
|
4421
|
-
if (Array.isArray(message[attr])) {
|
|
4422
|
-
if (filter[attr] instanceof RegExp) {
|
|
4423
|
-
match = false;
|
|
4424
|
-
this.logger.trace('cb test regex array', id, attr, filter[attr], message[attr]);
|
|
4425
|
-
message[attr].forEach((item) => {
|
|
4426
|
-
if (filter[attr].test(item)) {
|
|
4427
|
-
match = true;
|
|
4428
|
-
}
|
|
4429
|
-
});
|
|
4430
|
-
} else if (!message[attr].includes(filter[attr])) {
|
|
4431
|
-
this.logger.trace('cb mismatch array', id, attr, filter[attr], message[attr]);
|
|
4432
|
-
match = false;
|
|
4433
|
-
}
|
|
4434
|
-
} else if (filter[attr] instanceof RegExp) {
|
|
4435
|
-
if (!filter[attr].test(message[attr])) {
|
|
4436
|
-
this.logger.trace('cb mismatch regex', id, attr, filter[attr], message[attr]);
|
|
4437
|
-
match = false;
|
|
4438
|
-
}
|
|
4439
|
-
} else if (filter[attr] !== message[attr]) {
|
|
4440
|
-
this.logger.trace(
|
|
4441
|
-
'cb mismatch misc ' + id + ' ' + attr + ' ' + filter[attr] + ' ' + message[attr],
|
|
4442
|
-
);
|
|
4443
|
-
match = false;
|
|
4444
|
-
}
|
|
4445
|
-
}
|
|
4446
|
-
}
|
|
4447
|
-
|
|
4448
|
-
if (match) {
|
|
4449
|
-
//this.logger.trace('callCallback ' + id + ' ' + msg.datapointName + ' ' + msg.value);
|
|
4450
|
-
callback(RED.util.cloneMessage(message));
|
|
4451
|
-
this.callbackWhitelists[message.datapointName].add(id);
|
|
4452
|
-
} else {
|
|
4453
|
-
//this.logger.trace('add to blacklist ' + id + ' ' + msg.datapointName);
|
|
4454
|
-
this.callbackBlacklists[message.datapointName].add(id);
|
|
4517
|
+
const {filter, callback} = subscription;
|
|
4518
|
+
if (!filterMatches(filter, message, id, this.logger)) {
|
|
4519
|
+
return false;
|
|
4455
4520
|
}
|
|
4456
4521
|
|
|
4457
|
-
|
|
4522
|
+
callback(RED.util.cloneMessage(message));
|
|
4523
|
+
return true;
|
|
4458
4524
|
}
|
|
4459
4525
|
|
|
4460
4526
|
/**
|
|
4461
|
-
* Apply msg to callCallback() for
|
|
4527
|
+
* Apply msg to callCallback() for every subscription the index names as a candidate
|
|
4528
|
+
* for it (task 27: those keyed by one of the message's own attribute values, plus the
|
|
4529
|
+
* generic ones), in subscription order
|
|
4462
4530
|
* @param msg
|
|
4463
4531
|
*/
|
|
4464
4532
|
callCallbacks(message) {
|
|
4465
|
-
|
|
4466
|
-
|
|
4467
|
-
this.callbackBlacklists[message.datapointName] = new Set();
|
|
4468
|
-
}
|
|
4469
|
-
|
|
4470
|
-
if (!this.callbackWhitelists[message.datapointName]) {
|
|
4471
|
-
this.callbackWhitelists[message.datapointName] = new Set();
|
|
4533
|
+
for (const id of this.subscriptions.candidates(message)) {
|
|
4534
|
+
this.callCallback(message, id);
|
|
4472
4535
|
}
|
|
4473
|
-
|
|
4474
|
-
Object.keys(this.callbacks).forEach((key) => {
|
|
4475
|
-
if (this.callbackBlacklists[message.datapointName].has(key)) {
|
|
4476
|
-
//this.logger.trace('blacklistet ' + key + ' ' + msg.datapointName);
|
|
4477
|
-
return;
|
|
4478
|
-
}
|
|
4479
|
-
|
|
4480
|
-
this.callCallback(message, key);
|
|
4481
|
-
});
|
|
4482
4536
|
}
|
|
4483
4537
|
|
|
4484
4538
|
/**
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The connection's cache files (metadata, regadata, values) on disk.
|
|
3
|
+
*
|
|
4
|
+
* Written atomically: the JSON goes to a temporary file beside the target and is renamed over it,
|
|
5
|
+
* so a crash or a power cut while writing leaves the old file, never a truncated one (the files
|
|
6
|
+
* are read with JSON.parse at the next start, and a half-written one would throw the cache away).
|
|
7
|
+
*
|
|
8
|
+
* A burst of changes is written once: `DebouncedSave` collects the requests of a short window into
|
|
9
|
+
* one write - at start every interface reports its device list within the same second, and before
|
|
10
|
+
* there was one full rewrite and one log line per interface.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const fs = require('fs');
|
|
14
|
+
const path = require('path');
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Writes `data` as JSON to `file` by way of a temporary file and a rename; an existing file's mode
|
|
18
|
+
* is kept.
|
|
19
|
+
* @param {string} file
|
|
20
|
+
* @param {*} data
|
|
21
|
+
* @param {string|number} [indent] JSON.stringify's third argument
|
|
22
|
+
*/
|
|
23
|
+
function writeJsonAtomic(file, data, indent) {
|
|
24
|
+
const temporary = path.join(
|
|
25
|
+
path.dirname(file),
|
|
26
|
+
'.' + path.basename(file) + '.' + process.pid + '.' + Date.now().toString(36) + '.tmp',
|
|
27
|
+
);
|
|
28
|
+
try {
|
|
29
|
+
fs.writeFileSync(temporary, JSON.stringify(data, null, indent));
|
|
30
|
+
// the rename replaces the inode: an existing file's mode is kept (0600 on a system that
|
|
31
|
+
// made it so), a new one gets the umask's
|
|
32
|
+
let mode;
|
|
33
|
+
try {
|
|
34
|
+
mode = fs.statSync(file).mode & 0o777;
|
|
35
|
+
} catch {}
|
|
36
|
+
|
|
37
|
+
if (mode !== undefined) {
|
|
38
|
+
fs.chmodSync(temporary, mode);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
fs.renameSync(temporary, file);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
try {
|
|
44
|
+
fs.unlinkSync(temporary);
|
|
45
|
+
} catch {}
|
|
46
|
+
|
|
47
|
+
throw error;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
class DebouncedSave {
|
|
52
|
+
/**
|
|
53
|
+
* @param {Function} write does the write; called once per burst
|
|
54
|
+
* @param {number} [delay] the window, ms (default 1000)
|
|
55
|
+
*/
|
|
56
|
+
constructor(write, delay = 1000) {
|
|
57
|
+
this.write = write;
|
|
58
|
+
this.delay = delay;
|
|
59
|
+
this.timer = null;
|
|
60
|
+
this.pending = false;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** asks for a write; one write happens `delay` ms after the first request of a burst */
|
|
64
|
+
request() {
|
|
65
|
+
this.pending = true;
|
|
66
|
+
if (this.timer) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
this.timer = setTimeout(() => {
|
|
71
|
+
this.timer = null;
|
|
72
|
+
this.flush();
|
|
73
|
+
}, this.delay);
|
|
74
|
+
if (typeof this.timer.unref === 'function') {
|
|
75
|
+
this.timer.unref();
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** writes now when a request is pending (close, redeploy) */
|
|
80
|
+
flush() {
|
|
81
|
+
if (this.timer) {
|
|
82
|
+
clearTimeout(this.timer);
|
|
83
|
+
this.timer = null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
if (!this.pending) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
this.pending = false;
|
|
91
|
+
this.write();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** drops a pending request without writing */
|
|
95
|
+
cancel() {
|
|
96
|
+
if (this.timer) {
|
|
97
|
+
clearTimeout(this.timer);
|
|
98
|
+
this.timer = null;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
this.pending = false;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
module.exports = {writeJsonAtomic, DebouncedSave};
|
|
@@ -0,0 +1,333 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The datapoint subscriptions of a connection and their dispatch.
|
|
3
|
+
*
|
|
4
|
+
* Until 4.7.2 every subscription was tested against every event, and the outcome was remembered
|
|
5
|
+
* per datapoint in two Sets of subscription ids (a whitelist and a blacklist per datapoint name):
|
|
6
|
+
* a subscription that did not match a datapoint landed in that datapoint's blacklist, so the
|
|
7
|
+
* memory grew with datapoints x subscriptions (12,000 datapoints x 1,000 subscriptions was some
|
|
8
|
+
* 300 MB of live heap on a CCU3), and a `cache: true` subscription walked every cached value at
|
|
9
|
+
* subscribe time to fill them all at once. Worse, the replay added the id as a number while the
|
|
10
|
+
* event path added it as a string, so an unsubscribe (by number) left the string entries behind:
|
|
11
|
+
* every deploy grew the sets again.
|
|
12
|
+
*
|
|
13
|
+
* Now the subscriptions are indexed by one exact key: the most selective filter attribute given
|
|
14
|
+
* as a plain non-empty string (`datapointName`, then `channel`, `device`, ...). An event looks up
|
|
15
|
+
* the buckets of its own attribute values plus the small list of subscriptions without an exact
|
|
16
|
+
* key (rooms, functions, regular expressions, ...) and tests only those candidates - with the
|
|
17
|
+
* filter logic as it was. Memory is O(subscriptions): one entry per subscription, and unsubscribe
|
|
18
|
+
* removes exactly that entry.
|
|
19
|
+
*/
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The filter attributes an event can be looked up by, most selective first. A subscription is
|
|
23
|
+
* indexed by the first of these its filter names with a plain, non-empty string; the others
|
|
24
|
+
* (and anything that is a RegExp, a number, an array or empty) go into the generic list.
|
|
25
|
+
*/
|
|
26
|
+
const INDEXED_ATTRIBUTES = [
|
|
27
|
+
'datapointName',
|
|
28
|
+
'channel',
|
|
29
|
+
'device',
|
|
30
|
+
'channelName',
|
|
31
|
+
'deviceName',
|
|
32
|
+
'datapoint',
|
|
33
|
+
'channelType',
|
|
34
|
+
'deviceType',
|
|
35
|
+
'iface',
|
|
36
|
+
];
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The exact key a filter is indexed by.
|
|
40
|
+
* @param {object} filter
|
|
41
|
+
* @returns {{attr: string, value: string}|null} null for a subscription that goes into the generic list
|
|
42
|
+
*/
|
|
43
|
+
function keyOf(filter) {
|
|
44
|
+
for (const attr of INDEXED_ATTRIBUTES) {
|
|
45
|
+
const value = filter[attr];
|
|
46
|
+
if (typeof value === 'string' && value !== '') {
|
|
47
|
+
return {attr, value};
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Whether a message passes a subscription's filter. This is the attribute loop of the old
|
|
56
|
+
* callCallback(), unchanged in what it compares; only the per-datapoint whitelist shortcut is
|
|
57
|
+
* gone (it skipped the comparison for a datapoint that had matched once).
|
|
58
|
+
* @param {object} filter
|
|
59
|
+
* @param {object} message
|
|
60
|
+
* @param {number|string} [id] for the trace lines
|
|
61
|
+
* @param {{trace: Function}} [logger]
|
|
62
|
+
* @returns {boolean}
|
|
63
|
+
*/
|
|
64
|
+
function matches(filter, message, id, logger) {
|
|
65
|
+
if (!filter) {
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Checked before the attribute loop: only present in the filter when a node opted in, so
|
|
70
|
+
// existing subscriptions keep receiving values the ReGa could not timestamp (#96)
|
|
71
|
+
if (filter.uncertain === false && message.uncertain) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const attributes = Object.keys(filter);
|
|
76
|
+
|
|
77
|
+
for (let i = 0, {length} = attributes; i < length; i++) {
|
|
78
|
+
const attr = attributes[i];
|
|
79
|
+
|
|
80
|
+
if (attr === 'cache') {
|
|
81
|
+
// if filter.cache==false - Drop messages with msg.cache==true
|
|
82
|
+
if (!filter.cache && message.cache) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (attr === 'change') {
|
|
90
|
+
// if filter.change==true - Drop messages with msg.change==false - except msg.cache==true && filter.cache==true
|
|
91
|
+
if (filter.change && !message.change && !(filter.cache && message.cache)) {
|
|
92
|
+
return false;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
if (attr === 'stable') {
|
|
99
|
+
// if filter.stable==true - Drop messages with msg.stable==false
|
|
100
|
+
if (filter.stable && !message.stable) {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
continue;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (attr === 'uncertain') {
|
|
108
|
+
// already decided above, and not a value to compare
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (filter[attr] === '') {
|
|
113
|
+
// an empty value filters nothing
|
|
114
|
+
continue;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (attr === 'channelIndex' && typeof filter[attr] !== 'undefined') {
|
|
118
|
+
filter[attr] = Number.parseInt(filter[attr], 10);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (Array.isArray(message[attr])) {
|
|
122
|
+
if (filter[attr] instanceof RegExp) {
|
|
123
|
+
if (logger) {
|
|
124
|
+
logger.trace('cb test regex array', id, attr, filter[attr], message[attr]);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!message[attr].some((item) => filter[attr].test(item))) {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
130
|
+
} else if (!message[attr].includes(filter[attr])) {
|
|
131
|
+
if (logger) {
|
|
132
|
+
logger.trace('cb mismatch array', id, attr, filter[attr], message[attr]);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return false;
|
|
136
|
+
}
|
|
137
|
+
} else if (filter[attr] instanceof RegExp) {
|
|
138
|
+
if (!filter[attr].test(message[attr])) {
|
|
139
|
+
if (logger) {
|
|
140
|
+
logger.trace('cb mismatch regex', id, attr, filter[attr], message[attr]);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
} else if (filter[attr] !== message[attr]) {
|
|
146
|
+
if (logger) {
|
|
147
|
+
logger.trace('cb mismatch misc ' + id + ' ' + attr + ' ' + filter[attr] + ' ' + message[attr]);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return true;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
class SubscriptionIndex {
|
|
158
|
+
constructor() {
|
|
159
|
+
/** @type {Map<number, {filter: object, callback: Function, key: {attr: string, value: string}|null}>} */
|
|
160
|
+
this.subscriptions = new Map();
|
|
161
|
+
/** @type {Map<string, Map<string, Set<number>>>} attribute -> value -> ids */
|
|
162
|
+
this.buckets = new Map();
|
|
163
|
+
/** @type {Set<number>} the subscriptions without an exact key */
|
|
164
|
+
this.generic = new Set();
|
|
165
|
+
this.nextId = 0;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
get size() {
|
|
169
|
+
return this.subscriptions.size;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* @param {object} filter
|
|
174
|
+
* @param {Function} callback
|
|
175
|
+
* @returns {number} the subscription id
|
|
176
|
+
*/
|
|
177
|
+
add(filter, callback) {
|
|
178
|
+
const id = this.nextId;
|
|
179
|
+
this.nextId += 1;
|
|
180
|
+
const key = keyOf(filter);
|
|
181
|
+
this.subscriptions.set(id, {filter, callback, key});
|
|
182
|
+
|
|
183
|
+
if (key) {
|
|
184
|
+
let byValue = this.buckets.get(key.attr);
|
|
185
|
+
if (!byValue) {
|
|
186
|
+
byValue = new Map();
|
|
187
|
+
this.buckets.set(key.attr, byValue);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
let ids = byValue.get(key.value);
|
|
191
|
+
if (!ids) {
|
|
192
|
+
ids = new Set();
|
|
193
|
+
byValue.set(key.value, ids);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
ids.add(id);
|
|
197
|
+
} else {
|
|
198
|
+
this.generic.add(id);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return id;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* @param {number|string} id
|
|
206
|
+
* @returns {boolean} whether the subscription existed
|
|
207
|
+
*/
|
|
208
|
+
remove(id) {
|
|
209
|
+
const subscription = this.subscriptions.get(Number(id));
|
|
210
|
+
if (!subscription) {
|
|
211
|
+
return false;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
id = Number(id);
|
|
215
|
+
this.subscriptions.delete(id);
|
|
216
|
+
const {key} = subscription;
|
|
217
|
+
if (key) {
|
|
218
|
+
const byValue = this.buckets.get(key.attr);
|
|
219
|
+
const ids = byValue && byValue.get(key.value);
|
|
220
|
+
if (ids) {
|
|
221
|
+
ids.delete(id);
|
|
222
|
+
if (ids.size === 0) {
|
|
223
|
+
byValue.delete(key.value);
|
|
224
|
+
if (byValue.size === 0) {
|
|
225
|
+
this.buckets.delete(key.attr);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
this.generic.delete(id);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* @param {number|string} id
|
|
238
|
+
* @returns {{filter: object, callback: Function, key: object|null}|undefined}
|
|
239
|
+
*/
|
|
240
|
+
get(id) {
|
|
241
|
+
return this.subscriptions.get(Number(id));
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* The ids whose filter can match the message - those indexed by one of the message's own
|
|
246
|
+
* attribute values, and the generic ones - in subscription order, as every subscription was
|
|
247
|
+
* called in before.
|
|
248
|
+
* @param {object} message
|
|
249
|
+
* @returns {number[]}
|
|
250
|
+
*/
|
|
251
|
+
candidates(message) {
|
|
252
|
+
const ids = [];
|
|
253
|
+
for (const [attr, byValue] of this.buckets) {
|
|
254
|
+
const value = message[attr];
|
|
255
|
+
if (typeof value === 'string') {
|
|
256
|
+
const bucket = byValue.get(value);
|
|
257
|
+
if (bucket) {
|
|
258
|
+
for (const id of bucket) {
|
|
259
|
+
ids.push(id);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
for (const id of this.generic) {
|
|
266
|
+
ids.push(id);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
if (ids.length > 1) {
|
|
270
|
+
ids.sort((a, b) => a - b);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return ids;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* The number of index entries - what the memory grows with. Equals size: one entry per
|
|
278
|
+
* subscription, in a bucket or in the generic list.
|
|
279
|
+
* @returns {number}
|
|
280
|
+
*/
|
|
281
|
+
entries() {
|
|
282
|
+
let count = this.generic.size;
|
|
283
|
+
for (const byValue of this.buckets.values()) {
|
|
284
|
+
for (const ids of byValue.values()) {
|
|
285
|
+
count += ids.size;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
return count;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* For a `cache: true` subscription: the cached values it has to be replayed with. A filter that
|
|
294
|
+
* names the datapoint exactly (`datapointName`, or `iface` + `channel` + `datapoint`) is one
|
|
295
|
+
* lookup; any other walks the values, testing the exact key before the full filter.
|
|
296
|
+
* @param {object} filter
|
|
297
|
+
* @param {object} values datapointName -> cached message
|
|
298
|
+
* @returns {object[]} the cached messages that can match, in the values' order
|
|
299
|
+
*/
|
|
300
|
+
static replayCandidates(filter, values) {
|
|
301
|
+
const key = keyOf(filter);
|
|
302
|
+
let datapointName;
|
|
303
|
+
if (key && key.attr === 'datapointName') {
|
|
304
|
+
datapointName = key.value;
|
|
305
|
+
} else if (
|
|
306
|
+
typeof filter.iface === 'string' &&
|
|
307
|
+
filter.iface !== '' &&
|
|
308
|
+
typeof filter.channel === 'string' &&
|
|
309
|
+
filter.channel !== '' &&
|
|
310
|
+
typeof filter.datapoint === 'string' &&
|
|
311
|
+
filter.datapoint !== ''
|
|
312
|
+
) {
|
|
313
|
+
datapointName = filter.iface + '.' + filter.channel + '.' + filter.datapoint;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (datapointName !== undefined) {
|
|
317
|
+
const value = values[datapointName];
|
|
318
|
+
return value ? [value] : [];
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const result = [];
|
|
322
|
+
for (const name of Object.keys(values)) {
|
|
323
|
+
const value = values[name];
|
|
324
|
+
if (!key || value[key.attr] === key.value) {
|
|
325
|
+
result.push(value);
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
return result;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
module.exports = {SubscriptionIndex, keyOf, matches, INDEXED_ATTRIBUTES};
|