-
Notifications
You must be signed in to change notification settings - Fork 1
/
full-text-search-within-wkt-area.js
87 lines (82 loc) · 4.97 KB
/
full-text-search-within-wkt-area.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* NODE-RED-CONTRIB-SNAP4CITY-DEVELOPER
Copyright (C) 2018 DISIT Lab http://www.disit.org - University of Florence
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
module.exports = function (RED) {
function FullTextSearchWithinWktArea(config) {
RED.nodes.createNode(this, config);
var node = this;
var s4cUtility = require("./snap4city-utility.js");
const logger = s4cUtility.getLogger(RED, node);
var msgs = [{}, {}, {}];
node.on('input', function (msg) {
node.s4cAuth = RED.nodes.getNode(config.authentication);
var uri = s4cUtility.settingUrl(RED,node, "ascapiUrl", "https://www.snap4city.org", "/superservicemap/api/v1/");
var search = (msg.payload.search ? msg.payload.search : config.search);
var wktarea = (msg.payload.wktarea ? msg.payload.wktarea : config.wktarea);
var maxResults = (msg.payload.maxresults ? msg.payload.maxresults : config.maxresults);
var language = (msg.payload.lang ? msg.payload.lang : config.lang);
var geometry = (msg.payload.geometry ? msg.payload.geometry : config.geometry);
const uid = s4cUtility.retrieveAppID(RED);
var inPayload = msg.payload;
var accessToken = "";
accessToken = s4cUtility.retrieveAccessToken(RED, node, config.authentication, uid);
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();
logger.info(encodeURI(uri + "/?search=" + search + "&selection=wkt:" + wktarea + "&maxResults=" + maxResults + "&format=json" + "&lang=" + language + "&geometry=" + geometry + (typeof uid != "undefined" && uid != "" ? "&uid=" + uid : "") + "&appID=iotapp"));
xmlHttp.open("GET", encodeURI(uri + "/?search=" + search + "&selection=wkt:" + wktarea + "&maxResults=" + maxResults + "&format=json" + "&lang=" + language + "&geometry=" + geometry + (typeof uid != "undefined" && uid != "" ? "&uid=" + uid : "") + "&appID=iotapp"), true); // false for synchronous request
if (typeof accessToken != "undefined" && accessToken != "") {
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken);
} else {
logger.debug("Call without accessToken");
}
xmlHttp.onload = function (e) {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) {
logger.info("ResponseText: " + xmlHttp.responseText);
if (xmlHttp.responseText != "") {
var response = "";
try {
response = JSON.parse(xmlHttp.responseText);
} catch (error){
logger.error("Problem Parsing data " + xmlHttp.responseText);
}
logger.info("Response: " + response);
var serviceUriArray = [];
if (typeof response.features != "undefined") {
for (var i = 0; i < response.features.length; i++) {
serviceUriArray.push(response.features[i].properties.serviceUri);
}
}
msgs[0].payload = serviceUriArray;
msgs[1].payload = response;
} else {
msgs[0].payload = JSON.parse("{\"status\": \"error\"}");
msgs[1].payload = JSON.parse("{\"status\": \"error\"}");
logger.error("xmlHttp.responseText empty");
}
s4cUtility.eventLog(RED, inPayload, msgs, config, "Node-Red", "ASCAPI", uri, "RX");
node.send(msgs);
} else {
logger.error(xmlHttp.statusText);
node.error(xmlHttp.responseText);
}
}
};
xmlHttp.onerror = function (e) {
logger.error(xmlHttp.statusText);
node.error(xmlHttp.responseText);
};
xmlHttp.send(null);
});
}
RED.nodes.registerType("full-text-search-within-wkt-area", FullTextSearchWithinWktArea);
}