-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
278 lines (246 loc) · 9.42 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
const utils = require('@iobroker/adapter-core'); // Get common adapter utils
let PushBullet;
const adapterName = require('./package.json').name.split('.').pop();
import('pushbullet')
.then(result => PushBullet = result.default);
// you have to call the adapter function and pass an option object
// name has to be set and has to be equal to adapters folder name and main file name excluding extension
// adapter will be restarted automatically every time as the configuration changed, e.g system.adapter.example.0
let receiver;
let pusher;
let stream;
let tsHistory;
let myIden;
let adapter;
function startAdapter(options) {
options = options || {};
Object.assign(options, {name: adapterName});
adapter = new utils.Adapter(options);
// is called when databases are connected and adapter received configuration.
// start here!
adapter.on('ready', () => main());
// is called when adapter shuts down - callback has to be called under any circumstances!
adapter.on('unload', callback => {
try {
stream && stream.close();
adapter.log.info('cleaned everything up...');
callback();
} catch (e) {
callback();
}
});
// Some message was sent to adapter instance over message box. Used by email, pushover, text2speech, ...
adapter.on('message', async obj => {
adapter.log.debug('Message received');
await push(obj.message);
});
}
async function push(msg) {
let index;
let aryReceiver;
adapter.log.debug('Push message');
if (typeof msg !== 'object') {
msg = {message: msg, type: 'note', title: '[ioBroker]'};
}
msg.title = msg.hasOwnProperty('title') ? msg.title : '[ioBroker]';
if (typeof msg.receiver === 'undefined' || msg.receiver === null) {
if (receiver.includes(',')) {
aryReceiver = receiver.split(',')
} else {
aryReceiver = [receiver];
}
} else {
if (msg.receiver.includes(',')) {
aryReceiver = msg.receiver.split(',')
} else {
aryReceiver = [msg.receiver];
}
}
adapter.log.info(msg.type);
for (index = 0; index < aryReceiver.length; index++) {
switch (msg.type) {
case 'note':
try {
await pusher.note(aryReceiver[index], msg.title, msg.message);
} catch (error) {
adapter.log.warn(`Pushbullet error: ${error.message}`);
}
break;
case 'file':
try {
await pusher.file(aryReceiver[index], msg.file, msg.title);
} catch (error) {
adapter.log.warn(`Pushbullet error: ${error.message}`);
}
break;
case 'link':
try {
await pusher.link(aryReceiver[index], msg.title, msg.link);
} catch (error) {
adapter.log.warn(`Pushbullet error: ${error.message}`);
}
break;
default:
try {
await pusher.note(aryReceiver[index], msg.title, msg.message);
} catch (error) {
adapter.log.warn(`Pushbullet error: ${error.message}`);
}
break;
}
}
}
async function handleTickle(tickleMsg) {
if (pusher && tickleMsg.subtype === 'push') {
adapter.log.debug(`Pushbullet DEBUG - handleTickle : ${tickleMsg.subtype}`);
try {
const response = await pusher.history({ modified_after: tsHistory });
const history = await response.json();
for (let i = 0; i < history.pushes.length; i++) {
await pushMsg(history.pushes[i]);
}
tsHistory = history.pushes.length ? history.pushes[0].modified : 0;
} catch (err) {
adapter.log.info('Unable to get history.');
tsHistory = 0;
}
}
}
async function pushMsg(incoming) {
if (incoming.target_device_iden && incoming.target_device_iden !== myIden) {
adapter.log.debug(`Receiver: ${incoming.target_device_iden}`);
adapter.log.debug(`My ID: ${myIden}`);
return;
}
const msg = {
pushtype: incoming.type,
data: incoming,
};
adapter.log.debug(`pushMsg: ${incoming.type}`);
if (incoming.dismissed === true) {
msg.pushtype = 'dismissal';
msg.topic = 'Push dismissed';
msg.payload = incoming.iden;
} else if (incoming.active === false && incoming.type === undefined) {
msg.pushtype = 'delete';
msg.topic = 'Push deleted';
msg.payload = incoming.iden;
} else if (incoming.type === 'clip') {
msg.topic = 'Clipboard content';
msg.payload = incoming.body;
} else if (incoming.type === 'note') {
msg.topic = incoming.title;
msg.payload = incoming.body;
} else if (incoming.type === 'link') {
msg.topic = incoming.title;
msg.payload = incoming.url;
msg.message = incoming.body;
} else if (incoming.type === 'address') {
msg.topic = incoming.name;
msg.payload = incoming.address;
} else if (incoming.type === 'list') {
msg.topic = incoming.title;
msg.payload = incoming.items;
} else if (incoming.type === 'file') {
msg.topic = incoming.file_name;
msg.payload = incoming.file_url;
msg.message = incoming.body;
} else if (incoming.type === 'mirror') {
// Android specific, untested
msg.topic = incoming.title;
msg.payload = incoming.body;
} else if (incoming.type === 'dismissal') {
msg.topic = 'dismissal';
msg.topic = 'Push dismissed';
msg.payload = incoming.iden;
} else {
//adapter.log.error('unknown push type: ' + incoming.type + ' content: ' + JSON.stringify(incoming));
}
adapter.log.silly('Pushbullet DEBUG - ' + JSON.stringify(msg));
if (msg.pushtype !== 'dismissal' && msg.pushtype !== 'delete') {
adapter.log.silly('Pushbullet DEBUG Payload: ' + msg.payload);
await adapter.setStateAsync('push.type', incoming.type, true);
await adapter.setStateAsync('push.title', typeof msg.topic === 'object' ? JSON.stringify(msg.topic) : (msg.topic === undefined || msg.topic === null ? null : msg.topic.toString()), true);
await adapter.setStateAsync('push.message', typeof msg.message === 'object' ? JSON.stringify(msg.message) : (msg.message === undefined || msg.message === null ? null : msg.message.toString()), true);
await adapter.setStateAsync('push.payload', typeof msg.payload === 'object' ? JSON.stringify(msg.payload) : (msg.payload === undefined || msg.payload === null ? null : msg.payload.toString()), true);
await adapter.setStateAsync('push.forAll', !incoming.target_device_iden, true);
if (incoming.target_device_iden && !adapter.config.doNotDelete) {
await pusher.deletePush(incoming.iden);
}
}
}
async function main(retry) {
if (!retry && !PushBullet) {
setTimeout(() => main(true), 1000);
return;
}
if (!PushBullet) {
adapter.log.error('Pushbullet package not available!');
return;
}
receiver = adapter.config.receivermail;
pusher = new PushBullet(adapter.config.apikey);
try {
const response = await pusher.devices({});
const devices = (await response.json()).devices;
let found = false;
for (let i = 0; i < devices.length; i++) {
if (devices[i].nickname === 'ioBroker') {
found = true
myIden = devices[i].iden;
break;
}
}
if (!found) {
const resp = await pusher.createDevice({
nickname: 'ioBroker'
});
const device = await resp.json();
myIden = device.iden;
}
} catch (error) {
adapter.log.error(`Pushbullet error: ${error.message}`);
return;
}
if (adapter.config.password) {
let response = await pusher.me();
let user = await response.json();
pusher.enableEncryption(adapter.config.password, user.iden);
}
stream = pusher.stream();
stream.connect();
stream.on('error', error =>
// stream error
adapter.log.warn(`ERROR: ${error.message}`));
stream.on('message', async message => {
// message received
adapter.log.debug(`Message received - ${message.type}`);
if (message.type === 'tickle') {
await handleTickle(message);
} else if (message.type === 'push') {
await pushMsg(message.push);
} else if (message.type === 'nop') {
adapter.log.debug('Pushbullet DEBUG - keepalive');
}
});
stream.on('connect', async () => {
// stream has connected
adapter.log.debug('Pushbullet DEBUG - Stream connected');
try {
const response = await pusher.history({ limit: 1 });
const history = await response.json();
tsHistory = history.pushes.length ? history.pushes[0].modified : 0;
} catch (err) {
adapter.log.info('Unable to get history.');
tsHistory = 0;
}
});
}
// If started as allInOne mode => return function to create instance
// @ts-ignore
if (module.parent) {
module.exports = startAdapter;
} else {
// or start the instance directly
startAdapter();
}