Merge pull request #361 from kemathy/master

Http accessory improvement
This commit is contained in:
Nick Farina
2015-11-02 11:55:42 -08:00
2 changed files with 101 additions and 84 deletions
+20 -7
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 = {
@@ -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,29 @@ 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('set', this.setPowerState.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];
} }
}
}; };
+5 -1
View File
@@ -203,7 +203,11 @@
"on_url": "https://192.168.1.22:3030/devices/23222/on", "on_url": "https://192.168.1.22:3030/devices/23222/on",
"off_url": "https://192.168.1.22:3030/devices/23222/off", "off_url": "https://192.168.1.22:3030/devices/23222/off",
"brightness_url": "https://192.168.1.22:3030/devices/23222/brightness/%b", "brightness_url": "https://192.168.1.22:3030/devices/23222/brightness/%b",
"http_method": "POST" "username": "",
"password": "",
"http_method": "POST",
"service": "Switch",
"brightnessHandling": "no"
}, },
{ {
"accessory": "HttpHygrometer", "accessory": "HttpHygrometer",