node-red-contrib-ccu 4.12.0 → 4.14.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,6 +19,7 @@ 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 logic = require('./lib/logic.js');
22
23
  const {Pairings} = require('./lib/pairing.js');
23
24
  const {
24
25
  writeJsonAtomic,
@@ -29,6 +30,7 @@ const {
29
30
  DebouncedSave,
30
31
  } = require('./lib/cachefile.js');
31
32
  const {SubscriptionIndex, matches: filterMatches} = require('./lib/subscriptions.js');
33
+ const {deviceSummary: haDeviceSummary} = require('./lib/hadiscovery.js');
32
34
  const xmlrpc = require('homematic-xmlrpc');
33
35
  const binrpc = require('binrpc');
34
36
 
@@ -347,8 +349,20 @@ module.exports = function (RED) {
347
349
 
348
350
  case 'devices': {
349
351
  // ccu-homeassistant editor: every device (not channel) of every, or the
350
- // given, interface with its channels
352
+ // given, interface with its channels, their rooms and functions, and the
353
+ // units the Home Assistant discovery makes of them (task 33) - from the
354
+ // descriptions there are, none is fetched for this
351
355
  const all = (config.metadata && config.metadata.devices) || {};
356
+ const summary = {
357
+ devices: all,
358
+ generic: request.query.generic !== 'false',
359
+ channelName: (address) => config.channelNames[address],
360
+ description: (iface, address) => {
361
+ const channel = all[iface] && all[iface][address];
362
+ const key = channel && config.paramsetName(iface, channel, 'VALUES');
363
+ return key ? config.paramsetDescriptions[key] : undefined;
364
+ },
365
+ };
352
366
  const ifaces = request.query.iface ? [request.query.iface] : Object.keys(all);
353
367
  ifaces.forEach((iface) => {
354
368
  const devices = all[iface] || {};
@@ -369,7 +383,10 @@ module.exports = function (RED) {
369
383
  address: ch,
370
384
  name: config.channelNames[ch],
371
385
  type: devices[ch].TYPE,
386
+ rooms: (config.channelRooms && config.channelRooms[ch]) || [],
387
+ functions: (config.channelFunctions && config.channelFunctions[ch]) || [],
372
388
  })),
389
+ units: haDeviceSummary(summary, iface, device),
373
390
  };
374
391
  });
375
392
  });
