From 70c2586291fbf6ed73593d1e2838ce7436c458d5 Mon Sep 17 00:00:00 2001 From: Daniel Lando Date: Tue, 12 Mar 2019 17:09:51 +0100 Subject: [PATCH] Detect sleeping devices using `nodeAviable` event Fix #11 --- lib/ZwaveClient.js | 142 +++++++++++++++++++++++---------------------- 1 file changed, 74 insertions(+), 68 deletions(-) diff --git a/lib/ZwaveClient.js b/lib/ZwaveClient.js index 4c79d039..08aab775 100644 --- a/lib/ZwaveClient.js +++ b/lib/ZwaveClient.js @@ -205,6 +205,7 @@ function nodeRemoved(nodeid) { this.emitEvent("NODE_REMOVED", this.nodes[nodeid]); } +// Triggered when a node is added function nodeAdded(nodeid) { this.nodes[nodeid] = { node_id: nodeid, @@ -227,6 +228,23 @@ function nodeAdded(nodeid) { debug("Node added", nodeid) } +// Triggered after node added event when the node info are firstly loaded +function nodeAvailable(nodeid, nodeinfo){ + var ozwnode = this.nodes[nodeid]; + if (ozwnode) { + + for (var attrname in nodeinfo) { + if (nodeinfo.hasOwnProperty(attrname)) { + ozwnode[attrname] = nodeinfo[attrname]; + } + } + + ozwnode.status = NODE_STATUS[4]; // sleeping + debug('node %d AVAILABLE: %s - %s (%s)', nodeid, nodeinfo.manufacturer, nodeinfo.product, (nodeinfo.type || 'Unknown')); + } +} + +// Triggered after node available event when a value is added function valueAdded(nodeid, comclass, valueId) { var ozwnode = this.nodes[nodeid]; if (!ozwnode) { @@ -234,10 +252,65 @@ function valueAdded(nodeid, comclass, valueId) { }else{ parseValue(valueId); debug("ValueAdded: %s %s", valueId.value_id, valueId.label); - ozwnode.values[getValueID(valueId)] = valueId; + var id = getValueID(valueId); + ozwnode.values[id] = valueId; + this.emit('valueChanged', valueId, ozwnode, id); } } +// Triggered after all values have been added +function nodeReady(nodeid, nodeinfo) { + var ozwnode = this.nodes[nodeid]; + if (ozwnode) { + + ozwnode.ready = true; + ozwnode.status = NODE_STATUS[6]; + + // //enable poll + // for (var v in ozwnode.values) { + // var comclass = ozwnode.values[v].class_id; + // switch (comclass) { + // case 0x25: // COMMAND_CLASS_SWITCH_BINARY + // case 0x26: // COMMAND_CLASS_SWITCH_MULTILEVEL + // case 0x30: // COMMAND_CLASS_SENSOR_BINARY + // case 0x31: // COMMAND_CLASS_SENSOR_MULTILEVEL + // case 0x60: // COMMAND_CLASS_MULTI_INSTANCE + // if(!this.client.isPolled(ozwnode.values[v])) + // this.client.enablePoll(ozwnode.values[v], 1); + // break; + // } + // } + + var deviceID = getDeviceID(ozwnode); + ozwnode.device_id = deviceID; + + // add it to know devices types (if not already present) + if(!this.devices[deviceID]){ + this.devices[deviceID] = { + name: `${ozwnode.product} (${ozwnode.manufacturer})`, + values: JSON.parse(JSON.stringify(ozwnode.values)) + }; + + //remove node specific info from values + for (var v in this.devices[deviceID].values) { + var tmp = this.devices[deviceID].values[v]; + delete tmp.node_id; + tmp.value_id = getValueID(tmp); + } + } + + // if scan is complete update node groups + if(this.scanComplete){ + this.getGroups(nodeid) + } + + this.emit('nodeStatus', ozwnode); + + debug('node %d ready: %s - %s (%s)', nodeid, nodeinfo.manufacturer, nodeinfo.product, (nodeinfo.type || 'Unknown')); + } +} + +// Triggered when a node is ready and a value changes function valueChanged(nodeid, comclass, valueId) { var ozwnode = this.nodes[nodeid]; var value_id = getValueID(valueId); @@ -268,73 +341,6 @@ function valueRemoved(nodeid, comclass, instance, index) { } } - function nodeAvailable(nodeid, nodeinfo){ - debug("Node ID %d is available", nodeid, nodeinfo) - } - - function nodeReady(nodeid, nodeinfo) { - var ozwnode = this.nodes[nodeid]; - if (ozwnode) { - - for (var attrname in nodeinfo) { - if (nodeinfo.hasOwnProperty(attrname)) { - ozwnode[attrname] = nodeinfo[attrname]; - } - } - - ozwnode.ready = true; - ozwnode.status = NODE_STATUS[6]; - - // //enable poll - // for (var v in ozwnode.values) { - // var comclass = ozwnode.values[v].class_id; - // switch (comclass) { - // case 0x25: // COMMAND_CLASS_SWITCH_BINARY - // case 0x26: // COMMAND_CLASS_SWITCH_MULTILEVEL - // case 0x30: // COMMAND_CLASS_SENSOR_BINARY - // case 0x31: // COMMAND_CLASS_SENSOR_MULTILEVEL - // case 0x60: // COMMAND_CLASS_MULTI_INSTANCE - // if(!this.client.isPolled(ozwnode.values[v])) - // this.client.enablePoll(ozwnode.values[v], 1); - // break; - // } - // } - - var deviceID = getDeviceID(ozwnode); - - ozwnode.device_id = deviceID; - - // update all values in MQTT - for(var v in ozwnode.values){ - this.emit('valueChanged', ozwnode.values[v], ozwnode, v); - } - - // add it to know devices types (if not already present) - if(!this.devices[deviceID]){ - this.devices[deviceID] = { - name: `${ozwnode.product} (${ozwnode.manufacturer})`, - values: JSON.parse(JSON.stringify(ozwnode.values)) - }; - - //remove node specific info from values - for (var v in this.devices[deviceID].values) { - var tmp = this.devices[deviceID].values[v]; - delete tmp.node_id; - tmp.value_id = getValueID(tmp); - } - } - - // if scan is complete update node groups - if(this.scanComplete){ - this.getGroups(nodeid) - } - - this.emit('nodeStatus', ozwnode); - - debug('node %d ready: %s - %s (%s)', nodeid, nodeinfo.manufacturer, nodeinfo.product, (nodeinfo.type || 'Unknown')); - } - } - function nodeEvent(nodeid, evtcode) { debug('node event', nodeid, evtcode); }