266 lines
7.7 KiB
JavaScript
266 lines
7.7 KiB
JavaScript
/**
|
||
*
|
||
* yunkong2 pushover Adapter
|
||
*
|
||
* (c) 2014-2018 bluefox
|
||
*
|
||
* MIT License
|
||
*
|
||
*/
|
||
|
||
/* jshint -W097 */// jshint strict:false
|
||
/*jslint node: true */
|
||
'use strict';
|
||
|
||
var md5=require("md5")
|
||
|
||
const utils = require(__dirname + '/lib/utils'); // Get common adapter utils
|
||
const Pushover = require('pushover-notifications');
|
||
|
||
const request = require('request');
|
||
|
||
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();
|
||
}
|
||
});
|
||
}
|
||
|
||
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');
|
||
});
|
||
|
||
}
|
||
|
||
function main() {
|
||
// Adapter is started only if some one writes into "system.adapter.pushover.X.messagebox" new value
|
||
//createObject();
|
||
processMessages();
|
||
stop();
|
||
}
|
||
|
||
function sendNotification(message, callback) {
|
||
if (!message) message = {};
|
||
|
||
if (!pushover) {
|
||
if (adapter.config.user_name && adapter.config.token) {
|
||
pushover = new Pushover({
|
||
user: adapter.config.user_name,
|
||
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
|
||
}
|
||
|
||
//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));
|
||
adapter.log.info('adapter.config.smsUrl:' + adapter.config.smsUrl);
|
||
adapter.log.info('adapter.config.smsLoginUrl:' + adapter.config.smsLoginUrl);
|
||
|
||
var mobiles = message.phone;
|
||
var content = message.message;
|
||
smsLogin(mobiles,content);
|
||
|
||
/*
|
||
request(adapter.config.smsUrl, function (error, response, body) {
|
||
|
||
adapter.log.info('response.statusCode:' + response.statusCode);
|
||
adapter.log.info('body:' + body);
|
||
|
||
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');
|
||
}
|
||
}
|
||
});
|
||
*/
|
||
|
||
|
||
/*
|
||
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;
|
||
}
|
||
});
|
||
*/
|
||
}
|
||
|
||
|
||
|
||
// 短信发送登录
|
||
function smsLogin(mobiles,content)
|
||
{
|
||
var url = "http://mas.ecloud.10086.cn/app/http/authorize?ec_name=%E5%8D%8E%E7%BF%94%E7%BF%94%E8%83%BD%EF%BC%88%E6%B9%96%E5%8D%97%EF%BC%89%E8%83%BD%E6%BA%90%E7%A7%91%E6%8A%80%E6%9C%89%E9%99%90%E5%85%AC%E5%8F%B8&user_name=admin&user_passwd=bdDm@648";
|
||
|
||
console.log('url:' + url);
|
||
|
||
request(url, function (error, response, body) {
|
||
adapter.log.info('error:' + error);
|
||
adapter.log.info('statusCode:' + response && response.statusCode);
|
||
adapter.log.info('body:' + body);
|
||
|
||
if (!error && response.statusCode == 200) {
|
||
// 请求成功的处理逻辑
|
||
|
||
try {
|
||
var data = JSON.parse(body);
|
||
// do something with data
|
||
adapter.log.info(data.mas_user_id);
|
||
adapter.log.info(data.access_token);
|
||
|
||
var sign = 'uEbOVoPZ';
|
||
smsMess(data.mas_user_id, mobiles, content, sign, '', data.access_token);
|
||
} catch (e) {
|
||
adapter.log.info(e);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
|
||
//发送短信
|
||
function smsMess(mas_user_id,mobiles,content,sign,serial,access_token) {
|
||
// API输入参数签名结果,签名算法:将mas_user_id,mobiles,content,sign,serial,access_token按照顺序拼接,然后通过MD5+HEX计算后得出的值
|
||
var mac = mas_user_id + mobiles + content + sign + serial + access_token;
|
||
adapter.log.info('mac:' + mac);
|
||
|
||
mac = md5(mac);
|
||
adapter.log.info('mac-md5:' + mac);
|
||
|
||
mac = mac.toUpperCase(); //再把密文中的英文母全部转为大写
|
||
adapter.log.info('mac-upper:' + mac);
|
||
|
||
var form = {
|
||
mas_user_id:mas_user_id,
|
||
mobiles:mobiles,
|
||
content:content,
|
||
sign:sign,
|
||
serial:serial,
|
||
mac:mac
|
||
};
|
||
|
||
var url = 'http://mas.ecloud.10086.cn/app/http/sendSms';
|
||
request.post({url:url, form:form}, function(error, response, body) {
|
||
if (!error && response.statusCode == 200) {
|
||
adapter.log.info(body);
|
||
}
|
||
})
|
||
|
||
}
|
||
|