2014-11-04 12:34:49 +01:00
|
|
|
/**
|
2015-02-03 23:02:26 +01:00
|
|
|
* Copyright 2014, 2015 IBM Corp.
|
2014-11-04 12:34:49 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
var express = require('express');
|
|
|
|
var fs = require("fs");
|
|
|
|
var path = require("path");
|
|
|
|
var when = require('when');
|
|
|
|
|
|
|
|
var events = require("../events");
|
|
|
|
var redNodes = require("../nodes");
|
|
|
|
var comms = require("../comms");
|
|
|
|
var server = require("../server");
|
2015-02-03 23:02:26 +01:00
|
|
|
var log = require("../log");
|
2014-11-04 12:34:49 +01:00
|
|
|
|
|
|
|
var settings = require("../settings");
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
getAll: function(req,res) {
|
|
|
|
if (req.get("accept") == "application/json") {
|
2015-03-30 22:49:20 +02:00
|
|
|
res.json(redNodes.getNodeList());
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
|
|
|
res.send(redNodes.getNodeConfigs());
|
|
|
|
}
|
|
|
|
},
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
post: function(req,res) {
|
|
|
|
if (!settings.available()) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var node = req.body;
|
|
|
|
var promise;
|
2015-03-26 12:39:35 +01:00
|
|
|
if (node.module) {
|
2015-03-30 22:49:20 +02:00
|
|
|
var module = redNodes.getModuleInfo(node.module);
|
2014-11-04 12:34:49 +01:00
|
|
|
if (module) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"module_already_loaded", message:"Module already loaded"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
promise = server.installModule(node.module);
|
|
|
|
} else {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"invalid_request", message:"Invalid request"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
promise.then(function(info) {
|
2014-11-21 16:15:24 +01:00
|
|
|
res.json(redNodes.getModuleInfo(node.module));
|
2014-11-04 12:34:49 +01:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
if (err.code === 404) {
|
|
|
|
res.send(404);
|
|
|
|
} else {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
2014-11-04 12:34:49 +01:00
|
|
|
delete: function(req,res) {
|
|
|
|
if (!settings.available()) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-11-21 12:25:51 +01:00
|
|
|
var mod = req.params.mod;
|
2014-11-04 12:34:49 +01:00
|
|
|
try {
|
|
|
|
var promise = null;
|
2015-03-30 22:49:20 +02:00
|
|
|
var module = redNodes.getModuleInfo(mod);
|
2014-11-21 12:25:51 +01:00
|
|
|
if (!module) {
|
|
|
|
res.send(404);
|
|
|
|
return;
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
2014-11-21 12:25:51 +01:00
|
|
|
promise = server.uninstallModule(mod);
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
2014-11-20 16:17:13 +01:00
|
|
|
|
2014-11-21 12:25:51 +01:00
|
|
|
promise.then(function() {
|
|
|
|
res.send(204);
|
2014-11-04 12:34:49 +01:00
|
|
|
}).otherwise(function(err) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-04 12:34:49 +01:00
|
|
|
});
|
|
|
|
} catch(err) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
|
|
|
getSet: function(req,res) {
|
|
|
|
var id = req.params.mod + "/" + req.params.set;
|
2014-11-04 12:34:49 +01:00
|
|
|
var result = null;
|
2014-11-20 16:17:13 +01:00
|
|
|
if (req.get("accept") === "application/json") {
|
2014-11-04 12:34:49 +01:00
|
|
|
result = redNodes.getNodeInfo(id);
|
2014-12-08 17:26:54 +01:00
|
|
|
if (result) {
|
2015-03-30 22:49:20 +02:00
|
|
|
delete result.loaded;
|
2014-12-08 17:26:54 +01:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
} else {
|
|
|
|
result = redNodes.getNodeConfig(id);
|
|
|
|
}
|
|
|
|
if (result) {
|
|
|
|
res.send(result);
|
|
|
|
} else {
|
|
|
|
res.send(404);
|
|
|
|
}
|
|
|
|
},
|
2014-11-20 16:17:13 +01:00
|
|
|
|
|
|
|
getModule: function(req,res) {
|
|
|
|
var module = req.params.mod;
|
|
|
|
var result = redNodes.getModuleInfo(module);
|
|
|
|
if (result) {
|
2015-03-30 22:49:20 +02:00
|
|
|
res.json(result);
|
2014-11-20 16:17:13 +01:00
|
|
|
} else {
|
2015-03-26 12:39:35 +01:00
|
|
|
res.send(404);
|
2014-11-20 16:17:13 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2014-11-21 11:36:32 +01:00
|
|
|
putSet: function(req,res) {
|
2014-11-04 12:34:49 +01:00
|
|
|
if (!settings.available()) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var body = req.body;
|
|
|
|
if (!body.hasOwnProperty("enabled")) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"invalid_request", message:"Invalid request"});
|
2014-11-04 12:34:49 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
2014-12-08 17:26:54 +01:00
|
|
|
var id = req.params.mod + "/" + req.params.set;
|
2014-11-04 12:34:49 +01:00
|
|
|
var node = redNodes.getNodeInfo(id);
|
2014-11-21 11:36:32 +01:00
|
|
|
var info;
|
2014-11-04 12:34:49 +01:00
|
|
|
if (!node) {
|
|
|
|
res.send(404);
|
|
|
|
} else {
|
2015-03-30 22:49:20 +02:00
|
|
|
delete node.loaded;
|
2014-12-17 14:35:57 +01:00
|
|
|
res.json(putNode(node, body.enabled));
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
|
|
|
} catch(err) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-20 16:17:13 +01:00
|
|
|
}
|
2014-11-21 11:36:32 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
putModule: function(req,res) {
|
|
|
|
if (!settings.available()) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"settings_unavailable", message:"Settings unavailable"});
|
2014-11-21 11:36:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
var body = req.body;
|
|
|
|
if (!body.hasOwnProperty("enabled")) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:"invalid_request", message:"Invalid request"});
|
2014-11-21 11:36:32 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var mod = req.params.mod;
|
|
|
|
var module = redNodes.getModuleInfo(mod);
|
|
|
|
if (!module) {
|
2015-03-26 12:39:35 +01:00
|
|
|
return res.send(404);
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
2014-12-08 17:26:54 +01:00
|
|
|
|
2014-11-21 11:36:32 +01:00
|
|
|
var nodes = module.nodes;
|
|
|
|
for (var i = 0; i < nodes.length; ++i) {
|
|
|
|
var node = nodes[i];
|
|
|
|
var info;
|
|
|
|
if (node.err || node.enabled !== body.enabled) {
|
|
|
|
if (body.enabled) {
|
|
|
|
info = redNodes.enableNode(node.id);
|
|
|
|
} else {
|
|
|
|
info = redNodes.disableNode(node.id);
|
|
|
|
}
|
|
|
|
if (info.enabled === body.enabled && !info.err) {
|
|
|
|
comms.publish("node/"+(body.enabled?"enabled":"disabled"),info,false);
|
2015-02-03 23:02:26 +01:00
|
|
|
log.info(" "+(body.enabled?"Enabled":"Disabled")+" node types:");
|
2014-11-21 11:36:32 +01:00
|
|
|
for (var j = 0; j < info.types.length; j++) {
|
2015-02-03 23:02:26 +01:00
|
|
|
log.info(" - " + info.types[j]);
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
|
|
|
} else if (body.enabled && info.err) {
|
2015-02-03 23:02:26 +01:00
|
|
|
log.warn("Failed to enable node:");
|
|
|
|
log.warn(" - "+info.name+" : "+info.err);
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-21 17:34:57 +01:00
|
|
|
res.json(redNodes.getModuleInfo(mod));
|
2014-11-21 11:36:32 +01:00
|
|
|
} catch(err) {
|
2015-03-31 23:29:15 +02:00
|
|
|
res.json(400,{error:err.code||"unexpected_error", message:err.toString()});
|
2014-11-21 11:36:32 +01:00
|
|
|
}
|
2014-11-04 12:34:49 +01:00
|
|
|
}
|
2014-11-20 16:17:13 +01:00
|
|
|
};
|
2014-12-08 17:26:54 +01:00
|
|
|
|
2014-12-17 14:35:57 +01:00
|
|
|
function putNode(node, enabled) {
|
2014-12-08 17:26:54 +01:00
|
|
|
var info;
|
|
|
|
|
2014-12-17 14:35:57 +01:00
|
|
|
if (!node.err && node.enabled === enabled) {
|
|
|
|
info = node;
|
2014-12-08 17:26:54 +01:00
|
|
|
} else {
|
2014-12-17 14:35:57 +01:00
|
|
|
if (enabled) {
|
|
|
|
info = redNodes.enableNode(node.id);
|
|
|
|
} else {
|
|
|
|
info = redNodes.disableNode(node.id);
|
|
|
|
}
|
2014-12-08 17:26:54 +01:00
|
|
|
|
2014-12-17 14:35:57 +01:00
|
|
|
if (info.enabled === enabled && !info.err) {
|
|
|
|
comms.publish("node/"+(enabled?"enabled":"disabled"),info,false);
|
2015-02-03 23:02:26 +01:00
|
|
|
log.info(" "+(enabled?"Enabled":"Disabled")+" node types:");
|
2014-12-17 14:35:57 +01:00
|
|
|
for (var i=0;i<info.types.length;i++) {
|
2015-02-03 23:02:26 +01:00
|
|
|
log.info(" - "+info.types[i]);
|
2014-12-17 14:35:57 +01:00
|
|
|
}
|
|
|
|
} else if (enabled && info.err) {
|
2015-02-03 23:02:26 +01:00
|
|
|
log.warn("Failed to enable node:");
|
|
|
|
log.warn(" - "+info.name+" : "+info.err);
|
2014-12-08 17:26:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return info;
|
|
|
|
}
|