node-red-contrib-snap4city-user
Version:
Nodes for Snap4city project, targeted to standard user (no developer)
86 lines (81 loc) • 4.94 kB
JavaScript
/* NODE-RED-CONTRIB-SNAP4CITY-USER
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 ServiceInfo(config) {
RED.nodes.createNode(this, config);
var node = this;
var s4cUtility = require("./snap4city-utility.js");
const logger = s4cUtility.getLogger(RED, node);
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 serviceuri = (msg.payload.serviceuri ? msg.payload.serviceuri : config.serviceuri);
if (typeof serviceuri == "undefined" || (serviceuri == "" && typeof msg.payload == "string" && msg.payload.indexOf("://") != -1)) {
serviceuri = msg.payload;
}
var lang = (msg.payload.lang ? msg.payload.lang : config.lang);
var filter = (msg.payload.filter ? msg.payload.filter : config.filter);
var sortOnValue = (msg.payload.sortOnValue ? msg.payload.sortOnValue : config.sortOnValue);
var values = (msg.payload.values ? msg.payload.values : config.values);
var typeQuery = (msg.payload.typeQuery ? msg.payload.typeQuery : config.typeQuery);
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();
if (typeof serviceuri != "undefined" && serviceuri != "") {
var requestUri = uri + (typeQuery == "entity" ? "iot-search/" : "/") + "?serviceUri=" + serviceuri + "&realtime=true" + "&lang=" + lang + (typeof uid != "undefined" && uid != "" ? "&uid=" + uid : "");
if (typeQuery == "entity") {
requestUri += "&valueFilters=" + filter + (typeof sortOnValue != "undefined" && sortOnValue != "" ? "&sortOnValue=" + sortOnValue : "") + (typeof values != "undefined" && values != "" ? "&values=" + values : "");
}
requestUri += "&appID=iotapp";
logger.info(encodeURI(requestUri));
xmlHttp.open("GET", encodeURI(requestUri), true); // false for synchronous request
if (typeof accessToken != "undefined" && accessToken != "") {
xmlHttp.setRequestHeader('Authorization', 'Bearer ' + accessToken);
}
xmlHttp.onload = function (e) {
if (xmlHttp.readyState === 4) {
if (xmlHttp.status === 200) {
if (xmlHttp.responseText != "") {
try {
msg.payload = JSON.parse(xmlHttp.responseText);
} catch (e) {
logger.error("Problem Parsing data " + xmlHttp.responseText);
msg.payload = xmlHttp.responseText;
}
} else {
msg.payload = JSON.parse("{\"status\": \"There was some problem\"}");
logger.error("Problem Parsing data " + xmlHttp.responseText);
}
s4cUtility.eventLog(RED, inPayload, msg, config, "Node-Red", "ASCAPI", uri, "RX");
node.send(msg);
} else {
logger.error(xmlHttp.statusText);
node.error(xmlHttp.responseText, msg);
}
}
};
xmlHttp.onerror = function (e) {
logger.error(xmlHttp.statusText);
node.error(xmlHttp.responseText, msg);
};
xmlHttp.send(null);
} else {
node.error("Empty serviceuri");
}
});
}
RED.nodes.registerType("service-info", ServiceInfo);
}