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">
<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">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<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
- 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><b>Note:</b> If the msg contains a .location property it will operate on that in preference to the .payload.</p>
</script>
<script type="text/javascript">
@ -22,7 +25,8 @@
category: 'location',
color:"#DEBD5C",
defaults: {
name: {value:""}
name: {value:""},
property: {value:"payload",required:true}
},
inputs:1,
outputs:1,

View File

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

View File

@ -1,6 +1,6 @@
{
"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.",
"dependencies" : {
"ngeohash" : "0.6.0"

View File

@ -1,5 +1,9 @@
<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">
<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">
@ -20,7 +24,8 @@
category: 'function',
color:"#DEBD5C",
defaults: {
name: {value:""}
name: {value:""},
property: {value:"payload",required:true}
},
inputs:1,
outputs:1,

View File

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

View File

@ -1,6 +1,6 @@
{
"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",
"dependencies" : {
"msgpack-lite" : "^0.1.26"

View File

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

View File

@ -1,5 +1,9 @@
<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">
<label for="node-input-apikey"><i class="fa fa-key"></i> API Key</label>
<input type="password" id="node-input-apikey">
@ -41,9 +45,10 @@
RED.nodes.registerType('what3words',{
category: 'location',
defaults: {
name: {value:""},
property: {value:"payload",required:true},
title: {value:""},
lang: {value:"en"},
name: {value:""}
lang: {value:"en"}
},
credentials: {
apikey: {type: "password"}

View File

@ -6,74 +6,70 @@ module.exports = function(RED) {
var what3wordsNode = function(n) {
RED.nodes.createNode(this, n);
this.lang = n.lang || "en";
this.property = n.property||"payload";
var node = this;
//if ( !node.credentials.apikey ) { this.error("No what3words API key set"); }
this.w3w = new What3Words(node.credentials.apikey);
var w1 = /^\*\w{6,31}$/;
var w3 = /^\w+\.\w+\.\w+$/;
this.on("input", function(msg) {
if (msg.hasOwnProperty("location") && msg.location.hasOwnProperty("lat") && msg.location.hasOwnProperty("lon")) {
node.w3w.positionToWords({ position:msg.location.lat + "," + msg.location.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 (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 })
var value = RED.util.getMessageProperty(msg,node.property);
if (value !== undefined) {
if (value.hasOwnProperty("lat") && value.hasOwnProperty("lon")) {
node.w3w.positionToWords({ position:value.lat + "," + value.lon, lang:node.lang })
.then(function(response) {
msg.payload = response; // prom.cape.pump
value = response; // prom.cape.pump
if (!msg.hasOwnProperty("topic") || (msg.topic === "")) { msg.topic = "what3words"; }
node.send(msg);
})
.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]);
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
})
.catch(function(err) {
node.warn(err)
});
}
else if (msg.payload.match(w1)) { // see if it's a *Oneword
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]);
msg.payload = response;
node.send(msg);
})
.catch(function(err) {
node.warn(err);
});
else if (typeof (value) === "string") {
if (value.split(",").length === 2) { // see if it's 2 comma separated words
node.w3w.positionToWords({ position:value, lang:node.lang })
.then(function(response) {
value = response; // prom.cape.pump
if (!msg.hasOwnProperty("topic") || (msg.topic === "")) { msg.topic = "what3words"; }
RED.util.setMessageProperty(msg,node.property,value);
node.send(msg);
})
.catch(function(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."); }
});
}
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 helper = require('../../../test/helper.js');
@ -129,7 +144,7 @@ describe('geohash node', function() {
});
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"} ];
helper.load(testNode, flow, function() {
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) {
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"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");
@ -159,7 +174,7 @@ describe('geohash node', function() {
});
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"} ];
helper.load(testNode, flow, function() {
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) {
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"} ];
helper.load(testNode, flow, function() {
var n1 = helper.getNode("n1");