@@ -759,6 +776,13 @@ module.exports = function (RED) {
759
776
  // and, per interface, why it cannot be reached (the nodes show it)
760
777
  this.liteScopes = undefined;
761
778
  this.ifaceError = {};
779
+ // task 32: Logic (lite-addon-logic), the interface of the system variables and programs on
780
+ // openccu-lite - its name once lite-rpc listed it, whether the list was read, the objects by
781
+ // their value channel, and the writes waiting for the first picture
782
+ this.logicIface = null;
783
+ this.liteListed = false;
784
+ this.logicObjects = {};
785
+ this.logicWaiters = [];
762
786
  this.metaMode = false;
763
787
  this.meta = null;
764
788
  // #167: minutes between re-reads of channel names, rooms and
@@ -1867,7 +1891,7 @@ module.exports = function (RED) {
1867
1891
  ') - names, rooms and functions come from its metadata api',
1868
1892
  );
1869
1893
  this.logger.info(
1870
- 'this system has no ReGaHSS: system variables, programs and HM-Script are not available (sysvar, program, poll and script nodes stay idle)',
1894
+ 'this system has no ReGaHSS: system variables and programs come from the Logic addon through lite-rpc where it is installed, HM-Script is not available (the script node stays idle)',
1871
1895
  );
1872
1896
  if (!this.regaEnabled) {
1873
1897
  // B-46: the checkbox has nothing to switch here - the names are the metadata api's
@@ -2050,6 +2074,7 @@ module.exports = function (RED) {
2050
2074
  ' functions',
2051
2075
  );
2052
2076
  this.saveRegadata();
2077
+ this.relabelLogic();
2053
2078
  }
2054
2079
 
2055
2080
  /**
@@ -2249,12 +2274,11 @@ module.exports = function (RED) {
2249
2274
  * @returns {Promise}
2250
2275
  */
2251
2276
  programActive(name, active) {
2252
- return new Promise((resolve, reject) => {
2253
- if (this.metaMode) {
2254
- reject(this.regaMissingError('programs'));
2255
- return;
2256
- }
2277
+ if (this.metaMode) {
2278
+ return this.logicProgramCall(name, 'ACTIVE', Boolean(active));
2279
+ }
2257
2280
 
2281
+ return new Promise((resolve, reject) => {
2258
2282
  const program = this.program[name];
2259
2283
  if (program) {
2260
2284
  const script = `dom.GetObject(${program.id}).Active(${active});`;
@@ -2281,12 +2305,11 @@ module.exports = function (RED) {
2281
2305
  * @returns {Promise}
2282
2306
  */
2283
2307
  programExecute(name) {
2284
- return new Promise((resolve, reject) => {
2285
- if (this.metaMode) {
2286
- reject(this.regaMissingError('programs'));
2287
- return;
2288
- }
2308
+ if (this.metaMode) {
2309
+ return this.logicProgramCall(name, 'RUN_THEN', true);
2310
+ }
2289
2311
 
2312
+ return new Promise((resolve, reject) => {
2290
2313
  const program = this.program[name];
2291
2314
  if (program) {
2292
2315
  const d = new Date();
@@ -2320,12 +2343,11 @@ module.exports = function (RED) {
2320
2343
  * @returns {Promise}
2321
2344
  */
2322
2345
  setVariable(name, value) {
2323
- return new Promise((resolve, reject) => {
2324
- if (this.metaMode) {
2325
- reject(this.regaMissingError('system variables'));
2326
- return;
2327
- }
2346
+ if (this.metaMode) {
2347
+ return this.setLogicVariable(name, value);
2348
+ }
2328
2349
 
2350
+ return new Promise((resolve, reject) => {
2329
2351
  if (!this.hasRegaVariables) {
2330
2352
  this.logger.debug('variables not yet known. defer setVariable ' + name);
2331
2353
  clearTimeout(this.setVariableQueueTimeout[name]);
@@ -2534,29 +2556,57 @@ module.exports = function (RED) {
2534
2556
  cache: isNew,
2535
2557
  });
2536
2558
 
2537
- Object.keys(this.sysvarCallbacks).forEach((key) => {
2538
- const {filter, callback} = this.sysvarCallbacks[key];
2539
- let match = !filter.name || filter.name === sysvar.name;
2540
- if (this.sysvar[sysvar.name].cache && !filter.cache) {
2559
+ this.dispatchSysvar(sysvar.name);
2560
+ }
2561
+ }
2562
+
2563
+ /**
2564
+ * A system variable's state to the sysvar subscriptions whose filter takes it: its name, a
2565
+ * value from the start (cache) only where the filter asks for one, a value that did not
2566
+ * change only where the filter does not ask for changes alone.
2567
+ * @param {string} name
2568
+ */
2569
+ dispatchSysvar(name) {
2570
+ const sysvar = this.sysvar[name];
2571
+ if (!sysvar) {
2572
+ return;
2573
+ }
2574
+
2575
+ Object.keys(this.sysvarCallbacks).forEach((key) => {
2576
+ const {filter, callback} = this.sysvarCallbacks[key];
2577
+ let match = !filter.name || filter.name === name;
2578
+ if (sysvar.cache && !filter.cache) {
2579
+ match = false;
2580
+ } else if (filter.change && !sysvar.change) {
2581
+ if (!(sysvar.cache && filter.cache)) {
2541
2582
  match = false;
2542
- } else if (filter.change && !this.sysvar[sysvar.name].change) {
2543
- if (!(this.sysvar[sysvar.name].cache && filter.cache)) {
2544
- match = false;
2545
- }
2546
2583
  }
2584
+ }
2547
2585
 
2548
- //this.logger.trace('match', match, JSON.stringify(filter), 'name:' + sysvar.name + ' cache:' + this.sysvar[sysvar.name].cache + ' change:' + this.sysvar[sysvar.name].change);
2549
- if (match) {
2550
- callback(RED.util.cloneMessage(this.sysvar[sysvar.name]));
2551
- }
2552
- });
2553
- }
2586
+ if (match) {
2587
+ callback(RED.util.cloneMessage(sysvar));
2588
+ }
2589
+ });
2554
2590
  }
2555
2591
 
2556
2592
  /**
2557
- * Poll ReGaHSS variables and call subscription callbacks
2558
- * @returns {Promise}
2593
+ * A program's state to the program subscriptions of its name (or of every program).
2594
+ * @param {string} name
2559
2595
  */
2596
+ dispatchProgram(name) {
2597
+ const program = this.program[name];
2598
+ if (!program) {
2599
+ return;
2600
+ }
2601
+
2602
+ Object.keys(this.programCallbacks).forEach((key) => {
2603
+ const {filter, callback} = this.programCallbacks[key];
2604
+ if (!filter.name || filter.name === name) {
2605
+ callback(program);
2606
+ }
2607
+ });
2608
+ }
2609
+
2560
2610
  /**
2561
2611
  * Re-read the ReGa metadata that only ever got fetched once, in the
2562
2612
  * constructor: channel names, rooms and functions. A room or function
@@ -2679,12 +2729,7 @@ module.exports = function (RED) {
2679
2729
  ts: prg.ts,
2680
2730
  tsPrevious: this.program[prg.name].ts,
2681
2731
  };
2682
- Object.keys(this.programCallbacks).forEach((key) => {
2683
- const {filter, callback} = this.programCallbacks[key];
2684
- if (!filter.name || filter.name === prg.name) {
2685
- callback(this.program[prg.name]);
2686
- }
2687
- });
2732
+ this.dispatchProgram(prg.name);
2688
2733
  }
2689
2734
  });
2690
2735
  resolve();
@@ -2866,7 +2911,9 @@ module.exports = function (RED) {
2866
2911
  this.meta.attach(box);
2867
2912
  }
2868
2913
  },
2869
- initId: (iface) => this.liteInitId(iface),
2914
+ // task 32: the client follows every interface the system lists; the stream's calls of an
2915
+ // interface this connection does not use (unticked) get an id no handler takes
2916
+ initId: (iface) => (this.liteWants(iface) ? this.liteInitId(iface) : 'nr_ignored'),
2870
2917
  logger: this.logger,
2871
2918
  createClient: this.liteCreateClient,
2872
2919
  onCallback: (method, err, parameters, callback) => {
@@ -2891,7 +2938,20 @@ module.exports = function (RED) {
2891
2938
  }
2892
2939
  },
2893
2940
  onInterface: (iface, state) => {
2894
- if (!ifaces.includes(iface) || this.closing) {
2941
+ if (this.closing) {
2942
+ return;
2943
+ }
2944
+
2945
+ if (state === 'removed' && iface === this.logicIface) {
2946
+ this.logicGone();
2947
+ return;
2948
+ }
2949
+
2950
+ if (state !== 'removed' && !this.liteWants(iface)) {
2951
+ return;
2952
+ }
2953
+
2954
+ if (!this.liteIfaces.includes(iface)) {
2895
2955
  return;
2896
2956
  }
2897
2957
 
@@ -2912,12 +2972,13 @@ module.exports = function (RED) {
2912
2972
  }
2913
2973
 
2914
2974
  this.logger.debug('lite-rpc client', state);
2975
+ const followed = this.liteIfaces.slice();
2915
2976
  if (state === 'ready') {
2916
- ifaces.forEach((iface) => this.setIfaceStatus(iface, running(iface), !running(iface)));
2977
+ followed.forEach((iface) => this.setIfaceStatus(iface, running(iface), !running(iface)));
2917
2978
  } else if (state === 'degraded' || state === 'offline' || state === 'connecting') {
2918
- ifaces.forEach((iface) => this.setIfaceStatus(iface, false, true));
2979
+ followed.forEach((iface) => this.setIfaceStatus(iface, false, true));
2919
2980
  } else if (state === 'closed') {
2920
- ifaces.forEach((iface) => this.setIfaceStatus(iface, false));
2981
+ followed.forEach((iface) => this.setIfaceStatus(iface, false));
2921
2982
  }
2922
2983
  },
2923
2984
  onValues: (values) => this.applyLiteValues(values),
@@ -2933,7 +2994,9 @@ module.exports = function (RED) {
2933
2994
  return;
2934
2995
  }
2935
2996
 
2936
- const up = ifaces.filter((iface) => running(iface));
2997
+ // task 32: the addon interfaces the system lists (Logic), followed without a checkbox
2998
+ this.liteAddonsListed();
2999
+ const up = this.liteIfaces.filter((iface) => running(iface));
2937
3000
  this.logger.info('lite-rpc connected: ' + (up.join(', ') || 'no interface running'));
2938
3001
  if (!this.lite.may('rpc:operate')) {
2939
3002
  // B-44: events and device lists, but every command refused
@@ -2989,6 +3052,7 @@ module.exports = function (RED) {
2989
3052
  this.ifaceStatus[iface] = null;
2990
3053
  this.setIfaceStatus(iface, false);
2991
3054
  });
3055
+ this.logicSettled();
2992
3056
  return;
2993
3057
  }
2994
3058
 
@@ -3010,7 +3074,7 @@ module.exports = function (RED) {
3010
3074
  let count = 0;
3011
3075
  values.forEach((entry, ref) => {
3012
3076
  const [iface, channel, datapoint] = String(ref).split('.');
3013
- if (!this.liteIfaces.includes(iface) || !channel || !datapoint) {
3077
+ if (!channel || !datapoint || !this.liteWants(iface)) {
3014
3078
  return;
3015
3079
  }
3016
3080
 
@@ -3025,10 +3089,15 @@ module.exports = function (RED) {
3025
3089
  });
3026
3090
  this.values[message.datapointName] = message;
3027
3091
  count += 1;
3092
+ if (iface === this.logicIface) {
3093
+ this.logicValue(iface, channel, datapoint, entry.value, {ts: ts || now(), picture: true});
3094
+ }
3095
+
3028
3096
  if (!datapoint.startsWith('PRESS_')) {
3029
3097
  this.callCallbacks(message);
3030
3098
  }
3031
3099
  });
3100
+ this.logicPictureDone();
3032
3101
  this.logger.debug('lite-rpc: ' + count + ' values from the state store');
3033
3102
  this.cachedValuesReceived = true;
3034
3103
  this.saveValues();
@@ -3044,6 +3113,504 @@ module.exports = function (RED) {
3044
3113
  }
3045
3114
  }
3046
3115
 
3116
+ /**
3117
+ * task 32: whether this connection uses an interface the system's lite-rpc carries - one whose
3118
+ * checkbox put it on lite-rpc, or an addon's interface (Logic), which needs no checkbox: it is
3119
+ * taken the first time the stream or the system's list names it. An interface this package
3120
+ * knows by name (CUxD, CCU-Jack, an unticked one) keeps what its checkbox says.
3121
+ * @param {string} iface
3122
+ * @returns {boolean}
3123
+ */
3124
+ liteWants(iface) {
3125
+ if (typeof iface !== 'string' || iface === '') {
3126
+ return false;
3127
+ }
3128
+
3129
+ if (this.liteIfaces.includes(iface)) {
3130
+ return true;
3131
+ }
3132
+
3133
+ if (this.ifaceTypes[iface] || iface === META_IFACE || !this.lite || this.closing) {
3134
+ return false;
3135
+ }
3136
+
3137
+ return this.liteAddonIface(iface);
3138
+ }
3139
+
3140
+ /**
3141
+ * task 32: an addon's interface the system lists, followed through lite-rpc from now on: its
3142
+ * devices, datapoints and events are those of any interface (the value and event nodes can
3143
+ * address them), and Logic's are the system variables and programs as well.
3144
+ * @param {string} iface
3145
+ * @returns {boolean} whether it is followed
3146
+ */
3147
+ liteAddonIface(iface) {
3148
+ if (!/^[\w.-]+$/.test(iface)) {
3149
+ this.logger.warn('lite-rpc: the interface "' + iface + '" has a name this package cannot follow');
3150
+ return false;
3151
+ }
3152
+
3153
+ const info =
3154
+ (this.lite &&
3155
+ this.lite.box &&
3156
+ Array.isArray(this.lite.box.interfaces) &&
3157
+ this.lite.box.interfaces.find((entry) => entry.name === iface)) ||
3158
+ {};
3159
+ this.ifaceTypes[iface] = {
3160
+ conf: 'addon',
3161
+ addonIface: true,
3162
+ addon: info.addon || '',
3163
+ protocol: 'lite-rpc',
3164
+ init: false,
3165
+ ping: false,
3166
+ enabled: true,
3167
+ };
3168
+ this.liteIfaces.push(iface);
3169
+ this.lite.follow(iface);
3170
+ if (!this.enabledIfaces.includes(iface)) {
3171
+ this.enabledIfaces.push(iface);
3172
+ }
3173
+
3174
+ this.ifaceStatus[iface] = null;
3175
+ const isLogic = logic.isLogicIface(iface, info);
3176
+ if (isLogic) {
3177
+ this.logicIface = iface;
3178
+ }
3179
+
3180
+ this.logger.info(
3181
+ 'lite-rpc: the interface ' +
3182
+ iface +
3183
+ (info.addon ? ' of the addon ' + info.addon : '') +
3184
+ ' is followed as well' +
3185
+ (isLogic ? ' - the system variables and programs' : ''),
3186
+ );
3187
+ this.pushStatus();
3188
+ return true;
3189
+ }
3190
+
3191
+ /**
3192
+ * task 32: the system's list of interfaces, read by the client at connect: every addon
3193
+ * interface in it is followed. Then it is known whether Logic is installed.
3194
+ */
3195
+ liteAddonsListed() {
3196
+ const list = this.lite && this.lite.box && this.lite.box.interfaces;
3197
+ if (Array.isArray(list)) {
3198
+ list.forEach((entry) => this.liteWants(entry && entry.name));
3199
+ }
3200
+
3201
+ this.liteListed = true;
3202
+ if (!this.logicIface) {
3203
+ this.logger.info(
3204
+ 'openccu-lite: the Logic addon is not installed - the sysvar and program nodes have nothing to reach until it is',
3205
+ );
3206
+ }
3207
+
3208
+ this.logicSettled();
3209
+ this.pushStatus();
3210
+ }
3211
+
3212
+ /**
3213
+ * task 32: the Logic interface was removed (the addon uninstalled): its variables and programs
3214
+ * are gone, the nodes say so.
3215
+ */
3216
+ logicGone() {
3217
+ const iface = this.logicIface;
3218
+ this.logger.warn('lite-rpc: the interface ' + iface + ' was removed - the Logic addon is gone');
3219
+ Object.keys(this.logicObjects).forEach((channel) => this.forgetLogicObject(channel));
3220
+ this.setIfaceStatus(iface, false);
3221
+ this.logicIface = null;
3222
+ this.pushStatus();
3223
+ }
3224
+
3225
+ /** task 32: whether Logic is installed is known now (or cannot be known): waiting writes go on */
3226
+ logicSettled() {
3227
+ const waiters = this.logicWaiters;
3228
+ this.logicWaiters = [];
3229
+ waiters.forEach((resolve) => resolve());
3230
+ }
3231
+
3232
+ /**
3233
+ * task 32: resolves once it is known whether Logic is installed and its picture is loaded, or
3234
+ * after `timeout` - a write in the first seconds after a deploy waits for it, as the CCU path
3235
+ * waits for the first poll of the ReGa.
3236
+ * @param {number} [timeout] ms
3237
+ * @returns {Promise<void>}
3238
+ */
3239
+ whenLogicKnown(timeout = 30000) {
3240
+ if (this.liteListed || !this.lite) {
3241
+ return Promise.resolve();
3242
+ }
3243
+
3244
+ return new Promise((resolve) => {
3245
+ const timer = setTimeout(resolve, timeout);
3246
+ this.logicWaiters.push(() => {
3247
+ clearTimeout(timer);
3248
+ resolve();
3249
+ });
3250
+ });
3251
+ }
3252
+
3253
+ /**
3254
+ * task 32: why system variables and programs cannot be reached on this openccu-lite system, or
3255
+ * null when they can (or it is not known yet).
3256
+ * @param {string} feature for the message, e.g. "system variables"
3257
+ * @returns {Error|null}
3258
+ */
3259
+ logicUnavailable(feature) {
3260
+ if (this.logicIface) {
3261
+ return null;
3262
+ }
3263
+
3264
+ if (!this.lite) {
3265
+ return new Error(
3266
+ feature +
3267
+ " are reached through the system's lite-rpc (the Logic addon's interface), which this connection does not use - see the log for why",
3268
+ );
3269
+ }
3270
+
3271
+ if (this.liteListed) {
3272
+ return new Error(feature + ' are not available: the Logic addon is not installed on this system');
3273
+ }
3274
+
3275
+ return null;
3276
+ }
3277
+
3278
+ /**
3279
+ * task 32: what the sysvar, program and poll nodes show: the ReGaHSS's state on a CCU; on
3280
+ * openccu-lite the Logic interface's, or why there is none.
3281
+ * @returns {{iface: string, error?: string, waiting?: boolean}}
3282
+ */
3283
+ sysvarSource() {
3284
+ if (!this.metaMode) {
3285
+ return {iface: 'ReGaHSS'};
3286
+ }
3287
+
3288
+ if (this.logicIface) {
3289
+ return {iface: this.logicIface};
3290
+ }
3291
+
3292
+ if (!this.lite) {
3293
+ return {iface: logic.LOGIC_IFACE, error: 'needs lite-rpc'};
3294
+ }
3295
+
3296
+ if (this.liteListed) {
3297
+ return {iface: logic.LOGIC_IFACE, error: 'Logic addon not installed'};
3298
+ }
3299
+
3300
+ return {iface: logic.LOGIC_IFACE, waiting: true};
3301
+ }
3302
+
3303
+ /**
3304
+ * task 32: the description of a datapoint of a Logic channel, where it was fetched.
3305
+ * @param {string} channel
3306
+ * @param {string} datapoint
3307
+ * @returns {object|undefined}
3308
+ */
3309
+ logicDescription(channel, datapoint) {
3310
+ const devices = this.metadata.devices[this.logicIface];
3311
+ const description = devices && devices[channel];
3312
+ if (!description) {
3313
+ return undefined;
3314
+ }
3315
+
3316
+ const psKey = this.paramsetName(this.logicIface, description, 'VALUES');
3317
+ const paramset = psKey && this.paramsetDescriptions[psKey];
3318
+ return paramset ? paramset[datapoint] : undefined;
3319
+ }
3320
+
3321
+ /**
3322
+ * task 32: a datapoint of Logic's interface - a variable's VALUE, a program's ACTIVE or
3323
+ * LAST_RUN - into the tables the sysvar and program nodes read (this.sysvar, this.program).
3324
+ * @param {string} iface
3325
+ * @param {string} channel
3326
+ * @param {string} datapoint
3327
+ * @param {*} value
3328
+ * @param {{ts: number, picture?: boolean}} options picture: from the state store at connect
3329
+ */
3330
+ logicValue(iface, channel, datapoint, value, {ts, picture = false}) {
3331
+ const ref = logic.parseAddress(channel);
3332
+ if (!ref) {
3333
+ return;
3334
+ }
3335
+
3336
+ if (ref.kind === 'variable') {
3337
+ if (datapoint === 'VALUE') {
3338
+ this.updateLogicVariable(ref, value, ts, picture);
3339
+ }
3340
+ } else if (datapoint === 'ACTIVE') {
3341
+ this.updateLogicProgram(ref, {active: Boolean(value)}, ts, picture);
3342
+ } else if (datapoint === 'LAST_RUN') {
3343
+ this.updateLogicProgram(ref, {ran: true}, ts, picture);
3344
+ }
3345
+ }
3346
+
3347
+ /**
3348
+ * task 32: the entry of a Logic object under its current name in `table` (this.sysvar or
3349
+ * this.program): a rename moves it, a name another object holds is not taken over.
3350
+ * @param {object} table
3351
+ * @param {object} ref from logic.parseAddress()
3352
+ * @returns {{entry: object|undefined, name: string}}
3353
+ */
3354
+ logicEntry(table, ref) {
3355
+ const name = logic.nameOf(this.channelNames, ref);
3356
+ const entry = this.logicObjects[ref.channel];
3357
+ if (entry && entry.name !== name) {
3358
+ if (table[entry.name] === entry) {
3359
+ delete table[entry.name];
3360
+ }
3361
+
3362
+ entry.name = name;
3363
+ }
3364
+
3365
+ if (entry) {
3366
+ table[name] = entry;
3367
+ }
3368
+
3369
+ return {entry, name};
3370
+ }
3371
+
3372
+ /**
3373
+ * task 32: a system variable's value. A write is an event at every write, so a value that did
3374
+ * not change is an update (change false), as the CCU's poll reported a new timestamp.
3375
+ * @param {object} ref
3376
+ * @param {*} value
3377
+ * @param {number} ts
3378
+ * @param {boolean} picture
3379
+ */
3380
+ updateLogicVariable(ref, value, ts, picture) {
3381
+ const {entry, name} = this.logicEntry(this.sysvar, ref);
3382
+ const shape = logic.variableShape(this.logicDescription(ref.channel, 'VALUE'), value);
3383
+ if (entry && picture && entry.ts >= ts) {
3384
+ // the picture again (a resync): nothing new
3385
+ return;
3386
+ }
3387
+
3388
+ const isNew = !entry;
3389
+ const sysvar = entry || {
3390
+ topic: '',
3391
+ ccu: this.host,
3392
+ iface: this.logicIface,
3393
+ type: 'SYSVAR',
3394
+ name,
3395
+ info: '',
3396
+ key: ref.key,
3397
+ address: ref.channel,
3398
+ };
3399
+ Object.assign(sysvar, {
3400
+ payload: value,
3401
+ value,
3402
+ valueType: shape.valueType,
3403
+ enum: shape.enum,
3404
+ unit: shape.unit,
3405
+ valueEnum: shape.enum[Number(value)],
3406
+ valuePrevious: isNew ? undefined : sysvar.value,
3407
+ valueEnumPrevious: isNew ? undefined : sysvar.valueEnum,
3408
+ ts,
3409
+ tsPrevious: isNew ? undefined : sysvar.ts,
3410
+ lc: isNew || sysvar.value !== value ? ts : sysvar.lc,
3411
+ lcPrevious: isNew ? undefined : sysvar.lc,
3412
+ change: isNew ? false : sysvar.value !== value,
3413
+ // a value from the start is the cache; the first event of a variable made later is an event
3414
+ cache: isNew && picture,
3415
+ });
3416
+ if (shape.min !== undefined) {
3417
+ sysvar.min = shape.min;
3418
+ }
3419
+
3420
+ if (shape.max !== undefined) {
3421
+ sysvar.max = shape.max;
3422
+ }
3423
+
3424
+ if (isNew) {
3425
+ this.logicObjects[ref.channel] = sysvar;
3426
+ this.sysvar[name] = sysvar;
3427
+ }
3428
+
3429
+ this.dispatchSysvar(name);
3430
+ }
3431
+
3432
+ /**
3433
+ * task 32: a program's state: ACTIVE, and LAST_RUN as the time it last ran (the CCU's
3434
+ * ProgramLastExecuteTime, `ts`). The state store's picture is said once per program, after
3435
+ * both datapoints are in (logicPictureDone).
3436
+ * @param {object} ref
3437
+ * @param {{active?: boolean, ran?: boolean}} change
3438
+ * @param {number} ts
3439
+ * @param {boolean} picture
3440
+ */
3441
+ updateLogicProgram(ref, change, ts, picture) {
3442
+ const {entry, name} = this.logicEntry(this.program, ref);
3443
+ const program = entry || {
3444
+ ccu: this.host,
3445
+ iface: this.logicIface,
3446
+ type: 'PROGRAM',
3447
+ name,
3448
+ key: ref.key,
3449
+ address: ref.channel,
3450
+ };
3451
+ const previous = {active: program.active, ts: program.ts};
3452
+ if (change.active !== undefined) {
3453
+ program.active = change.active;
3454
+ program.payload = change.active;
3455
+ program.value = change.active;
3456
+ }
3457
+
3458
+ if (change.ran) {
3459
+ program.ts = ts;
3460
+ }
3461
+
3462
+ if (!entry) {
3463
+ this.logicObjects[ref.channel] = program;
3464
+ this.program[name] = program;
3465
+ program.pictured = picture;
3466
+ }
3467
+
3468
+ if (picture) {
3469
+ program.pictured = true;
3470
+ return;
3471
+ }
3472
+
3473
+ if (previous.active === program.active && previous.ts === program.ts) {
3474
+ return;
3475
+ }
3476
+
3477
+ program.activePrevious = previous.active;
3478
+ program.tsPrevious = previous.ts;
3479
+ this.dispatchProgram(name);
3480
+ }
3481
+
3482
+ /**
3483
+ * task 32: the state store's picture is in: each program once to its nodes, as the CCU's
3484
+ * first poll does.
3485
+ */
3486
+ logicPictureDone() {
3487
+ Object.keys(this.program).forEach((name) => {
3488
+ const program = this.program[name];
3489
+ if (program.pictured) {
3490
+ delete program.pictured;
3491
+ this.dispatchProgram(name);
3492
+ }
3493
+ });
3494
+ }
3495
+
3496
+ /**
3497
+ * task 32: the names changed - a variable or program that was renamed (or whose name came
3498
+ * after its value) is found by its new name; a node waiting for it gets the value it would
3499
+ * have got at the start.
3500
+ */
3501
+ relabelLogic() {
3502
+ Object.keys(this.logicObjects).forEach((channel) => {
3503
+ const entry = this.logicObjects[channel];
3504
+ const ref = logic.parseAddress(channel);
3505
+ const before = entry.name;
3506
+ const {name} = this.logicEntry(entry.type === 'SYSVAR' ? this.sysvar : this.program, ref);
3507
+ if (name === before) {
3508
+ return;
3509
+ }
3510
+
3511
+ this.logger.debug(
3512
+ 'Logic',
3513
+ channel,
3514
+ 'is',
3515
+ JSON.stringify(name),
3516
+ 'now (was',
3517
+ JSON.stringify(before) + ')',
3518
+ );
3519
+ if (entry.type === 'SYSVAR') {
3520
+ const {cache, change} = entry;
3521
+ entry.cache = true;
3522
+ entry.change = false;
3523
+ this.dispatchSysvar(name);
3524
+ entry.cache = cache;
3525
+ entry.change = change;
3526
+ } else {
3527
+ this.dispatchProgram(name);
3528
+ }
3529
+ });
3530
+ }
3531
+
3532
+ /**
3533
+ * task 32: a variable or program deleted in Logic (its device, or its value channel).
3534
+ * @param {string} address
3535
+ */
3536
+ forgetLogicObject(address) {
3537
+ const channel = String(address).includes(':') ? address : address + ':1';
3538
+ const entry = this.logicObjects[channel];
3539
+ if (!entry) {
3540
+ return;
3541
+ }
3542
+
3543
+ delete this.logicObjects[channel];
3544
+ const table = entry.type === 'SYSVAR' ? this.sysvar : this.program;
3545
+ if (table[entry.name] === entry) {
3546
+ delete table[entry.name];
3547
+ }
3548
+ }
3549
+
3550
+ /**
3551
+ * task 32: a write to a system variable on openccu-lite - setValue of VALUE on Logic's
3552
+ * interface, cast as the CCU path casts it; the addon refuses what the variable does not take.
3553
+ * @param {string} name
3554
+ * @param {*} value
3555
+ * @returns {Promise<object>} the variable
3556
+ */
3557
+ setLogicVariable(name, value) {
3558
+ return this.whenLogicKnown().then(() => {
3559
+ const unavailable = this.logicUnavailable('system variables');
3560
+ if (unavailable) {
3561
+ throw unavailable;
3562
+ }
3563
+
3564
+ const sysvar = this.sysvar[name];
3565
+ if (!sysvar || sysvar.iface !== this.logicIface) {
3566
+ throw new Error('setVariable ' + name + ' unknown');
3567
+ }
3568
+
3569
+ const shape = logic.variableShape(this.logicDescription(sysvar.address, 'VALUE'), sysvar.value);
3570
+ const cast = logic.castVariable(value, shape);
3571
+ this.logger.debug('setVariable', name, sysvar.address, JSON.stringify(cast));
3572
+ return this.methodCall(this.logicIface, 'setValue', [sysvar.address, 'VALUE', cast]).then(() => sysvar);
3573
+ });
3574
+ }
3575
+
3576
+ /**
3577
+ * task 32: a program on openccu-lite: ACTIVE, or RUN_THEN - what the CCU's ProgramExecute()
3578
+ * does, the first "Then" without the conditions (the addon's interface calls it the CCU's
3579
+ * "Start"); RUN would evaluate the conditions first.
3580
+ * @param {string} name
3581
+ * @param {string} datapoint ACTIVE | RUN_THEN
3582
+ * @param {boolean} value
3583
+ * @returns {Promise<object>} the program
3584
+ */
3585
+ logicProgramCall(name, datapoint, value) {
3586
+ return this.whenLogicKnown().then(() => {
3587
+ const unavailable = this.logicUnavailable('programs');
3588
+ if (unavailable) {
3589
+ throw unavailable;
3590
+ }
3591
+
3592
+ const program = this.program[name];
3593
+ if (!program || program.iface !== this.logicIface) {
3594
+ throw new Error(
3595
+ (datapoint === 'ACTIVE' ? 'programActive ' : 'programExecute ') + name + ' not found',
3596
+ );
3597
+ }
3598
+
3599
+ return this.methodCall(this.logicIface, 'setValue', [program.address, datapoint, value]).then(() => {
3600
+ if (datapoint === 'ACTIVE') {
3601
+ program.activePrevious = program.active;
3602
+ program.active = value;
3603
+ program.payload = value;
3604
+ program.value = value;
3605
+ } else {
3606
+ program.ts = now();
3607
+ }
3608
+
3609
+ return program;
3610
+ });
3611
+ });
3612
+ }
3613
+
3047
3614
  /**
3048
3615
  *
3049
3616
  * @param iface
@@ -3312,7 +3879,10 @@ module.exports = function (RED) {
3312
3879
  */
3313
3880
  rereadDevices() {
3314
3881
  const ifaces = Object.keys(this.ifaceTypes).filter(
3315
- (iface) => this.ifaceTypes[iface].enabled && this.ifaceTypes[iface].init && iface !== 'CUxD',
3882
+ (iface) =>
3883
+ this.ifaceTypes[iface].enabled &&
3884
+ (this.ifaceTypes[iface].init || this.ifaceTypes[iface].addonIface) &&
3885
+ iface !== 'CUxD',
3316
3886
  );
3317
3887
  this.logger.info('re-reading device data:', ifaces.join(', '));
3318
3888
  return Promise.all(
@@ -3996,6 +4566,9 @@ module.exports = function (RED) {
3996
4566
  */
3997
4567
  deleteDevice(iface, device) {
3998
4568
  this.logger.debug('deleteDevice', iface, device);
4569
+ if (iface === this.logicIface) {
4570
+ this.forgetLogicObject(device);
4571
+ }
3999
4572
  const known = this.metadata.devices[iface] && this.metadata.devices[iface][device];
4000
4573
  if (this.metadata.devices[iface]) {
4001
4574
  delete this.metadata.devices[iface][device];
@@ -4204,7 +4777,7 @@ module.exports = function (RED) {
4204
4777
  return idInit;
4205
4778
  }
4206
4779
 
4207
- const match = idInit.match(/^nr_[\da-zA-Z]{6}_([a-zA-Z-]+)$/);
4780
+ const match = idInit.match(/^nr_[\da-zA-Z]{6}_([\w.-]+)$/);
4208
4781
  return match && this.ifaceTypes[match[1]] ? match[1] : null;
4209
4782
  }
4210
4783
 
@@ -4709,6 +5282,11 @@ module.exports = function (RED) {
4709
5282
  return;
4710
5283
  }
4711
5284
 
5285
+ if (iface === this.logicIface) {
5286
+ // task 32: a variable's or a program's datapoint is the sysvar or program nodes' event too
5287
+ this.logicValue(iface, channel, datapoint, payload, {ts: now()});
5288
+ }
5289
+
4712
5290
  //this.logger.trace('publishEvent', JSON.stringify(params));
4713
5291
 
4714
5292
  const message = this.createMessage(iface, channel, datapoint, payload, {