Add SSL and basic auth support for Domoticz platform.

This commit is contained in:
Luke Redpath
2015-07-22 15:27:52 +01:00
parent 38f3d9b82e
commit 34110039f5

View File

@@ -22,6 +22,14 @@
// }
// ],
//
// If your server uses HTTPS, you can specify "ssl": true in your config. If
// your server uses a self-signed certificate, you'll need to run the following
// before starting the server or you will get an error:
//
// export NODE_TLS_REJECT_UNAUTHORIZED=0
//
// For basic auth support, specify the "user" and "password" in your config.
//
// When you attempt to add a device, it will ask for a "PIN code".
// The default code for all HomeBridge accessories is 031-45-154.
//
@@ -30,8 +38,11 @@ var request = require("request");
function DomoticzPlatform(log, config){
this.log = log;
this.user = config["user"];
this.password = config["password"];
this.server = config["server"];
this.port = config["port"];
this.protocol = config["ssl"] ? "https" : "http";
this.roomid = 0;
if (typeof config["roomid"] != 'undefined') {
this.roomid = config["roomid"];
@@ -46,15 +57,22 @@ function sortByKey(array, key) {
}
DomoticzPlatform.prototype = {
urlForQuery: function(query) {
var serverString = this.server;
if (this.user && this.password) {
serverString = this.user + ":" + this.password + "@" + serverString;
}
return this.protocol + "://" + serverString + ":" + this.port + "/json.htm?" + query;
},
accessories: function(callback) {
this.log("Fetching Domoticz lights and switches...");
var that = this;
var foundAccessories = [];
if (this.roomid == 0) {
//Get Lights
request.get({
url: "http://" + this.server + ":" + this.port + "/json.htm?type=devices&filter=light&used=true&order=Name",
url: this.urlForQuery("type=devices&filter=light&used=true&order=Name"),
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {
@@ -67,14 +85,14 @@ DomoticzPlatform.prototype = {
}
callback(foundAccessories);
} else {
that.log("There was a problem connecting to Domoticz.");
that.log("There was a problem connecting to Domoticz. (" + err + ")");
}
});
}
else {
//Get all devices specified in the room
request.get({
url: "http://" + this.server + ":" + this.port + "/json.htm?type=devices&plan=" + this.roomid,
url: this.urlForQuery("type=devices&plan=" + this.roomid),
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {
@@ -97,7 +115,7 @@ DomoticzPlatform.prototype = {
//Get Scenes
foundAccessories = [];
request.get({
url: "http://" + this.server + ":" + this.port + "/json.htm?type=scenes",
url: this.urlForQuery("type=scenes"),
json: true
}, function(err, response, json) {
if (!err && response.statusCode == 200) {