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

Merge branch 'dev' into repackage

This commit is contained in:
Nick O'Leary 2018-08-31 21:18:23 +01:00
commit 18b5b4901f
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
9 changed files with 246 additions and 14 deletions

View File

@ -19,7 +19,11 @@ module.exports = function(RED) {
if (this.tosidebar === undefined) { this.tosidebar = true; } if (this.tosidebar === undefined) { this.tosidebar = true; }
this.severity = n.severity || 40; this.severity = n.severity || 40;
this.active = (n.active === null || typeof n.active === "undefined") || n.active; this.active = (n.active === null || typeof n.active === "undefined") || n.active;
this.status({}); if (this.tostatus) {
this.oldStatus = {fill:"grey", shape:this.active?"dot":"ring"};
this.status(this.oldStatus);
}
else { this.status({}); }
var node = this; var node = this;
var levels = { var levels = {
@ -122,7 +126,7 @@ module.exports = function(RED) {
if (state === "enable") { if (state === "enable") {
node.active = true; node.active = true;
res.sendStatus(200); res.sendStatus(200);
if (node.tostatus) { node.status({}); } if (node.tostatus) { node.status({fill:"grey", shape:"dot"}); }
} else if (state === "disable") { } else if (state === "disable") {
node.active = false; node.active = false;
res.sendStatus(201); res.sendStatus(201);

View File

@ -53,12 +53,14 @@
<p>The Catch node can also be used to handle errors. To invoke a Catch node, <p>The Catch node can also be used to handle errors. To invoke a Catch node,
pass <code>msg</code> as a second argument to <code>node.error</code>:</p> pass <code>msg</code> as a second argument to <code>node.error</code>:</p>
<pre>node.error("Error",msg);</pre> <pre>node.error("Error",msg);</pre>
<h4>Referring Node Information</h4> <h4>Accessing Node Information</h4>
<p>In the function block, id and name of the node can be referenced using the following properties:</p> <p>In the function block, id and name of the node can be referenced using the following properties:</p>
<ul> <ul>
<li><code>node.id</code> - id of the node</li> <li><code>node.id</code> - id of the node</li>
<li><code>node.name</code> - name of the node</li> <li><code>node.name</code> - name of the node</li>
</ul> </ul>
<h4>Using environment variables</h4>
<p>Environment variables can be accessed using <code>env.get("MY_ENV_VAR")</code>.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">

View File

@ -156,6 +156,13 @@ module.exports = function(RED) {
return node.context().global.keys.apply(node,arguments); return node.context().global.keys.apply(node,arguments);
} }
}, },
env: {
get: function(envVar) {
// For now, just return the env var. This will eventually
// also return project settings and subflow instance properties
return process.env[envVar]
}
},
setTimeout: function () { setTimeout: function () {
var func = arguments[0]; var func = arguments[0];
var timerId; var timerId;

View File

@ -33,9 +33,7 @@ module.exports = function(RED) {
*/ */
const enqueue = (queue, item) => { const enqueue = (queue, item) => {
// drop msgs from front of queue if size is going to be exceeded // drop msgs from front of queue if size is going to be exceeded
if (queue.size() === msgQueueSize) { if (queue.size() === msgQueueSize) { queue.shift(); }
queue.shift();
}
queue.push(item); queue.push(item);
return queue; return queue;
}; };
@ -646,7 +644,7 @@ module.exports = function(RED) {
} }
else if (!clients[connection_id].connecting && clients[connection_id].connected) { else if (!clients[connection_id].connecting && clients[connection_id].connected) {
if (clients[connection_id] && clients[connection_id].client) { if (clients[connection_id] && clients[connection_id].client) {
clients[connection_id].client.write(dequeue(clients[connection_id].msgQueue)); clients[connection_id].client.write(dequeue(clients[connection_id].msgQueue).payload);
} }
} }
}); });

View File

@ -19,6 +19,7 @@ module.exports = function(RED) {
const Ajv = require('ajv'); const Ajv = require('ajv');
const ajv = new Ajv({allErrors: true, schemaId: 'auto'}); const ajv = new Ajv({allErrors: true, schemaId: 'auto'});
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json')); ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'));
ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-06.json'));
function JSONNode(n) { function JSONNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
@ -29,6 +30,7 @@ module.exports = function(RED) {
this.compiledSchema = null; this.compiledSchema = null;
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
var validate = false; var validate = false;
if (msg.schema) { if (msg.schema) {
@ -65,7 +67,17 @@ module.exports = function(RED) {
} }
catch(e) { node.error(e.message,msg); } catch(e) { node.error(e.message,msg); }
} else { } else {
// If node.action is str and value is str
if (validate) {
if (this.compiledSchema(JSON.parse(msg[node.property]))) {
node.send(msg); node.send(msg);
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
}
} else {
node.send(msg);
}
} }
} }
else if (typeof value === "object") { else if (typeof value === "object") {
@ -84,13 +96,22 @@ module.exports = function(RED) {
RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent)); RED.util.setMessageProperty(msg,node.property,JSON.stringify(value,null,node.indent));
node.send(msg); node.send(msg);
} }
} }
catch(e) { node.error(RED._("json.errors.dropped-error")); } catch(e) { node.error(RED._("json.errors.dropped-error")); }
} }
else { node.warn(RED._("json.errors.dropped-object")); } else { node.warn(RED._("json.errors.dropped-object")); }
} else { } else {
// If node.action is obj and value is object
if (validate) {
if (this.compiledSchema(value)) {
node.send(msg); node.send(msg);
} else {
msg.schemaError = this.compiledSchema.errors;
node.error(`${RED._("json.errors.schema-error")}: ${ajv.errorsText(this.compiledSchema.errors)}`, msg);
}
} else {
node.send(msg);
}
} }
} }
else { node.warn(RED._("json.errors.dropped")); } else { node.warn(RED._("json.errors.dropped")); }

