2017-06-11 00:29:00 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const Config = require('./config.js');
|
2017-06-11 01:52:11 +00:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const events = require('events');
|
|
|
|
const logger = require('./logger.js');
|
2017-06-11 00:29:00 +00:00
|
|
|
|
|
|
|
var eventEmitter = new events.EventEmitter();
|
|
|
|
|
|
|
|
var self = module.exports = {
|
|
|
|
emit: function(eventName, args) {
|
|
|
|
logger.log.debug("Emit "+eventName);
|
|
|
|
eventEmitter.emit(eventName, args);
|
|
|
|
},
|
|
|
|
on: function(eventName, listener) {
|
|
|
|
logger.log.debug("Register listener for "+eventName);
|
|
|
|
eventEmitter.on(eventName, listener);
|
|
|
|
},
|
2017-06-11 01:52:11 +00:00
|
|
|
remove: function(eventName, listener) {
|
|
|
|
logger.log.debug("Remove listener for "+eventName);
|
|
|
|
eventEmitter.removeListener(eventName, listener);
|
|
|
|
},
|
2017-06-11 00:29:00 +00:00
|
|
|
registerModules: function() {
|
2017-06-12 02:44:34 +00:00
|
|
|
const moduleUtil = require('./module_util.js');
|
2017-06-11 00:29:00 +00:00
|
|
|
|
2017-06-12 02:44:34 +00:00
|
|
|
moduleUtil.getModulePaths().forEach(function(modulePath) {
|
|
|
|
var mods = fs.readdirSync(modulePath);
|
|
|
|
mods.forEach(function(item) {
|
|
|
|
var modPath = modulePath+item;
|
|
|
|
if (item.substr(item.length-3) != '.js') {
|
|
|
|
modPath += path.sep+item+'.js';
|
|
|
|
}
|
|
|
|
if (fs.existsSync(modPath)) {
|
|
|
|
var module = require(modPath);
|
2017-06-11 01:52:11 +00:00
|
|
|
|
2017-06-12 02:44:34 +00:00
|
|
|
if (module.registerEvents !== undefined) {
|
|
|
|
logger.log.debug(modPath+" calling registerEvents function");
|
|
|
|
module.registerEvents();
|
|
|
|
} else {
|
|
|
|
logger.log.debug(modPath+" has no registerEvents function");
|
|
|
|
}
|
2017-06-11 00:29:00 +00:00
|
|
|
} else {
|
2017-06-12 02:44:34 +00:00
|
|
|
logger.log.debug(modPath+" - file not found");
|
2017-06-11 00:29:00 +00:00
|
|
|
}
|
2017-06-12 02:44:34 +00:00
|
|
|
});
|
2017-06-11 00:29:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|