2018-09-25 10:24:46 +08:00
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* yunkong2 pushover Adapter
|
|
|
|
|
*
|
|
|
|
|
* (c) 2014-2018 bluefox
|
|
|
|
|
*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* jshint -W097 */// jshint strict:false
|
|
|
|
|
/*jslint node: true */
|
|
|
|
|
'use strict';
|
|
|
|
|
const utils = require(__dirname + '/lib/utils'); // Get common adapter utils
|
|
|
|
|
const Pushover = require('pushover-notifications');
|
|
|
|
|
|
2018-09-29 11:06:29 +08:00
|
|
|
|
const request = require('request');
|
2018-09-25 12:53:08 +08:00
|
|
|
|
|
2018-09-25 10:24:46 +08:00
|
|
|
|
const adapter = new utils.Adapter('pushover');
|
|
|
|
|
|
|
|
|
|
adapter.on('message', obj => {
|
|
|
|
|
if (obj && obj.command === 'send') {
|
|
|
|
|
processMessage(obj);
|
|
|
|
|
}
|
|
|
|
|
processMessages();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
adapter.on('ready', main);
|
|
|
|
|
|
|
|
|
|
let stopTimer = null;
|
|
|
|
|
let pushover;
|
|
|
|
|
let lastMessageTime = 0;
|
|
|
|
|
let lastMessageText = '';
|
|
|
|
|
|
|
|
|
|
// Terminate adapter after 30 seconds idle
|
|
|
|
|
function stop() {
|
|
|
|
|
if (stopTimer) {
|
|
|
|
|
clearTimeout(stopTimer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Stop only if subscribe mode
|
|
|
|
|
if (adapter.common && adapter.common.mode === 'subscribe') {
|
|
|
|
|
stopTimer = setTimeout(() => {
|
|
|
|
|
stopTimer = null;
|
|
|
|
|
adapter.stop();
|
|
|
|
|
}, 30000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function processMessage(obj) {
|
|
|
|
|
if (!obj || !obj.message) return;
|
|
|
|
|
|
|
|
|
|
// filter out double messages
|
|
|
|
|
const json = JSON.stringify(obj.message);
|
|
|
|
|
if (lastMessageTime && lastMessageText === JSON.stringify(obj.message) && new Date().getTime() - lastMessageTime < 1000) {
|
|
|
|
|
adapter.log.debug('Filter out double message [first was for ' + (new Date().getTime() - lastMessageTime) + 'ms]: ' + json);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
lastMessageTime = new Date().getTime();
|
|
|
|
|
lastMessageText = json;
|
|
|
|
|
|
|
|
|
|
if (stopTimer) clearTimeout(stopTimer);
|
|
|
|
|
|
|
|
|
|
sendNotification(obj.message, (err, response) => {
|
|
|
|
|
if (obj.callback) adapter.sendTo(obj.from, 'send', { error: err, response: response}, obj.callback);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function processMessages() {
|
|
|
|
|
adapter.getMessage((err, obj) => {
|
|
|
|
|
if (obj) {
|
|
|
|
|
processMessage(obj);
|
|
|
|
|
processMessages();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-27 12:19:21 +08:00
|
|
|
|
function createObject() {
|
|
|
|
|
//adapter.setObject('config.0.voltage.50002', json, function (err) {
|
|
|
|
|
// if (err) log('Cannot write object: ' + err);
|
|
|
|
|
//});
|
|
|
|
|
adapter.setObject('config.0.voltage.50002', {
|
|
|
|
|
custom: {
|
|
|
|
|
min: 320,
|
|
|
|
|
max: 400,
|
|
|
|
|
interval: 5,
|
|
|
|
|
count: 2
|
|
|
|
|
},
|
|
|
|
|
common: {
|
|
|
|
|
name: "50002",
|
|
|
|
|
role: "",
|
|
|
|
|
type: "array",
|
|
|
|
|
desc: "pushover创建",
|
|
|
|
|
read: true,
|
|
|
|
|
write: true
|
|
|
|
|
}
|
|
|
|
|
}, function () {
|
|
|
|
|
adapter.log.info('Save ' + userdataDir + 'pushover.json');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-25 10:24:46 +08:00
|
|
|
|
function main() {
|
|
|
|
|
// Adapter is started only if some one writes into "system.adapter.pushover.X.messagebox" new value
|
2018-09-29 11:06:29 +08:00
|
|
|
|
//createObject();
|
2018-09-25 10:24:46 +08:00
|
|
|
|
processMessages();
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function sendNotification(message, callback) {
|
|
|
|
|
if (!message) message = {};
|
|
|
|
|
|
|
|
|
|
if (!pushover) {
|
|
|
|
|
if (adapter.config.user && adapter.config.token) {
|
|
|
|
|
pushover = new Pushover({
|
|
|
|
|
user: adapter.config.user,
|
|
|
|
|
token: adapter.config.token
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
adapter.log.error('Cannot send notification while not configured');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!pushover) return;
|
|
|
|
|
|
|
|
|
|
if (typeof message !== 'object') {
|
|
|
|
|
message = {message: message};
|
|
|
|
|
}
|
|
|
|
|
if (message.hasOwnProperty('token')) {
|
|
|
|
|
pushover.token = message.token
|
|
|
|
|
} else {
|
|
|
|
|
pushover.token = adapter.config.token
|
|
|
|
|
}
|
2018-09-25 12:53:08 +08:00
|
|
|
|
|
2018-09-25 10:24:46 +08:00
|
|
|
|
message.title = message.title || adapter.config.title;
|
|
|
|
|
message.sound = message.sound || (adapter.config.sound ? adapter.config.sound : undefined);
|
|
|
|
|
message.priority = message.priority || adapter.config.priority;
|
|
|
|
|
message.url = message.url || adapter.config.url;
|
|
|
|
|
message.url_title = message.url_title || adapter.config.url_title;
|
|
|
|
|
message.device = message.device || adapter.config.device;
|
|
|
|
|
message.message = message.message || '';
|
|
|
|
|
|
|
|
|
|
// if timestamp in ms => make seconds // if greater than 2000.01.01 00:00:00
|
|
|
|
|
if (message.timestamp && message.timestamp > 946681200000) {
|
|
|
|
|
message.timestamp = Math.round(message.timestamp / 1000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// mandatory parameters if priority is high (2)
|
|
|
|
|
if (message.priority === 2) {
|
|
|
|
|
message.retry = parseInt(message.retry, 10) || 60;
|
|
|
|
|
message.expire = parseInt(message.expire, 10) || 3600;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
adapter.log.info('Send pushover notification: ' + JSON.stringify(message));
|
2018-09-29 11:23:01 +08:00
|
|
|
|
adapter.log.info('adapter.config.smsUrl:' + adapter.config.smsUrl);
|
2018-09-25 12:53:08 +08:00
|
|
|
|
|
|
|
|
|
request(adapter.config.smsUrl, function (error, response, body) {
|
|
|
|
|
|
2018-09-29 11:23:01 +08:00
|
|
|
|
adapter.log.info('response.statusCode:' + response.statusCode);
|
|
|
|
|
adapter.log.info('body:' + body);
|
|
|
|
|
|
2018-09-25 12:53:08 +08:00
|
|
|
|
if (error || response.statusCode !== 200) {
|
|
|
|
|
adapter.log.error(error || response.statusCode);
|
|
|
|
|
} else {
|
|
|
|
|
// try to parse answer
|
|
|
|
|
try {
|
|
|
|
|
var data = JSON.parse(body);
|
|
|
|
|
// do something with data
|
|
|
|
|
adapter.log.info(JSON.parse(data));
|
|
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
|
adapter.log.error('Cannot parse answer');
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
2018-09-25 10:24:46 +08:00
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
pushover.send(message, (err, result) => {
|
|
|
|
|
if (err) {
|
|
|
|
|
adapter.log.error('Cannot send notification: ' + JSON.stringify(err));
|
|
|
|
|
if (callback) callback(err);
|
|
|
|
|
return false;
|
|
|
|
|
} else {
|
|
|
|
|
if (callback) callback(null, result);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
*/
|
|
|
|
|
}
|