1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00
node-red-nodes/social/prowl/57-prowl.js
2016-03-01 21:10:58 +00:00

74 lines
2.6 KiB
JavaScript

/**
* Copyright 2013 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function(RED) {
"use strict";
var Prowl = require('node-prowl');
try {
var pushkeys = RED.settings.prowl || require(process.env.NODE_RED_HOME+"/../pushkey.js");
}
catch(err) { }
function ProwlNode(n) {
RED.nodes.createNode(this,n);
this.title = n.title;
this.priority = parseInt(n.priority);
if (this.priority > 2) { this.priority = 2; }
if (this.priority < -2) { this.priority = -2; }
var credentials = this.credentials;
if ((credentials) && (credentials.hasOwnProperty("pushkey"))) { this.pushkey = credentials.pushkey; }
else {
if (pushkeys) { this.pushkey = pushkeys.prowlkey; }
else { this.error("No Prowl credentials set."); }
}
this.prowl = false;
if (this.pushkey) { this.prowl = new Prowl(this.pushkey); }
var node = this;
this.on("input",function(msg) {
var titl = this.title||msg.topic||"Node-RED";
var pri = msg.priority||this.priority;
var url = this.url||msg.url;
var options = { priority:pri };
if (url) { options.url = url; }
if (typeof(msg.payload) === 'object') {
msg.payload = JSON.stringify(msg.payload);
}
else { msg.payload = msg.payload.toString(); }
if (node.pushkey) {
try {
node.prowl.push(msg.payload, titl, options, function(err, remaining) {
if (err) { node.error(err); }
node.log( remaining + ' calls to Prowl api during current hour.' );
});
}
catch (err) {
node.error(err);
}
}
else {
node.warn("Prowl credentials not set.");
}
});
}
RED.nodes.registerType("prowl",ProwlNode,{
credentials: {
pushkey: {type: "password"}
}
});
}