View File

@ -24,7 +24,7 @@ var i18n = require("@node-red/util").i18n; // TODO: separate module
var settings; var settings;
var disableNodePathScan = false; var disableNodePathScan = false;
var iconFileExtensions = [".png", ".gif"]; var iconFileExtensions = [".png", ".gif", ".svg"];
function init(runtime) { function init(runtime) {
settings = runtime.settings; settings = runtime.settings;

View File

@ -1249,6 +1249,36 @@ describe('function node', function() {
}); });
}); });
it('should allow accessing env vars', function(done) {
var flow = [{id:"n1",type:"function",wires:[["n2"]],func:"msg.payload = env.get('_TEST_FOO_'); return msg;"},
{id:"n2", type:"helper"}];
helper.load(functionNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var count = 0;
delete process.env._TEST_FOO_;
n2.on("input", function(msg) {
try {
if (count === 0) {
msg.should.have.property('payload', undefined);
process.env._TEST_FOO_ = "hello";
count++;
n1.receive({payload:"foo",topic: "bar"});
} else {
msg.should.have.property('payload', "hello");
done();
}
} catch(err) {
done(err);
} finally {
delete process.env._TEST_FOO_;
}
});
n1.receive({payload:"foo",topic: "bar"});
});
});
describe('Logger', function () { describe('Logger', function () {
it('should log an Info Message', function (done) { it('should log an Info Message', function (done) {
var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.log('test');"}]; var flow = [{id: "n1", type: "function", wires: [["n2"]], func: "node.log('test');"}];

View File

@ -15,7 +15,8 @@
**/ **/
var should = require("should"); var should = require("should");
var rpi = require("nr-test-utils").require("@node-red/nodes/core/hardware/36-rpi-gpio.js"); var rpiNode = require("nr-test-utils").require("@node-red/nodes/core/hardware/36-rpi-gpio.js");
var statusNode = require("nr-test-utils").require("@node-red/nodes/core/core/25-status.js");
var helper = require("node-red-node-test-helper"); var helper = require("node-red-node-test-helper");
var fs = require("fs"); var fs = require("fs");
@ -50,7 +51,7 @@ describe('RPI GPIO Node', function() {
it('should load Input node', function(done) { it('should load Input node', function(done) {
var flow = [{id:"n1", type:"rpi-gpio in", name:"rpi-gpio in" }]; var flow = [{id:"n1", type:"rpi-gpio in", name:"rpi-gpio in" }];
helper.load(rpi, flow, function() { helper.load(rpiNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
n1.should.have.property('name', 'rpi-gpio in'); n1.should.have.property('name', 'rpi-gpio in');
try { try {
@ -69,7 +70,7 @@ describe('RPI GPIO Node', function() {
it('should load Output node', function(done) { it('should load Output node', function(done) {
var flow = [{id:"n1", type:"rpi-gpio out", name:"rpi-gpio out" }]; var flow = [{id:"n1", type:"rpi-gpio out", name:"rpi-gpio out" }];
helper.load(rpi, flow, function() { helper.load(rpiNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
n1.should.have.property('name', 'rpi-gpio out'); n1.should.have.property('name', 'rpi-gpio out');
try { try {
@ -86,4 +87,68 @@ describe('RPI GPIO Node', function() {
}); });
}); });
it('should read a dummy value high (not on Pi)', function(done) {
var flow = [{id:"n1", type:"rpi-gpio in", pin:"7", intype:"up", debounce:"25", read:true, wires:[["n2"]] },
{id:"n2", type:"helper"}];
helper.load(rpiNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'pi/7');
msg.should.have.property('payload', 1);
done();
} catch(err) {
done(err);
}
});
});
});
it('should read a dummy value low (not on Pi)', function(done) {
var flow = [{id:"n1", type:"rpi-gpio in", pin:"11", intype:"down", debounce:"25", read:true, wires:[["n2"]] },
{id:"n2", type:"helper"}];
helper.load(rpiNode, flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
n2.on("input", function(msg) {
try {
msg.should.have.property('topic', 'pi/11');
msg.should.have.property('payload', 0);
done();
} catch(err) {
done(err);
}
});
});
});
it('should be able preset out to a dummy value (not on Pi)', function(done) {
var flow = [{id:"n1", type:"rpi-gpio out", pin:"7", out:"out", level:"0", set:true, freq:"", wires:[], z:"1"},
{id:"n2", type:"status", scope:null, wires:[["n3"]], z:"1"},
{id:"n3", type:"helper", z:"1"}];
helper.load([rpiNode,statusNode], flow, function() {
var n1 = helper.getNode("n1");
var n2 = helper.getNode("n2");
var n3 = helper.getNode("n3");
var count = 0;
n3.on("input", function(msg) {
// Only check the first status message received as it may get a
// 'closed' status as the test is tidied up.
if (count === 0) {
count++;
try {
msg.should.have.property('status');
msg.status.should.have.property('text', "rpi-gpio.status.na");
done();
} catch(err) {
done(err);
}
}
});
n1.receive({payload:"1"});
});
});
}); });

View File

@ -265,6 +265,23 @@ describe('JSON node', function() {
}); });
}); });
it('should pass an object if provided a valid object and schema and action is object', function(done) {
var flow = [{id:"jn1",type:"json",action:"obj",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
jn2.on("input", function(msg) {
should.equal(msg.payload.number, 3);
should.equal(msg.payload.string, "allo");
done();
});
var obj = {"number": 3, "string": "allo"};
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
jn1.receive({payload:obj, schema:schema});
});
});
it('should pass a string if provided a valid object and schema', function(done) { it('should pass a string if provided a valid object and schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]}, var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}]; {id:"jn2", type:"helper"}];
@ -281,6 +298,22 @@ describe('JSON node', function() {
}); });
}); });
it('should pass a string if provided a valid JSON string and schema and action is string', function(done) {
var flow = [{id:"jn1",type:"json",action:"str",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
jn2.on("input", function(msg) {
should.equal(msg.payload, '{"number":3,"string":"allo"}');
done();
});
var jsonString = '{"number":3,"string":"allo"}';
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
jn1.receive({payload:jsonString, schema:schema});
});
});
it('should log an error if passed an invalid object and valid schema', function(done) { it('should log an error if passed an invalid object and valid schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]}, var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}]; {id:"jn2", type:"helper"}];
@ -305,6 +338,78 @@ describe('JSON node', function() {
}); });
}); });
it('should log an error if passed an invalid object and valid schema and action is object', function(done) {
var flow = [{id:"jn1",type:"json",action:"obj",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
try {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
var obj = {"number": "foo", "string": 3};
jn1.receive({payload:obj, schema:schema});
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.should.equal("json.errors.schema-error: data.number should be number, data.string should be string");
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {
done(err);
}
});
});
it('should log an error if passed an invalid JSON string and valid schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
try {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
var jsonString = '{"number":"Hello","string":3}';
jn1.receive({payload:jsonString, schema:schema});
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.should.equal("json.errors.schema-error: data.number should be number, data.string should be string");
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {
done(err);
}
});
});
it('should log an error if passed an invalid JSON string and valid schema and action is string', function(done) {
var flow = [{id:"jn1",type:"json",action:"str",wires:[["jn2"]]},
{id:"jn2", type:"helper"}];
helper.load(jsonNode, flow, function() {
try {
var jn1 = helper.getNode("jn1");
var jn2 = helper.getNode("jn2");
var schema = {title: "testSchema", type: "object", properties: {number: {type: "number"}, string: {type: "string" }}};
var jsonString = '{"number":"Hello","string":3}';
jn1.receive({payload:jsonString, schema:schema});
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "json";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.should.equal("json.errors.schema-error: data.number should be number, data.string should be string");
logEvents[0][0].should.have.a.property('level',helper.log().ERROR);
done();
} catch(err) {
done(err);
}
});
});
it('should log an error if passed a valid object and invalid schema', function(done) { it('should log an error if passed a valid object and invalid schema', function(done) {
var flow = [{id:"jn1",type:"json",wires:[["jn2"]]}, var flow = [{id:"jn1",type:"json",wires:[["jn2"]]},
{id:"jn2", type:"helper"}]; {id:"jn2", type:"helper"}];