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

View File

@@ -3,109 +3,123 @@ var Characteristic = require("hap-nodejs").Characteristic;
var request = require("request"); var request = require("request");
module.exports = { module.exports = {
accessory: HttpAccessory accessory: HttpAccessory
} }
function HttpAccessory(log, config) { function HttpAccessory(log, config) {
this.log = log; this.log = log;
// url info // url info
this.on_url = config["on_url"]; this.on_url = config["on_url"];
this.off_url = config["off_url"]; this.off_url = config["off_url"];
this.brightness_url = config["brightness_url"]; this.brightness_url = config["brightness_url"];
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 = {
httpRequest: function(url, method, username, password, callback) { httpRequest: function(url, method, username, password, callback) {
request({ request({
url: url, url: url,
method: method, method: method,
auth: { auth: {
user: username, user: username,
pass: password, pass: password,
sendImmediately: false sendImmediately: false
} }
}, },
function (error, response, body) { function(error, response, body) {
callback(error, response, body) callback(error, response, body)
}) })
}, },
setPowerState: function(powerOn, callback) { setPowerState: function(powerOn, callback) {
var url; var url;
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"); }
}
this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) { this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) {
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); this.log(this.username);
this.log(this.username); this.log(this.password);
this.log(this.password); callback();
callback(); }
} }.bind(this));
}.bind(this)); },
},
setBrightness: function(level, callback) { setBrightness: function(level, callback) {
var url = this.brightness_url.replace("%b", level) var url = this.brightness_url.replace("%b", level)
this.log("Setting brightness to %s", level); this.log("Setting brightness to %s", level);
this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) { this.httpRequest(url, this.http_method, this.username, this.password, function(error, response, body) {
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(); }
} }.bind(this));
}.bind(this)); },
},
identify: function(callback) { identify: function(callback) {
this.log("Identify requested!"); this.log("Identify requested!");
callback(); // success callback(); // success
}, },
getServices: function() { getServices: function() {
// you can OPTIONALLY create an information service if you wish to override // you can OPTIONALLY create an information service if you wish to override
// the default values for things like serial number, model, etc. // the default values for things like serial number, model, etc.
var informationService = new Service.AccessoryInformation(); var informationService = new Service.AccessoryInformation();
informationService informationService
.setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer") .setCharacteristic(Characteristic.Manufacturer, "HTTP Manufacturer")
.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);
lightbulbService switchService
.getCharacteristic(Characteristic.On) .getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this)); .on('get', this.getPowerOn.bind(this))
.on('set', this.setPowerOn.bind(this));
lightbulbService return [switchService];
.addCharacteristic(new Characteristic.Brightness()) } else if (this.service == "Light") {
.on('set', this.setBrightness.bind(this)); var lightbulbService = new Service.Lightbulb(this.name);
return [informationService, lightbulbService]; lightbulbService
} .getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this));
if (this.brightnessHandling == "yes") {
lightbulbService
.addCharacteristic(new Characteristic.Brightness())
.on('set', this.setBrightness.bind(this));
}
return [informationService, lightbulbService];
}
}
}; };