node-red-contrib-ccu 4.7.2 → 4.9.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.
@@ -19,7 +19,9 @@ 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 {Pairings} = require('./lib/pairing.js');
22
23
  const {writeJsonAtomic, DebouncedSave} = require('./lib/cachefile.js');
24
+ const {SubscriptionIndex, matches: filterMatches} = require('./lib/subscriptions.js');
23
25
  const xmlrpc = require('homematic-xmlrpc');
24
26
  const binrpc = require('binrpc');
25
27
 
@@ -164,6 +166,42 @@ module.exports = function (RED) {
164
166
  .catch((error) => res.status(500).send(JSON.stringify({error: error.message})));
165
167
  });
166
168
 
169
+ // pairing with an openccu-lite system from the connection node's dialog (lib/pairing.js) - for a
170
+ // Node-RED elsewhere; on the system itself the connection configures itself and pairs nothing
171
+ const pairings = new Pairings({
172
+ load: lite.load,
173
+ isLocal: (host) => isLocalCcu(host),
174
+ appVersion: pkg.version,
175
+ logger: {
176
+ info: (message) => RED.log.info('ccu: ' + message),
177
+ warn: (message) => RED.log.warn('ccu: ' + message),
178
+ },
179
+ });
180
+
181
+ RED.httpAdmin.post('/ccu/pair', RED.auth.needsPermission('ccu.write'), (request, res) => {
182
+ const {host, tls, port} = request.body;
183
+ try {
184
+ const id = pairings.start({host, tls: tls === true || tls === 'true', port});
185
+ res.status(202).json({id});
186
+ } catch (error) {
187
+ res.status(error.status).json({error: error.code, message: error.message});
188
+ }
189
+ });
190
+
191
+ RED.httpAdmin.get('/ccu/pair/:id', RED.auth.needsPermission('ccu.write'), (request, res) => {
192
+ const view = pairings.get(request.params.id);
193
+ if (view) {
194
+ res.status(200).json(view);
195
+ } else {
196
+ res.status(404).json({error: 'not-found', message: 'No such pairing request (it ended or was withdrawn).'});
197
+ }
198
+ });
199
+
200
+ RED.httpAdmin.delete('/ccu/pair/:id', RED.auth.needsPermission('ccu.write'), (request, res) => {
201
+ pairings.cancel(request.params.id);
202
+ res.status(204).end();
203
+ });
204
+
167
205
  RED.httpAdmin.get('/ccu', RED.auth.needsPermission('ccu.read'), (request, res) => {
168
206
  if (request.query.config && request.query.config !== '_ADD_') {
169
207
  const config = RED.nodes.getNode(request.query.config);
@@ -682,6 +720,9 @@ module.exports = function (RED) {
682
720
  this.inSecure = inSecure;
683
721
  this.metaPort = Number.parseInt(config.metaPort, 10) || (tls ? 443 : 80);
684
722
  this.metaToken = config.metaToken || metaProvider.readLocalToken();
723
+ // the system's certificate as pinned at pairing (the dialog fills it): the metadata
724
+ // api and lite-rpc accept exactly that one, whatever "ignore invalid certificates" says
725
+ this.fingerprint256 = tls ? String((this.credentials && this.credentials.fingerprint256) || '').trim() : '';
685
726
  // task 14: the way to the interfaces on openccu-lite - `auto` (the default) goes through
686
727
  // the system's lite-rpc where the system is openccu-lite and a credential allows it,
687
728
  // `openccu-lite` insists on it, `ccu` keeps the direct connection with callbacks there
@@ -719,13 +760,19 @@ module.exports = function (RED) {
719
760
  this.paramsQueue = [];
720
761
 
721
762
  this.paramsetFile = path.join(RED.settings.userDir || path.join(__dirname, '..'), 'paramsets.json');
722
-
723
- this.loadParamsets();
724
-
725
- this.callbacks = {};
726
- this.idCallback = 0;
727
- this.callbackBlacklists = {};
728
- this.callbackWhitelists = {};
763
+ // task 28: the descriptions in memory are those of the device types present (and
764
+ // whatever was fetched); the file on disk keeps them all. Fetched since the last
765
+ // save, merged into the file by saveParamsets():
766
+ this.paramsetNew = {};
767
+ // names the file does not have, so a miss does not parse it again
768
+ this.paramsetFileMissing = new Set();
769
+ this.paramsetFilePass = null;
770
+ this.paramsetDescriptions = {};
771
+
772
+ // task 27: the datapoint subscriptions, indexed by their exact key - memory grows with
773
+ // the subscriptions, not with datapoints x subscriptions as the per-datapoint
774
+ // whitelists/blacklists did
775
+ this.subscriptions = new SubscriptionIndex();
729
776
 
730
777
  this.sysvarCallbacks = {};
731
778
  this.idSysvarCallback = 0;
@@ -787,6 +834,8 @@ module.exports = function (RED) {
787
834
  this.metadataSave = new DebouncedSave(() => this.writeMetadata(), 1000);
788
835
 
789
836
  this.loadMetadata();
837
+ // after the metadata: the descriptions of the device table's types are what is loaded
838
+ this.loadParamsets();
790
839
  this.loadRegadata();
791
840
  this.loadValues();
792
841
  this.pruneCaches();
@@ -987,7 +1036,7 @@ module.exports = function (RED) {
987
1036
  this.logger.debug(
988
1037
  'stats rpc rx: ' + JSON.stringify(this.rxCounters) + ' tx: ' + JSON.stringify(this.txCounters),
989
1038
  );
990
- this.logger.debug('stats rpc subscribers ' + Object.keys(this.callbacks).length);
1039
+ this.logger.debug('stats rpc subscribers ' + this.subscriptions.size);
991
1040
  this.logger.debug(
992
1041
  'stats rega subscribers ' +
993
1042
  (Object.keys(this.programCallbacks).length + Object.keys(this.sysvarCallbacks).length),
@@ -1291,44 +1340,180 @@ module.exports = function (RED) {
1291
1340
  }
1292
1341
 
1293
1342
  /**
1294
- *
1343
+ * Merges the descriptions fetched since the last save into the file on disk. The file
1344
+ * holds every description ever known (the vendored ones and the fetched ones, in the
1345
+ * format other consumers and the editor read); the memory holds only the present types,
1346
+ * so the file is read, merged and written back atomically (task 28).
1295
1347
  * @returns {Promise<any>}
1296
1348
  */
1297
1349
  saveParamsets() {
1298
1350
  return new Promise((resolve) => {
1299
- fs.writeFileSync(this.paramsetFile, JSON.stringify(this.paramsetDescriptions, null, ' '));
1300
- this.logger.info(
1301
- 'paramsets saved to',
1302
- this.paramsetFile,
1303
- this.paramsetDescriptions ? Object.keys(this.paramsetDescriptions).length : 0,
1304
- );
1351
+ const names = Object.keys(this.paramsetNew);
1352
+ if (names.length === 0) {
1353
+ resolve();
1354
+ return;
1355
+ }
1356
+
1357
+ try {
1358
+ const all = this.readParamsetFile() || {};
1359
+ for (const name of names) {
1360
+ all[name] = this.paramsetNew[name];
1361
+ }
1362
+
1363
+ writeJsonAtomic(this.paramsetFile, all, ' ');
1364
+ this.paramsetNew = {};
1365
+ this.logger.info(
1366
+ 'paramsets saved to',
1367
+ this.paramsetFile,
1368
+ names.length,
1369
+ 'new,',
1370
+ Object.keys(all).length,
1371
+ 'total',
1372
+ );
1373
+ } catch (error) {
1374
+ this.logger.error('error saving paramsets ' + error.message);
1375
+ }
1376
+
1305
1377
  resolve();
1306
1378
  });
1307
1379
  }
1308
1380
 
1309
1381
  /**
1310
- *
1382
+ * Parses the paramsets file - the userDir's, or the vendored one when the userDir has none
1383
+ * yet (then it is copied there, as before, so the format on disk stays the same).
1384
+ * @returns {object|null} every description the file holds, or null when there is none
1385
+ */
1386
+ readParamsetFile() {
1387
+ if (!fs.existsSync(this.paramsetFile)) {
1388
+ const vendored = path.join(__dirname, '..', 'paramsets.json');
1389
+ try {
1390
+ fs.copyFileSync(vendored, this.paramsetFile);
1391
+ this.logger.info('paramsets copied from', vendored, 'to', this.paramsetFile);
1392
+ } catch (error) {
1393
+ this.logger.error('error copying paramsets ' + error.message);
1394
+ }
1395
+ }
1396
+
1397
+ try {
1398
+ return JSON.parse(fs.readFileSync(this.paramsetFile));
1399
+ } catch (error) {
1400
+ if (error.code === 'ENOENT') {
1401
+ this.logger.info('paramsets new empty');
1402
+ } else {
1403
+ this.logger.error('error loading paramsets ' + error.message);
1404
+ }
1405
+
1406
+ return null;
1407
+ }
1408
+ }
1409
+
1410
+ /**
1411
+ * The paramset names the device table needs: every PARAMSETS entry of every device and
1412
+ * channel of every interface.
1413
+ * @returns {Set<string>}
1414
+ */
1415
+ neededParamsetNames() {
1416
+ const names = new Set();
1417
+ const tables = (this.metadata && this.metadata.devices) || {};
1418
+ for (const iface of Object.keys(tables)) {
1419
+ for (const address of Object.keys(tables[iface])) {
1420
+ const device = tables[iface][address];
1421
+ if (device && Array.isArray(device.PARAMSETS)) {
1422
+ for (const paramset of device.PARAMSETS) {
1423
+ const name = this.paramsetName(iface, device, paramset);
1424
+ if (name) {
1425
+ names.add(name);
1426
+ }
1427
+ }
1428
+ }
1429
+ }
1430
+ }
1431
+
1432
+ return names;
1433
+ }
1434
+
1435
+ /**
1436
+ * Takes the descriptions the device table needs (and `extra`) from the file into memory;
1437
+ * one parse of the file per call. A name the file does not have is remembered, so the
1438
+ * queue asks the interface for it without another parse.
1439
+ * @param {Iterable<string>} [extra] further names
1440
+ * @returns {number} how many descriptions came in
1441
+ */
1442
+ paramsetsFromFile(extra) {
1443
+ const needed = this.neededParamsetNames();
1444
+ for (const name of extra || []) {
1445
+ needed.add(name);
1446
+ }
1447
+
1448
+ const all = this.readParamsetFile();
1449
+ let loaded = 0;
1450
+ for (const name of needed) {
1451
+ if (this.paramsetDescriptions[name]) {
1452
+ continue;
1453
+ }
1454
+
1455
+ if (all && all[name]) {
1456
+ this.paramsetDescriptions[name] = all[name];
1457
+ this.paramsetFileMissing.delete(name);
1458
+ loaded += 1;
1459
+ } else {
1460
+ this.paramsetFileMissing.add(name);
1461
+ }
1462
+ }
1463
+
1464
+ this.logger.info(
1465
+ 'paramsets loaded from',
1466
+ this.paramsetFile,
1467
+ loaded,
1468
+ 'of',
1469
+ all ? Object.keys(all).length : 0,
1470
+ '(' + Object.keys(this.paramsetDescriptions).length + ' in memory for the device types present)',
1471
+ );
1472
+ return loaded;
1473
+ }
1474
+
1475
+ /**
1476
+ * One file pass on the next turn of the event loop, however often it is asked for until then.
1477
+ */
1478
+ paramsetFilePassSoon() {
1479
+ if (this.paramsetFilePass) {
1480
+ return;
1481
+ }
1482
+
1483
+ this.paramsetFilePass = setImmediate(() => {
1484
+ this.paramsetFilePass = null;
1485
+ try {
1486
+ this.paramsetsFromFile();
1487
+ } catch (error) {
1488
+ this.logger.error('error loading paramsets ' + error.message);
1489
+ }
1490
+ });
1491
+ }
1492
+
1493
+ /**
1494
+ * At start: the descriptions of the cached device table's types from the file. An obsolete
1495
+ * `ccu_paramsets_v2.json` (the format of 2019) still lying in the userDir is removed.
1311
1496
  * @returns {Promise<any>}
1312
1497
  */
1313
1498
  loadParamsets() {
1314
1499
  return new Promise((resolve) => {
1315
- const load = (file) => {
1316
- try {
1317
- this.paramsetDescriptions = JSON.parse(fs.readFileSync(file));
1318
- this.logger.info('paramsets loaded from', file);
1319
- } catch {
1320
- this.logger.info('paramsets new empty');
1321
- this.paramsetDescriptions = {};
1500
+ const obsolete = path.join(path.dirname(this.paramsetFile), 'ccu_paramsets_v2.json');
1501
+ try {
1502
+ if (fs.existsSync(obsolete)) {
1503
+ fs.unlinkSync(obsolete);
1504
+ this.logger.info('removed the obsolete', obsolete);
1322
1505
  }
1323
- };
1506
+ } catch (error) {
1507
+ this.logger.warn('could not remove ' + obsolete + ': ' + error.message);
1508
+ }
1324
1509
 
1325
- if (fs.existsSync(this.paramsetFile)) {
1326
- load(this.paramsetFile);
1327
- resolve();
1328
- } else {
1329
- load(path.join(__dirname, '..', 'paramsets.json'));
1330
- this.saveParamsets().then(resolve);
1510
+ try {
1511
+ this.paramsetsFromFile();
1512
+ } catch (error) {
1513
+ this.logger.error('error loading paramsets ' + error.message);
1331
1514
  }
1515
+
1516
+ resolve();
1332
1517
  });
1333
1518
  }
1334
1519
 
@@ -1361,6 +1546,9 @@ module.exports = function (RED) {
1361
1546
  });
1362
1547
  Object.keys(this.liveness).forEach((iface) => this.stopLiveness(iface));
1363
1548
  clearTimeout(this.valuesPruneTimer);
1549
+ clearTimeout(this.getParamsetTimeout);
1550
+ clearImmediate(this.paramsetFilePass);
1551
+ this.paramsetFilePass = null;
1364
1552
 
1365
1553
  this.metadataSave.flush();
1366
1554
  this.saveRegadata();
@@ -1465,6 +1653,7 @@ module.exports = function (RED) {
1465
1653
  port: this.metaPort,
1466
1654
  tls: Boolean(this.tlsEnabled),
1467
1655
  insecure: Boolean(this.inSecure),
1656
+ fingerprint256: this.fingerprint256,
1468
1657
  logger: this.logger,
1469
1658
  });
1470
1659
  }
@@ -1615,6 +1804,7 @@ module.exports = function (RED) {
1615
1804
  port: this.metaPort,
1616
1805
  tls: Boolean(this.tlsEnabled),
1617
1806
  insecure: Boolean(this.inSecure),
1807
+ fingerprint256: this.fingerprint256,
1618
1808
  token: this.metaToken,
1619
1809
  logger: this.logger,
1620
1810
  onNames: (names) => {
@@ -2470,6 +2660,7 @@ module.exports = function (RED) {
2470
2660
  url,
2471
2661
  token: this.liteToken,
2472
2662
  insecure: Boolean(this.inSecure),
2663
+ fingerprint256: this.fingerprint256,
2473
2664
  interfaces: ifaces,
2474
2665
  initId: (iface) => this.liteInitId(iface),
2475
2666
  logger: this.logger,
@@ -3349,6 +3540,12 @@ module.exports = function (RED) {
3349
3540
  if (device && device.PARAMSETS) {
3350
3541
  device.PARAMSETS.forEach((paramset) => {
3351
3542
  const name = this.paramsetName(iface, device, paramset);
3543
+ if (name && !this.paramsetDescriptions[name] && !this.paramsetFileMissing.has(name)) {
3544
+ // task 28: the file may have it - one pass after this burst of pushes (a
3545
+ // device list arrives as one callback), not one parse per device
3546
+ this.paramsetFilePassSoon();
3547
+ }
3548
+
3352
3549
  if (
3353
3550
  !this.paramsetDescriptions[name] &&
3354
3551
  // B-36: once per description key in the queue, and never again
@@ -3385,6 +3582,17 @@ module.exports = function (RED) {
3385
3582
  if (item) {
3386
3583
  const {iface, name, address, paramset} = item;
3387
3584
 
3585
+ if (!this.paramsetDescriptions[name] && name && !this.paramsetFileMissing.has(name)) {
3586
+ // task 28: the file first - one parse takes every name the device table
3587
+ // needs by now, the rest is asked of the interface
3588
+ try {
3589
+ this.paramsetsFromFile([name]);
3590
+ } catch (error) {
3591
+ this.logger.error('error loading paramsets ' + error.message);
3592
+ this.paramsetFileMissing.add(name);
3593
+ }
3594
+ }
3595
+
3388
3596
  if (this.paramsetDescriptions[name]) {
3389
3597
  //this.logger.trace('paramset', name, 'already known');
3390
3598
  this.paramsetPending = false;
@@ -3397,6 +3605,8 @@ module.exports = function (RED) {
3397
3605
  this.newParamsetDescriptionCount += 1;
3398
3606
  this.newParamsetDescription = true;
3399
3607
  this.paramsetDescriptions[name] = res;
3608
+ this.paramsetNew[name] = res;
3609
+ this.paramsetFileMissing.delete(name);
3400
3610
  if (this.newParamsetDescriptionCount >= 30) {
3401
3611
  this.newParamsetDescription = false;
3402
3612
  this.newParamsetDescriptionCount = 0;
@@ -4182,27 +4392,18 @@ module.exports = function (RED) {
4182
4392
  }
4183
4393
  }
4184
4394
 
4185
- const id = this.idCallback;
4186
- this.idCallback += 1;
4187
-
4188
- //this.logger.trace('subscribe', id, JSON.stringify(filter));
4189
- this.callbacks[id] = {filter, callback};
4395
+ const id = this.subscriptions.add(filter, callback);
4190
4396
 
4191
4397
  if (filter.cache && this.cachedValuesReceived) {
4192
- Object.keys(this.values).forEach((dp) => {
4193
- const message = {...this.values[dp]};
4398
+ // the replay goes through the index: one lookup for a filter that names its
4399
+ // datapoint, a walk over the cached values for the others (as before, but
4400
+ // nothing is remembered per datapoint any more)
4401
+ for (const cached of SubscriptionIndex.replayCandidates(filter, this.values)) {
4402
+ const message = {...cached};
4194
4403
  message.cache = true;
4195
4404
  message.change = false;
4196
- if (!this.callbackBlacklists[message.datapointName]) {
4197
- this.callbackBlacklists[message.datapointName] = new Set();
4198
- }
4199
-
4200
- if (!this.callbackWhitelists[message.datapointName]) {
4201
- this.callbackWhitelists[message.datapointName] = new Set();
4202
- }
4203
-
4204
4405
  this.callCallback(message, id);
4205
- });
4406
+ }
4206
4407
  }
4207
4408
 
4208
4409
  return id;
@@ -4214,18 +4415,8 @@ module.exports = function (RED) {
4214
4415
  * @returns {boolean}
4215
4416
  */
4216
4417
  unsubscribe(id) {
4217
- if (this.callbacks[id]) {
4418
+ if (this.subscriptions.remove(id)) {
4218
4419
  this.logger.trace('unsubscribe', id);
4219
- delete this.callbacks[id];
4220
-
4221
- Object.keys(this.callbackBlacklists).forEach((dp) => {
4222
- this.callbackBlacklists[dp].delete(id);
4223
- });
4224
-
4225
- Object.keys(this.callbackWhitelists).forEach((dp) => {
4226
- this.callbackWhitelists[dp].delete(id);
4227
- });
4228
-
4229
4420
  return true;
4230
4421
  }
4231
4422
 
@@ -4361,145 +4552,30 @@ module.exports = function (RED) {
4361
4552
  * @returns {boolean}
4362
4553
  */
4363
4554
  callCallback(message, id) {
4364
- const {filter, callback} = this.callbacks[id];
4365
- //this.logger.trace('filter', JSON.stringify(filter));
4366
-
4367
- let match = true;
4368
- let matchCache;
4369
- let matchChange;
4370
- let matchStable;
4371
-
4372
- // Checked before the attribute loop below: that loop may break
4373
- // early for a whitelisted datapoint, and this decision has to be
4374
- // made for every message regardless of key order. Only present in
4375
- // the filter when a node opted in, so existing subscriptions keep
4376
- // receiving values the ReGa could not timestamp (#96).
4377
- if (filter && filter.uncertain === false && message.uncertain) {
4555
+ const subscription = this.subscriptions.get(id);
4556
+ if (!subscription) {
4378
4557
  return false;
4379
4558
  }
4380
4559
 
4381
- if (filter) {
4382
- const arrayAttr = Object.keys(filter);
4383
-
4384
- for (let i = 0, {length} = arrayAttr; match && i < length; i++) {
4385
- const attr = arrayAttr[i];
4386
-
4387
- if (attr === 'cache') {
4388
- // if filter.cache==false - Drop messages with msg.cache==true
4389
- if (!filter.cache && message.cache) {
4390
- //this.logger.trace('cb mismatch cache ' + id + ' ' + filter.cache + ' ' + msg.cache);
4391
- return false;
4392
- }
4393
-
4394
- matchCache = true;
4395
- continue;
4396
- }
4397
-
4398
- if (attr === 'change') {
4399
- // if filter.change==true - Drop messages with msg.change==false - except msg.cache==true && filter.cache==true
4400
- if (filter.change && !message.change && !(filter.cache && message.cache)) {
4401
- //this.logger.trace('cb mismatch change ' + id + ' ' + filter.change + ' ' + msg.change + ' ' + msg.cache);
4402
- return false;
4403
- }
4404
-
4405
- matchChange = true;
4406
- continue;
4407
- }
4408
-
4409
- if (attr === 'stable') {
4410
- // if filter.stable==true - Drop messages with msg.stable==false
4411
- if (filter.stable && !message.stable) {
4412
- //this.logger.trace('cb mismatch stable ' + id + ' ' + filter.stable + ' ' + msg.stable);
4413
- return false;
4414
- }
4415
-
4416
- matchStable = true;
4417
- continue;
4418
- }
4419
-
4420
- if (attr === 'uncertain') {
4421
- // already decided above, and not a value to compare
4422
- continue;
4423
- }
4424
-
4425
- if (this.callbackWhitelists[message.datapointName].has(id)) {
4426
- if (matchCache && matchChange && matchStable) {
4427
- break;
4428
- }
4429
-
4430
- continue;
4431
- }
4432
-
4433
- if (filter[attr] === '') {
4434
- // TODO rethink
4435
- continue;
4436
- }
4437
-
4438
- if (attr === 'channelIndex' && typeof filter[attr] !== 'undefined') {
4439
- filter[attr] = Number.parseInt(filter[attr], 10);
4440
- }
4441
-
4442
- if (Array.isArray(message[attr])) {
4443
- if (filter[attr] instanceof RegExp) {
4444
- match = false;
4445
- this.logger.trace('cb test regex array', id, attr, filter[attr], message[attr]);
4446
- message[attr].forEach((item) => {
4447
- if (filter[attr].test(item)) {
4448
- match = true;
4449
- }
4450
- });
4451
- } else if (!message[attr].includes(filter[attr])) {
4452
- this.logger.trace('cb mismatch array', id, attr, filter[attr], message[attr]);
4453
- match = false;
4454
- }
4455
- } else if (filter[attr] instanceof RegExp) {
4456
- if (!filter[attr].test(message[attr])) {
4457
- this.logger.trace('cb mismatch regex', id, attr, filter[attr], message[attr]);
4458
- match = false;
4459
- }
4460
- } else if (filter[attr] !== message[attr]) {
4461
- this.logger.trace(
4462
- 'cb mismatch misc ' + id + ' ' + attr + ' ' + filter[attr] + ' ' + message[attr],
4463
- );
4464
- match = false;
4465
- }
4466
- }
4467
- }
4468
-
4469
- if (match) {
4470
- //this.logger.trace('callCallback ' + id + ' ' + msg.datapointName + ' ' + msg.value);
4471
- callback(RED.util.cloneMessage(message));
4472
- this.callbackWhitelists[message.datapointName].add(id);
4473
- } else {
4474
- //this.logger.trace('add to blacklist ' + id + ' ' + msg.datapointName);
4475
- this.callbackBlacklists[message.datapointName].add(id);
4560
+ const {filter, callback} = subscription;
4561
+ if (!filterMatches(filter, message, id, this.logger)) {
4562
+ return false;
4476
4563
  }
4477
4564
 
4478
- return match;
4565
+ callback(RED.util.cloneMessage(message));
4566
+ return true;
4479
4567
  }
4480
4568
 
4481
4569
  /**
4482
- * Apply msg to callCallback() for all (not blacklisted) callbacks
4570
+ * Apply msg to callCallback() for every subscription the index names as a candidate
4571
+ * for it (task 27: those keyed by one of the message's own attribute values, plus the
4572
+ * generic ones), in subscription order
4483
4573
  * @param msg
4484
4574
  */
4485
4575
  callCallbacks(message) {
4486
- //this.logger.trace('callCallbacks', this.callbacks.length, JSON.stringify({datapointName: msg.datapointName, value: msg.value, cache: msg.cache, change: msg.change, stable: msg.stable}));
4487
- if (!this.callbackBlacklists[message.datapointName]) {
4488
- this.callbackBlacklists[message.datapointName] = new Set();
4489
- }
4490
-
4491
- if (!this.callbackWhitelists[message.datapointName]) {
4492
- this.callbackWhitelists[message.datapointName] = new Set();
4576
+ for (const id of this.subscriptions.candidates(message)) {
4577
+ this.callCallback(message, id);
4493
4578
  }
4494
-
4495
- Object.keys(this.callbacks).forEach((key) => {
4496
- if (this.callbackBlacklists[message.datapointName].has(key)) {
4497
- //this.logger.trace('blacklistet ' + key + ' ' + msg.datapointName);
4498
- return;
4499
- }
4500
-
4501
- this.callCallback(message, key);
4502
- });
4503
4579
  }
4504
4580
 
4505
4581
  /**
@@ -4894,6 +4970,7 @@ module.exports = function (RED) {
4894
4970
  credentials: {
4895
4971
  password: {type: 'password'},
4896
4972
  metaToken: {type: 'password'},
4973
+ fingerprint256: {type: 'text'},
4897
4974
  },
4898
4975
  });
4899
4976
  };
package/nodes/lib/lite.js CHANGED
@@ -38,15 +38,15 @@ let modules = null;
38
38
 
39
39
  /**
40
40
  * occulite-client, loaded once. Throws with a readable message where it cannot be loaded.
41
- * @returns {{OccuLite: Function, callbackEvents: Function, nodeTransport: Function}}
41
+ * @returns {{OccuLite: Function, callbackEvents: Function, nodeTransport: Function, pair: Function, probe: Function}}
42
42
  */
43
43
  function load() {
44
44
  if (!modules) {
45
45
  try {
46
46
  const {OccuLite} = require('occulite-client');
47
47
  const {callbackEvents} = require('occulite-client/callbacks');
48
- const {nodeTransport} = require('occulite-client/node');
49
- modules = {OccuLite, callbackEvents, nodeTransport};
48
+ const {nodeTransport, pair, probe} = require('occulite-client/node');
49
+ modules = {OccuLite, callbackEvents, nodeTransport, pair, probe};
50
50
  } catch (error) {
51
51
  throw new Error(
52
52
  'occulite-client cannot be loaded (' +
@@ -203,12 +203,27 @@ function tierAwareTransport(transport) {
203
203
  };
204
204
  }
205
205
 
206
+ /**
207
+ * The certificate trust for occulite-client's transport: the fingerprint pinned at pairing, else
208
+ * any certificate where the node says so, else Node's own store.
209
+ * @param {{fingerprint256?: string, insecure?: boolean}} options
210
+ * @returns {object}
211
+ */
212
+ function trustOf({fingerprint256, insecure}) {
213
+ if (fingerprint256) {
214
+ return {fingerprint256};
215
+ }
216
+
217
+ return insecure ? {insecure: true} : {};
218
+ }
219
+
206
220
  class LiteConnection {
207
221
  /**
208
222
  * @param {object} options
209
223
  * @param {string} options.url the system, scheme and authority
210
224
  * @param {string} options.token the credential
211
225
  * @param {boolean} [options.insecure] accept any certificate
226
+ * @param {string} [options.fingerprint256] accept exactly this certificate (pinned at pairing; wins over insecure)
212
227
  * @param {string[]} options.interfaces the interfaces to follow (the enabled ones lite-rpc carries)
213
228
  * @param {function(string): string} options.initId the init id the connection's callback table expects per interface
214
229
  * @param {function(string, *, Array, Function)} options.onCallback (method, err, params, answer): a callback server's call
@@ -249,7 +264,7 @@ class LiteConnection {
249
264
  const clientOptions = {
250
265
  url: this.options.url,
251
266
  auth: {token: this.options.token},
252
- transport: tierAwareTransport(nodeTransport(this.options.insecure ? {insecure: true} : {})),
267
+ transport: tierAwareTransport(nodeTransport(trustOf(this.options))),
253
268
  interfaces: this.interfaces,
254
269
  values: true,
255
270
  meta: false,
@@ -357,6 +372,7 @@ module.exports = {
357
372
  readAddonToken,
358
373
  liteValue,
359
374
  tierAwareTransport,
375
+ trustOf,
360
376
  translateError,
361
377
  isFault,
362
378
  load,