-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
326 lines (272 loc) · 8.49 KB
/
app.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
"use strict"
const net = require('net');
const events = require('events');
const Log = require('homey-log').Log;
const fritz = require('fritzapi').Fritz;
const Router = require('./lib/router.js');
const findFritzboxesInterval = 600000;
var host = 'fritz.box'; // ipaddress | 'fritz.box'
var port = 1012;
var lastData = null;
var socket = null;
var phoneBook = [];
/*
#96*5* – Callmonitor inschakelen
#96*4* – Callmonitor uitschakelen
*/
function initCallMonitor() {
Homey.log("Load Phonebook");
phoneBook = [];
let phoneBookxml = Homey.manager('settings').get('fritz_phonebook');
if (phoneBookxml) {
var parseString = require('xml2js').parseString;
parseString(phoneBookxml,
function(err, result) {
if (!err) {
if(typeof result.phonebooks === 'undefined' ||
typeof result.phonebooks.phonebook[0] === 'undefined' ||
typeof result.phonebooks.phonebook[0].contact === 'undefined')
{
// Skip invalid phonebooks and log message, prevent app from crashing.
Homey.log("Phonebook XML contains invalid data.");
return false;
}
result.phonebooks.phonebook[0].contact.forEach(function(item) {
if(typeof item.telephony[0] === 'undefined' ||
typeof item.telephony[0].number === 'undefined' ||
typeof item.person[0] === 'undefined' ||
typeof item.person[0].realName[0] === 'undefined')
{
// Skip invalid items.
return;
}
item.telephony[0].number.forEach(function(data) {
var number = data._;
if (!isNaN(number))
{
phoneBook[number] = item.person[0].realName[0];
}
})
})
}
});
}
Homey.log("Load Settings");
host = Homey.manager('settings').get('fritz_host');
port = Homey.manager('settings').get('fritz_port');
Homey.log("Init Socket");
if (port) {
socket = new net.Socket();
socket.connect(port, host);
socket.on('connect', handleConnect);
socket.on('data', handleData);
socket.on('error', handleError);
process.on('SIGINT', closeSocket);
process.on('SIGTERM', closeSocket);
process.on('SIGBREAK', closeSocket);
}
// setInterval(simCall, 20 * 1000); // for testing
}
function simCall() {
// parseCallMonitorLine('22.09.16 19:03:31;RING;0;0263561234;0263561234;SIP2;');
// parseCallMonitorLine('22.09.16 12:45:47;CONNECT;0;12;0263561234;');
// parseCallMonitorLine('22.09.16 12:46:01;DISCONNECT;0;11;');
// parseCallMonitorLine('15.01.17 12:22:31;CALL;1;12;0263233889;0263561234;SIP2;')
}
function parseCallMonitorLine(line) {
var chunks = line.split(';');
var result = {}; // for later use to add actions
result.date = chunks[0];
result.type = chunks[1];
result.connectionId = chunks[2];
switch (result.type) {
case "CALL":
result.line = chunks[3];
result.localNumber = chunks[4];
result.remoteNumber = chunks[5];
Homey.manager('flow').trigger('fb_call', {
fb_tel_nr: result.remoteNumber,
fb_abonnee_name: findNameInPB(result.remoteNumber),
fb_datetime: new Date().toLocaleString()
});
break;
case "RING":
result.remoteNumber = chunks[3];
result.localNumber = chunks[4];
console.log(findNameInPB(result.remoteNumber));
Homey.manager('flow').trigger('fb_incomming_call', {
fb_tel_nr: result.remoteNumber,
fb_abonnee_name: findNameInPB(result.remoteNumber),
fb_datetime: new Date().toLocaleString()
});
break;
case "CONNECT":
result.line = chunks[3];
result.remoteNumber = chunks[4];
Homey.manager('flow').trigger('fb_call_anwsered', {
fb_tel_nr: result.remoteNumber,
fb_abonnee_name: findNameInPB(result.remoteNumber),
fb_datetime: new Date().toLocaleString()
});
break;
case "DISCONNECT":
result.duration = chunks[3];
Homey.manager('flow').trigger('fb_disconnect_call', {
fb_duration: result.duration,
});
break;
}
return result;
}
Homey.manager('flow').on('condition.TelNumber', function(callback, args) {
if (lastData) {
if (lastData.type != 'DISCONNECT') {
if (lastData.remoteNumber == args.telnr) {
callback(null, true);
}
}
}
callback(null, false);
});
Homey.manager('flow').on('trigger.fb_incomming_call', function(callback, args) {
Homey.log('Trigger fired');
callback(null, true); // true to make the flow continue, or false to abort
});
Homey.manager('settings').on('set', function(name) {
Homey.log('Variable ' + name + ' has been set');
// Don't reload when it is the fritzbox settings from the devices
if(name.indexOf("fritzbox_settings_") > -1)
{
return;
}
closeSocket()
initCallMonitor();
});
function findNameInPB(number) {
var unknown = __('unknown');
if (phoneBook)
{
if (number in phoneBook)
{
return phoneBook[number];
}
else
{
return unknown;
}
}
else
{
return unknown;
}
}
function handleConnect() {
Homey.log('fritz connected to ' + host);
}
function handleData(data) {
var line = data.toString();
lastData = parseCallMonitorLine(line);
Homey.log(line);
}
function handleError(err) {
// Catch some errors
Homey.error('Could not connect to ' + host);
if (err.code === 'ECONNREFUSED') {
Homey.error('Is the CallMonitor enabled?');
} else if (err.code === 'ENOTFOUND') {
Homey.error('Host ' + host + ' not found.');
} else if (err.code === 'EHOSTUNREACH') {
Homey.error('Host ' + host + ' not found.');
} else {
Homey.error(err.code);
}
}
function closeSocket() {
if (socket) {
socket.end();
socket = null;
}
}
Homey.on('unload', function() {
closeSocket();
});
//module.exports.init = init;
/******
***
*** To handle FRITZBOX smart home functionalities
***
*******/
class App extends events.EventEmitter {
constructor() {
super();
this.setMaxListeners(0);
this._fritzboxes = {};
this.init = this._onExportsInit.bind(this);
}
/*
Helper methods
*/
log() {
console.log.bind(this, '[log]' ).apply( this, arguments );
}
error() {
console.error.bind( this, '[error]' ).apply( this, arguments );
}
/*
Fritzbox methods
*/
findFritzboxes() {
// Get IP settings from app settings page.
// I don't know how to do a proper discovery function otherwise that would be nice
// Then you can add multiple FritzBoxes
let host = Homey.manager('settings').get('fritz_host');
if(typeof host === 'undefined' || host === null || host === '')
{
return;
}
let fritzbox = {
id: "abc-123",
ip: "http://" + host
};
this._initFritzbox(fritzbox);
}
_initFritzbox( fritzbox ) {
console.log("_initFritzbox");
fritzbox.id = fritzbox.id.toLowerCase();
// skip if already found but update ip if changed
if( this._fritzboxes[ fritzbox.id ] instanceof Router ) {
if( this._fritzboxes[ fritzbox.id ].address !== fritzbox.ip ) {
this.log(`Fritzbox ip has changed from ${this._fritzboxes[ fritzbox.id ].address} to ${fritzbox.ip}`);
this._fritzboxes[ fritzbox.id ].setAddress( fritzbox.ip );
}
return;
}
console.log("Found fritzbox");
this.log(`Found fritzbox ${fritzbox.id} @ ${fritzbox.ip}`);
this._fritzboxes[ fritzbox.id ] = new Router( fritzbox.id, fritzbox.ip );
this._fritzboxes[ fritzbox.id ]
.on('log', this.log.bind( this, `[${fritzbox.id}]`) )
.on('error', this.error.bind( this, `[${fritzbox.id}]`) )
.on('fritzbox_available', () => {
this.emit('fritzbox_available', this._fritzboxes[ fritzbox.id ] );
})
.init()
}
getFritzboxes() {
return this._fritzboxes;
}
getFritzbox( fritzboxId ) {
if( typeof fritzboxId !== 'string' ) return new Error('invalid_fritzbox');
return this._fritzboxes[ fritzboxId.toLowerCase() ] || new Error('invalid_fritzbox');
}
/*
Export methods
*/
_onExportsInit() {
console.log(`${Homey.manifest.id} running...`);
this.findFritzboxes();
setInterval( this.findFritzboxes.bind(this), findFritzboxesInterval );
initCallMonitor();
}
}
module.exports = new App();