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

Merge branch 'master' into tabs

This commit is contained in:
Nicholas O'Leary 2013-10-25 21:35:01 +01:00
commit adc6b44840
4 changed files with 182 additions and 172 deletions

View File

@ -30,17 +30,20 @@ function Xml2jsNode(n) {
this.useEyes = n.useEyes; this.useEyes = n.useEyes;
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
parseString(msg.payload, function (err, result) { try {
if (err) { node.error(err); } parseString(msg.payload, function (err, result) {
else { if (err) { node.error(err); }
msg.payload = result; else {
node.send(msg); msg.payload = result;
if (node.useEyes == true) { node.send(msg);
if (gotEyes == true) { eyes.inspect(msg); } if (node.useEyes == true) {
else { node.log(JSON.stringify(msg)); } if (gotEyes == true) { eyes.inspect(msg); }
} else { node.log(JSON.stringify(msg)); }
} }
}); }
});
}
catch(e) { console.log(e); }
}); });
} }
RED.nodes.registerType("xml2js",Xml2jsNode); RED.nodes.registerType("xml2js",Xml2jsNode);

View File

@ -22,103 +22,99 @@ var urllib = require("url");
var bodyParser = require("express").bodyParser(); var bodyParser = require("express").bodyParser();
function HTTPIn(n) { function HTTPIn(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.url = n.url; this.url = n.url;
this.method = n.method; this.method = n.method;
var node = this; var node = this;
this.callback = function(req,res) { this.callback = function(req,res) {
node.send({req:req,res:res}); node.send({req:req,res:res});
} }
if (this.method == "get") { if (this.method == "get") {
RED.app.get(this.url,this.callback); RED.app.get(this.url,this.callback);
} else if (this.method == "post") { } else if (this.method == "post") {
RED.app.post(this.url,bodyParser,this.callback); RED.app.post(this.url,bodyParser,this.callback);
} else if (this.method == "put") { } else if (this.method == "put") {
RED.app.put(this.url,bodyParser,this.callback); RED.app.put(this.url,bodyParser,this.callback);
} else if (this.method == "delete") { } else if (this.method == "delete") {
RED.app.delete(this.url,this.callback); RED.app.delete(this.url,this.callback);
} }
this.on("close",function() { this.on("close",function() {
var routes = RED.app.routes[this.method]; var routes = RED.app.routes[this.method];
for (var i in routes) { for (var i in routes) {
if (routes[i].path == this.url) { if (routes[i].path == this.url) {
routes.splice(i,1); routes.splice(i,1);
//break; //break;
} }
} }
}); });
} }
RED.nodes.registerType("http in",HTTPIn); RED.nodes.registerType("http in",HTTPIn);
function HTTPOut(n) { function HTTPOut(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
if (msg.res) { if (msg.res) {
if (msg.headers) { if (msg.headers) {
res.set(msg.headers); res.set(msg.headers);
} }
var statusCode = msg.statusCode || 200; var statusCode = msg.statusCode || 200;
msg.res.send(statusCode,msg.payload); msg.res.send(statusCode,msg.payload);
} else { } else {
node.warn("No response object"); node.warn("No response object");
} }
}); });
} }
RED.nodes.registerType("http response",HTTPOut); RED.nodes.registerType("http response",HTTPOut);
function HTTPRequest(n) { function HTTPRequest(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
var url = n.url; var url = n.url;
var method = n.method || "GET"; var method = n.method || "GET";
var httplib = (/^https/.test(url))?https:http; var httplib = (/^https/.test(url))?https:http;
var node = this; var node = this;
this.on("input",function(msg) { this.on("input",function(msg) {
if (msg.url) {
httplib = (/^https/.test(msg.url))?https:http; var opts = urllib.parse(msg.url||url);
} opts.method = (msg.method||method).toUpperCase();
var opts = urllib.parse(msg.url||url); if (msg.headers) {
opts.method = (msg.method||method).toUpperCase(); opts.headers = msg.headers;
if (msg.headers) { }
opts.headers = msg.headers; var req = httplib.request(opts,function(res) {
} res.setEncoding('utf8');
var req = httplib.request(opts,function(res) { msg.statusCode = res.statusCode;
res.setEncoding('utf8'); msg.headers = res.headers;
var message = { msg.payload = "";
statusCode: res.statusCode, res.on('data',function(chunk) {
headers: res.headers, msg.payload += chunk;
payload: "" });
}; res.on('end',function() {
res.on('data',function(chunk) { node.send(msg);
message.payload += chunk; });
}); });
res.on('end',function() { req.on('error',function(err) {
node.send(message); msg.payload = err.toString();
}); msg.statusCode = err.code;
}); node.send(msg);
req.on('error',function(err) { });
msg.payload = err.toString(); if (msg.payload && (method == "PUSH" || method == "PUT") ) {
msg.statusCode = err.code; if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
node.send(msg);
});
if (msg.payload && (method == "POST" || method == "PUT") ) {
if (typeof msg.payload === "string" || Buffer.isBuffer(msg.payload)) {
req.write(msg.payload); req.write(msg.payload);
} else if (typeof msg.payload == "number") { } else if (typeof msg.payload == "number") {
req.write(msg.payload+""); req.write(msg.payload+"");
} else { } else {
req.write(JSON.stringify(msg.payload)); req.write(JSON.stringify(msg.payload));
} }
} }
req.end(); req.end();
}); });
} }
RED.nodes.registerType("http request",HTTPRequest); RED.nodes.registerType("http request",HTTPRequest);

