From e23afdf3eace7c56d4391552b7973c068cd23c92 Mon Sep 17 00:00:00 2001 From: jghaanstra Date: Tue, 11 Jul 2017 22:53:43 +0200 Subject: [PATCH] Fix for app crashing over status updates by Yeelights --- APPSTORE.md | 3 + app.json | 2 +- drivers/yeelight-bulb/driver.js | 137 +++++++++++++++++--------------- package.json | 2 +- 4 files changed, 79 insertions(+), 65 deletions(-) diff --git a/APPSTORE.md b/APPSTORE.md index 6755235..e9b1635 100644 --- a/APPSTORE.md +++ b/APPSTORE.md @@ -44,6 +44,9 @@ Donating is completely optional. [![Donate](https://www.paypalobjects.com/webstatic/en_US/i/btn/png/btn_donate_92x26.png)](https://paypal.me/jghaanstra) ## Changelog +### 2017-07-11 -- v2.1.2 +* FIX: fixed app crashing due to error in processing Yeelight status + ### 2017-07-10 -- v2.1.1 * FIX: fixed app crashing due to error in connection with Yeelight (which could be the case then the Yeelight is unplugged) diff --git a/app.json b/app.json index 69f0cda..eb46314 100644 --- a/app.json +++ b/app.json @@ -8,7 +8,7 @@ "en": [ "Xiaomi", "Mi", "Mi Home", "vacuumcleaner", "robot", "yeelight", "yeelights", "purifier", "humidifier" ], "nl": [ "Xiaomi", "Mi", "Mi home", "stofzuiger", "robot", "yeelight", "yeelights", "luchtreiniger", "luchtbevochtiger" ] }, - "version": "2.1.1", + "version": "2.1.2", "compatibility": "0.x || 1.x", "author": { "name": "Jelger Haanstra", diff --git a/drivers/yeelight-bulb/driver.js b/drivers/yeelight-bulb/driver.js index d235bed..d1d5604 100644 --- a/drivers/yeelight-bulb/driver.js +++ b/drivers/yeelight-bulb/driver.js @@ -240,73 +240,84 @@ function createDeviceSocket(device_data) { var result = result.replace('{"id":1, "result":["ok"]}',''); var result = result.replace('\r\n',''); if (result.includes('props')) { - var result = JSON.parse(result); - var key = Object.keys(result.params)[0]; - - switch (key) { - case 'power': - if(result.params.power == 'on') { - device.state.onoff = true; - module.exports.realtime(device_data, 'onoff', true); - } else { - device.state.onoff = false; - module.exports.realtime(device_data, 'onoff', false); - } - break; - case 'bright': - device.state.dim = result.params.bright; - var dim = result.params.bright / 100; - module.exports.realtime(device_data, 'dim', dim); - break; - case 'ct': - device.state.temperature = result.params.ct; - var color_temp = utils.normalize(result.params.ct, 1700, 6500); - module.exports.realtime(device_data, 'light_temperature', color_temp); - break; - case 'rgb': - device.state.rgb = result.params.rgb; - var color = tinycolor(result.params.rgb.toString(16)); - var hsv = color.toHsv(); - var hue = Math.round(hsv.h) / 359; - var saturation = Math.round(hsv.s); - module.exports.realtime(device_data, 'light_hue', hue); - module.exports.realtime(device_data, 'light_saturation', saturation); - break; - case 'hue': - device.state.hue = result.params.hue; - var hue = result.params.hue / 359; - module.exports.realtime(device_data, 'light_hue', hue); - break; - case 'sat': - device.state.saturation = result.params.sat; - var saturation = result.params.sat / 100; - module.exports.realtime(device_data, 'light_saturation', saturation); - break; - case 'color_mode': - device.state.mode = result.params.color_mode; - if (result.params.color_mode == 2) { - module.exports.realtime(device_data, 'light_mode', 'temperature'); - } else { - module.exports.realtime(device_data, 'light_mode', 'color'); - } - break; - default: - break; + try { + var result = JSON.parse(result); + var key = Object.keys(result.params)[0]; + + switch (key) { + case 'power': + if(result.params.power == 'on') { + device.state.onoff = true; + module.exports.realtime(device_data, 'onoff', true); + } else { + device.state.onoff = false; + module.exports.realtime(device_data, 'onoff', false); + } + break; + case 'bright': + device.state.dim = result.params.bright; + var dim = result.params.bright / 100; + module.exports.realtime(device_data, 'dim', dim); + break; + case 'ct': + device.state.temperature = result.params.ct; + var color_temp = utils.normalize(result.params.ct, 1700, 6500); + module.exports.realtime(device_data, 'light_temperature', color_temp); + break; + case 'rgb': + device.state.rgb = result.params.rgb; + var color = tinycolor(result.params.rgb.toString(16)); + var hsv = color.toHsv(); + var hue = Math.round(hsv.h) / 359; + var saturation = Math.round(hsv.s); + module.exports.realtime(device_data, 'light_hue', hue); + module.exports.realtime(device_data, 'light_saturation', saturation); + break; + case 'hue': + device.state.hue = result.params.hue; + var hue = result.params.hue / 359; + module.exports.realtime(device_data, 'light_hue', hue); + break; + case 'sat': + device.state.saturation = result.params.sat; + var saturation = result.params.sat / 100; + module.exports.realtime(device_data, 'light_saturation', saturation); + break; + case 'color_mode': + device.state.mode = result.params.color_mode; + if (result.params.color_mode == 2) { + module.exports.realtime(device_data, 'light_mode', 'temperature'); + } else { + module.exports.realtime(device_data, 'light_mode', 'color'); + } + break; + default: + break; + } + + } catch (err) { + Homey.log('Unable to process message because of error: '+ err); } } else if (result.includes('result')) { - var result = JSON.parse(result); + try { + var result = JSON.parse(result); - if(result.result[0] == 'on') { - device.state.onoff = true; - } else { - device.state.onoff = false; + if(result.result[0] == 'on') { + device.state.onoff = true; + } else { + device.state.onoff = false; + } + device.state.dim = result.result[1]; + device.state.mode = result.result[2]; + device.state.temperature = result.result[3]; + device.state.rgb = result.result[4]; + device.state.hue = result.result[5]; + device.state.saturation = result.result[6]; + } catch (err) { + Homey.log('Unable to process message because of error: '+ err); } - device.state.dim = result.result[1]; - device.state.mode = result.result[2]; - device.state.temperature = result.result[3]; - device.state.rgb = result.result[4]; - device.state.hue = result.result[5]; - device.state.saturation = result.result[6]; + + } }.bind(this)); }.bind(this)); diff --git a/package.json b/package.json index cd0e154..b0ff96e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "com.xiaomi-miio", - "version": "2.1.1", + "version": "2.1.2", "description": "Xiaomi Mi Home", "main": "app.js", "dependencies": {