-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.js
219 lines (189 loc) · 8.12 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
*
* ioBroker pushsafer Adapter
*
* (c) 2016-2020 bluefox
* (c) 2019 appzer (pushsafer.com)
*
* MIT License
*
*/
/* jshint -W097 */
/* jshint strict: false */
/* jslint node: true */
'use strict';
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
const Pushsafer = require('pushsafer-notifications');
const fs = require('fs');
const adapterName = require('./package.json').name.split('.').pop();
let pushsafer;
let lastMessageTime = 0;
let lastMessageText = '';
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {name: adapterName});
adapter = new utils.Adapter(options);
adapter.on('message', obj => obj && obj.command === 'send' && obj.message && processMessage(obj.message, obj));
adapter.on('ready', () => main());
return adapter;
}
function processMessage(message, obj) {
// filter out double messages
const json = JSON.stringify(message);
if (lastMessageTime && lastMessageText === JSON.stringify(message) && new Date().getTime() - lastMessageTime < 1000) {
return adapter.log.debug('Filter out double message [first was for ' + (new Date().getTime() - lastMessageTime) + 'ms]: ' + json);
}
lastMessageTime = new Date().getTime();
lastMessageText = json;
sendNotification(message, error =>
obj && obj.callback && adapter.sendTo(obj.from, obj.command, {error}, obj.callback));
}
function main() {
// Adapter is listening on messages
}
function sendNotification(message, callback) {
if (!message) message = {};
let push;
let isDelete = false;
if (message.token) {
isDelete = true;
push = new Pushsafer({
k: message.token,
debug: process.argv[3] === 'debug'
});
} else if (!pushsafer) {
if (adapter.config.token) {
pushsafer = new Pushsafer({
k: adapter.config.token,
debug: process.argv[3] === 'debug'
});
push = pushsafer;
} else {
adapter.log.error('Cannot send notification while not configured');
return callback && callback('Cannot send notification while not configured');
}
} else {
push = pushsafer;
}
if (!push) {
return;
}
if (typeof message !== 'object') {
message = {message: message};
}
message.m = message.message || message.m || '';
message.t = message.title || message.t || adapter.config.title;
message.s = message.sound || message.s || (adapter.config.sound ? adapter.config.sound : undefined);
message.i = message.icon || message.i || adapter.config.icon;
message.d = message.device || message.d || adapter.config.device;
message.pr = message.priority || message.pr || adapter.config.priority;
message.re = message.retry || message.re || adapter.config.retry;
message.ex = message.expire || message.ex || adapter.config.expire;
message.a = message.answer || message.a || adapter.config.answer;
message.v = message.vibration || message.v || adapter.config.vibration;
message.l = message.time2live || message.l || adapter.config.time2live;
message.c = message.color || message.c || adapter.config.color || undefined;
if (message.url) {
message.u = message.url;
}
if (message.urlTitle) {
message.ut = message.urlTitle;
}
let data;
let parts;
if (message.picture) {
if (message.picture.substring(0, 5) !== 'data:') {
try {
data = Buffer.from(fs.readFileSync(message.picture)).toString('base64');
parts = message.picture.split('.');
message.p = 'data:image/' + parts.pop().toLowerCase() + ';base64,' + data;
} catch (e) {
adapter.log.error('Cannot read image "' + message.picture + '": '+ e);
}
} else {
message.p = message.picture;
}
}
if (message.picture2) {
if (message.picture2.substring(0, 5) !== 'data:') {
try {
data = Buffer.from(fs.readFileSync(message.picture2)).toString('base64');
parts = message.picture2.split('.');
message.p2 = 'data:image/' + parts.pop().toLowerCase() + ';base64,' + data;
} catch (e) {
adapter.log.error('Cannot read image "' + message.picture2 + '": '+ e);
}
} else {
message.p2 = message.picture2;
}
}
if (message.picture3) {
if (message.picture3.substring(0, 5) !== 'data:') {
try {
data = Buffer.from(fs.readFileSync(message.picture3)).toString('base64');
parts = message.picture3.split('.');
message.p3 = 'data:image/' + parts.pop().toLowerCase() + ';base64,' + data;
} catch (e) {
adapter.log.error('Cannot read image "' + message.picture3 + '": '+ e);
}
} else {
message.p3 = message.picture3;
}
}
if (message.message !== undefined) delete message.message;
if (message.title !== undefined) delete message.title;
if (message.sound !== undefined) delete message.sound;
if (message.icon !== undefined) delete message.icon;
if (message.device !== undefined) delete message.device;
if (message.vibration !== undefined) delete message.vibration;
if (message.priorty !== undefined) delete message.priorty;
if (message.retry !== undefined) delete message.retry;
if (message.expire !== undefined) delete message.expire;
if (message.answer !== undefined) delete message.answer;
if (message.picture !== undefined) delete message.picture;
if (message.picture2 !== undefined) delete message.picture2;
if (message.picture3 !== undefined) delete message.picture3;
if (message.url !== undefined) delete message.url;
if (message.urlTitle !== undefined) delete message.urlTitle;
if (message.time2live !== undefined) delete message.time2live;
if (message.token !== undefined) delete message.token;
if (message.color !== undefined) delete message.color;
adapter.log.debug('Send pushsafer notification: ' + message.m);
if (message.s !== null && message.s !== undefined) message.s = message.s.toString();
if (message.i !== null && message.i !== undefined) message.i = message.i.toString();
if (message.d !== null && message.d !== undefined) message.d = message.d.toString();
if (message.v !== null && message.v !== undefined) message.v = message.v.toString();
if (message.re !== null && message.re !== undefined) message.re = message.re.toString();
if (message.ex !== null && message.ex !== undefined) message.ex = message.ex.toString();
if (message.pr !== null && message.pr !== undefined) message.pr = message.pr.toString();
if (message.a !== null && message.a !== undefined) message.a = message.a.toString();
if (message.l !== null && message.l !== undefined) message.l = message.l.toString();
if (message.l !== null && message.l !== undefined) message.l = message.l.toString();
if (message.c !== null && message.c !== undefined) message.c = message.c.toString();
push.send(message, (err, result) => {
if (isDelete) {
setTimeout(() => push = null, 500);
}
try {
result = JSON.parse(result);
} catch (e) {
adapter.log.error('Cannot parse answer: ' + result);
}
if (err || result.error) {
adapter.log.error('Cannot send notification: ' + JSON.stringify(err || result.error));
callback && callback(err || result.error);
return false;
} else {
callback && callback();
return true;
}
});
}
// If started as allInOne/compact mode => return function to create instance
if (module && module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}