Start making non-red-nodes tolerant to missing payloads

This commit is contained in:
dceejay 2015-03-31 13:18:25 +01:00
parent 32f6dc0346
commit 3896a82bd8
13 changed files with 210 additions and 122 deletions

View File

@ -14,7 +14,7 @@
* limitations under the License.
**/
// Project configuration for Node-RED-nodes
// Configuration for Node-RED-nodes project
module.exports = function(grunt) {
grunt.initConfig({
simplemocha: {

View File

@ -29,6 +29,7 @@ module.exports = function(RED) {
var old = null;
this.on('input', function (msg) {
if msg.hasOwnProperty("payload")) {
var n = Number(msg.payload);
if (!isNaN(n)) {
if ((node.action === "low") || (node.action === "high")) {
@ -55,6 +56,7 @@ module.exports = function(RED) {
node.send(msg);
}
else { node.log("Not a number: "+msg.payload); }
} // ignore msg with no payload property.
});
}
RED.nodes.registerType("smooth", SmoothNode);

View File

@ -21,6 +21,7 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
if (Buffer.isBuffer(msg.payload)) {
// Take binary buffer and make into a base64 string
msg.payload = msg.payload.toString('base64');
@ -42,6 +43,7 @@ module.exports = function(RED) {
else {
node.warn("This node only handles strings or buffers.");
}
} else { node.warn("No payload found to process"); }
});
}
RED.nodes.registerType("base64",Base64Node);

View File

@ -22,6 +22,7 @@ module.exports = function(RED) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) {
if (Buffer.isBuffer(msg.payload)) {
var l = msg.payload.length;
try {
@ -40,6 +41,7 @@ module.exports = function(RED) {
node.send(msg);
node.status({text:le +" o->b "+ msg.payload.length});
}
} else { node.warn("No payload found to process");
});
}
RED.nodes.registerType("msgpack",MsgPackNode);

View File

@ -35,19 +35,10 @@ describe('exif node', function() {
it('extracts location data from Exif data of JPEG', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
// the jpg file is a single black dot but it was originally a photo taken at IBM Hursley
//console.log(process.cwd());
var data = fs.readFileSync("test/utility/exif/exif_test_image.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
var eD;
new ExifImage({ image : data }, function (error, exifData) {
if (error) {
done(error);
} else {
eD = exifData;
}
//var data = fs.readFileSync("exif_test_image.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
@ -61,14 +52,114 @@ describe('exif node', function() {
done();
});
var stub = sinon.stub(ExifImage.prototype, 'loadImage', function(error, callback) {
stub.restore();
callback(null, eD);
exifNode1.receive({payload:data});
});
});
it('should report if no data found', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
// the jpg file is a single black dot but it was originally a photo taken at IBM Hursley
//console.log(process.cwd());
var data = fs.readFileSync("test/utility/exif/exif_test_image2.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
//var data = fs.readFileSync("exif_test_image2.jpg", null); // extracting genuine exif data to be fed back as the result of the stubbed ExifImage constructor
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exif";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("The incoming image did not contain Exif GPS");
done();
},150);
exifNode1.receive({payload:data});
});
});
it('should report if not a jpeg', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
// the jpg file is a single black dot but it was originally a photo taken at IBM Hursley
var data = new Buffer("hello");
var eD;
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exif";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Error: The given image is not a JPEG");
done();
},150);
exifNode1.receive({payload:data});
});
});
it('should report if bad payload', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
var data = "hello";
var eD;
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exif";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("Invalid payload received, ");
done();
},150);
exifNode1.receive({payload:data});
});
});
it('should report if no payload', function(done) {
var exif = require('exif');
var ExifImage = exif.ExifImage;
var data = new Buffer("hello");
var eD;
var flow = [{id:"exifNode1", type:"exif", wires:[["helperNode1"]]},
{id:"helperNode1", type:"helper"}];
helper.load(exifNode, flow, function() {
var exifNode1 = helper.getNode("exifNode1");
var helperNode1 = helper.getNode("helperNode1");
setTimeout(function() {
var logEvents = helper.log().args.filter(function(evt) {
return evt[0].type == "exif";
});
logEvents.should.have.length(1);
logEvents[0][0].should.have.a.property('msg');
logEvents[0][0].msg.toString().should.startWith("No payload received, ");
done();
},150);
exifNode1.receive({topic:data});
});
});
});

