Merge pull request #322 from mvanholstyn/master

adding support for usernames and password in http accessories
This commit is contained in:
Nick Farina
2015-10-25 10:03:31 -04:00

View File

@@ -14,14 +14,21 @@ function HttpAccessory(log, config) {
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.password = config["password"];
} }
HttpAccessory.prototype = { HttpAccessory.prototype = {
httpRequest: function(url, method, callback) { httpRequest: function(url, method, username, password, callback) {
request({ request({
url: url, url: url,
method: method method: method,
auth: {
user: username,
pass: password,
sendImmediately: false
}
}, },
function (error, response, body) { function (error, response, body) {
callback(error, response, body) callback(error, response, body)
@@ -40,13 +47,17 @@ HttpAccessory.prototype = {
this.log("Setting power state to off"); this.log("Setting power state to off");
} }
this.httpRequest(url, this.http_method, 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(body);
this.log(this.username);
this.log(this.password);
callback(); callback();
} }
}.bind(this)); }.bind(this));
@@ -57,7 +68,7 @@ HttpAccessory.prototype = {
this.log("Setting brightness to %s", level); this.log("Setting brightness to %s", level);
this.httpRequest(url, this.http_method, 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);
@@ -68,33 +79,33 @@ HttpAccessory.prototype = {
} }
}.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(); var lightbulbService = new Service.Lightbulb();
lightbulbService lightbulbService
.getCharacteristic(Characteristic.On) .getCharacteristic(Characteristic.On)
.on('set', this.setPowerState.bind(this)); .on('set', this.setPowerState.bind(this));
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];
} }
}; };