forked from Canonip/MMM-FRITZ-Box-Callmonitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-FRITZ-Box-Callmonitor.js
328 lines (278 loc) · 8.5 KB
/
MMM-FRITZ-Box-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
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
327
328
/* global Module */
/* Magic Mirror
* Module: MMM-FRITZ-Box-Callmonitor
*
* By Paul-Vincent Roll http://paulvincentroll.com
* MIT Licensed.
*/
const CALL_TYPE = Object.freeze({
INCOMING: "1",
MISSED: "2",
OUTGOING: "3"
})
Module.register("MMM-FRITZ-Box-Callmonitor", {
requiresVersion: "2.0.0",
// Default module config.
defaults: {
numberFontSize: 30,
vCard: false,
fritzIP: "192.168.178.1",
fritzPort: 1012,
minimumCallLength: 0,
maximumCallDistance: 60,
maximumCalls: 5,
fade: true,
debug: false,
fadePoint: 0.25,
username: "",
password: "",
reloadContactsInterval: 30, // 30 minutes, set to 0 to disable
deviceFilter: [], // [] means no filtering
showContactsStatus: false,
showOutgoing: false,
colorEnabled: false
},
// Define required translations.
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
/*
* ToDo: enable the following method after version MM 2.1.0 is released
*/
//getHeader: function() {
// return this.data.header + this.getContactsSymbol();
//},
getScripts: function () {
return ["moment.js"];
},
getStyles: function () {
return ["font-awesome.css", "MMM-FRITZ-Box-Callmonitor.css"];
},
start: function () {
//Create callHistory array
this.callHistory = [];
this.activeAlert = null;
//Set helper variable this so it is available in the timer
var self = this;
//Update doom every minute so that the time of the call updates and calls get removed after a certain time
setInterval(function () {
self.updateDom();
}, 60000);
this.contactsLoaded = false;
this.numberOfContacts = 0;
this.contactLoadError = false;
this.errorCode = "";
//Send config to the node helper
this.sendSocketNotification("CONFIG", this.config);
//set up the contact reloading
if (this.config.password !== "" && this.config.reloadContactsInterval !== 0) {
setInterval(function () {
self.sendSocketNotification("RELOAD_CONTACTS");
}, this.config.reloadContactsInterval * 60000);
}
Log.info("Starting module: " + this.name);
},
// Override socket notification handler.
socketNotificationReceived: function (notification, payload) {
if (notification === "call") {
//Show alert on UI
this.sendNotification("SHOW_ALERT", {
title: this.translate("title"),
message: "<span style='font-size:" + this.config.numberFontSize.toString() + "px'>" + payload + "<span>",
imageFA: "phone"
});
//Set active Alert to current call
this.activeAlert = payload;
}
if (notification === "connected") {
//Send notification for currentCall module
this.sendNotification("CALL_CONNECTED", payload);
//Remove alert only on connect if it is the current alert shown
if (this.activeAlert === payload) {
//Remove alert from UI when call is connected
this.sendNotification("HIDE_ALERT");
this.activeAlert = null;
}
}
if (notification === "disconnected") {
//Send notification for currentCall module
this.sendNotification("CALL_DISCONNECTED", payload.caller);
if (this.config.password !== "") {
// if we have an API connection, check if the call was really missed
this.sendSocketNotification("RELOAD_CALLS");
} else {
//Add call to callHistory (timestamp and caller) or if minimumCallLength is set only missed calls
if (this.config.minimumCallLength === 0 || payload.duration <= this.config.minimumCallLength) {
this.callHistory.unshift({ "time": moment(), "caller": payload.caller });
}
//Update call list on UI
this.updateDom(3000);
}
//Remove alert only on disconnect if it is the current alert shown
if (this.activeAlert === payload.caller) {
//Remove alert from UI when call is connected
this.sendNotification("HIDE_ALERT");
this.activeAlert = null;
}
}
if (notification === "call_history") {
//update call history from API
this.callHistory = payload;
this.updateDom(3000);
}
if (notification === "error") {
this.contactsLoaded = true;
this.contactLoadError = true;
this.errorCode = payload;
if (this.config.showContactsStatus) {
this.updateDom();
}
}
if (notification === "contacts_loaded") {
this.contactsLoaded = true;
this.numberOfContacts = payload;
if (this.errorCode === "network_error") {
// clear previous network errors, since we got a connection now
this.contactLoadError = false;
this.errorCode = "";
}
if (this.config.showContactsStatus) {
this.updateDom();
}
}
},
getContactsSymbol: function () {
var html = "";
if (this.config.showContactsStatus && (this.config.vCard || this.config.password !== "")) {
html += " (<span class='small fa fa-book'/></span>";
if (this.contactsLoaded) {
html += " " + this.numberOfContacts;
}
else {
html += " <span class='small fa fa-refresh fa-spin fa-fw'></span>";
}
if (this.contactLoadError) {
html += " <span class='small fa fa-exclamation-triangle'/></span> ";
html += this.translate(this.errorCode);
}
html += ")";
}
return html;
},
sortHistory: function () {
var history = this.callHistory;
//Sort history by time
history.sort(function (a, b) {
return moment(moment(a.time)).diff(moment(b.time)) < 0;
});
this.callHistory = history;
},
trimHistory: function () {
var history = this.callHistory;
//For each call in callHistory
for (var i = 0; i < history.length; i++) {
//Check if call is older than maximumCallDistance
if (moment(moment()).diff(moment(history[i].time)) > this.config.maximumCallDistance * 60000) {
//is older -> remove from list
history.splice(i, 1);
i--;
}
}
this.callHistory = history;
},
getDom: function () {
//remove old calls
this.sortHistory();
this.trimHistory();
//get maximum number of calls from call history
var calls = this.callHistory.slice(0, this.config.maximumCalls);
//Create table
var wrapper = document.createElement("table");
//set table style
wrapper.className = "small";
//If there are no calls, set "noCall" text.
if (calls.length === 0) {
content = this.translate("noCall");
// ToDo: remove this line after version MM 2.1.0 is released
content += this.getContactsSymbol();
wrapper.innerHTML = content;
wrapper.className = "xsmall dimmed";
return wrapper;
}
//For each call in calls
for (var i = 0; i < calls.length; i++) {
//Create callWrapper
var callWrapper = document.createElement("tr");
callWrapper.className = "normal";
//Set icon
var icon = document.createElement("i")
icon.style = "padding-right: 10px"
switch (calls[i].type) {
case CALL_TYPE.INCOMING:
icon.className = "fas fa-long-arrow-alt-down"
break
case CALL_TYPE.OUTGOING:
//Skip outgoing calls if not wanted
if (!this.config.showOutgoing)
continue
icon.className = "fas fa-long-arrow-alt-up"
break
case CALL_TYPE.MISSED:
if (this.config.colorEnabled)
icon.style += ";color: red"
icon.className = "fas fa-times"
break
}
callWrapper.appendChild(icon);
//Set caller of row
var caller = document.createElement("td");
caller.innerHTML = calls[i].caller;
caller.className = "title bright";
callWrapper.appendChild(caller);
//Set time of row
var time = document.createElement("td");
time.innerHTML = moment(calls[i].time).fromNow();
time.className = "time light xsmall";
callWrapper.appendChild(time);
//Add to wrapper
wrapper.appendChild(callWrapper);
// Create fade effect by MichMich (MIT)
if (this.config.fade && this.config.fadePoint < 1) {
if (this.config.fadePoint < 0) {
this.config.fadePoint = 0;
}
var startingPoint = calls.length * this.config.fadePoint;
var steps = calls.length - startingPoint;
if (i >= startingPoint) {
var currentStep = i - startingPoint;
callWrapper.style.opacity = 1 - (1 / steps * currentStep);
}
}
// End Create fade effect by MichMich (MIT)
}
return wrapper;
},
/* cmpVersions(a,b)
* Compare two symantic version numbers and return the difference.
*
* argument a string - Version number a.
* argument a string - Version number b.
*/
cmpVersions: function (a, b) {
var i, diff;
var regExStrip0 = /(\.0+)+$/;
var segmentsA = a.replace(regExStrip0, "").split(".");
var segmentsB = b.replace(regExStrip0, "").split(".");
var l = Math.min(segmentsA.length, segmentsB.length);
for (i = 0; i < l; i++) {
diff = parseInt(segmentsA[i], 10) - parseInt(segmentsB[i], 10);
if (diff) {
return diff;
}
}
return segmentsA.length - segmentsB.length;
}
});