2014-04-20 19:01:38 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var nma = require('nma');
|
2014-04-20 19:01:38 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
function NMANode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.title = n.title;
|
2015-02-06 22:10:14 +01:00
|
|
|
var credentials = this.credentials;
|
2014-06-29 00:34:59 +02:00
|
|
|
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);
|
2014-04-20 19:01:38 +02:00
|
|
|
}
|
2015-12-17 18:15:43 +01:00
|
|
|
else {
|
|
|
|
msg.payload = msg.payload.toString();
|
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
if (node.pushkey) {
|
2015-05-11 20:25:39 +02:00
|
|
|
nma({
|
2014-12-13 00:15:09 +01:00
|
|
|
"apikey": node.pushkey,
|
|
|
|
"application": "Node-RED",
|
|
|
|
"event": titl,
|
|
|
|
"description": msg.payload,
|
|
|
|
"priority": 0
|
|
|
|
}, function (error) {
|
|
|
|
if (error) {
|
2015-12-17 18:15:43 +01:00
|
|
|
node.error("NMA error: " + error,msg);
|
2014-12-13 00:15:09 +01:00
|
|
|
}
|
|
|
|
});
|
2014-06-29 00:34:59 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("NMA credentials not set.");
|
|
|
|
}
|
|
|
|
});
|
2014-04-20 19:01:38 +02:00
|
|
|
}
|
|
|
|
|
2015-02-06 22:10:14 +01:00
|
|
|
RED.nodes.registerType("nma",NMANode, {
|
|
|
|
credentials: {
|
|
|
|
pushkey: {type: "password"}
|
2014-04-20 19:01:38 +02:00
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
|
|
|
}
|