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

updates to serial, watch, websocket, udp, twitter, email to handle no payload.

This commit is contained in:
dceejay 2015-03-31 09:21:11 +01:00
parent 78d1da5fbc
commit 255d708fb6
7 changed files with 214 additions and 100 deletions

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013,2015 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -181,7 +181,7 @@ module.exports = function(RED) {
if (this.serverConfig.wholemsg) { if (this.serverConfig.wholemsg) {
delete msg._session; delete msg._session;
payload = JSON.stringify(msg); payload = JSON.stringify(msg);
} else { } else if (msg.hasOwnProperty("payload")) {
if (!Buffer.isBuffer(msg.payload)) { // if it's not a buffer make sure it's a string. if (!Buffer.isBuffer(msg.payload)) { // if it's not a buffer make sure it's a string.
payload = RED.util.ensureString(msg.payload); payload = RED.util.ensureString(msg.payload);
} }
@ -189,6 +189,7 @@ module.exports = function(RED) {
payload = msg.payload; payload = msg.payload;
} }
} }
if (payload) {
if (msg._session && msg._session.type == "websocket") { if (msg._session && msg._session.type == "websocket") {
node.serverConfig.reply(msg._session.id,payload); node.serverConfig.reply(msg._session.id,payload);
} else { } else {
@ -198,6 +199,7 @@ module.exports = function(RED) {
} }
}); });
} }
}
}); });
} }
RED.nodes.registerType("websocket out",WebSocketOutNode); RED.nodes.registerType("websocket out",WebSocketOutNode);

View File

@ -23,11 +23,11 @@ module.exports = function(RED) {
function WatchNode(n) { function WatchNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.files = n.files.split(","); this.files = (n.files || "").split(",");
for (var f=0; f < this.files.length; f++) { for (var f=0; f < this.files.length; f++) {
this.files[f] = this.files[f].trim(); this.files[f] = this.files[f].trim();
} }
this.p = (this.files.length == 1) ? this.files[0] : JSON.stringify(this.files); this.p = (this.files.length === 1) ? this.files[0] : JSON.stringify(this.files);
var node = this; var node = this;
var notifications = new Notify(node.files); var notifications = new Notify(node.files);
@ -39,11 +39,12 @@ module.exports = function(RED) {
} catch(e) { } } catch(e) { }
var type = "other"; var type = "other";
if (stat.isFile()) { type = "file"; } if (stat.isFile()) { type = "file"; }
if (stat.isDirectory()) { type = "directory"; } else if (stat.isDirectory()) { type = "directory"; }
if (stat.isBlockDevice()) { type = "blockdevice"; } else if (stat.isBlockDevice()) { type = "blockdevice"; }
if (stat.isCharacterDevice()) { type = "characterdevice"; } else if (stat.isCharacterDevice()) { type = "characterdevice"; }
if (stat.isSocket()) { type = "socket"; } else if (stat.isSocket()) { type = "socket"; }
if (stat.isFIFO()) { type = "fifo"; } else if (stat.isFIFO()) { type = "fifo"; }
else { type = "n/a"; }
var msg = { payload:path, topic:node.p, file:file, type:type, size:stat.size }; var msg = { payload:path, topic:node.p, file:file, type:type, size:stat.size };
node.send(msg); node.send(msg);
}); });

View File

@ -55,6 +55,7 @@ module.exports = function(RED) {
node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0"); node.addCh = this.serialConfig.newline.replace("\\n","\n").replace("\\r","\r").replace("\\t","\t").replace("\\e","\e").replace("\\f","\f").replace("\\0","\0");
} }
node.on("input",function(msg) { node.on("input",function(msg) {
if (msg.hasOwnProperty("payload")) {
var payload = msg.payload; var payload = msg.payload;
if (!Buffer.isBuffer(payload)) { if (!Buffer.isBuffer(payload)) {
if (typeof payload === "object") { if (typeof payload === "object") {
@ -72,6 +73,7 @@ module.exports = function(RED) {
node.error(errmsg,msg); node.error(errmsg,msg);
} }
}); });
}
}); });
node.port.on('ready', function() { node.port.on('ready', function() {
node.status({fill:"green",shape:"dot",text:"connected"}); node.status({fill:"green",shape:"dot",text:"connected"});

View File

@ -134,7 +134,7 @@ module.exports = function(RED) {
} }
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg.payload != null) { if (msg.hasOwnProperty("payload")) {
var add = node.addr || msg.ip || ""; var add = node.addr || msg.ip || "";
var por = node.port || msg.port || 0; var por = node.port || msg.port || 0;
if (add == "") { if (add == "") {

View File

@ -280,6 +280,7 @@ module.exports = function(RED) {
access_token_secret: credentials.access_token_secret access_token_secret: credentials.access_token_secret
}); });
node.on("input", function(msg) { node.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
node.status({fill:"blue",shape:"dot",text:"tweeting"}); node.status({fill:"blue",shape:"dot",text:"tweeting"});
if (msg.payload.length > 140) { if (msg.payload.length > 140) {
@ -322,6 +323,8 @@ module.exports = function(RED) {
node.status({}); node.status({});
}); });
} }
}
else { node.warn("No payload to tweet"); }
}); });
} }
} }

