Http Accessory improvements : accessory type (switch or light), and ligtht brightness handling toggle

This commit is contained in:
Kevin Mathy
2015-10-30 22:55:53 +01:00
parent 3da52388e8
commit 255a064c08
+22 -8
View File
@@ -16,6 +16,9 @@ function HttpAccessory(log, config) {
this.http_method = config["http_method"]; this.http_method = config["http_method"];
this.username = config["username"]; this.username = config["username"];
this.password = config["password"]; this.password = config["password"];
this.service = config["service"] || "Switch";
this.name = config["name"];
this.brightnessHandling = config["brightnessHandling"] || "no";
} }
HttpAccessory.prototype = { HttpAccessory.prototype = {
@@ -30,7 +33,7 @@ HttpAccessory.prototype = {
sendImmediately: false sendImmediately: false
} }
}, },
function (error, response, body) { function(error, response, body) {
callback(error, response, body) callback(error, response, body)
}) })
}, },
@@ -41,8 +44,7 @@ HttpAccessory.prototype = {
if (powerOn) { if (powerOn) {
url = this.on_url; url = this.on_url;
this.log("Setting power state to on"); this.log("Setting power state to on");
} } else {
else {
url = this.off_url; url = this.off_url;
this.log("Setting power state to off"); this.log("Setting power state to off");
} }
@@ -51,8 +53,7 @@ HttpAccessory.prototype = {
if (error) { if (error) {
this.log('HTTP power function failed: %s', error.message); this.log('HTTP power function failed: %s', error.message);
callback(error); callback(error);
} } else {
else {
this.log('HTTP power function succeeded!'); this.log('HTTP power function succeeded!');
this.log(response); this.log(response);
this.log(body); this.log(body);
@@ -72,8 +73,7 @@ HttpAccessory.prototype = {
if (error) { if (error) {
this.log('HTTP brightness function failed: %s', error); this.log('HTTP brightness function failed: %s', error);
callback(error); callback(error);
} } else {
else {
this.log('HTTP brightness function succeeded!'); this.log('HTTP brightness function succeeded!');
callback(); callback();
} }
@@ -96,16 +96,30 @@ HttpAccessory.prototype = {
.setCharacteristic(Characteristic.Model, "HTTP Model") .setCharacteristic(Characteristic.Model, "HTTP Model")
.setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number"); .setCharacteristic(Characteristic.SerialNumber, "HTTP Serial Number");
var lightbulbService = new Service.Lightbulb(); if (this.service == "Switch") {
var switchService = new Service.Switch(this.name);
switchService
.getCharacteristic(Characteristic.On)
.on('get', this.getPowerOn.bind(this))
.on('set', this.setPowerOn.bind(this));
return [switchService];
} else if (this.service == "Light") {
var lightbulbService = new Service.Lightbulb(this.name);
lightbulbService lightbulbService
.getCharacteristic(Characteristic.On) .getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this)); .on('set', this.setPowerState.bind(this));
if (this.brightnessHandling == "yes") {
lightbulbService lightbulbService
.addCharacteristic(new Characteristic.Brightness()) .addCharacteristic(new Characteristic.Brightness())
.on('set', this.setBrightness.bind(this)); .on('set', this.setBrightness.bind(this));
}
return [informationService, lightbulbService]; return [informationService, lightbulbService];
} }
}
}; };