Skip to content

Commit

Permalink
Release 2.11.4
Browse files Browse the repository at this point in the history
  • Loading branch information
jghaanstra committed Mar 3, 2019
1 parent aaa2486 commit 7ef200f
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions lib/yeelight.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ const advertisements = dgram.createSocket('udp4');

var new_devices = {};
var added_devices = {};
var yeelights = {};

/* send discovery message during pair wizard */
exports.fillAddedDevices = function () {
added_devices = {}
var yeelights = Homey.ManagerDrivers.getDriver('yeelights').getDevices();
yeelights = Homey.ManagerDrivers.getDriver('yeelights').getDevices();
Object.keys(yeelights).forEach(function(key) {
added_devices[yeelights[key].getData().id] = {
id: yeelights[key].getData().id,
Expand Down Expand Up @@ -41,51 +42,38 @@ exports.listenUpdates = function () {
advertisements.setMulticastTTL(255);
});



advertisements.on('message', (message, address) => {
process.nextTick(function() {


parseMessage(message)
.then(result => {
if (result !== 'no devices') {

// new devices for pairing
if (result.message_type == 'discover' && !new_devices.hasOwnProperty(result.device.id) && !added_devices.hasOwnProperty(result.device.id)) {
new_devices[result.device.id] = result.device;
console.log('device added to new devices ', result.device.id);
} else {
console.log('device not added to new devices ', result.device.id);
console.log('result.message_type: ', result.message_type);
}

var yeelights = Homey.ManagerDrivers.getDriver('yeelights').getDevices();
Object.keys(yeelights).forEach(function(key) {
// update ip for changed ip's
if (result.message_type != 'discover' && yeelights[key].getData().id == result.device.id && (yeelights[key].getSetting('address') != result.device.address || yeelights[key].getSetting('port') != result.device.port) ) {
yeelights[key].setSettings({address: result.device.address, port: result.device.port});
console.log('device has changed ip ', result.device.address);
}

// new sockets for broken devices
if (result.message_type != 'discover' && yeelights[key].getData().id == result.device.id && !yeelights[key].isConnected(result.device.id)) {
yeelights[key].createDeviceSocket(result.device.id);
console.log('existing device but with broken socket: ', result.device.id)
}
});
} else {
console.log('No devices found or message not a discovery message');
}
})
.catch(error => {
console.log(error);
console.error(error);
})
});
});

advertisements.on('error', (error) => {
console.log(error);
console.error(error);
});
}

Expand Down

0 comments on commit 7ef200f

Please sign in to comment.