From 34110039f57c5fe81bd5f5de9044a388cd9decac Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Wed, 22 Jul 2015 15:27:52 +0100 Subject: [PATCH 1/2] Add SSL and basic auth support for Domoticz platform. --- platforms/Domoticz.js | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/platforms/Domoticz.js b/platforms/Domoticz.js index aacce99..0ba50cf 100644 --- a/platforms/Domoticz.js +++ b/platforms/Domoticz.js @@ -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) { From 7b01e6c1d007adf16f21c4f24b2b39495e3a531c Mon Sep 17 00:00:00 2001 From: Luke Redpath Date: Wed, 22 Jul 2015 16:12:22 +0100 Subject: [PATCH 2/2] =?UTF-8?q?Won=E2=80=99t=20be=20able=20to=20do=20much?= =?UTF-8?q?=20unless=20accessories=20can=20use=20SSL/basic=20auth=20too.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- platforms/Domoticz.js | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/platforms/Domoticz.js b/platforms/Domoticz.js index 0ba50cf..a9e8e72 100644 --- a/platforms/Domoticz.js +++ b/platforms/Domoticz.js @@ -79,7 +79,7 @@ DomoticzPlatform.prototype = { if (json['result'] != undefined) { var sArray=sortByKey(json['result'],"Name"); sArray.map(function(s) { - accessory = new DomoticzAccessory(that.log, that.server, that.port, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); + accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); foundAccessories.push(accessory); }) } @@ -101,7 +101,7 @@ DomoticzPlatform.prototype = { sArray.map(function(s) { //only accept switches for now if (typeof s.SwitchType != 'undefined') { - accessory = new DomoticzAccessory(that.log, that.server, that.port, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); + accessory = new DomoticzAccessory(that.log, that, false, s.idx, s.Name, s.HaveDimmer, s.MaxDimLevel, (s.SubType=="RGB")||(s.SubType=="RGBW")); foundAccessories.push(accessory); } }) @@ -122,7 +122,7 @@ DomoticzPlatform.prototype = { if (json['result'] != undefined) { var sArray=sortByKey(json['result'],"Name"); sArray.map(function(s) { - accessory = new DomoticzAccessory(that.log, that.server, that.port, true, s.idx, s.Name, false, 0, false); + accessory = new DomoticzAccessory(that.log, that, true, s.idx, s.Name, false, 0, false); foundAccessories.push(accessory); }) } @@ -134,7 +134,7 @@ DomoticzPlatform.prototype = { } } -function DomoticzAccessory(log, server, port, IsScene, idx, name, HaveDimmer, MaxDimLevel, HaveRGB) { +function DomoticzAccessory(log, platform, IsScene, idx, name, HaveDimmer, MaxDimLevel, HaveRGB) { // device info this.IsScene = IsScene; this.idx = idx; @@ -143,8 +143,7 @@ function DomoticzAccessory(log, server, port, IsScene, idx, name, HaveDimmer, Ma this.MaxDimLevel = MaxDimLevel; this.HaveRGB = HaveRGB; this.log = log; - this.server = server; - this.port = port; + this.platform = platform; } DomoticzAccessory.prototype = { @@ -153,13 +152,13 @@ DomoticzAccessory.prototype = { if (this.IsScene == false) { //Lights if (c == "On" || c == "Off") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=" + c + "&level=0"; + url = this.platform.urlForQuery("type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=" + c + "&level=0"); } else if (c == "setHue") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=setcolbrightnessvalue&idx=" + this.idx + "&hue=" + value + "&brightness=100" + "&iswhite=false"; + url = this.platform.urlForQuery("type=command¶m=setcolbrightnessvalue&idx=" + this.idx + "&hue=" + value + "&brightness=100" + "&iswhite=false"); } else if (c == "setLevel") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value; + url = this.platform.urlForQuery("type=command¶m=switchlight&idx=" + this.idx + "&switchcmd=Set%20Level&level=" + value); } else if (value != undefined) { this.log(this.name + " Unhandled Light command! cmd=" + c + ", value=" + value); @@ -168,7 +167,7 @@ DomoticzAccessory.prototype = { else { //Scenes if (c == "On" || c == "Off") { - url = "http://" + this.server + ":" + this.port + "/json.htm?type=command¶m=switchscene&idx=" + this.idx + "&switchcmd=" + c; + url = this.platform.urlForQuery("type=command¶m=switchscene&idx=" + this.idx + "&switchcmd=" + c); } else if (value != undefined) { this.log(this.name + " Unhandled Scene command! cmd=" + c + ", value=" + value);