diff --git a/lib/yeelight.js b/lib/yeelight.js index 7f0f8da..1eed004 100644 --- a/lib/yeelight.js +++ b/lib/yeelight.js @@ -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, @@ -41,12 +42,8 @@ exports.listenUpdates = function () { advertisements.setMulticastTTL(255); }); - - advertisements.on('message', (message, address) => { process.nextTick(function() { - - parseMessage(message) .then(result => { if (result !== 'no devices') { @@ -54,38 +51,29 @@ exports.listenUpdates = function () { // 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); }); }