-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathcallmonitor.js
132 lines (115 loc) · 3.36 KB
/
callmonitor.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
var net = require('net');
module.exports = function(RED) {
function FritzboxCallmonitor(n) {
RED.nodes.createNode(this,n);
var node = this;
node.topic = n.topic;
node.config = RED.nodes.getNode(n.device);
var client = new net.Socket();
client.setKeepAlive(true, 118000);
var connections = {};
var timeout;
var options = {
port: 1012,
host: node.config.host,
};
var reconnect = function() {
node.log('Connecting to fritzbox...');
if(timeout) clearTimeout(timeout);
client.connect(options);
};
node.status({fill:"yellow",shape:"ring",text:"Initialization"});
client.on('connect', function() {
node.log('Connected to fritzbox');
node.status({fill:"green",shape:"dot",text:"Connected"});
});
client.on('close', function(hadError) {
if(hadError) {
node.error("Disconnected with Error");
node.status({fill:"red",shape:"ring",text:"Disconnected with Error"});
} else {
node.warn("Disconnected");
node.status({fill:"red",shape:"ring",text:"Disconnected"});
}
node.log("Reconnecting in 30sec");
timeout = setTimeout(reconnect, 30000);
});
client.on('error', function(e) {
node.error(e);
});
client.on('timeout', function() {
node.error('Connection to fritzbox timed out');
client.end();
});
client.on('data', function(data) {
const raw = data.toString();
const array = raw.split(";");
const type = array[1];
const id = array[2];
const timestamp = array[0];
let message;
if (connections[id] !== undefined) {
message = connections[id]
} else {
message = {}
}
switch(type) {
case "CALL":
message.type = "OUTBOUND";
message.id = id;
message.timestamp = timestamp;
message.caller = array[4];
message.callee = array[5];
message.extension = array[3];
connections[id] = message;
break;
case "RING":
message.type = "INBOUND";
message.id = id;
message.timestamp = timestamp;
message.caller = array[3];
message.callee = array[4];
connections[id] = message;
break;
case "CONNECT":
message.type = "CONNECT";
message.id = id;
message.timestamp = timestamp;
message.extension = array[3];
connections[id] = message;
break;
case "DISCONNECT":
switch(message.type) {
case "INBOUND":
message.type = "MISSED";
break;
case "CONNECT":
message.type = "DISCONNECT";
break;
case "OUTBOUND":
message.type = "UNREACHED";
break;
}
message.id = id;
message.timestamp = timestamp;
message.duration = array[3];
delete connections[id];
break;
}
node.send({
topic: node.topic ? node.topic : "",
payload: message
});
});
reconnect();
node.on('close', function() {
client.setKeepAlive(false, 118000);
client.end(function() {
client.removeAllListeners();
node.log('Disconnected from fritzbox');
});
if(timeout) clearTimeout(timeout);
});
}
RED.nodes.registerType("fritzbox-callmonitor", FritzboxCallmonitor);
};