yunkong2.pushover/main.js

266 lines
7.7 KiB
JavaScript
Raw Normal View History

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';
2018-09-29 17:48:37 +08:00
var md5=require("md5")
2018-09-25 10:24:46 +08:00
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) {
2018-09-29 15:20:10 +08:00
if (adapter.config.user_name && adapter.config.token) {
2018-09-25 10:24:46 +08:00
pushover = new Pushover({
2018-09-29 15:20:10 +08:00
user: adapter.config.user_name,
2018-09-25 10:24:46 +08:00
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-29 18:03:22 +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 || '';
2018-09-25 10:24:46 +08:00
// 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-29 14:59:49 +08:00
adapter.log.info('adapter.config.smsLoginUrl' + adapter.config.smsLoginUrl);
2018-09-25 12:53:08 +08:00
2018-09-29 18:03:22 +08:00
var mobiles = message.phone;
var content = message.message;
2018-09-29 17:43:56 +08:00
smsLogin(mobiles,content);
/*
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-29 17:43:56 +08:00
*/
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;
}
});
*/
}
2018-09-29 17:38:33 +08:00
// 短信发送登录
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) {
2018-09-29 17:46:38 +08:00
adapter.log.info(e);
2018-09-29 17:38:33 +08:00
}
}
});
}
//发送短信
function smsMess(mas_user_id,mobiles,content,sign,serial,access_token) {
// API输入参数签名结果签名算法将mas_user_idmobilescontentsignserial,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);
}
})
}