2014-04-20 19:00:43 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var PushOver = require('pushover-notifications');
|
|
|
|
var util = require('util');
|
2018-12-13 10:33:50 +01:00
|
|
|
var fs = require('fs');
|
2014-04-20 19:00:43 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
function PushoverNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.title = n.title;
|
2015-08-25 22:04:54 +02:00
|
|
|
this.device = n.device;
|
2014-06-29 00:34:59 +02:00
|
|
|
this.priority = n.priority;
|
2015-12-23 13:42:22 +01:00
|
|
|
this.sound = n.sound;
|
2018-08-13 12:17:51 +02:00
|
|
|
this.html = n.html;
|
2015-12-23 13:42:22 +01:00
|
|
|
if (this.sound === '') { this.sound = null; }
|
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 Pushover api token set"); }
|
|
|
|
if ((credentials) && (credentials.hasOwnProperty("deviceid"))) { this.deviceid = credentials.deviceid; }
|
|
|
|
else { this.error("No Pushover user key set"); }
|
|
|
|
var pusher = false;
|
|
|
|
if (this.pushkey && this.deviceid) {
|
|
|
|
pusher = new PushOver({
|
|
|
|
user: this.deviceid,
|
|
|
|
token: this.pushkey,
|
|
|
|
onerror: function(err) {
|
|
|
|
util.log('[57-pushover.js] Error: '+err);
|
|
|
|
}
|
2014-04-20 19:00:43 +02:00
|
|
|
});
|
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
var node = this;
|
2014-04-20 19:00:43 +02:00
|
|
|
|
2014-06-29 00:34:59 +02:00
|
|
|
this.on("input",function(msg) {
|
2018-12-13 10:33:50 +01:00
|
|
|
var title = node.title || msg.topic || "Node-RED";
|
|
|
|
var pri = node.priority || msg.priority || 0;
|
|
|
|
var dev = node.device || msg.device;
|
|
|
|
var sound = node.sound || msg.sound || null;
|
|
|
|
var url = node.url || msg.url || null;
|
|
|
|
var url_title = node.url_title || msg.url_title || null;
|
|
|
|
var html = node.html || false;
|
|
|
|
var attachment = msg.attachment || null;
|
2014-06-29 00:34:59 +02:00
|
|
|
if (isNaN(pri)) {pri=0;}
|
|
|
|
if (pri > 2) {pri = 2;}
|
2015-08-15 21:00:40 +02:00
|
|
|
if (pri < -2) {pri = -2;}
|
2014-06-29 00:34:59 +02:00
|
|
|
if (typeof(msg.payload) === 'object') {
|
|
|
|
msg.payload = JSON.stringify(msg.payload);
|
|
|
|
}
|
|
|
|
else { msg.payload = msg.payload.toString(); }
|
|
|
|
if (pusher) {
|
|
|
|
var pushmsg = {
|
|
|
|
message: msg.payload,
|
2018-12-13 10:33:50 +01:00
|
|
|
title: title,
|
2014-06-29 00:34:59 +02:00
|
|
|
priority: pri,
|
|
|
|
retry: 30,
|
2018-08-13 12:17:51 +02:00
|
|
|
expire: 600,
|
|
|
|
html: html
|
2014-06-29 00:34:59 +02:00
|
|
|
};
|
2015-08-25 22:04:54 +02:00
|
|
|
if (dev) { pushmsg.device = dev; }
|
2015-12-23 11:11:04 +01:00
|
|
|
if (typeof(sound) === 'string') { pushmsg.sound = sound; }
|
2016-10-17 23:56:24 +02:00
|
|
|
if (typeof(url) === 'string') { pushmsg.url = url; }
|
|
|
|
if (typeof(url_title) === 'string') { pushmsg.url_title = url_title; }
|
2018-08-13 12:17:51 +02:00
|
|
|
if (html) { pushmsg.html = 1; }
|
2018-12-13 10:33:50 +01:00
|
|
|
if (typeof(attachment) === 'string') {
|
|
|
|
// Treat attachment as a path
|
|
|
|
fs.readFile(attachment,function(err, data) {
|
|
|
|
if (err) {
|
|
|
|
node.error("[57-pushover.js] Error: File Read Error: "+err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pushmsg.file = { data: data };
|
|
|
|
pushMessage(pushmsg);
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (attachment instanceof Buffer) {
|
|
|
|
// Is it base64 encoded or binary?
|
|
|
|
var attachmentString = attachment.toString();
|
|
|
|
var attachmentBuffer = Buffer.from(attachmentString,'base64');
|
2019-08-11 14:37:15 +02:00
|
|
|
if (attachmentString === attachmentBuffer.toString('base64')) {
|
2018-12-13 10:33:50 +01:00
|
|
|
// If converts back to same, then it was base64 so set to binary
|
|
|
|
// https://stackoverflow.com/a/48770228
|
|
|
|
attachment = attachmentBuffer;
|
|
|
|
}
|
|
|
|
// Unset these temporary values
|
|
|
|
attachmentBuffer = attachmentString = undefined;
|
|
|
|
// attach the buffer
|
|
|
|
pushmsg.file = { data: attachment };
|
|
|
|
}
|
|
|
|
else if (attachment) {
|
|
|
|
node.error("[57-pushover.js] Error: attachment property must be a path to a local file or a Buffer containing an image");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
pushMessage(pushmsg);
|
2014-06-29 00:34:59 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("Pushover credentials not set.");
|
|
|
|
}
|
|
|
|
});
|
2018-12-13 10:33:50 +01:00
|
|
|
|
|
|
|
function pushMessage(pushmsg) {
|
|
|
|
pusher.send( pushmsg, function(err, response) {
|
2019-06-18 09:52:55 +02:00
|
|
|
if (err) { node.error(err); }
|
|
|
|
else {
|
2019-09-08 16:30:49 +02:00
|
|
|
try {
|
|
|
|
response = JSON.parse(response);
|
|
|
|
if (response.status !== 1) { node.error("[57-pushover.js] Error: "+response); }
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
node.error("[57-pushover.js] Error: "+response);
|
|
|
|
}
|
2019-06-18 09:52:55 +02:00
|
|
|
}
|
2018-12-13 10:33:50 +01:00
|
|
|
});
|
|
|
|
}
|
2014-04-20 19:00:43 +02:00
|
|
|
}
|
2015-02-06 22:10:14 +01:00
|
|
|
RED.nodes.registerType("pushover",PushoverNode,{
|
|
|
|
credentials: {
|
|
|
|
deviceid: {type:"text"},
|
|
|
|
pushkey: {type: "password"}
|
2015-08-15 21:00:40 +02:00
|
|
|
}
|
2014-06-29 00:34:59 +02:00
|
|
|
});
|
|
|
|
}
|