Binary file not shown.

After

Width:  |  Height:  |  Size: 285 B

View File

Before

Width:  |  Height:  |  Size: 581 B

After

Width:  |  Height:  |  Size: 581 B

View File

@ -1,5 +1,5 @@
/**
* Copyright 2014 IBM Corp.
* Copyright 2014, 2015 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -24,6 +24,10 @@ module.exports = function(RED) {
return result;
}
function ExifNode(n) {
RED.nodes.createNode(this,n);
var node = this;
/***
* Extracts GPS location information from Exif data. If enough information is
* provided, convert the Exif data into a pair of single floating point number
@ -32,7 +36,6 @@ module.exports = function(RED) {
* Assume that the GPS data saved into Exif provides a valid value
*/
function addMsgLocationDataFromExifGPSData(msg) {
if(msg.exif.gps) {
var gpsData = msg.exif.gps; // declaring variable purely to make checks more readable
if(gpsData.GPSAltitude) {
if(!msg.location) {
@ -44,26 +47,22 @@ module.exports = function(RED) {
// The data provided in Exif is in degrees, minutes, seconds, this is to be converted into a single floating point degree
if(gpsData.GPSLatitude.length === 3) { // OK to convert latitude
if(gpsData.GPSLongitude.length === 3) { // OK to convert longitude
var latitude = convertDegreesMinutesSecondsToDecimals(gpsData.GPSLatitude[0], gpsData.GPSLatitude[1], gpsData.GPSLatitude[2]);
latitude = Math.round(latitude * 100000)/100000;
// (N)orth means positive latitude
// (S)outh means negative latitude
// (E)ast means positive longitude
// (W)est means negative longitude
if(gpsData.GPSLatitudeRef.toString() === 'S' || gpsData.GPSLatitudeRef.toString() === 's') {
var latitude = convertDegreesMinutesSecondsToDecimals(gpsData.GPSLatitude[0], gpsData.GPSLatitude[1], gpsData.GPSLatitude[2]);
latitude = Math.round(latitude * 100000)/100000; // 5dp is approx 1m resolution...
// (N)orth means positive latitude, (S)outh means negative latitude
if (gpsData.GPSLatitudeRef.toString() === 'S' || gpsData.GPSLatitudeRef.toString() === 's') {
latitude = latitude * -1;
}
var longitude = convertDegreesMinutesSecondsToDecimals(gpsData.GPSLongitude[0], gpsData.GPSLongitude[1], gpsData.GPSLongitude[2]);
longitude = Math.round(longitude * 100000)/100000;
if(gpsData.GPSLongitudeRef.toString() === 'W' || gpsData.GPSLongitudeRef.toString() === 'w') {
longitude = Math.round(longitude * 100000)/100000; // 5dp is approx 1m resolution...
// (E)ast means positive longitude, (W)est means negative longitude
if (gpsData.GPSLongitudeRef.toString() === 'W' || gpsData.GPSLongitudeRef.toString() === 'w') {
longitude = longitude * -1;
}
if(!msg.location) {
msg.location = {};
}
// Create location property if not exists
if (!msg.location) { msg.location = {}; }
msg.location.lat = latitude;
msg.location.lon = longitude;
return;
@ -76,30 +75,22 @@ module.exports = function(RED) {
} else {
node.log("The location of this image cannot be determined safely so no location information has been added to the message.");
}
} else {
node.log("No location info found in this image.");
}
}
function ExifNode(n) {
RED.nodes.createNode(this,n);
var node = this;
this.on("input", function(msg) {
try {
if (msg.payload) {
if (Buffer.isBuffer(msg.payload)) {
new ExifImage({ image : msg.payload }, function (error, exifData) {
if (error) {
//node.error('Failed to extract Exif data from image.');
//node.log('Failed to extract Exif data from image. '+ error);
node.log(error);
node.log(error.toString());
} else {
//msg.payload remains the same buffer
if(exifData) {
if ((exifData) && (exifData.hasOwnProperty("gps")) && (Object.keys(exifData.gps).length !== 0)) {
msg.exif = exifData;
addMsgLocationDataFromExifGPSData(msg);
} else {
node.warn("The incoming buffer did not contain Exif data, nothing to do. ");
node.warn("The incoming image did not contain Exif GPS data, nothing to do. ");
}
}
node.send(msg);