-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathc8y-alarms.js
209 lines (177 loc) · 4.94 KB
/
c8y-alarms.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
199
200
201
202
203
204
205
206
207
208
209
// 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 = '/alarm/alarms?'; // this is a constant, dependent on c8y
module.exports = function(RED) {
function c8yAlarms(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 alarms..."
});
// 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
}
switch (n.severity) {
case "critical":
reqQuery.severity = "CRITICAL";
break;
case "major":
reqQuery.severity = "MAJOR";
break;
case "minor":
reqQuery.severity = "MINOR";
break;
case "warning":
reqQuery.severity = "WARNING";
break;
default: // all alarms, don't filter (do nothing)
break;
}
switch (n.alarmStatus) {
case "active":
reqQuery.status = "ACTIVE";
break;
case "acknowledged":
reqQuery.status = "ACKNOWLEDGED";
break;
case "cleared":
reqQuery.status = "CLEARED";
break;
default: // all alarms, don't filter (do nothing)
break;
}
if (n.deviceId) reqQuery.source = n.deviceId;
// 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 = {};
if (body) {
try {
parsedBody = JSON.parse(body);
} catch (e) {
msg.payload = e;
msg.statusCode = 499;
nodeStatusText = 'Error parsing response';
node.status({
fill: "red",
shape: "ring",
text: nodeStatusText
});
return node.send(msg);
}
}
if (err || !resp || parsedBody.error) {
if (err) {
msg.payload = err.toString();
msg.statusCode = 499;
nodeStatusText = 'Error';
} else if (!resp) {
msg.statusCode = 500;
msg.payload = "Server error: No response object";
nodeStatusText = "Server error";
} else if (parsedBody.error) {
msg.payload = parsedBody;
msg.statusCode = 499;
nodeStatusText = JSON.parse(body).error;
}
node.status({
fill: "red",
shape: "ring",
text: nodeStatusText
});
return node.send(msg);
} else {
var alarms = parsedBody.alarms;
msg.payload = alarms;
msg.statusCode = resp.statusCode || resp.status;
if (!alarms) {
msg.payload = parsedBody;
msg.statusCode = 499;
nodeStatusText = "Body did not contain alarms attribute";
node.status({
fill: "red",
shape: "ring",
text: nodeStatusText
});
} else {
if (alarms.length < 1) msg.statusCode = 244;
msg.headers = resp.headers;
// Transform output
if (node.ret !== "bin") {
msg.payload = JSON.stringify(alarms).toString('utf8'); // txt
if (node.ret === "obj") {
try {
msg.payload = alarms;
} catch (e) {
node.warn(RED._("c8yalarms.errors.json-error"));
}
}
}
node.send(msg);
node.status({});
} // body contained alarms
}
});
}); // end of on.input
} // end of c8yalarms fxn
// Register the Node
RED.nodes.registerType("c8y-alarms", c8yAlarms, {});
};