jarvisnode
Version:
A library written in Node.js used to communicate with my Jarvis project
83 lines (66 loc) • 2.25 kB
JavaScript
/**
* Handles the end of the library initialization.
* @callback initializationCallback
* @param {Object} error - Contains the error if there was
* some problems during the library initialization.
*/
var fs = require('fs');
var BehaviourController = require('./modules/controller');
var behaviours = new BehaviourController();
var debug = require('debug')('jarvisnode:main');
var baseConfig = {
name: '',
description: '',
commands: [],
events: [],
};
/**
* Jarvisnode entry point.
* @module jarvisnode/index
*/
module.exports = {
/**
* Initialize the library.
* @param {Object} def - The object that contains the information in order to get
* Jarvis aware about the services which the device using the library will provide.
* @param {initializationCallback} cb - The callback function.
*/
init: function(def, key, cloud, confPath, cb) {
//First load the local configuration file
require('./modules/config').init(confPath, function(err) {
if (!key)
return cb(new Error('User key is required'));
// Throw an error if there isn't at least
// the device name in the definition object
if (!def.name)
return cb(new Error('Satellite name is required'));
var conf = require('./modules/config').get();
conf.SATELLITE_CONFIGURATION_FILE_PATH = require('path').join(__dirname, 'satellite.json');
conf.key = key;
if (cloud) {
conf.CLOUD = true;
conf.J_IP = cloud;
}
def['key'] = key;
//Save the device definition
fs.writeFile(conf.SATELLITE_CONFIGURATION_FILE_PATH,
JSON.stringify(require('extend')(baseConfig, def)), function(err) {
// Huston, we have a problem
if (err)
return cb(err);
debug('starting');
//Start monitoring connections, and requests
behaviours.control();
// Nothing to worry about...for now
return cb(null)
});
});
},
event: function(code, message) {
behaviours.event(code, message);
},
/**
* Expose the listener for incoming requests
*/
requests: require('./modules/bridge')
};