-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathzwave-node.js
544 lines (486 loc) · 17.7 KB
/
zwave-node.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
/**
*
* ZWaveNode - represents a node on the ZWave network.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.*
*/
'use strict';
const {Device, Event, Utils} = require('gateway-addon');
const padLeft = Utils.padLeft;
const padRight = Utils.padRight;
const repeatChar = Utils.repeatChar;
const {
CENTRAL_SCENE,
COMMAND_CLASS,
GENERIC_TYPE_STR,
} = require('./zwave-constants');
const BASIC_STR = [
'???',
'Controller',
'StaticController',
'Slave',
'RoutingSlave',
];
const {
DEBUG_node,
DEBUG_valueId,
} = require('./zwave-debug');
const DEBUG = DEBUG_node;
class ZWaveNode extends Device {
constructor(adapter, nodeId) {
// Our nodeId is a number from 1-255 and is only unique within
// the ZWave controller. So we extend this by appending the node id
// to the controller id and use that as the device's id.
const deviceId = `${adapter.id.toString(16)}-${nodeId}`;
super(adapter, deviceId);
this.zwInfo = {
location: '',
nodeId: nodeId,
manufacturer: '',
manufacturerId: '',
product: '',
productId: '',
productType: '',
type: '',
};
this.nodeId = nodeId;
this.location = '';
this.zwClasses = [];
this.zwValues = {};
this.ready = false;
this.lastStatus = 'constructed';
this.disablePoll = false;
this.canSleep = false;
this.classified = false;
}
asDict() {
const dict = super.asDict();
dict.lastStatus = this.lastStatus;
dict.zwInfo = this.zwInfo;
dict.zwClasses = this.zwClasses;
dict.zwValues = this.zwValues;
// Remove invisible properties from the Thing Description
// See https://github.com/WebThingsIO/zwave-adapter/issues/140
for (const prop of Object.values(dict.properties)) {
if (prop.hasOwnProperty('visible') && prop.visible === false) {
delete dict.properties[prop.name];
}
}
return dict;
}
centralSceneLevelTimerCallback(property, delta) {
let newValue = Math.round(property.value + delta);
newValue = Math.max(0, newValue);
newValue = Math.min(100, newValue);
DEBUG && console.log('centralSceneLevelTimerCallback:',
`node${this.zwInfo.nodeId}`,
'property:', property.name,
'value:', property.value,
'delta:', delta,
'newValue:', newValue);
// Cancel the timer if we don't need it any more.
if ((newValue == property.value) ||
(newValue == 0 && delta < 0) ||
(newValue == 100 && delta > 0)) {
this.handleCentralSceneButtonStopCommand(property);
}
// Update the value, if it changed
if (newValue != property.value) {
this.setPropertyValue(property, newValue);
}
}
/**
* @method findValueId
*
* Searches through the valueId's associated with this node, and returns
* the first one which matches the given criteria.
*
* @param {number} commandClass The command class of the valueId to find
* @param {number} [instance] a specific instance number associated with
* the valueId
* @param {number} [index] a specific index associated with the valueId
* @returns {String} The valueId key associated with the found value, or
* undefined if no valueId was found.
*/
findValueId(commandClass, instance, index) {
for (const valueId in this.zwValues) {
const value = this.zwValues[valueId];
if (value.class_id == commandClass &&
(typeof instance === 'undefined' || value.instance == instance) &&
(typeof index === 'undefined' || value.index == index)) {
return valueId;
}
}
}
makeValueId(commandClass, instance, index) {
return `${this.zwInfo.nodeId}-${commandClass}-${instance}-${index}`;
}
findPropertyFromValueId(valueId) {
for (const property of this.properties.values()) {
if (property.valueId == valueId) {
return property;
}
}
}
getSceneCount() {
const sceneCountValueId =
this.findValueId(COMMAND_CLASS.CENTRAL_SCENE,
1,
CENTRAL_SCENE.SCENE_COUNT);
if (sceneCountValueId) {
return this.zwValues[sceneCountValueId].value;
}
return 0;
}
handleCentralSceneProperty(sceneProperty) {
DEBUG && console.log('handleCentralSceneProperty:',
`node${this.zwInfo.nodeId}:`,
'value:', sceneProperty.value);
const valueId = sceneProperty.valueId;
const zwValue = this.zwValues[valueId];
const valueIdx = zwValue.values.indexOf(sceneProperty.value);
if (valueIdx < 0) {
// This shouldn't happen - just ignore it
console.error('handleCentralSceneProperty:',
`node${this.zwInfo.nodeId}:`,
`Unable to determine index of '${sceneProperty.value}'`,
`for valueId ${valueId} - ignoring`);
return;
}
const onProperty = sceneProperty.info.onProperty;
const levelProperty = sceneProperty.info.levelProperty;
const buttonNum = sceneProperty.info.buttonNum;
switch (valueIdx) {
case 0: // Inactive
// It always eventually enters this state after the other
// states. Currently we don't do anything.
break;
case 1: // pressed & released (short press)
DEBUG && console.log('handleCentralSceneProperty: press',
'pressAction:', sceneProperty.info.pressAction);
switch (sceneProperty.info.pressAction) {
case 'on':
this.setPropertyValue(onProperty, true);
break;
case 'off':
this.setPropertyValue(onProperty, false);
break;
case 'toggle':
this.setPropertyValue(onProperty, !onProperty.value);
break;
}
this.notifyEvent(`${buttonNum}-pressed`);
break;
case 2: // released (long press)
DEBUG && console.log('handleCentralSceneProperty: release',
'moveDir:', sceneProperty.info.moveDir);
if (sceneProperty.info.moveDir) {
this.handleCentralSceneButtonStopCommand(levelProperty);
}
this.notifyEvent(`${buttonNum}-released`);
sceneProperty.longPressed = false;
break;
case 3: { // long pressed
DEBUG && console.log('handleCentralSceneProperty: longPress',
'moveDir:', sceneProperty.info.moveDir);
if (sceneProperty.info.moveDir) {
this.handleCentralSceneButtonMoveCommand(
levelProperty, sceneProperty.info.moveDir);
}
if (!sceneProperty.longPressed) {
this.notifyEvent(`${buttonNum}-longPressed`);
}
sceneProperty.longPressed = true;
break;
}
}
}
handleCentralSceneButtonMoveCommand(property, moveDir) {
DEBUG && console.log('handleCentralSceneButtonMoveCommand:',
`node${this.zwInfo.nodeId}`,
'property:', property.name,
'moveDir:', moveDir);
// moveDir: 1 = up, -1 = down
if (property.moveTimer) {
// There's already a timer running.
return;
}
const updatesPerSecond = 4;
const delta = moveDir * 10;
this.centralSceneLevelTimerCallback(property, delta);
if ((property.value > 0 && delta < 0) ||
(property.value < 100 && delta > 0)) {
// We haven't hit the end, setup a timer to move towards it.
property.moveTimer = setInterval(
this.centralSceneLevelTimerCallback.bind(this),
1000 / updatesPerSecond,
property, delta);
}
}
handleCentralSceneButtonStopCommand(property) {
DEBUG && console.log('handleCentralSceneButtonStopCommand:',
`node${this.zwInfo.nodeId}`,
'property:', property.name);
if (property.moveTimer) {
clearInterval(property.moveTimer);
property.moveTimer = null;
}
}
handleSlideEnd(property) {
// The value is a 32-bit number. The first 8 bits are the
// button number, and the next 8 bits are a direction
// (0 = down, 1 = up) and the bottom 16 bits are a position.
const buttonNum = (property.value >> 24) & 0xff;
const dir = (property.value >> 16) & 0xff;
const endPosn = property.value & 0xffff;
const startPosn = this.slideStartProperty.value & 0xffff;
const slideSize = endPosn - startPosn;
// 26000 was determined empirically as the size of one of
// the squares on the WallMote Quad.
const slidePercent = Math.round(slideSize * 100 / 26000);
const sceneProperty = this.sceneProperty[buttonNum];
if (!sceneProperty) {
console.error('handleSlideEnd: node:', this.id,
'no sceneProperty for button', buttonNum);
return;
}
const levelProperty = sceneProperty.info.levelProperty;
if (!levelProperty) {
console.error('handleSlideEnd: node:', this.id,
'no levelProperty for button', buttonNum);
}
let newValue = levelProperty.value + slidePercent;
newValue = Math.max(0, newValue);
newValue = Math.min(100, newValue);
DEBUG && console.log('handleSlideEnd: button', buttonNum,
'dir:', dir,
'size:', slideSize,
`${slidePercent}%`,
'oldVal:', levelProperty.value,
'newVal:', newValue);
if (newValue != levelProperty.value) {
this.setPropertyValue(levelProperty, newValue);
}
}
notifyEvent(eventName, eventData) {
if (eventData) {
console.log(this.name, 'event:', eventName, 'data:', eventData);
} else {
console.log(this.name, 'event:', eventName);
}
this.eventNotify(new Event(this, eventName, eventData));
}
notifyPropertyChanged(property) {
const deferredSet = property.deferredSet;
if (deferredSet) {
property.deferredSet = null;
deferredSet.resolve(property.value);
}
super.notifyPropertyChanged(property);
if (property.hasOwnProperty('updated')) {
if (!property.updating) {
property.updating = true;
property.updated();
property.updating = false;
}
}
}
static oneLineHeader(line) {
if (line === 0) {
return `Node LastStat ${padRight('Basic Type', 16)} ${
padRight('Generic Type', 19)} ${padRight('Spec', 4)} ${
padRight('Product Name', 40)} ${padRight('Name', 30)}`;
}
return `${repeatChar('-', 4)} ${repeatChar('-', 8)} ${
repeatChar('-', 16)} ${repeatChar('-', 19)} ${repeatChar('-', 4)} ${
repeatChar('-', 40)} ${repeatChar('-', 30)}`;
}
oneLineSummary() {
const nodeId = this.zwInfo.nodeId;
const zwave = this.adapter.zwave;
const basic = zwave.getNodeBasic(nodeId);
const basicStr =
(basic >= 1 && basic < BASIC_STR.length) ?
BASIC_STR[basic] :
`??? ${basic} ???`;
const generic = zwave.getNodeGeneric(nodeId);
const genericStr = GENERIC_TYPE_STR[generic] ||
`Unknown 0x${generic.toString(16)}`;
const specific = zwave.getNodeSpecific(nodeId);
const specificStr = `0x${specific.toString(16)}`;
return `${padLeft(nodeId, 3)}: ${padRight(this.lastStatus, 8)} ${
padRight(basicStr, 16)} ${padRight(genericStr, 19)} ${
padRight(specificStr, 4)} ${padRight(this.zwInfo.product, 40)} ${
padRight(this.name, 30)}`;
}
performAction(action) {
console.log(`node${this.zwInfo.nodeId}`,
`Performing action '${action.name}'`);
if (this.doorLockAction) {
return Promise.reject('Lock/Unlock already in progress - ignoring');
}
action.start();
switch (action.name) {
case 'lock': // Start locking the door
if (this.doorLockState.value === 'locked') {
console.log('Door already locked - ignoring');
action.finish();
return Promise.resolve();
}
this.doorLockAction = action;
this.doorLockProperty.setValue(true);
this.setPropertyValue(this.doorLockState, 'unknown');
break;
case 'unlock': // Start unlocking the door
if (this.doorLockState.value === 'unlocked') {
console.log('Door already unlocked - ignoring');
action.finish();
return Promise.resolve();
}
this.doorLockAction = action;
this.doorLockProperty.setValue(false);
this.setPropertyValue(this.doorLockState, 'unknown');
break;
default:
action.finish();
return Promise.reject(`Unrecognized action: ${action.name}`);
}
if (this.doorLockAction) {
this.doorLockTimeout = setTimeout(() => {
// We didn't receive any type of status update. Assume jammed.
this.setPropertyValue(this.doorLockState, 'jammed');
const doorLockAction = this.doorLockAction;
if (doorLockAction) {
this.doorLockAction = null;
doorLockAction.finish();
}
}, 10000);
}
return Promise.resolve();
}
// Used to set properties which don't have an associated valueId
setPropertyValue(property, value) {
property.setCachedValue(value);
const units = property.units || '';
console.log('node%d setPropertyValue: %s = %s%s',
this.zwInfo.nodeId, property.name, value, units);
this.notifyPropertyChanged(property);
}
zwValueAdded(comClass, zwValue) {
if (DEBUG_valueId) {
console.log(zwValue);
}
this.lastStatus = 'value-added';
if (this.zwClasses.indexOf(comClass) < 0) {
this.zwClasses.push(comClass);
}
this.zwValues[zwValue.value_id] = zwValue;
let units = '';
if (zwValue.units) {
units = ` ${zwValue.units}`;
}
let propertyFound = false;
this.properties.forEach((property) => {
if (property.valueId == zwValue.value_id) {
propertyFound = true;
const [value, logValue] = property.parseZwValue(zwValue.value);
property.setCachedValue(value);
console.log('node%d valueAdded: %s:%s property: %s = %s%s',
this.zwInfo.nodeId, zwValue.value_id, zwValue.label,
property.name, logValue, units);
if (this.classified) {
this.notifyPropertyChanged(property);
} else {
console.log('node not classified');
}
}
});
if (!propertyFound && (zwValue.genre === 'user' || DEBUG)) {
console.log('node%d valueAdded: %s:%s = %s%s',
this.zwInfo.nodeId, zwValue.value_id,
zwValue.label, zwValue.value, units);
}
if (zwValue.genre === 'user' && !this.defaultName) {
// We use the label from the first 'user' value that we see to help
// disambiguate different nodes.
this.defaultName = `${this.id}-${zwValue.label}`;
// Assign a name if we don't yet have one.
if (!this.name) {
this.name = this.defaultName;
}
}
}
zwValueChanged(comClass, zwValue) {
this.lastStatus = 'value-changed';
this.zwValues[zwValue.value_id] = zwValue;
let units = '';
if (zwValue.units) {
units = ` ${zwValue.units}`;
}
let propertyFound = false;
this.properties.forEach((property) => {
if (property.valueId == zwValue.value_id ||
property.valueId2 == zwValue.value_id) {
propertyFound = true;
let value;
let logValue;
if (property.valueId2) {
// The Aeotect Water leak sensor has 2 instances. We basically
// or the results together.
const [value1, logValue1] =
property.parseZwValue(this.zwValues[property.valueId].value);
const [value2, logValue2] =
property.parseZwValue(this.zwValues[property.valueId2].value);
value = value1 || value2;
logValue = `(1:${logValue1} 2:${logValue2})`;
} else {
[value, logValue] = property.parseZwValue(zwValue.value);
}
property.setCachedValue(value);
console.log('node%d valueChanged: %s:%s property: %s = %s%s',
this.zwInfo.nodeId, zwValue.value_id, zwValue.label,
property.name, logValue, units);
this.notifyPropertyChanged(property);
}
});
if (!propertyFound) {
console.log('node%d valueChanged: %s:%s = %s%s (no property found)',
this.zwInfo.nodeId, zwValue.value_id,
zwValue.label, zwValue.value, units);
}
}
zwValueRemoved(comClass, instance, index) {
this.lastStatus = 'value-removed';
const valueId = `${this.zwInfo.nodeId}-${comClass}-${instance}-${index}`;
const zwValue = this.zwValues[valueId];
if (zwValue) {
let units = '';
if (zwValue.units) {
units = ` ${zwValue.units}`;
}
delete this.zwValues[valueId];
let propertyFound = false;
this.properties.forEach((property) => {
if (property.valueId == zwValue.value_id) {
propertyFound = true;
const [_value, logValue] = property.parseZwValue(zwValue.value);
delete property.valueId;
delete property.value;
console.log('node%d valueRemoved: %s:%s %s property: %s = %s%s',
this.zwInfo.nodeId, zwValue.value_id, zwValue.label,
property.name, logValue, units);
}
});
if (!propertyFound) {
console.log('node%d valueRemoved: %s:%s = %s%s',
this.zwInfo.nodeId, zwValue.value_id,
zwValue.label, zwValue.value, units);
}
} else {
console.log('zwValueRemoved unknown valueId:', valueId);
}
}
}
module.exports = ZWaveNode;