-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdelegate-my-device.js
93 lines (81 loc) · 4.76 KB
/
delegate-my-device.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
88
89
90
91
92
93
/* 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 DelegateMyDevice(config) {
RED.nodes.createNode(this, config);
var node = this;
node.selectedDeviceDataId = config.selectedDeviceDataId;
node.deviceId = config.deviceId;
node.usernamedelegated = config.delegated;
node.on('input', function (msg) {
var s4cUtility = require("./snap4city-utility.js");
const logger = s4cUtility.getLogger(RED, node);
const uid = s4cUtility.retrieveAppID(RED);
var deviceId = (msg.payload.id ? msg.payload.id : node.deviceId);
if (deviceId) {
var uri = (RED.settings.myPersonalDataUrl ? RED.settings.myPersonalDataUrl : "https://www.snap4city.org/mypersonaldata/api/v1") + "/username/" + (s4cUtility.retrieveCurrentUser(RED, node, config.authentication)).toLowerCase() + "/delegation";
var usernamedelegated = (msg.payload.usernamedelegated ? msg.payload.usernamedelegated : node.usernamedelegated);
var inPayload = msg.payload;
var accessToken = "";
accessToken = s4cUtility.retrieveAccessToken(RED, node, config.authentication, uid);
if (accessToken != "" && typeof accessToken != "undefined") {
var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", encodeURI(uri + "/?sourceRequest=iotapp"), true);
logger.info(encodeURI(uri + "/?sourceRequest=iotapp"));
xmlHttp.setRequestHeader("Content-Type", "application/json");
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) {
msg.payload = xmlHttp.responseText;
logger.error("Problem Parsing data " + xmlHttp.responseText);
}
} else {
msg.payload = JSON.parse("{\"status\": \"There was some problem\"}");
}
s4cUtility.eventLog(RED, inPayload, msg, config, "Node-Red", "IOTDirectory", uri, "RX");
node.send(msg);
} else if (xmlHttp.status === 401) {
node.error("Unauthorized");
logger.error("Unauthorized, accessToken: " + accessToken);
} else {
logger.error(xmlHttp.statusText);
node.error(xmlHttp.responseText);
}
}
};
xmlHttp.onerror = function (e) {
logger.error(xmlHttp.statusText);
node.error(xmlHttp.responseText);
};
xmlHttp.send(JSON.stringify({
"usernameDelegated": (usernamedelegated).toLowerCase(),
"usernameDelegator": (s4cUtility.retrieveCurrentUser(RED, node, config.authentication)).toLowerCase(),
"elementId": deviceId,
"elementType": "IOTID"
}));
} else {
node.error("Open the configuration of the node and redeploy");
}
} else {
node.error("Device ID not configured or sent to input");
}
});
}
RED.nodes.registerType("delegate-my-device", DelegateMyDevice);
}