Refactor execute method to use API

This commit is contained in:
Stephen Yeargin
2015-06-16 23:08:44 -05:00
parent da5162b358
commit 49c4af79ad
+21 -32
View File
@@ -41,52 +41,41 @@ function PhilipsHueAccessory(log, device, api) {
this.log = log; this.log = log;
} }
// @todo Use the node module for all of this // Execute changes for various characteristics
var execute = function(accessory, lightID, characteristic, value) { var execute = function(api, device, characteristic, value) {
var http = require('http');
var body = {}; var state = lightState.create();
characteristic = characteristic.toLowerCase(); characteristic = characteristic.toLowerCase();
if (characteristic === "identify") { if (characteristic === "identify") {
body = {alert:"select"}; state.alert('select');
} }
else if (characteristic === "on") { else if (characteristic === "on") {
body = {on:value}; state.on();
} }
else if (characteristic === "hue") { else if (characteristic === "hue") {
body = {hue:value}; state.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}; state.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);
body = {sat:value}; state.sat(value);
} }
var post_data = JSON.stringify(body); api.setLightState(device.id, state, function(err, lights) {
var post_options = { if (!err) {
host: config["ip_address"], console.log("executed accessory: " + device.name + ", and characteristic: " + characteristic + ", with value: " + value + ".");
port: '80', }
path: '/api/' + config["username"] + '/lights/' + lightID + '/state/', else {
method: 'PUT', console.log(err);
headers: {
'Content-Type': 'application/json',
'Content-Length': post_data.length
} }
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
}); });
post_req.write(post_data);
post_req.end();
console.log("executed accessory: " + accessory + ", and characteristic: " + characteristic + ", with value: " + value + ".");
}; };
PhilipsHuePlatform.prototype = { PhilipsHuePlatform.prototype = {
@@ -204,7 +193,7 @@ PhilipsHueAccessory.prototype = {
designedMaxLength: 255 designedMaxLength: 255
},{ },{
cType: types.IDENTIFY_CTYPE, cType: types.IDENTIFY_CTYPE,
onUpdate: function(value) { console.log("Change:",value); execute(this.name, this.id, "identify", value); }, onUpdate: function(value) { console.log("Change:",value); execute(this.device, this.id, "identify", value); },
perms: ["pw"], perms: ["pw"],
format: "bool", format: "bool",
initialValue: false, initialValue: false,
@@ -227,7 +216,7 @@ PhilipsHueAccessory.prototype = {
designedMaxLength: 255 designedMaxLength: 255
},{ },{
cType: types.POWER_STATE_CTYPE, cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) { console.log("Change:",value); execute(this.name, this.id, "on", value); }, onUpdate: function(value) { console.log("Change:",value); execute(this.api, this.device, "on", value); },
perms: ["pw","pr","ev"], perms: ["pw","pr","ev"],
format: "bool", format: "bool",
initialValue: false, initialValue: false,
@@ -237,7 +226,7 @@ PhilipsHueAccessory.prototype = {
designedMaxLength: 1 designedMaxLength: 1
},{ },{
cType: types.HUE_CTYPE, cType: types.HUE_CTYPE,
onUpdate: function(value) { console.log("Change:",value); execute(this.name, this.id, "hue", value); }, onUpdate: function(value) { console.log("Change:",value); execute(this.api, this.device, "hue", value); },
perms: ["pw","pr","ev"], perms: ["pw","pr","ev"],
format: "int", format: "int",
initialValue: 0, initialValue: 0,
@@ -250,7 +239,7 @@ PhilipsHueAccessory.prototype = {
unit: "arcdegrees" unit: "arcdegrees"
},{ },{
cType: types.BRIGHTNESS_CTYPE, cType: types.BRIGHTNESS_CTYPE,
onUpdate: function(value) { console.log("Change:",value); execute(this.name, this.id, "brightness", value); }, onUpdate: function(value) { console.log("Change:",value); execute(this.api, this.device, "brightness", value); },
perms: ["pw","pr","ev"], perms: ["pw","pr","ev"],
format: "int", format: "int",
initialValue: 0, initialValue: 0,
@@ -263,7 +252,7 @@ PhilipsHueAccessory.prototype = {
unit: "%" unit: "%"
},{ },{
cType: types.SATURATION_CTYPE, cType: types.SATURATION_CTYPE,
onUpdate: function(value) { console.log("Change:",value); execute(this.name, this.id, "saturation", value); }, onUpdate: function(value) { console.log("Change:",value); execute(this.api, this.device, "saturation", value); },
perms: ["pw","pr","ev"], perms: ["pw","pr","ev"],
format: "int", format: "int",
initialValue: 0, initialValue: 0,