From 40fc7acbed9f97ccf683b26c628e0b9453feb0f0 Mon Sep 17 00:00:00 2001 From: straccio Date: Wed, 27 Jan 2016 07:40:26 +0100 Subject: [PATCH 1/8] Added pluginPath based on npm --- lib/plugin.js | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index e7e9c1f..7f2bab8 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -13,7 +13,7 @@ module.exports = { /** * Homebridge Plugin. - * + * * Allows for discovering and loading installed Homebridge plugins. */ @@ -28,39 +28,39 @@ Plugin.prototype.name = function() { Plugin.prototype.load = function(options) { options = options || {}; - + // does this plugin exist at all? if (!fs.existsSync(this.pluginPath)) { throw new Error("Plugin " + this.pluginPath + " was not found. Make sure the module '" + this.pluginPath + "' is installed."); } - + // attempt to load package.json var pjson = Plugin.loadPackageJSON(this.pluginPath); - + // very temporary fix for first wave of plugins if (pjson.peerDepdendencies && (!pjson.engines || !pjson.engines.homebridge)) { var engines = pjson.engines || {} engines.homebridge = pjson.peerDepdendencies.homebridge; pjson.engines = engines; } - + // pluck out the HomeBridge version requirement if (!pjson.engines || !pjson.engines.homebridge) { throw new Error("Plugin " + this.pluginPath + " does not contain the 'homebridge' package in 'engines'."); } - + var versionRequired = pjson.engines.homebridge; // make sure the version is satisfied by the currently running version of HomeBridge if (!semver.satisfies(version, versionRequired)) { throw new Error("Plugin " + this.pluginPath + " requires a HomeBridge version of " + versionRequired + " which does not satisfy the current HomeBridge version of " + version + ". You may need to upgrade your installation of HomeBridge."); } - + // figure out the main module - index.js unless otherwise specified var main = pjson.main || "./index.js"; var mainPath = path.join(this.pluginPath, main); - + // try to require() it and grab the exported initialization hook this.initializer = require(mainPath); } @@ -69,11 +69,11 @@ Plugin.loadPackageJSON = function(pluginPath) { // check for a package.json var pjsonPath = path.join(pluginPath, "package.json"); var pjson = null; - + if (!fs.existsSync(pjsonPath)) { throw new Error("Plugin " + pluginPath + " does not contain a package.json."); } - + try { // attempt to parse package.json pjson = JSON.parse(fs.readFileSync(pjsonPath)); @@ -81,7 +81,7 @@ Plugin.loadPackageJSON = function(pluginPath) { catch (err) { throw new Error("Plugin " + pluginPath + " contains an invalid package.json. Error: " + err); } - + // make sure the name is prefixed with 'homebridge-' if (!pjson.name || pjson.name.indexOf('homebridge-') != 0) { throw new Error("Plugin " + pluginPath + " does not have a package name that begins with 'homebridge-'."); @@ -91,7 +91,7 @@ Plugin.loadPackageJSON = function(pluginPath) { if (!pjson.keywords || pjson.keywords.indexOf("homebridge-plugin") == -1) { throw new Error("Plugin " + pluginPath + " package.json does not contain the keyword 'homebridge-plugin'."); } - + return pjson; } @@ -119,9 +119,10 @@ Plugin.getDefaultPaths = function() { } else { paths.push('/usr/local/lib/node_modules'); paths.push('/usr/lib/node_modules'); + const exec = require('child_process').execSync + paths.push(exec('/bin/echo -n "$(npm -g prefix)/lib/node_modules"').toString('utf8')); } } - return paths; } @@ -138,17 +139,17 @@ Plugin.installed = function() { var plugins = []; var pluginsByName = {}; // don't add duplicate plugins var searchedPaths = {}; // don't search the same paths twice - + // search for plugins among all known paths, in order for (var index in Plugin.paths) { var requirePath = Plugin.paths[index]; - + // did we already search this path? if (searchedPaths[requirePath]) continue; - + searchedPaths[requirePath] = true; - + // just because this path is in require.main.paths doesn't mean it necessarily exists! if (!fs.existsSync(requirePath)) continue; @@ -158,17 +159,17 @@ Plugin.installed = function() { // does this path point inside a single plugin and not a directory containing plugins? if (fs.existsSync(path.join(requirePath, "package.json"))) names = [""]; - + // read through each directory in this node_modules folder for (var index2 in names) { var name = names[index2]; - + // reconstruct full path var pluginPath = path.join(requirePath, name); - + // we only care about directories if (!fs.statSync(pluginPath).isDirectory()) continue; - + // does this module contain a package.json? var pjson; try { @@ -180,14 +181,14 @@ Plugin.installed = function() { if (!name || name.indexOf('homebridge-') == 0) { log.warn(err.message); } - + // skip this module continue; } - + // get actual name if this path points inside a single plugin if (!name) name = pjson.name; - + // add it to the return list if (!pluginsByName[name]) { pluginsByName[name] = pluginPath; @@ -198,6 +199,6 @@ Plugin.installed = function() { } } } - + return plugins; } From fa9561d98a58699074a3551ba7c2ef394077bf7e Mon Sep 17 00:00:00 2001 From: straccio Date: Mon, 22 Feb 2016 10:01:25 +0100 Subject: [PATCH 2/8] missed ";" --- lib/plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugin.js b/lib/plugin.js index 7f2bab8..6f56daa 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -119,7 +119,7 @@ Plugin.getDefaultPaths = function() { } else { paths.push('/usr/local/lib/node_modules'); paths.push('/usr/lib/node_modules'); - const exec = require('child_process').execSync + const exec = require('child_process').execSync; paths.push(exec('/bin/echo -n "$(npm -g prefix)/lib/node_modules"').toString('utf8')); } } From ffe4232c3b9cf856609b99c7e7de0a365ad466eb Mon Sep 17 00:00:00 2001 From: straccio Date: Wed, 27 Jan 2016 07:40:26 +0100 Subject: [PATCH 3/8] Added pluginPath based on npm --- lib/plugin.js | 51 ++++++++++++++++++++++++++------------------------- 1 file changed, 26 insertions(+), 25 deletions(-) diff --git a/lib/plugin.js b/lib/plugin.js index e7e9c1f..7f2bab8 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -13,7 +13,7 @@ module.exports = { /** * Homebridge Plugin. - * + * * Allows for discovering and loading installed Homebridge plugins. */ @@ -28,39 +28,39 @@ Plugin.prototype.name = function() { Plugin.prototype.load = function(options) { options = options || {}; - + // does this plugin exist at all? if (!fs.existsSync(this.pluginPath)) { throw new Error("Plugin " + this.pluginPath + " was not found. Make sure the module '" + this.pluginPath + "' is installed."); } - + // attempt to load package.json var pjson = Plugin.loadPackageJSON(this.pluginPath); - + // very temporary fix for first wave of plugins if (pjson.peerDepdendencies && (!pjson.engines || !pjson.engines.homebridge)) { var engines = pjson.engines || {} engines.homebridge = pjson.peerDepdendencies.homebridge; pjson.engines = engines; } - + // pluck out the HomeBridge version requirement if (!pjson.engines || !pjson.engines.homebridge) { throw new Error("Plugin " + this.pluginPath + " does not contain the 'homebridge' package in 'engines'."); } - + var versionRequired = pjson.engines.homebridge; // make sure the version is satisfied by the currently running version of HomeBridge if (!semver.satisfies(version, versionRequired)) { throw new Error("Plugin " + this.pluginPath + " requires a HomeBridge version of " + versionRequired + " which does not satisfy the current HomeBridge version of " + version + ". You may need to upgrade your installation of HomeBridge."); } - + // figure out the main module - index.js unless otherwise specified var main = pjson.main || "./index.js"; var mainPath = path.join(this.pluginPath, main); - + // try to require() it and grab the exported initialization hook this.initializer = require(mainPath); } @@ -69,11 +69,11 @@ Plugin.loadPackageJSON = function(pluginPath) { // check for a package.json var pjsonPath = path.join(pluginPath, "package.json"); var pjson = null; - + if (!fs.existsSync(pjsonPath)) { throw new Error("Plugin " + pluginPath + " does not contain a package.json."); } - + try { // attempt to parse package.json pjson = JSON.parse(fs.readFileSync(pjsonPath)); @@ -81,7 +81,7 @@ Plugin.loadPackageJSON = function(pluginPath) { catch (err) { throw new Error("Plugin " + pluginPath + " contains an invalid package.json. Error: " + err); } - + // make sure the name is prefixed with 'homebridge-' if (!pjson.name || pjson.name.indexOf('homebridge-') != 0) { throw new Error("Plugin " + pluginPath + " does not have a package name that begins with 'homebridge-'."); @@ -91,7 +91,7 @@ Plugin.loadPackageJSON = function(pluginPath) { if (!pjson.keywords || pjson.keywords.indexOf("homebridge-plugin") == -1) { throw new Error("Plugin " + pluginPath + " package.json does not contain the keyword 'homebridge-plugin'."); } - + return pjson; } @@ -119,9 +119,10 @@ Plugin.getDefaultPaths = function() { } else { paths.push('/usr/local/lib/node_modules'); paths.push('/usr/lib/node_modules'); + const exec = require('child_process').execSync + paths.push(exec('/bin/echo -n "$(npm -g prefix)/lib/node_modules"').toString('utf8')); } } - return paths; } @@ -138,17 +139,17 @@ Plugin.installed = function() { var plugins = []; var pluginsByName = {}; // don't add duplicate plugins var searchedPaths = {}; // don't search the same paths twice - + // search for plugins among all known paths, in order for (var index in Plugin.paths) { var requirePath = Plugin.paths[index]; - + // did we already search this path? if (searchedPaths[requirePath]) continue; - + searchedPaths[requirePath] = true; - + // just because this path is in require.main.paths doesn't mean it necessarily exists! if (!fs.existsSync(requirePath)) continue; @@ -158,17 +159,17 @@ Plugin.installed = function() { // does this path point inside a single plugin and not a directory containing plugins? if (fs.existsSync(path.join(requirePath, "package.json"))) names = [""]; - + // read through each directory in this node_modules folder for (var index2 in names) { var name = names[index2]; - + // reconstruct full path var pluginPath = path.join(requirePath, name); - + // we only care about directories if (!fs.statSync(pluginPath).isDirectory()) continue; - + // does this module contain a package.json? var pjson; try { @@ -180,14 +181,14 @@ Plugin.installed = function() { if (!name || name.indexOf('homebridge-') == 0) { log.warn(err.message); } - + // skip this module continue; } - + // get actual name if this path points inside a single plugin if (!name) name = pjson.name; - + // add it to the return list if (!pluginsByName[name]) { pluginsByName[name] = pluginPath; @@ -198,6 +199,6 @@ Plugin.installed = function() { } } } - + return plugins; } From 6ae2a19d37cccd6ad60d332a39e7ee896a52fb7f Mon Sep 17 00:00:00 2001 From: straccio Date: Mon, 22 Feb 2016 10:01:25 +0100 Subject: [PATCH 4/8] missed ";" --- lib/plugin.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/plugin.js b/lib/plugin.js index 7f2bab8..6f56daa 100644 --- a/lib/plugin.js +++ b/lib/plugin.js @@ -119,7 +119,7 @@ Plugin.getDefaultPaths = function() { } else { paths.push('/usr/local/lib/node_modules'); paths.push('/usr/lib/node_modules'); - const exec = require('child_process').execSync + const exec = require('child_process').execSync; paths.push(exec('/bin/echo -n "$(npm -g prefix)/lib/node_modules"').toString('utf8')); } } From 1fb58be2b9dae155f68b1b91a4b8f3f96f24c8a8 Mon Sep 17 00:00:00 2001 From: straccio Date: Wed, 2 Mar 2016 08:47:15 +0100 Subject: [PATCH 5/8] ignore idea --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 137b758..7608131 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ npm-debug.log # Ignore any extra plugins in the example directory that aren't in Git already # (this is a sandbox for the user) -example-plugins \ No newline at end of file +example-plugins + +.idea \ No newline at end of file From 911f088df93ee0dd0d8df5d22a0751cfff3eb665 Mon Sep 17 00:00:00 2001 From: straccio Date: Mon, 7 Mar 2016 17:32:18 +0100 Subject: [PATCH 6/8] A way to skip cached accessories from being loaded in HAP --- lib/server.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/server.js b/lib/server.js index 37b13d9..098857f 100644 --- a/lib/server.js +++ b/lib/server.js @@ -318,13 +318,13 @@ Server.prototype._configCachedPlatformAccessories = function() { } if (platformInstance) { - platformInstance.configureAccessory(accessory); + if(platformInstance.configureAccessory(accessory)!=false){ + accessory._prepareAssociatedHAPAccessory(); + this._bridge.addBridgedAccessory(accessory._associatedHAPAccessory); + } } else { console.log("Failed to find plugin to handle accessory " + accessory.displayName); } - - accessory._prepareAssociatedHAPAccessory(); - this._bridge.addBridgedAccessory(accessory._associatedHAPAccessory); } } From 73fdec5928ee20cc1338c0971ab767f7cf4c1c84 Mon Sep 17 00:00:00 2001 From: straccio Date: Tue, 8 Mar 2016 08:44:52 +0100 Subject: [PATCH 7/8] Revert to upstream/master, no need to skip cached accessories --- lib/server.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/server.js b/lib/server.js index 098857f..37b13d9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -318,13 +318,13 @@ Server.prototype._configCachedPlatformAccessories = function() { } if (platformInstance) { - if(platformInstance.configureAccessory(accessory)!=false){ - accessory._prepareAssociatedHAPAccessory(); - this._bridge.addBridgedAccessory(accessory._associatedHAPAccessory); - } + platformInstance.configureAccessory(accessory); } else { console.log("Failed to find plugin to handle accessory " + accessory.displayName); } + + accessory._prepareAssociatedHAPAccessory(); + this._bridge.addBridgedAccessory(accessory._associatedHAPAccessory); } } From b94c3caa3bc19cfd493a945736e2f2fdeb642940 Mon Sep 17 00:00:00 2001 From: straccio Date: Tue, 8 Mar 2016 15:59:44 +0100 Subject: [PATCH 8/8] Changed color for logging timestamp to white. --- lib/logger.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/logger.js b/lib/logger.js index 5aa5633..6050ec0 100644 --- a/lib/logger.js +++ b/lib/logger.js @@ -67,7 +67,7 @@ Logger.prototype.log = function(level, msg) { // prepend timestamp var date = new Date(); - msg = chalk.black("[" + date.toLocaleString() + "]") + " " + msg; + msg = chalk.white("[" + date.toLocaleString() + "]") + " " + msg; func(msg); }