webapi-eca/js-coffee/components-manager.js
2014-04-01 23:31:54 +02:00

345 lines
10 KiB
JavaScript

// Generated by CoffeeScript 1.6.3
/*
Components Manager
==================
> The components manager takes care of the dynamic JS modules and the rules.
> Event Poller and Action Invoker modules are loaded as strings and stored in the database,
> then compiled into node modules and rules and used in the engine and event poller.
*/
(function() {
var commandFunctions, db, dynmod, events, exports, fs, hasRequiredParams, path, vm,
_this = this;
db = require('./persistence');
dynmod = require('./dynamic-modules');
fs = require('fs');
vm = require('vm');
path = require('path');
events = require('events');
/*
Module call
-----------
Initializes the HTTP listener and its request handler.
@param {Object} args
*/
exports = module.exports = function(args) {
_this.log = args.logger;
_this.ee = new events.EventEmitter();
db(args);
dynmod(args);
return module.exports;
};
/*
Add an event handler (eh) for a certain event (evt).
Current events are:
- init: as soon as an event handler is added, the init events are emitted for all existing rules.
- newRule: If a new rule is activated, the newRule event is emitted
@public addListener ( *evt, eh* )
@param {String} evt
@param {function} eh
*/
exports.addListener = function(evt, eh) {
_this.ee.addListener(evt, eh);
if (evt === 'init') {
return db.getRules(function(err, obj) {
var id, rule, _results;
_results = [];
for (id in obj) {
rule = obj[id];
_results.push(_this.ee.emit('init', rule));
}
return _results;
});
}
};
/*
Processes a user request coming through the request-handler.
- `user` is the user object as it comes from the DB.
- `oReq` is the request object that contains:
- `command` as a string
- `payload` an optional stringified JSON object
The callback function `cb( obj )` will receive an object containing the HTTP
response code and a corresponding message.
@public processRequest ( *user, oReq, cb* )
@param {Object} user
@param {Object} oReq
@param {function} cb
*/
exports.processRequest = function(user, oReq, cb) {
var dat, err;
if (!oReq.payload) {
oReq.payload = '{}';
}
try {
dat = JSON.parse(oReq.payload);
} catch (_error) {
err = _error;
return cb({
code: 404,
message: 'You had a strange payload in your request!'
});
}
if (commandFunctions[oReq.command]) {
return commandFunctions[oReq.command](user, dat, cb);
} else {
return cb({
code: 404,
message: 'Strange request!'
});
}
};
hasRequiredParams = function(arrParams, oReq) {
var answ, param, _i, _len;
answ = {
code: 400,
message: "Your request didn't contain all necessary fields! id and params required"
};
for (_i = 0, _len = arrParams.length; _i < _len; _i++) {
param = arrParams[_i];
if (!oReq[param]) {
return answ;
}
}
answ.code = 200;
answ.message = 'All required properties found';
return answ;
};
commandFunctions = {
forge_event_poller: function(user, oReq, cb) {
var answ;
answ = hasRequiredParams(['id', 'params', 'lang', 'data'], oReq);
if (answ.code !== 200) {
return cb(answ);
} else {
return db.eventPollers.getModule(oReq.id, function(err, mod) {
var cm, id, name, src, _ref;
if (mod) {
answ.code = 409;
answ.message = 'Event Poller module name already existing: ' + oReq.id;
} else {
src = oReq.data;
cm = dynmod.compileString(src, oReq.id, {}, oReq.lang);
answ = cm.answ;
if (answ.code === 200) {
events = [];
_ref = cm.module;
for (name in _ref) {
id = _ref[name];
events.push(name);
}
_this.log.info("CM | Storing new eventpoller with events " + events);
answ.message = "Event Poller module successfully stored! Found following event(s): " + events;
oReq.events = JSON.stringify(events);
db.eventPollers.storeModule(oReq.id, user.username, oReq);
if (oReq["public"] === 'true') {
db.eventPollers.publish(oReq.id);
}
}
}
return cb(answ);
});
}
},
get_event_pollers: function(user, oReq, cb) {
return db.eventPollers.getAvailableModuleIds(user.username, function(err, arrNames) {
var answReq, fGetEvents, id, oRes, sem, _i, _len, _results;
oRes = {};
answReq = function() {
return cb({
code: 200,
message: oRes
});
};
sem = arrNames.length;
if (sem === 0) {
return answReq();
} else {
fGetEvents = function(id) {
return db.eventPollers.getModule(id, function(err, oModule) {
oRes[id] = oModule.events;
if (--sem === 0) {
return answReq();
}
});
};
_results = [];
for (_i = 0, _len = arrNames.length; _i < _len; _i++) {
id = arrNames[_i];
_results.push(fGetEvents(id));
}
return _results;
}
});
},
get_event_poller_params: function(user, oReq, cb) {
var answ;
answ = hasRequiredParams(['id'], oReq);
if (answ.code !== 200) {
return cb(answ);
} else {
return db.eventPollers.getModuleParams(oReq.id, function(err, oReq) {
answ.message = oReq;
return cb(answ);
});
}
},
get_action_invokers: function(user, oReq, cb) {
return db.actionInvokers.getAvailableModuleIds(user.username, function(err, arrNames) {
var answReq, fGetActions, id, oRes, sem, _i, _len, _results;
oRes = {};
answReq = function() {
return cb({
code: 200,
message: oRes
});
};
sem = arrNames.length;
if (sem === 0) {
return answReq();
} else {
fGetActions = function(id) {
return db.actionInvokers.getModule(id, function(err, oModule) {
oRes[id] = oModule.actions;
if (--sem === 0) {
return answReq();
}
});
};
_results = [];
for (_i = 0, _len = arrNames.length; _i < _len; _i++) {
id = arrNames[_i];
_results.push(fGetActions(id));
}
return _results;
}
});
},
get_action_invoker_params: function(user, oReq, cb) {
var answ;
answ = hasRequiredParams(['id'], oReq);
if (answ.code !== 200) {
return cb(answ);
} else {
return db.actionInvokers.getModuleParams(oReq.id, function(err, oReq) {
answ.message = oReq;
return cb(answ);
});
}
},
forge_action_invoker: function(user, oReq, cb) {
var answ;
answ = hasRequiredParams(['id', 'params', 'lang', 'data'], oReq);
if (answ.code !== 200) {
return cb(answ);
} else {
return db.actionInvokers.getModule(oReq.id, function(err, mod) {
var actions, cm, id, name, src, _ref;
if (mod) {
answ.code = 409;
answ.message = 'Action Invoker module name already existing: ' + oReq.id;
} else {
src = oReq.data;
cm = dynmod.compileString(src, oReq.id, {}, oReq.lang);
answ = cm.answ;
if (answ.code === 200) {
actions = [];
_ref = cm.module;
for (name in _ref) {
id = _ref[name];
actions.push(name);
}
_this.log.info("CM | Storing new eventpoller with actions " + actions);
answ.message = "Action Invoker module successfully stored! Found following action(s): " + actions;
oReq.actions = JSON.stringify(actions);
db.actionInvokers.storeModule(oReq.id, user.username, oReq);
if (oReq["public"] === 'true') {
db.actionInvokers.publish(oReq.id);
}
}
}
return cb(answ);
});
}
},
get_rules: function(user, oReq, cb) {
return console.log('CM | Implement get_rules');
},
forge_rule: function(user, oReq, cb) {
console.log(oReq);
return db.getRule(oReq.id, function(err, oExisting) {
var answ, arrParams, id, params, rule, strRule;
if (oExisting !== null) {
answ = {
code: 409,
message: 'Rule name already existing!'
};
} else {
if (!oReq.id || !oReq.event || !oReq.conditions || !oReq.actions) {
answ = {
code: 400,
message: 'Missing properties in rule!'
};
} else {
try {
rule = {
id: oReq.id,
event: oReq.event,
conditions: JSON.parse(oReq.conditions),
actions: JSON.parse(oReq.actions)
};
strRule = JSON.stringify(rule);
db.storeRule(rule.id, strRule);
db.linkRule(rule.id, user.username);
db.activateRule(rule.id, user.username);
if (oReq.event_params) {
db.eventPollers.storeUserParams(ep.module, user.username, oReq.event_params);
}
arrParams = JSON.parse(oReq.action_params);
for (id in arrParams) {
params = arrParams[id];
db.actionInvokers.storeUserParams(id, user.username, JSON.stringify(params));
}
_this.ee.emit('newRule', strRule);
answ = {
code: 200,
message: 'Rule stored and activated!'
};
} catch (_error) {
err = _error;
answ = {
code: 400,
message: 'bad bad request...'
};
console.log(err);
}
}
}
return cb(answ);
});
}
};
}).call(this);