View File

@ -62,7 +62,8 @@
<script type="text/x-red" data-help-name="udp in"> <script type="text/x-red" data-help-name="udp in">
<p>A udp input node, that produces a <b>msg.payload</b> containing a <i>BUFFER</i>, string, or base64 encoded string. Supports multicast.</p> <p>A udp input node, that produces a <b>msg.payload</b> containing a <i>BUFFER</i>, string, or base64 encoded string. Supports multicast.</p>
<p>It also provides <b>msg.fromip</b> in the form ipaddress:port .</p> <p>It also provides <b>msg.ip</b> and <b>msg.port</b> to the ip address and port from which the message was received.</b>
<p>On some systems you may need to be root to use ports below 1024 and/or broadcast.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -122,6 +123,7 @@
<label for="node-input-name"><i class="icon-tag"></i> Name</label> <label for="node-input-name"><i class="icon-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
</div> </div>
<div class="form-tips">Tip: leave address and port blank if you want to set using <b>msg.ip</b> and <b>msg.port</b>.</div>
<script> <script>
$("#node-input-multicast").change(function() { $("#node-input-multicast").change(function() {
var id = $("#node-input-multicast option:selected").val(); var id = $("#node-input-multicast option:selected").val();
@ -145,8 +147,9 @@
<script type="text/x-red" data-help-name="udp out"> <script type="text/x-red" data-help-name="udp out">
<p>This node sends <b>msg.payload</b> to the designated udp host and port. Supports multicast.</p> <p>This node sends <b>msg.payload</b> to the designated udp host and port. Supports multicast.</p>
<p>You may also use <b>msg.ip</b> and <b>msg.port</b> to set the destination values.<br/><b>Note</b>: the statically configured values have precedence.</p>
<p>If you select broadcast either set the address to the local broadcast ip address, or maybe try 255.255.255.255, which is the global broadcast address.</p> <p>If you select broadcast either set the address to the local broadcast ip address, or maybe try 255.255.255.255, which is the global broadcast address.</p>
<p>On some systems you may need to be root to use broadcast.</p> <p>On some systems you may need to be root to use ports below 1024 and/or broadcast.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -155,10 +158,9 @@
color:"Silver", color:"Silver",
defaults: { defaults: {
name: {value:""}, name: {value:""},
addr: {value:"",required:true}, addr: {value:""},
//group: {value:""},
iface: {value:""}, iface: {value:""},
port: {value:"",required:true,validate:RED.validators.number()}, port: {value:""},
base64: {value:false,required:true}, base64: {value:false,required:true},
multicast: {value:"false"} multicast: {value:"false"}
}, },

View File

@ -19,97 +19,106 @@ var dgram = require('dgram');
// The Input Node // The Input Node
function UDPin(n) { function UDPin(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.group = n.group; this.group = n.group;
this.port = n.port; this.port = n.port;
this.host = n.host || null; this.host = n.host || null;
this.datatype = n.datatype; this.datatype = n.datatype;
this.iface = n.iface || null; this.iface = n.iface || null;
this.multicast = n.multicast; this.multicast = n.multicast;
var node = this; var node = this;
var server = dgram.createSocket('udp4'); var server = dgram.createSocket('udp4');
server.on("error", function (err) { server.on("error", function (err) {
console.log("udp listener error:\n" + err.stack); //console.log("udp listener error:\n" + err.stack);
server.close(); if ((err.code == "EACCES") && (node.port < 1024)) { node.error("UDP access error, you may need root access for ports below 1024"); }
}); else { node.error("UDP error : "+err.code); }
server.close();
});
server.on('message', function (message, remote) { server.on('message', function (message, remote) {
var msg; var msg;
if (node.datatype =="base64") { msg = { payload:message.toString('base64'), fromip:remote.address+':'+remote.port }; } if (node.datatype =="base64") { msg = { payload:message.toString('base64'), fromip:remote.address+':'+remote.port }; }
else if (node.datatype =="utf8") { msg = { payload:message.toString('utf8'), fromip:remote.address+':'+remote.port }; } else if (node.datatype =="utf8") { msg = { payload:message.toString('utf8'), fromip:remote.address+':'+remote.port }; }
else { msg = { payload:message, fromip:remote.address+':'+remote.port }; } else { msg = { payload:message, fromip:remote.address+':'+remote.port, ip:remote.address, port:remote.port }; }
node.send(msg); node.send(msg);
}); });
server.on('listening', function () { server.on('listening', function () {
var address = server.address(); var address = server.address();
node.log('udp listener at ' + address.address + ":" + address.port); node.log('udp listener at ' + address.address + ":" + address.port);
if (node.multicast == "true") { if (node.multicast == "true") {
server.setBroadcast(true) server.setBroadcast(true)
server.setMulticastTTL(128); server.setMulticastTTL(128);
server.addMembership(node.group,node.iface); server.addMembership(node.group,node.iface);
node.log("udp multicast group "+node.group); node.log("udp multicast group "+node.group);
} }
}); });
node.on("close", function() { node.on("close", function() {
try { try {
server.close(); server.close();
node.log('udp listener stopped'); node.log('udp listener stopped');
} }
catch (err) { console.log(err); } catch (err) { console.log(err); }
}); });
server.bind(node.port,node.host); server.bind(node.port,node.host);
} }
RED.nodes.registerType("udp in",UDPin); RED.nodes.registerType("udp in",UDPin);
// The Output Node // The Output Node
function UDPout(n) { function UDPout(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
//this.group = n.group; //this.group = n.group;
this.port = n.port; this.port = n.port;
this.base64 = n.base64; this.base64 = n.base64;
this.addr = n.addr; this.addr = n.addr;
this.iface = n.iface || null; this.iface = n.iface || null;
this.multicast = n.multicast; this.multicast = n.multicast;
var node = this; var node = this;
var sock = dgram.createSocket('udp4'); // only use ipv4 for now var sock = dgram.createSocket('udp4'); // only use ipv4 for now
sock.bind(node.port); // have to bind before you can enable broadcast... sock.bind(node.port); // have to bind before you can enable broadcast...
if (this.multicast != "false") { if (this.multicast != "false") {
sock.setBroadcast(true); // turn on broadcast sock.setBroadcast(true); // turn on broadcast
if (this.multicast == "multi") { if (this.multicast == "multi") {
sock.setMulticastTTL(128); sock.setMulticastTTL(128);
sock.addMembership(node.addr,node.iface); // Add to the multicast group sock.addMembership(node.addr,node.iface); // Add to the multicast group
node.log('udp multicast ready : '+node.addr+":"+node.port); node.log('udp multicast ready : '+node.addr+":"+node.port);
} }
else node.log('udp broadcast ready : '+node.addr+":"+node.port); else node.log('udp broadcast ready : '+node.addr+":"+node.port);
} }
else node.log('udp ready : '+node.addr+":"+node.port); else node.log('udp ready : '+node.addr+":"+node.port);
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg.payload != null) { if (msg.payload != null) {
//console.log("UDP:",msg.payload); var add = node.addr || msg.ip || "";
var message; var por = node.port || msg.port || 0;
if (node.base64) { message = new Buffer(b64string, 'base64'); } if (add == "") { node.warn("udp: ip address not set"); }
else { message = new Buffer(""+msg.payload); } else if (por == 0) { node.warn("udp: port not set"); }
console.log("UDP send :",node.addr,node.port); else if (isNaN(por) || (por < 1) || (por > 65535)) { node.warn("udp: port number not valid"); }
sock.send(message, 0, message.length, node.port, node.addr, function(err, bytes) { else {
if (err) node.error("udp : "+err); var message;
}); if (node.base64) { message = new Buffer(b64string, 'base64'); }
} else if (msg.payload instanceof Buffer) { message = msg.payload; }
}); else { message = new Buffer(""+msg.payload); }
console.log("UDP send :",add,por,msg.payload.toString());
sock.send(message, 0, message.length, por, add, function(err, bytes) {
if (err) node.error("udp : "+err);
});
}
}
});
node.on("close", function() { node.on("close", function() {
try { try {
sock.close(); sock.close();
node.log('udp output stopped'); node.log('udp output stopped');
} }
catch (err) { console.log(err); } catch (err) { console.log(err); }
}); });
} }
RED.nodes.registerType("udp out",UDPout); RED.nodes.registerType("udp out",UDPout);