From b4e8cedb2ce810842556977a74aaee9d7d103f4d Mon Sep 17 00:00:00 2001 From: Marty Skinner Date: Thu, 14 Jan 2016 21:10:15 -0500 Subject: [PATCH 1/2] fix homepage link; add xml2json as a dependence; update version number --- package.json | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 989ffc9..7c4a09c 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "homebridge-indigo", "description": "Indigo plugin for homebridge: https://github.com/nfarina/homebridge", - "version": "0.2.11", + "version": "0.2.12", "repository": { "type": "git", "url": "https://github.com/webdeck/homebridge-indigo.git" @@ -13,7 +13,7 @@ "bugs": { "url": "https://github.com/webdeck/homebridge-indigo/issues" }, - "homepage": "https://github.com/rodtoll/homebridge-indigo", + "homepage": "https://github.com/webdeck/homebridge-indigo", "preferGlobal": true, "keywords": [ "homebridge-plugin", @@ -29,6 +29,7 @@ "dependencies": { "async": "^1.4.2", "inherits": "^2.0.1", - "request": "2.49.x" + "request": "2.49.x", + "xml2json": "^0.9.0" } } From 012b15755da0de4f8ec56a57355015bf2bcb8005 Mon Sep 17 00:00:00 2001 From: Marty Skinner Date: Thu, 14 Jan 2016 21:11:14 -0500 Subject: [PATCH 2/2] support Indigo 5's XML format of RESTful API --- index.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 9c6af2b..c54df56 100644 --- a/index.js +++ b/index.js @@ -59,6 +59,7 @@ will NOT be exposed to HomeKit, because Indigo excludes those devices from its R var request = require("request"); var async = require("async"); var inherits = require('util').inherits; +var parser = require("xml2json"); var Service, Characteristic, Accessory, uuid; module.exports = function(homebridge) { @@ -94,6 +95,10 @@ function fixInheritance(subclass, superclass) { function IndigoPlatform(log, config) { this.log = log; + if (config.version) { + this.version = config.version; + } + var protocol = "http"; if (config.protocol) { protocol = config.protocol; @@ -149,9 +154,16 @@ function IndigoPlatform(log, config) { IndigoPlatform.prototype.accessories = function(callback) { this.foundAccessories = []; - var requestURLs = [ this.path + "/devices.json/" ]; - if (this.includeActions) { - requestURLs.push(this.path + "/actions.json/"); + if (this.version == '5') { + var requestURLs = [ this.path + "/devices.xml/" ]; + if (this.includeActions) { + requestURLs.push(this.path + "/actions.xml/"); + } + } else { + var requestURLs = [ this.path + "/devices.json/" ]; + if (this.includeActions) { + requestURLs.push(this.path + "/actions.json/"); + } } async.eachSeries(requestURLs, @@ -291,6 +303,32 @@ IndigoPlatform.prototype.indigoRequestJSON = function(path, method, qs, callback } var json; try { + if (this.version == '5') { + // see toJson options here: https://github.com/buglabs/node-xml2json + var json = parser.toJson(body, {coerce: true, sanitize: false}); + + // replace the "$t" (toJson does this as tag for link text) with "name" + // replace the "href" tag with "restURL" + json = json.replace(/\$t/g, 'name'); + json = json.replace(/"href":/g, '"restURL":'); + + // the JSON returned by toJson is more complex than needed here + // so take off the outer layer + + // here we handle devices.xml and actions.xml pieces + json = json.replace(/^{"devices":{"device":/, ''); + json = json.replace(/^{"actions":{"action":/, ''); + json = json.replace(/]}}$/, ']'); + + // we need to insert an appropriate "restParent" + json = json.replace(/^{"device":{/, '{"restParent":"devices",'); + json = json.replace(/{"action":{/, '{"restParent":"actions",'); + + // cleanup any trailing brace + json = json.replace(/}$/, ''); + + body = json; + } var json = JSON.parse(body); } catch (e) { var msg2 = "Error parsing Indigo response for " + path +