Skip to content

Commit

Permalink
Initial implementation of alarms-update
Browse files Browse the repository at this point in the history
  • Loading branch information
Elizabeth Southerland authored and Elizabeth Southerland committed Jul 30, 2018
1 parent 186e5f0 commit 6fc8259
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
1 change: 0 additions & 1 deletion c8y-alarms-update.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
<p>
The output message contains the following properties:
<ul>
<li><code>payload</code> is an array of all alarms</li>
<li><code>statusCode</code> is the status code of the response, or the error code if the request could not be completed</li>
<li><code>headers</code> is an object containing the response headers</li>
</ul>
Expand Down
33 changes: 24 additions & 9 deletions c8y-alarms-update.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,13 @@ module.exports = function(RED) {
});

// Build PUT body based on node input
var putBody = {};
var reqBody = {};

// Initialize msg object
if (!msg.payload) msg.payload = {};
if (!msg.statusCode) msg.statusCode = 400;

console.log('Made it to here');

if ((n.alarmStatus == 'noChange' || n.alarmStatus == '') && (n.severity == 'noChange' || n.severity == '')) {
console.log('Detected no changes');
msg.statusCode = 244;
msg.payload = {"message":"No changes detected in input body"};
// Return with soft-error
Expand All @@ -55,10 +52,8 @@ module.exports = function(RED) {

} else {

console.log('Detected a change:\n > alarmStatus: ' + n.alarmStatus + '\n > severity: ' + n.severity);

if (n.alarmStatus != 'noChange') putBody.status = n.alarmStatus;
if (n.severity != 'noChange') putBody.severity = n.severity;
if (n.alarmStatus != 'noChange') reqBody.status = n.alarmStatus;
if (n.severity != 'noChange') reqBody.severity = n.severity;

// Build auth header
var encodedCreds = '';
Expand All @@ -85,13 +80,33 @@ module.exports = function(RED) {

// Build request
var options = {
method: 'PUT',
url: "https://" + domain + basePath + n.alarmId,
headers: {
'Authorization': 'Basic ' + encodedCreds
}
},
json: reqBody
};

// Send request
request(options, function(err, response, body) {
if (err) {
console.log('Error: ' + err);
msg.error = err;
msg.statusCode = resp.statusCode || resp.status;
node.status({
fill: "red",
shape: "ring",
text: err.toString()
});
return node.send(msg);
} else {
msg.statusCode = 200;
msg.payload = {"message": "Alarm updated successfully"};
node.status({});
return node.send(msg);
}
});

// Send Node response

Expand Down

0 comments on commit 6fc8259

Please sign in to comment.