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

update rest of parsers to allow property to be selected

This commit is contained in:
Dave Conway-Jones 2018-01-31 11:18:30 +00:00
parent f664fa0e31
commit b2de928860
No known key found for this signature in database
GPG Key ID: 9E7F9C73F5168CD4
10 changed files with 158 additions and 139 deletions

View File

@ -1,5 +1,9 @@
<script type="text/x-red" data-template-name="geohash"> <script type="text/x-red" data-template-name="geohash">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -14,7 +18,6 @@
<p>If the <code>msg.payload</code> is an object with properties lat or latitude and lon or longitude <p>If the <code>msg.payload</code> is an object with properties lat or latitude and lon or longitude
- it will add a <code>geohash</code> property to the payload.</p> - it will add a <code>geohash</code> property to the payload.</p>
<p>The precision can be set by <code>msg.payload.precision</code> from 1 to 9. <p>The precision can be set by <code>msg.payload.precision</code> from 1 to 9.
<p><b>Note:</b> If the msg contains a .location property it will operate on that in preference to the .payload.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
@ -22,7 +25,8 @@
category: 'location', category: 'location',
color:"#DEBD5C", color:"#DEBD5C",
defaults: { defaults: {
name: {value:""} name: {value:""},
property: {value:"payload",required:true}
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,

View File

@ -5,6 +5,7 @@ module.exports = function(RED) {
function GeohashNode(n) { function GeohashNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.property = n.property||"payload";
var node = this; var node = this;
var round = function(value, decimals) { var round = function(value, decimals) {
@ -12,82 +13,71 @@ module.exports = function(RED) {
} }
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("location")) { var value = RED.util.getMessageProperty(msg,node.property);
if (msg.location.hasOwnProperty("geohash")) { if (value !== undefined) {
var pos = geohash.decode(msg.location.geohash); if (typeof value === "string") {
msg.location.lat = round(pos.latitude,5); // try to decode it...
msg.location.lon = round(pos.longitude,5); var regexp = new RegExp('^[a-z0-9]{1,9}$'); // can only contain a-z or 0-9 and length 1-9
msg.location.error = { lat:round(pos.error.latitude,5), lon:round(pos.error.longitude,5) }; if (regexp.test(value)) {
node.send(msg); var po = geohash.decode(value);
value = { lat:round(po.latitude,5), lon:round(po.longitude,5) };
value.error = { lat:round(po.error.latitude,5), lon:round(po.error.longitude,5) };
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else if (value.indexOf(",") !== -1) {
// has a comma so assume it's lat,lon(,precision)
var bits = value.split(",");
if ((bits.length === 2) || (bits.length === 3)) {
var li = 9;
if (bits.length === 3) {
li = parseInt(bits[2]);
if (li < 1) { li = 1; }
if (li > 9) { li = 9; }
}
var la = Number(bits[0]);
if (la < -90) { la = -90; }
if (la > 90) { la = 90; }
var lo = Number(bits[1]);
if (lo < -180) { lo = ((lo-180)%360)+180; }
if (lo > 180) { lo = ((lo+180)%360)-180; }
if (!isNaN(la) && !isNaN(lo)) {
value = geohash.encode(la, lo, li);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
}
else {
node.warn("Incorrect string format - should be lat,lon");
}
}
else { node.warn("Unexpected string format - should be lat,lon"); }
}
else { node.warn("Unexpected string format - should either be lat,lon or geohash"); }
} }
else { else if (typeof value === "object") {
var lt = msg.location.lat; if (value.hasOwnProperty("geohash")) {
var ln = msg.location.lon; var pos = geohash.decode(value.geohash);
var le = parseInt(msg.location.precision || 9); value.lat = round(pos.latitude,5);
if (le < 1) { le = 1; } value.lon = round(pos.longitude,5);
if (le > 9) { le = 9; } value.error = { lat:round(pos.error.latitude,5), lon:round(pos.error.longitude,5) };
if (lt && ln) { RED.util.setMessageProperty(msg,node.property,value);
msg.location.geohash = geohash.encode(lt, ln, le);
node.send(msg); node.send(msg);
} }
else { else {
node.warn("lat or lon missing from msg.location"); var lat = value.lat || value.latitude;
} var lon = value.lon || value.longitude;
} var len = parseInt(value.precision || 9);
} if (len < 1) { len = 1; }
else if (typeof msg.payload === "string") { if (len > 9) { len = 9; }
// try to decode it... if (lat && lon) {
var regexp = new RegExp('^[a-z0-9]{1,9}$'); // can only contain a-z or 0-9 and length 1-9 value.geohash = geohash.encode(lat, lon, len);
if (regexp.test(msg.payload)) { RED.util.setMessageProperty(msg,node.property,value);
var po = geohash.decode(msg.payload);
msg.payload = { lat:round(po.latitude,5), lon:round(po.longitude,5) };
msg.payload.error = { lat:round(po.error.latitude,5), lon:round(po.error.longitude,5) };
node.send(msg);
}
else if (msg.payload.indexOf(",") !== -1) {
// has a comma so assume it's lat,lon(,precision)
var bits = msg.payload.split(",");
if ((bits.length === 2) || (bits.length === 3)) {
var li = 9;
if (bits.length === 3) {
li = parseInt(bits[2]);
if (li < 1) { li = 1; }
if (li > 9) { li = 9; }
}
var la = Number(bits[0]);
if (la < -90) { la = -90; }
if (la > 90) { la = 90; }
var lo = Number(bits[1]);
if (lo < -180) { lo = ((lo-180)%360)+180; }
if (lo > 180) { lo = ((lo+180)%360)-180; }
if (!isNaN(la) && !isNaN(lo)) {
msg.payload = geohash.encode(la, lo, li);
node.send(msg); node.send(msg);
} }
else { else { node.warn("lat or lon missing from msg."+node.property); }
node.warn("Incorrect string format - should be lat,lon");
}
} }
else { node.warn("Unexpected string format - should be lat,lon"); }
} }
else { node.warn("Unexpected string format - should either be lat,lon or geohash"); } else { node.warn("This node only expects strings or objects."); }
}
else if (typeof msg.payload === "object") {
var lat = msg.payload.lat || msg.payload.latitude;
var lon = msg.payload.lon || msg.payload.longitude;
var len = parseInt(msg.payload.precision || 9);
if (len < 1) { len = 1; }
if (len > 9) { len = 9; }
if (lat && lon) {
msg.payload.geohash = geohash.encode(lat, lon, len);
node.send(msg);
}
else {
node.warn("lat or lon missing from msg.payload");
}
}
else {
node.warn("This node only expects strings or objects.");
} }
}); });
} }

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-geohash", "name" : "node-red-node-geohash",
"version" : "0.0.7", "version" : "0.1.7",
"description" : "A Node-RED node to encode and decode lat,lon pairs to a geohash.", "description" : "A Node-RED node to encode and decode lat,lon pairs to a geohash.",
"dependencies" : { "dependencies" : {
"ngeohash" : "0.6.0" "ngeohash" : "0.6.0"

View File

@ -1,5 +1,9 @@
<script type="text/x-red" data-template-name="msgpack"> <script type="text/x-red" data-template-name="msgpack">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label> <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="node-red:common.label.name"></span></label>
<input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name"> <input type="text" id="node-input-name" data-i18n="[placeholder]node-red:common.label.name">
@ -20,7 +24,8 @@
category: 'function', category: 'function',
color:"#DEBD5C", color:"#DEBD5C",
defaults: { defaults: {
name: {value:""} name: {value:""},
property: {value:"payload",required:true}
}, },
inputs:1, inputs:1,
outputs:1, outputs:1,

View File

@ -5,15 +5,18 @@ module.exports = function(RED) {
function MsgPackNode(n) { function MsgPackNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.property = n.property||"payload";
var node = this; var node = this;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("payload")) { var value = RED.util.getMessageProperty(msg,node.property);
if (Buffer.isBuffer(msg.payload)) { if (value !== undefined) {
var l = msg.payload.length; if (Buffer.isBuffer(value)) {
var l = value.length;
try { try {
msg.payload = msgpack.decode(msg.payload); value = msgpack.decode(value);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg); node.send(msg);
node.status({text:l +" b->o "+ JSON.stringify(msg.payload).length}); node.status({text:l +" b->o "+ JSON.stringify(value).length});
} }
catch (e) { catch (e) {
node.error("Bad decode",msg); node.error("Bad decode",msg);
@ -21,9 +24,10 @@ module.exports = function(RED) {
} }
} }
else { else {
var le = JSON.stringify(msg.payload).length; var le = JSON.stringify(value).length;
msg.payload = msgpack.encode(msg.payload); value = msgpack.encode(value);
node.status({text:le +" o->b "+ msg.payload.length}); RED.util.setMessageProperty(msg,node.property,value);
node.status({text:le +" o->b "+ value.length});
node.send(msg); node.send(msg);
} }
} }

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-msgpack", "name" : "node-red-node-msgpack",
"version" : "1.0.1", "version" : "1.1.1",
"description" : "A Node-RED node to pack and unpack objects to msgpack format", "description" : "A Node-RED node to pack and unpack objects to msgpack format",
"dependencies" : { "dependencies" : {
"msgpack-lite" : "^0.1.26" "msgpack-lite" : "^0.1.26"

View File

@ -1,6 +1,6 @@
{ {
"name" : "node-red-node-what3words", "name" : "node-red-node-what3words",
"version" : "0.0.7", "version" : "0.1.7",
"description" : "A Node-RED node to convert locations to/from what3words", "description" : "A Node-RED node to convert locations to/from what3words",
"dependencies" : { "dependencies" : {
"geo.what3words" : "^2.0.0" "geo.what3words" : "^2.0.0"

View File

@ -1,5 +1,9 @@
<script type="text/x-red" data-template-name="what3words"> <script type="text/x-red" data-template-name="what3words">
<div class="form-row">
<label for="node-input-property"><i class="fa fa-ellipsis-h"></i> <span data-i18n="node-red:common.label.property"></span></label>
<input type="text" id="node-input-property" style="width:70%;"/>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label> <label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-input-apikey"> <input type="password" id="node-input-apikey">
@ -41,9 +45,10 @@
RED.nodes.registerType('what3words',{ RED.nodes.registerType('what3words',{
category: 'location', category: 'location',
defaults: { defaults: {
name: {value:""},
property: {value:"payload",required:true},
title: {value:""}, title: {value:""},
lang: {value:"en"}, lang: {value:"en"}
name: {value:""}
}, },
credentials: { credentials: {
apikey: {type: "password"} apikey: {type: "password"}

View File

@ -6,74 +6,70 @@ module.exports = function(RED) {
var what3wordsNode = function(n) { var what3wordsNode = function(n) {
RED.nodes.createNode(this, n); RED.nodes.createNode(this, n);
this.lang = n.lang || "en"; this.lang = n.lang || "en";
this.property = n.property||"payload";
var node = this; var node = this;
//if ( !node.credentials.apikey ) { this.error("No what3words API key set"); } //if ( !node.credentials.apikey ) { this.error("No what3words API key set"); }
this.w3w = new What3Words(node.credentials.apikey); this.w3w = new What3Words(node.credentials.apikey);
var w1 = /^\*\w{6,31}$/; var w1 = /^\*\w{6,31}$/;
var w3 = /^\w+\.\w+\.\w+$/; var w3 = /^\w+\.\w+\.\w+$/;
this.on("input", function(msg) { this.on("input", function(msg) {
if (msg.hasOwnProperty("location") && msg.location.hasOwnProperty("lat") && msg.location.hasOwnProperty("lon")) { var value = RED.util.getMessageProperty(msg,node.property);
node.w3w.positionToWords({ position:msg.location.lat + "," + msg.location.lon, lang:node.lang }) if (value !== undefined) {
.then(function(response) { if (value.hasOwnProperty("lat") && value.hasOwnProperty("lon")) {
msg.payload = response; // prom.cape.pump node.w3w.positionToWords({ position:value.lat + "," + value.lon, lang:node.lang })
if (!msg.hasOwnProperty("topic") || (msg.topic === "")) { msg.topic = "what3words"; }
node.send(msg);
})
.catch(function(err) {
node.warn(err)
});
}
else if (msg.hasOwnProperty("payload") && msg.payload.hasOwnProperty("lat") && msg.payload.hasOwnProperty("lon")) {
node.w3w.positionToWords({ position:msg.payload.lat + "," + msg.payload.lon, lang:node.lang })
.then(function(response) {
msg.payload = response; // prom.cape.pump
if (!msg.hasOwnProperty("topic") || (msg.topic === "")) { msg.topic = "what3words"; }
node.send(msg);
})
.catch(function(err) {
node.warn(err)
});
}
else if (typeof (msg.payload) === "string") {
if (msg.payload.split(",").length === 2) { // see if it's 2 comma separated words
node.w3w.positionToWords({ position:msg.payload, lang:node.lang })
.then(function(response) { .then(function(response) {
msg.payload = response; // prom.cape.pump value = response; // prom.cape.pump
if (!msg.hasOwnProperty("topic") || (msg.topic === "")) { msg.topic = "what3words"; } if (!msg.hasOwnProperty("topic") || (msg.topic === "")) { msg.topic = "what3words"; }
node.send(msg); RED.util.setMessageProperty(msg,node.property,value);
})
.catch(function(err) {
node.warn(err);
});
}
else if (msg.payload.match(w3)) { // see if it's 3 dot separated words
node.w3w.wordsToPosition({ words:msg.payload })
.then(function(response) {
if (!msg.hasOwnProperty("location")) { msg.location = {}; }
msg.location.lat = Number(response.split(",")[0]);
msg.location.lon = Number(response.split(",")[1]);
node.send(msg); node.send(msg);
}) })
.catch(function(err) { .catch(function(err) {
node.warn(err) node.warn(err)
}); });
} }
else if (msg.payload.match(w1)) { // see if it's a *Oneword else if (typeof (value) === "string") {
node.w3w.wordsToPosition({ words:msg.payload }) if (value.split(",").length === 2) { // see if it's 2 comma separated words
.then(function(response) { node.w3w.positionToWords({ position:value, lang:node.lang })
if (!msg.hasOwnProperty("location")) { msg.location = {}; } .then(function(response) {
msg.location.lat = Number(response.split(",")[0]); value = response; // prom.cape.pump
msg.location.lon = Number(response.split(",")[1]); if (!msg.hasOwnProperty("topic") || (msg.topic === "")) { msg.topic = "what3words"; }
msg.payload = response; RED.util.setMessageProperty(msg,node.property,value);
node.send(msg); node.send(msg);
}) })
.catch(function(err) { .catch(function(err) {
node.warn(err); node.warn(err);
}); });
}
else if (value.match(w3)) { // see if it's 3 dot separated words
node.w3w.wordsToPosition({ words:value })
.then(function(response) {
value.lat = Number(response.split(",")[0]);
value.lon = Number(response.split(",")[1]);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
})
.catch(function(err) {
node.warn(err)
});
}
else if (value.match(w1)) { // see if it's a *Oneword
node.w3w.wordsToPosition({ words:value })
.then(function(response) {
if (!msg.hasOwnProperty("location")) { value = {}; }
value.lat = Number(response.split(",")[0]);
value.lon = Number(response.split(",")[1]);
value = response;
node.send(msg);
})
.catch(function(err) {
node.warn(err);
});
}
else { node.warn("No useable data found. See info."); }
} }
else { node.warn("No useable data found. See info."); } else { node.warn("No useable data found. See info."); }
} }
else { node.warn("No useable data found. See info."); }
}); });
} }
RED.nodes.registerType("what3words", what3wordsNode, { RED.nodes.registerType("what3words", what3wordsNode, {

View File

@ -1,3 +1,18 @@
/**
* Copyright 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.
* 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.
**/
var should = require("should"); var should = require("should");
var helper = require('../../../test/helper.js'); var helper = require('../../../test/helper.js');
@ -129,7 +144,7 @@ describe('geohash node', function() {
}); });
it('should convert location lat, lon to geohash', function(done) { it('should convert location lat, lon to geohash', function(done) {
var flow = [{"id":"n1", "type":"geohash", func:"geohash", gap:0, wires:[["n2"]] }, var flow = [{id:"n1", type:"geohash", func:"geohash", property:"location", gap:0, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() { helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
@ -144,7 +159,7 @@ describe('geohash node', function() {
}); });
it('should convert location lat, lon to geohash (low precision)', function(done) { it('should convert location lat, lon to geohash (low precision)', function(done) {
var flow = [{"id":"n1", "type":"geohash", func:"geohash", gap:0, wires:[["n2"]] }, var flow = [{"id":"n1", "type":"geohash", func:"geohash", property:"location", gap:0, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() { helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
@ -159,7 +174,7 @@ describe('geohash node', function() {
}); });
it('should convert location geohash to lat.lon', function(done) { it('should convert location geohash to lat.lon', function(done) {
var flow = [{"id":"n1", "type":"geohash", func:"geohash", gap:0, wires:[["n2"]] }, var flow = [{"id":"n1", "type":"geohash", func:"geohash", property:"location", gap:0, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() { helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");
@ -289,7 +304,7 @@ describe('geohash node', function() {
}); });
it('should warn if location object with only a lat (or lon)', function(done) { it('should warn if location object with only a lat (or lon)', function(done) {
var flow = [{"id":"n1", "type":"geohash", func:"gap", gap:10, wires:[["n2"]] }, var flow = [{"id":"n1", "type":"geohash", func:"gap", property:"location", gap:10, wires:[["n2"]] },
{id:"n2", type:"helper"} ]; {id:"n2", type:"helper"} ];
helper.load(testNode, flow, function() { helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1"); var n1 = helper.getNode("n1");