Fix brightness, format some logic.

This commit is contained in:
Stephen Yeargin
2015-06-15 23:10:18 -05:00
parent 9346688d56
commit 3cba55560f

View File

@@ -33,7 +33,6 @@ function PhilipsHuePlatform(log, config) {
} }
function PhilipsHueAccessory(log, device, api) { function PhilipsHueAccessory(log, device, api) {
// device info
this.name = device.name; this.name = device.name;
this.model = device.modelid; this.model = device.modelid;
this.device = device; this.device = device;
@@ -46,18 +45,22 @@ var execute = function(accessory, lightID, characteristic, value) {
var http = require('http'); var http = require('http');
var body = {}; var body = {};
characteristic = characteristic.toLowerCase(); characteristic = characteristic.toLowerCase();
if(characteristic === "identify") { if (characteristic === "identify") {
body = {alert:"select"}; body = {alert:"select"};
} else if(characteristic === "on") { }
else if (characteristic === "on") {
body = {on:value}; body = {on:value};
} else if(characteristic === "hue") { }
else if (characteristic === "hue") {
body = {hue:value}; body = {hue:value};
} else if(characteristic === "brightness") { }
else if (characteristic === "brightness") {
value = value/100; value = value/100;
value = value*255; value = value*255;
value = Math.round(value); value = Math.round(value);
body = {bri:value}; body = {bri:value};
} else if(characteristic === "saturation") { }
else if (characteristic === "saturation") {
value = value/100; value = value/100;
value = value*255; value = value*255;
value = Math.round(value); value = Math.round(value);
@@ -107,7 +110,7 @@ PhilipsHuePlatform.prototype = {
}; };
PhilipsHueAccessory.prototype = { PhilipsHueAccessory.prototype = {
// Set Power State
setPowerState: function(powerOn) { setPowerState: function(powerOn) {
if (!this.device) { if (!this.device) {
this.log("No '"+this.name+"' device found (yet?)"); this.log("No '"+this.name+"' device found (yet?)");
@@ -123,23 +126,26 @@ PhilipsHueAccessory.prototype = {
that.api.setLightState(that.id, state, function(err, result) { that.api.setLightState(that.id, state, function(err, result) {
if (err) { if (err) {
that.log("Error setting power state on for '"+that.name+"'"); that.log("Error setting power state on for '"+that.name+"'");
} else { }
else {
that.log("Successfully set power state on for '"+that.name+"'"); that.log("Successfully set power state on for '"+that.name+"'");
} }
}); });
}else{ }
else {
this.log("Setting power state on the '"+this.name+"' to off"); this.log("Setting power state on the '"+this.name+"' to off");
state = lightState.create().off(); state = lightState.create().off();
that.api.setLightState(that.id, state, function(err, result) { that.api.setLightState(that.id, state, function(err, result) {
if (err) { if (err) {
that.log("Error setting power state off for '"+that.name+"'"); that.log("Error setting power state off for '"+that.name+"'");
} else { }
else {
that.log("Successfully set power state off for '"+that.name+"'"); that.log("Successfully set power state off for '"+that.name+"'");
} }
}); });
} }
}, },
// Set Brightness
setBrightness: function(level) { setBrightness: function(level) {
if (!this.device) { if (!this.device) {
this.log("No '"+this.name+"' device found (yet?)"); this.log("No '"+this.name+"' device found (yet?)");
@@ -147,17 +153,20 @@ PhilipsHueAccessory.prototype = {
} }
var that = this; var that = this;
var state;
this.log("Setting brightness on the '"+this.name+"' to " + level); this.log("Setting brightness on the '"+this.name+"' to " + level);
this.device.brightness(level, function(response) { state = lightState.create().brightness(level);
if (response === undefined) { that.api.setLightState(that.id, state, function(err, result) {
that.log("Error setting brightness on the '"+that.name+"'"); if (err) {
} else { that.log("Error setting brightness for '"+that.name+"'");
that.log("Successfully set brightness on the '"+that.name+"' to " + level); }
else {
that.log("Successfully set brightness for '"+that.name+"'");
} }
}); });
}, },
// Get Services
getServices: function() { getServices: function() {
var that = this; var that = this;
return [{ return [{