Skip to content
This repository has been archived by the owner on Dec 28, 2022. It is now read-only.

Commit

Permalink
cleanup event listeners, increase log verbosity (rdmtc/RedMatic#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbyquaker committed Dec 5, 2019
1 parent fbb7532 commit fb6f858
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 25 deletions.
17 changes: 13 additions & 4 deletions nodes/command.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ module.exports = function (RED) {
}

let nodeStatus = {text: ''};
shepherdNode.proxy.on('nodeStatus', status => {
nodeStatus = status;
this.status(status);
});

this.shepherd = shepherdNode.herdsman;

Expand Down Expand Up @@ -96,6 +92,19 @@ module.exports = function (RED) {
this.status({fill: 'red', shape: 'dot', text: err.message});
});
});

const nodeStatusHandler = status => {
nodeStatus = status;
this.status(status);
};

this.debug('adding event listeners');
shepherdNode.proxy.on('nodeStatus', nodeStatusHandler);

this.on('close', () => {
this.debug('removing event listeners');
shepherdNode.proxy.removeListener('nodeStatus', nodeStatusHandler);
});
}
}

Expand Down
34 changes: 23 additions & 11 deletions nodes/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,6 @@ module.exports = function (RED) {
}

let nodeStatus = {text: ''};
herdsmanNode.proxy.on('nodeStatus', status => {
nodeStatus = status;
this.status(status);
});
herdsmanNode.proxy.on('permitJoin', status => {
if (status) {
this.status({shape: 'ring', fill: 'blue', text: 'join permitted'});
} else {
this.status(nodeStatus);
}
});

this.herdsman = herdsmanNode.herdsman;

Expand Down Expand Up @@ -80,6 +69,29 @@ module.exports = function (RED) {
done(new Error(`Unknown command ${msg.payload}`));
}
});

const nodeStatusHandler = status => {
nodeStatus = status;
this.status(status);
};

const permitJoinHandler = status => {
if (status) {
this.status({shape: 'ring', fill: 'blue', text: 'join permitted'});
} else {
this.status(nodeStatus);
}
};

this.debug('adding event listeners');
herdsmanNode.proxy.on('nodeStatus', nodeStatusHandler);
herdsmanNode.proxy.on('permitJoin', permitJoinHandler);

this.on('close', () => {
this.debug('removing event listeners');
herdsmanNode.proxy.removeListener('nodeStatus', nodeStatusHandler);
herdsmanNode.proxy.removeListener('permitJoin', permitJoinHandler);
});
}
}

Expand Down
4 changes: 2 additions & 2 deletions nodes/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,16 @@ module.exports = function (RED) {
this.status(status);
};

this.debug('adding event listeners');
shepherdNode.proxy.on('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.on('message', messageHandler);
shepherdNode.proxy.on('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.on('ready', devicesHandler);
shepherdNode.proxy.on('devices', devicesHandler);

this.on('close', () => {
this.debug('removing event listeners');
shepherdNode.proxy.removeListener('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.removeListener('message', messageHandler);
shepherdNode.proxy.removeListener('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.removeListener('ready', devicesHandler);
shepherdNode.proxy.removeListener('devices', devicesHandler);
});
Expand Down
12 changes: 7 additions & 5 deletions nodes/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = function (RED) {
return;
}

this.shepherd = shepherdNode.proxy;
this.proxy = shepherdNode.proxy;

const nodeStatusHandler = status => {
this.status(status);
Expand Down Expand Up @@ -70,12 +70,14 @@ module.exports = function (RED) {
}
};

shepherdNode.proxy.on('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.on('message', messageHandler);
this.debug('adding event listeners');
this.proxy.on('nodeStatus', nodeStatusHandler);
this.proxy.on('message', messageHandler);

this.on('close', () => {
shepherdNode.proxy.removeListener('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.removeListener('message', messageHandler);
this.debug('removing event listeners');
this.proxy.removeListener('nodeStatus', nodeStatusHandler);
this.proxy.removeListener('message', messageHandler);
});
}

Expand Down
8 changes: 6 additions & 2 deletions nodes/hue-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ module.exports = function (RED) {
this.proxy = shepherdNode.proxy;
this.config = config;

shepherdNode.proxy.on('nodeStatus', status => {
const nodeStatusHandler = status => {
this.status(status);
});
};

this.lastState = {};

Expand Down Expand Up @@ -112,16 +112,20 @@ module.exports = function (RED) {
}
};

this.debug('adding event listeners');
this.proxy.on('ready', readyHandler);
this.proxy.on('devices', readyHandler);
this.proxy.on('message', messageHandler);
this.proxy.on('offline', offlineHandler);
this.proxy.on('nodeStatus', nodeStatusHandler);

this.on('close', () => {
this.debug('removing event listeners');
this.proxy.removeListener('ready', readyHandler);
this.proxy.removeListener('devices', readyHandler);
this.proxy.removeListener('message', messageHandler);
this.proxy.removeListener('offline', offlineHandler);
this.proxy.removeListener('nodeStatus', nodeStatusHandler);
});

this.on('input', (msg, send, done) => {
Expand Down
2 changes: 2 additions & 0 deletions nodes/offline.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ module.exports = function (RED) {
this.shepherdNode.herdsman.getDevices().forEach(sendMessage);
};

this.debug('adding event listeners');
shepherdNode.proxy.on('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.on('offline', sendMessage);
shepherdNode.proxy.on('ready', readyHandler);

this.on('close', () => {
this.debug('removing event listeners');
shepherdNode.proxy.removeListener('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.removeListener('offline', sendMessage);
shepherdNode.proxy.removeListener('ready', readyHandler);
Expand Down
7 changes: 6 additions & 1 deletion nodes/shepherd.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,8 +504,13 @@ module.exports = function (RED) {
this.status = '';
this.proxy.emit('nodeStatus', {});
setTimeout(() => {
this.debug('remaining proxy listeners for event nodeStatus ' + this.proxy.listenerCount('nodeStatus'));
this.debug('remaining proxy listeners for event ready ' + this.proxy.listenerCount('ready'));
this.debug('remaining proxy listeners for event devices ' + this.proxy.listenerCount('devices'));
this.debug('remaining proxy listeners for event permitJoin ' + this.proxy.listenerCount('permitJoin'));
this.debug('remaining proxy listeners for event message ' + this.proxy.listenerCount('message'));
this.proxy.removeAllListeners();
this.debug('removed proxy event listeners');
this.debug('remaining proxy listeners removed');
done();
}, 100);
});
Expand Down

0 comments on commit fb6f858

Please sign in to comment.