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
10 changed files with 158 additions and 139 deletions

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, {