-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathc8y-measurements.js
198 lines (168 loc) · 4.96 KB
/
c8y-measurements.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// require in libs
var request = require('request'),
queryString = require('query-string'),
utf8 = require('utf8'),
base64 = require('base-64');
// define constants
// NOTE: not using 'const' bc we want this node to be compatible with early ES versions (<ES6)
var basePath = '/measurement/measurements?'; // this is a constant, dependent on c8y
module.exports = function(RED) {
function c8yMeasurements(n) {
// Setup node
RED.nodes.createNode(this, n);
var node = this;
this.config = RED.nodes.getNode(n.cumulocityConfig);
var tenant = this.config.tenant,
domain = this.config.host;
var fullBase = tenant + "." + domain + basePath;
this.ret = n.ret || "txt"; // default return type is text
if (RED.settings.httpRequestTimeout) {
this.reqTimeout = parseInt(RED.settings.httpRequestTimeout) || 60000;
} else {
this.reqTimeout = 60000;
}
// 1) Process inputs to Node
this.on("input", function(msg) {
node.status({
fill: "blue",
shape: "dot",
text: "Fetching measurements..."
});
// Build query obj
var reqQuery = {};
if (n.startDate) {
reqQuery.dateFrom = new Date(n.startDate).toISOString();
}
if(n.endDate) {
reqQuery.dateTo = new Date(n.endDate).toISOString();
}
if (n.pageSize) {
reqQuery.pageSize = n.pageSize;
} else {
reqQuery.pageSize = 25; // TODO: externalize default value
}
if (n.deviceId) reqQuery.source = n.deviceId;
if (n.type) reqQuery.type = n.type;
// Stringify query obj
var thisQueryString = queryString.stringify(reqQuery);
var encodedCreds = '';
if (this.config.user && this.config.password) {
var rawCreds = tenant + '/' + this.config.user + ':' + this.config.password;
var byteCreds = utf8.encode(rawCreds);
encodedCreds = base64.encode(byteCreds);
// Trim off trailing =
// if (encodedCreds[encodedCreds.length-1]== '=') {
// encodedCreds = encodedCreds.substring(0,encodedCreds.length-2);
// }
} else {
msg.error = "Missing credentials";
msg.statusCode = 403;
msg.payload = "error: Missing Credentials";
node.status({
fill: "red",
shape: "ring",
text: "Missing credentials!"
});
return node.send(msg);
}
var respBody, respStatus;
var options = {
url: "https://" + fullBase + thisQueryString,
headers: {
'Authorization': 'Basic ' + encodedCreds
}
};
var thisReq = request.get(options, function(err, resp, body) {
var nodeStatusText = "Unexpected error";
var parsedBody;
try {
parsedBody = JSON.parse(body);
} catch (parseError) {
console.error('Error parsing JSON: ' + parseError);
node.status({
fill: "red",
shape: "ring",
text: "JSON parse error"
});
return node.send(msg);
}
if (err) {
msg.payload = err.toString();
msg.statusCode = 499;
nodeStatusText = 'Error';
node.status({
fill: "red",
shape: "ring",
text: nodeStatusText
});
return node.send(msg);
} else if (!resp) {
msg.statusCode = 500;
msg.payload = "Server error: No response object";
nodeStatusText = "Server error";
node.status({
fill: "red",
shape: "ring",
text: nodeStatusText
});
return node.send(msg);
} else if (parsedBody.error) {
msg.payload = parsedBody.error;
console.error("Error: " + parsedBody.error);
node.status({
fill: "red",
shape: "ring",
text: parsedBody.error
});
return node.send(msg);
} else if (!parsedBody.measurements) {
msg.statusCode = 499;
msg.payload = "No measurements in response body";
nodeStatusText = 'No measurements in response body';
console.error('Unexpected response body: ' + body);
node.status({
fill: "red",
shape: "ring",
text: nodeStatusText
});
return node.send(msg);
} else { // no errors
msg.payload = parsedBody.measurements;
msg.statusCode = resp.statusCode || resp.status;
if (measurements && measurements.length < 1) msg.statusCode = 244;
msg.headers = resp.headers;
// Error-handling
if (body.error || resp.statusCode > 299) {
node.status({
fill: "red",
shape: "ring",
text: body.message
});
}
// Transform output
msg.payload = JSON.stringify(measurements).toString('utf8'); // txt
if (node.ret === "obj") {
try {
msg.payload = measurements;
} catch (e) {
node.warn(RED._("c8ymeasurements.errors.json-error"));
}
}
}
node.send(msg);
node.status({});
});
}); // end of on.input
} // end of c8ymeasurements fxn
// Register the Node
RED.nodes.registerType("c8y-measurements", c8yMeasurements, {
credentials: {
user: {
type: "text"
},
password: {
type: "password"
}
}
});
};