View File

@ -69,6 +69,7 @@ module.exports = function(RED) {
}); });
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
if (smtpTransport) { if (smtpTransport) {
node.status({fill:"blue",shape:"dot",text:"sending"}); node.status({fill:"blue",shape:"dot",text:"sending"});
if (msg.to && node.name && (msg.to !== node.name)) { if (msg.to && node.name && (msg.to !== node.name)) {
@ -102,6 +103,8 @@ module.exports = function(RED) {
}); });
} }
else { node.warn("No Email credentials found. See info panel."); } else { node.warn("No Email credentials found. See info panel."); }
}
else { node.warn("No payload to send");
}); });
} }
RED.nodes.registerType("e-mail",EmailNode,{ RED.nodes.registerType("e-mail",EmailNode,{

View File

@ -55,7 +55,7 @@ function getSocket(listenerid) {
return node.server; return node.server;
} }
describe('websocket node', function() { describe('websocket Node', function() {
before(function(done) { before(function(done) {
helper.startServer(done); helper.startServer(done);
@ -149,6 +149,22 @@ describe('websocket node', function() {
}); });
}); });
it('should receive wholemsg when data not JSON', function(done) {
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws", wholemsg: "true" },
{ id: "n2", type: "websocket in", server: "n1", wires: [["n3"]] },
{ id: "n3", type: "helper" }];
helper.load(websocketNode, flow, function() {
createClient("n1").then(function(sock) {
sock.send('hello');
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("payload", "hello");
done();
});
});
});
});
it('should send', function(done) { it('should send', function(done) {
var flow = [ var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" }, { id: "n1", type: "websocket-listener", path: "/ws" },
@ -185,6 +201,25 @@ describe('websocket node', function() {
}); });
}); });
it('should do nothing if no payload', function(done) {
var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" },
{ id: "n2", type: "helper", wires: [["n3"]] },
{ id: "n3", type: "websocket out", server: "n1" }];
helper.load(websocketNode, flow, function() {
createClient("n1").then(function(sock) {
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "file";
});
logEvents.should.have.length(0);
done();
},100);
helper.getNode("n2").send({topic: "hello"});
});
});
});
it('should echo', function(done) { it('should echo', function(done) {
var flow = [ var flow = [
{ id: "n1", type: "websocket-listener", path: "/ws" }, { id: "n1", type: "websocket-listener", path: "/ws" },
@ -333,7 +368,6 @@ describe('websocket node', function() {
getSocket('server').on('connection', function(sock) { getSocket('server').on('connection', function(sock) {
sock.send('{"text":"hello"}'); sock.send('{"text":"hello"}');
}); });
helper.getNode("n3").on("input", function(msg) { helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("text", "hello"); msg.should.have.property("text", "hello");
done(); done();
@ -341,6 +375,23 @@ describe('websocket node', function() {
}); });
}); });
it('should receive wholemsg when data not JSON', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws"), wholemsg: "true" },
{ id: "n2", type: "websocket in", client: "n1", wires: [["n3"]] },
{ id: "n3", type: "helper" }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
sock.send('hello');
});
helper.getNode("n3").on("input", function(msg) {
msg.should.have.property("payload", "hello");
done();
});
});
});
it('should send', function(done) { it('should send', function(done) {
var flow = [ var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" }, { id: "server", type: "websocket-listener", path: "/ws" },
@ -362,6 +413,27 @@ describe('websocket node', function() {
}); });
}); });
it('should send buffer', function(done) {
var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" },
{ id: "n1", type: "websocket-client", path: getWsUrl("/ws") },
{ id: "n2", type: "websocket out", client: "n1" },
{ id: "n3", type: "helper", wires: [["n2"]] }];
helper.load(websocketNode, flow, function() {
getSocket('server').on('connection', function(sock) {
sock.on('message', function(msg) {
msg.should.have.length(5).and.be.a.buffer;
done();
});
});
getSocket("n1").on("open", function() {
helper.getNode("n3").send({
payload: new Buffer("hello")
});
});
});
});
it('should send wholemsg', function(done) { it('should send wholemsg', function(done) {
var flow = [ var flow = [
{ id: "server", type: "websocket-listener", path: "/ws" }, { id: "server", type: "websocket-listener", path: "/ws" },
@ -407,4 +479,35 @@ describe('websocket node', function() {
}); });
}); });
}); });
describe('websocket in node', function() {
it('should report error if no server config', function(done) {
var flow = [{ id: "n1", type: "websocket in", mode: "server" }];
helper.load(websocketNode, flow, function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "websocket in";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Missing server configuration");
done();
});
});
});
describe('websocket out node', function() {
it('should report error if no server config', function(done) {
var flow = [{ id: "n1", type: "websocket out", mode: "server" }];
helper.load(websocketNode, flow, function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "websocket out";
});
//console.log(logEvents);
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Missing server configuration");
done();
});
});
});
}); });