1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Updated to match cli changes

This commit is contained in:
Anna Thomas 2014-12-08 16:26:54 +00:00
parent 8c5fab61e6
commit 475d9e110e
3 changed files with 73 additions and 21 deletions

View File

@ -97,6 +97,9 @@ module.exports = {
var result = null; var result = null;
if (req.get("accept") === "application/json") { if (req.get("accept") === "application/json") {
result = redNodes.getNodeInfo(id); result = redNodes.getNodeInfo(id);
if (result) {
result.version = redNodes.getModuleVersion(req.params.mod);
}
} else { } else {
result = redNodes.getNodeConfig(id); result = redNodes.getNodeConfig(id);
} }
@ -112,9 +115,15 @@ module.exports = {
var result = redNodes.getModuleInfo(module); var result = redNodes.getModuleInfo(module);
if (result) { if (result) {
res.send(result); res.send(result);
} else {
// check if module is actually a node-set
var matching = getMatchingNodes(module);
if (matching.length > 0) {
res.json(matching);
} else { } else {
res.send(404); res.send(404);
} }
}
}, },
putSet: function(req,res) { putSet: function(req,res) {
@ -128,7 +137,7 @@ module.exports = {
return; return;
} }
try { try {
var id = req.params.mod+"/"+req.params.set; var id = req.params.mod + "/" + req.params.set;
var node = redNodes.getNodeInfo(id); var node = redNodes.getNodeInfo(id);
var info; var info;
if (!node) { if (!node) {
@ -136,22 +145,7 @@ module.exports = {
} else if (!node.err && node.enabled === body.enabled) { } else if (!node.err && node.enabled === body.enabled) {
res.json(node); res.json(node);
} else { } else {
if (body.enabled) { res.json(putNode(id, body.enabled));
info = redNodes.enableNode(id);
} else {
info = redNodes.disableNode(id);
}
if (info.enabled == body.enabled && !info.err) {
comms.publish("node/"+(body.enabled?"enabled":"disabled"),info,false);
util.log("[red] "+(body.enabled?"Enabled":"Disabled")+" node types:");
for (var i=0;i<info.types.length;i++) {
util.log("[red] - "+info.types[i]);
}
} else if (body.enabled && info.err) {
util.log("[red] Failed to enable node:");
util.log("[red] - "+info.name+" : "+info.err);
}
res.json(info);
} }
} catch(err) { } catch(err) {
res.send(400,err.toString()); res.send(400,err.toString());
@ -172,9 +166,26 @@ module.exports = {
var mod = req.params.mod; var mod = req.params.mod;
var module = redNodes.getModuleInfo(mod); var module = redNodes.getModuleInfo(mod);
if (!module) { if (!module) {
var matching = getMatchingNodes(mod);
if (matching.length === 1) {
// One match, assume correct
res.json(putNode(matching[0].id, body.enabled));
return;
} else if (matching.length > 1) {
// Multiple matches, need clarification
result = {
multipleMatches: true,
matches: matching
};
res.json(result);
return;
} else {
// Doesn't exist
res.send(404); res.send(404);
return; return;
} }
}
var nodes = module.nodes; var nodes = module.nodes;
for (var i = 0; i < nodes.length; ++i) { for (var i = 0; i < nodes.length; ++i) {
var node = nodes[i]; var node = nodes[i];
@ -203,3 +214,40 @@ module.exports = {
} }
} }
}; };
function getMatchingNodes(node) {
var nodes = redNodes.getNodeList();
var matching = [];
nodes.forEach(function(n) {
if (n.name === node) {
n.version = redNodes.getModuleVersion(n.module);
matching.push(n);
}
});
return matching;
}
function putNode(id, enabled) {
var info;
if (enabled) {
info = redNodes.enableNode(id);
} else {
info = redNodes.disableNode(id);
}
if (info.enabled === enabled && !info.err) {
comms.publish("node/"+(enabled?"enabled":"disabled"),info,false);
util.log("[red] "+(enabled?"Enabled":"Disabled")+" node types:");
for (var i=0;i<info.types.length;i++) {
util.log("[red] - "+info.types[i]);
}
} else if (enabled && info.err) {
util.log("[red] Failed to enable node:");
util.log("[red] - "+info.name+" : "+info.err);
}
return info;
}

View File

@ -124,6 +124,7 @@ module.exports = {
getModuleInfo: registry.getModuleInfo, getModuleInfo: registry.getModuleInfo,
getModuleList: registry.getModuleList, getModuleList: registry.getModuleList,
getModuleVersion: registry.getModuleVersion,
getNodeConfigs: registry.getNodeConfigs, getNodeConfigs: registry.getNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,

View File

@ -275,6 +275,9 @@ var registry = (function() {
return null; return null;
} }
}, },
getModuleVersion: function(module) {
return moduleConfigs[module].version;
},
registerNodeConstructor: function(type,constructor) { registerNodeConstructor: function(type,constructor) {
if (nodeConstructors[type]) { if (nodeConstructors[type]) {
throw new Error(type+" already registered"); throw new Error(type+" already registered");
@ -287,7 +290,6 @@ var registry = (function() {
events.emit("type-registered",type); events.emit("type-registered",type);
}, },
/** /**
* Gets all of the node template configs * Gets all of the node template configs
* @return all of the node templates in a single string * @return all of the node templates in a single string
@ -807,6 +809,7 @@ module.exports = {
getModuleInfo: registry.getModuleInfo, getModuleInfo: registry.getModuleInfo,
getModuleList: registry.getModuleList, getModuleList: registry.getModuleList,
getModuleVersion: registry.getModuleVersion,
getNodeConfigs: registry.getAllNodeConfigs, getNodeConfigs: registry.getAllNodeConfigs,
getNodeConfig: registry.getNodeConfig, getNodeConfig: registry.getNodeConfig,