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

Commit

Permalink
getDevices on start if shepherd already connected (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
hobbyquaker committed Dec 5, 2019
1 parent d3e3d2b commit 2bbb2e8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
28 changes: 23 additions & 5 deletions nodes/converter.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,27 @@ module.exports = function (RED) {
herdsmanConverters.toZigbeeConverters.ignore_transition
];

const devicesHandler = () => {
this.debug('devicesHandler');
const getDevices = () => {
this.debug('getDevices');
const devices = this.herdsman.getDevices();
this.groups = this.herdsman.getGroups();
devices.forEach(device => {
this.ieeeAddresses[device.ieeeAddr] = device;
this.names[device.meta.name] = device;
});
this.groups = this.herdsman.getGroups();
this.gotDevices = true;
};

const readyHandler = () => {
this.debug('readyHandler');
if (!this.gotDevices) {
getDevices();
}
};

const devicesHandler = () => {
this.debug('devicesHandler');
getDevices();
};

this.on('input', (msg, send, done) => {
Expand Down Expand Up @@ -339,15 +352,20 @@ module.exports = function (RED) {
this.debug('adding event listeners');
shepherdNode.proxy.on('nodeStatus', nodeStatusHandler);
shepherdNode.proxy.on('message', messageHandler);
shepherdNode.proxy.on('ready', devicesHandler);
shepherdNode.proxy.on('ready', readyHandler);
shepherdNode.proxy.on('devices', devicesHandler);

if (!this.gotDevices && shepherdNode.status === 'connected') {
getDevices();
}

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

Expand Down
26 changes: 22 additions & 4 deletions nodes/hue-light.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,26 @@ module.exports = function (RED) {

this.lastState = {};

const readyHandler = () => {
this.debug('readyHandler');
const getDevices = () => {
this.debug('getDevices');
this.devices = shepherdNode.herdsman.getDevices();
this.groups = shepherdNode.herdsman.getGroups();
this.devices.forEach(device => {
this.initLight(device);
});
this.gotDevices = true;
};

const readyHandler = () => {
this.debug('readyHandler');
if (!this.gotDevices) {
getDevices();
}
};

const devicesHandler = () => {
this.debug('devicesHandler');
getDevices();
};

const publishTimeouts = {};
Expand Down Expand Up @@ -115,18 +128,23 @@ module.exports = function (RED) {

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

if (!this.gotDevices && shepherdNode.status === 'connected') {
getDevices();
}

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

this.on('input', (msg, send, done) => {
Expand Down

0 comments on commit 2bbb2e8

Please sign in to comment.