Extensions are JavaScript files that contain custom code that Bitdog Hub can run. Extension can do almost anything that can be done in Node.js. An example blank extension can be found in the extensions directory. I installed Bitdog Hub in /home/pi/hub.

extensionsls

Open your favorite text editor and lets take a look at  what is inside blankExtension.js

// Extension will inherit functions from ExtensionBase
// Make sure the path to extensionBase is correct
var ExtensionBase = require('../lib/extensionBase.js');
var util = require('util');

function Extension() {
}
// Extension inherits from ExtensionBase
util.inherits(Extension, ExtensionBase);

Extension.prototype.onMessage = function (message, configuration, logger) {
};

Extension.prototype.onInitialize = function (configuration, logger) {
};

// Export your Extension class so it can be loaded by the framework
module.exports = Extension;

We have tried to keep extensions as simple as possible. Extensions have only two key methods onMessage and onInitialize.

onInitialize is called by the Bitdog Hub framework once during process startup. It is where schemas, commands, and data collectors can be defined. Any initialization you need to do to make your extension work should be done here.

onMessage is called by the Bitdog Hub framework for every inbound and outbound message.  Messages can be examined and logic executed. This is a good place to capture OpenZWave messages and run logic.

The Extension function/class inherits from ExtensionBase which provides additional methods for interacting with the Bitdog Hub.

You can run an extension by starting Bitdog Hub with the -e option. For example:

./bitdoghub start -e node_modules/bitdog-hub/extensions/{your extension file name}

The extension file path is stored in the configuration file and will continue to be run every time Bitdog Hub is started, even if the -e option is not specified. Use the -e clear option to remove the extension for the configuration. For example:

./bitdoghub start -e clear