forked from WebThingsIO/zwave-adapter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zwave-node.js
244 lines (219 loc) · 7.33 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
/**
*
* 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';
let Device, Utils;
try {
Device = require('../device');
Utils = require('../utils');
} catch (e) {
if (e.code !== 'MODULE_NOT_FOUND') {
throw e;
}
const gwa = require('gateway-addon');
Device = gwa.Device;
Utils = gwa.Utils;
}
const padLeft = Utils.padLeft;
const padRight = Utils.padRight;
const repeatChar = Utils.repeatChar;
const BASIC_STR = [
'???',
'Controller',
'StaticController',
'Slave',
'RoutingSlave',
];
const DEBUG = false;
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';
}
asDict() {
const dict = super.asDict();
dict.lastStatus = this.lastStatus;
dict.zwInfo = this.zwInfo;
dict.zwClasses = this.zwClasses;
dict.zwValues = this.zwValues;
return dict;
}
/**
* @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;
}
}
}
findPropertyFromValueId(valueId) {
for (const property of this.properties.values()) {
if (property.valueId == valueId) {
return property;
}
}
}
notifyPropertyChanged(property) {
const deferredSet = property.deferredSet;
if (deferredSet) {
property.deferredSet = null;
deferredSet.resolve(property.value);
}
super.notifyPropertyChanged(property);
}
static oneLineHeader(line) {
if (line === 0) {
return `Node LastStat ${padRight('Basic Type', 16)} ${
padRight('Type', 24)} ${padRight('Product Name', 50)} ${
padRight('Name', 30)} Location`;
}
return `${repeatChar('-', 4)} ${repeatChar('-', 8)} ${
repeatChar('-', 16)} ${repeatChar('-', 24)} ${repeatChar('-', 50)} ${
repeatChar('-', 30)} ${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} ???`;
return `${padLeft(nodeId, 3)}: ${padRight(this.lastStatus, 8)} ${
padRight(basicStr, 16)} ${padRight(this.zwInfo.type, 24)} ${
padRight(this.zwInfo.product, 50)} ${padRight(this.name, 30)} ${
this.zwInfo.location}`;
}
zwValueAdded(comClass, 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 (!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) {
propertyFound = true;
const [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',
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;