2013-09-05 16:02:48 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2013 IBM Corp.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2014-02-16 01:39:30 +01:00
|
|
|
var express = require('express');
|
2013-09-05 16:02:48 +02:00
|
|
|
var util = require('util');
|
2014-03-06 23:32:23 +01:00
|
|
|
var when = require('when');
|
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
var createUI = require("./ui");
|
|
|
|
var redNodes = require("./nodes");
|
2014-05-07 21:47:25 +02:00
|
|
|
var comms = require("./comms");
|
2013-10-13 22:01:46 +02:00
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
var app = null;
|
2014-02-16 01:39:30 +01:00
|
|
|
var nodeApp = null;
|
2013-09-05 16:02:48 +02:00
|
|
|
var server = null;
|
2013-11-12 18:13:06 +01:00
|
|
|
var settings = null;
|
|
|
|
var storage = null;
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-11-12 18:13:06 +01:00
|
|
|
function createServer(_server,_settings) {
|
2013-09-05 16:02:48 +02:00
|
|
|
server = _server;
|
2013-11-12 18:13:06 +01:00
|
|
|
settings = _settings;
|
2014-05-07 21:47:25 +02:00
|
|
|
comms.init(_server,_settings);
|
2013-11-12 18:13:06 +01:00
|
|
|
storage = require("./storage");
|
2013-09-05 16:02:48 +02:00
|
|
|
app = createUI(settings);
|
2014-02-16 01:39:30 +01:00
|
|
|
nodeApp = express();
|
2013-09-05 16:02:48 +02:00
|
|
|
|
|
|
|
app.get("/nodes",function(req,res) {
|
2014-04-21 23:17:52 +02:00
|
|
|
res.send(redNodes.getNodeConfigs());
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/flows",function(req,res) {
|
2014-05-03 23:26:35 +02:00
|
|
|
res.json(redNodes.getFlows());
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
|
|
|
|
2014-04-15 23:30:07 +02:00
|
|
|
app.post("/flows",
|
|
|
|
express.json(),
|
|
|
|
function(req,res) {
|
|
|
|
var flows = req.body;
|
2014-05-03 23:26:35 +02:00
|
|
|
redNodes.setFlows(flows).then(function() {
|
2014-07-31 08:04:10 +02:00
|
|
|
res.send(204);
|
2014-04-15 23:30:07 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
util.log("[red] Error saving flows : "+err);
|
|
|
|
res.send(500,err.message);
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
2014-04-15 23:30:07 +02:00
|
|
|
},
|
|
|
|
function(error,req,res,next) {
|
|
|
|
res.send(400,"Invalid Flow");
|
|
|
|
}
|
|
|
|
);
|
2013-09-20 18:15:45 +02:00
|
|
|
}
|
2013-10-13 22:01:46 +02:00
|
|
|
|
2013-09-20 18:15:45 +02:00
|
|
|
function start() {
|
2014-03-06 23:32:23 +01:00
|
|
|
var defer = when.defer();
|
|
|
|
|
2013-11-12 18:13:06 +01:00
|
|
|
storage.init(settings).then(function() {
|
|
|
|
console.log("\nWelcome to Node-RED\n===================\n");
|
2014-07-17 22:32:30 +02:00
|
|
|
if (settings.version) {
|
|
|
|
util.log("[red] Version: "+settings.version);
|
|
|
|
}
|
2013-11-12 18:13:06 +01:00
|
|
|
util.log("[red] Loading palette nodes");
|
2014-05-03 23:26:35 +02:00
|
|
|
redNodes.init(settings,storage);
|
2014-08-01 23:05:49 +02:00
|
|
|
redNodes.load().then(function() {
|
|
|
|
var nodes = redNodes.getNodeList();
|
|
|
|
var nodeErrors = nodes.filter(function(n) { return n.err!=null;});
|
2014-05-02 12:16:07 +02:00
|
|
|
if (nodeErrors.length > 0) {
|
|
|
|
util.log("------------------------------------------");
|
|
|
|
if (settings.verbose) {
|
|
|
|
for (var i=0;i<nodeErrors.length;i+=1) {
|
2014-07-28 17:37:39 +02:00
|
|
|
util.log("["+nodeErrors[i].name+"] "+nodeErrors[i].err);
|
2014-05-02 12:16:07 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
util.log("[red] Failed to register "+nodeErrors.length+" node type"+(nodeErrors.length==1?"":"s"));
|
|
|
|
util.log("[red] Run with -v for details");
|
2014-03-06 23:32:23 +01:00
|
|
|
}
|
2014-05-02 12:16:07 +02:00
|
|
|
util.log("------------------------------------------");
|
2014-03-06 23:32:23 +01:00
|
|
|
}
|
2014-05-02 12:16:07 +02:00
|
|
|
defer.resolve();
|
2014-05-03 23:26:35 +02:00
|
|
|
|
|
|
|
redNodes.loadFlows();
|
2013-11-12 18:13:06 +01:00
|
|
|
});
|
2014-05-07 21:47:25 +02:00
|
|
|
comms.start();
|
2014-07-22 14:43:03 +02:00
|
|
|
}).otherwise(function(err) {
|
|
|
|
defer.reject(err);
|
2013-09-05 16:02:48 +02:00
|
|
|
});
|
2014-03-06 23:32:23 +01:00
|
|
|
|
|
|
|
return defer.promise;
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-09-20 18:15:45 +02:00
|
|
|
|
2013-10-13 20:14:39 +02:00
|
|
|
function stop() {
|
|
|
|
redNodes.stopFlows();
|
2014-08-01 22:55:05 +02:00
|
|
|
comms.stop();
|
2013-10-13 20:14:39 +02:00
|
|
|
}
|
|
|
|
|
2013-09-05 16:02:48 +02:00
|
|
|
module.exports = {
|
2013-09-20 18:15:45 +02:00
|
|
|
init: createServer,
|
2013-10-13 20:14:39 +02:00
|
|
|
start: start,
|
|
|
|
stop: stop
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.__defineGetter__("app", function() { return app });
|
2014-02-16 01:39:30 +01:00
|
|
|
module.exports.__defineGetter__("nodeApp", function() { return nodeApp });
|
2013-09-05 16:02:48 +02:00
|
|
|
module.exports.__defineGetter__("server", function() { return server });
|