Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Commit

Permalink
Detect sleeping devices using nodeAviable event
Browse files Browse the repository at this point in the history
Fix #11
  • Loading branch information
robertsLando committed Mar 12, 2019
1 parent bdc29ec commit 70c2586
Showing 1 changed file with 74 additions and 68 deletions.
142 changes: 74 additions & 68 deletions lib/ZwaveClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -227,17 +228,89 @@ 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) {
debug('ValueAdded: no such node: '+nodeid, 'error');
}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);
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 70c2586

Please sign in to comment.