-
Notifications
You must be signed in to change notification settings - Fork 591
/
57-nma.js
45 lines (42 loc) · 1.37 KB
/
57-nma.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
module.exports = function(RED) {
"use strict";
var nma = require('nma');
function NMANode(n) {
RED.nodes.createNode(this,n);
this.title = n.title;
var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; }
else { this.error("No NMA API key set"); }
var node = this;
this.on("input",function(msg) {
var titl = this.title||msg.topic||"Node-RED";
if (typeof(msg.payload) === 'object') {
msg.payload = JSON.stringify(msg.payload);
}
else {
msg.payload = msg.payload.toString();
}
if (node.pushkey) {
nma({
"apikey": node.pushkey,
"application": "Node-RED",
"event": titl,
"description": msg.payload,
"priority": 0
}, function (error) {
if (error) {
node.error("NMA error: " + error,msg);
}
});
}
else {
node.warn("NMA credentials not set.");
}
});
}
RED.nodes.registerType("nma",NMANode, {
credentials: {
pushkey: {type: "password"}
}
});
}