mirror of
https://github.com/Hopiu/webapi-eca.git
synced 2026-03-16 22:10:31 +00:00
295 lines
9 KiB
JavaScript
295 lines
9 KiB
JavaScript
// Generated by CoffeeScript 1.7.1
|
|
|
|
/*
|
|
|
|
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, path, vm;
|
|
|
|
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(_this) {
|
|
return function(args) {
|
|
_this.log = args.logger;
|
|
_this.ee = new events.EventEmitter();
|
|
db(args);
|
|
dynmod(args);
|
|
return module.exports;
|
|
};
|
|
})(this);
|
|
|
|
|
|
/*
|
|
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(_this) {
|
|
return 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;
|
|
});
|
|
}
|
|
};
|
|
})(this);
|
|
|
|
exports.processRequest = (function(_this) {
|
|
return function(user, obj, cb) {
|
|
var answ;
|
|
if (commandFunctions[obj.command]) {
|
|
return answ = commandFunctions[obj.command](user, obj, cb);
|
|
} else {
|
|
return cb({
|
|
code: 404,
|
|
message: 'Strange request!'
|
|
});
|
|
}
|
|
};
|
|
})(this);
|
|
|
|
commandFunctions = {
|
|
forge_event_poller: (function(_this) {
|
|
return function(user, obj, cb) {
|
|
var answ;
|
|
answ = {
|
|
code: 200
|
|
};
|
|
return db.eventPollers.getModule(obj.id, function(err, mod) {
|
|
var cm, id, name, src, _ref;
|
|
if (mod) {
|
|
answ.code = 409;
|
|
answ.message = 'Event Poller module name already existing: ' + obj.id;
|
|
} else {
|
|
src = obj.data;
|
|
cm = dynmod.compileString(src, obj.id, {}, obj.lang);
|
|
answ = cm.answ;
|
|
if (answ.code === 200) {
|
|
if (!obj.id || !obj.params) {
|
|
answ.code = 400;
|
|
answ.message = "Your request didn't contain all necessary fields! id and params required";
|
|
} else {
|
|
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;
|
|
obj.events = JSON.stringify(events);
|
|
db.eventPollers.storeModule(obj.id, user.username, obj);
|
|
if (obj["public"] === 'true') {
|
|
db.eventPollers.publish(obj.id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return cb(answ);
|
|
});
|
|
};
|
|
})(this),
|
|
get_event_pollers: function(user, obj, cb) {
|
|
return db.eventPollers.getAvailableModuleIds(user.username, function(err, obj) {
|
|
var fGetEvents, id, oRes, sem, _i, _len, _results;
|
|
oRes = {};
|
|
sem = obj.length;
|
|
fGetEvents = function(id) {
|
|
return db.eventPollers.getModule(id, function(err, obj) {
|
|
oRes[id] = obj.events;
|
|
if (--sem === 0) {
|
|
return cb({
|
|
code: 200,
|
|
message: oRes
|
|
});
|
|
}
|
|
});
|
|
};
|
|
_results = [];
|
|
for (_i = 0, _len = obj.length; _i < _len; _i++) {
|
|
id = obj[_i];
|
|
_results.push(fGetEvents(id));
|
|
}
|
|
return _results;
|
|
});
|
|
},
|
|
get_event_poller_params: function(user, obj, cb) {
|
|
return db.eventPollers.getModuleParams(obj.id, function(err, obj) {
|
|
return cb({
|
|
code: 200,
|
|
message: obj
|
|
});
|
|
});
|
|
},
|
|
get_action_invokers: function(user, obj, cb) {
|
|
return db.actionInvokers.getAvailableModuleIds(user.username, function(err, obj) {
|
|
var fGetActions, id, oRes, sem, _i, _len, _results;
|
|
oRes = {};
|
|
sem = obj.length;
|
|
fGetActions = function(id) {
|
|
return db.actionInvokers.getModule(id, function(err, obj) {
|
|
oRes[id] = obj.actions;
|
|
if (--sem === 0) {
|
|
return cb({
|
|
code: 200,
|
|
message: oRes
|
|
});
|
|
}
|
|
});
|
|
};
|
|
_results = [];
|
|
for (_i = 0, _len = obj.length; _i < _len; _i++) {
|
|
id = obj[_i];
|
|
_results.push(fGetActions(id));
|
|
}
|
|
return _results;
|
|
});
|
|
},
|
|
get_action_invoker_params: function(user, obj, cb) {
|
|
return db.actionInvokers.getModuleParams(obj.id, function(err, obj) {
|
|
return cb({
|
|
code: 200,
|
|
message: obj
|
|
});
|
|
});
|
|
},
|
|
forge_action_invoker: (function(_this) {
|
|
return function(user, obj, cb) {
|
|
var answ;
|
|
answ = {
|
|
code: 200
|
|
};
|
|
return db.actionInvokers.getModule(obj.id, function(err, mod) {
|
|
var actions, cm, id, name, src, _ref;
|
|
if (mod) {
|
|
answ.code = 409;
|
|
answ.message = 'Action Invoker module name already existing: ' + obj.id;
|
|
} else {
|
|
src = obj.data;
|
|
cm = dynmod.compileString(src, obj.id, {}, obj.lang);
|
|
answ = cm.answ;
|
|
if (answ.code === 200) {
|
|
if (!obj.id || !obj.params) {
|
|
answ.code = 400;
|
|
answ.message = "Your request didn't contain all necessary fields! id and params required";
|
|
} else {
|
|
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;
|
|
obj.actions = JSON.stringify(actions);
|
|
db.actionInvokers.storeModule(obj.id, user.username, obj);
|
|
if (obj["public"] === 'true') {
|
|
db.actionInvokers.publish(obj.id);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return cb(answ);
|
|
});
|
|
};
|
|
})(this),
|
|
get_rules: function(user, obj, cb) {
|
|
return console.log('CM | Implement get_rules');
|
|
},
|
|
forge_rule: (function(_this) {
|
|
return function(user, obj, cb) {
|
|
return db.getRule(obj.id, function(err, oExisting) {
|
|
var answ, arrParams, ep, id, params, rule, strRule;
|
|
try {
|
|
if (oExisting !== null) {
|
|
answ = {
|
|
code: 409,
|
|
message: 'Rule name already existing!'
|
|
};
|
|
} else {
|
|
if (!obj.id || !obj.event || !obj.conditions || !obj.actions) {
|
|
answ = {
|
|
code: 400,
|
|
message: 'Missing properties in rule!'
|
|
};
|
|
} else {
|
|
ep = JSON.parse(obj.event);
|
|
rule = {
|
|
id: obj.id,
|
|
event: ep,
|
|
conditions: JSON.parse(obj.conditions),
|
|
actions: JSON.parse(obj.actions)
|
|
};
|
|
strRule = JSON.stringify(rule);
|
|
db.storeRule(rule.id, strRule);
|
|
db.linkRule(rule.id, user.username);
|
|
db.activateRule(rule.id, user.username);
|
|
if (obj.event_params) {
|
|
db.eventPollers.storeUserParams(ep.module, user.username, obj.event_params);
|
|
}
|
|
arrParams = JSON.parse(obj.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);
|
|
});
|
|
};
|
|
})(this)
|
|
};
|
|
|
|
}).call(this);
|