forked from paviro/MMM-Callmonitor-Current-Call
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Callmonitor-Current-Call.js
129 lines (111 loc) · 3.2 KB
/
MMM-Callmonitor-Current-Call.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
/* global Module */
/* Magic Mirror
* Module: MMM-Callmonitor-Current-Call
*
* By Paul-Vincent Roll http://paulvincentroll.com
* MIT Licensed.
*/
Module.register("MMM-Callmonitor-Current-Call", {
// Default module config.
defaults: {
showDirection: false,
colorEnabled: false
},
// Define required translations.
getTranslations: function () {
return {
en: "translations/en.json",
de: "translations/de.json"
};
},
getScripts: function () {
return ["moment.js"];
},
notificationReceived: function (notification, payload, sender) {
if (notification === "CALL_CONNECTED") {
if (payload.hasOwnProperty('direction')) {
this.active_calls[payload.caller] = {start: moment(), direction: payload.direction};
} else {
this.active_calls[payload] = {start: moment(), direction: null};
}
if (!this.timer) {
console.log("timer set");
var self = this;
this.timer = setInterval(function () {self.updateDom();}, 1000);
this.updateDom(300);
}
console.log(this.active_calls);
} else if (notification === "CALL_DISCONNECTED") {
delete this.active_calls[payload];
if (Object.keys(this.active_calls).length == 0) {
clearInterval(this.timer);
this.timer = false;
console.log("timer cleared");
}
this.updateDom(300);
}
},
start: function () {
//Create active_calls array
this.active_calls = {};
//set timer
this.timer = false;
Log.info("Starting module: " + this.name);
},
getDom: function () {
//Create table
var wrapper = document.createElement("table");
//set table style
wrapper.className = "small";
var calls = this.active_calls;
console.log(this.active_calls);
//If there are no calls, set "noCall" text.
if (Object.keys(this.active_calls).length === 0 && this.data.header != null) {
wrapper.innerHTML = this.translate("NoActiveCalls");
wrapper.className = "xsmall dimmed";
return wrapper;
}
//For each call in calls
for (var call in calls) {
if (!calls.hasOwnProperty(call)) continue;
//Create callWrapper
var callWrapper = document.createElement("tr");
callWrapper.className = "normal";
if (this.config.showDirection) {
var iconWrapper = document.createElement("td");
//Set icon
var icon = document.createElement("i")
icon.style = "padding-right: 10px"
switch (calls[call].direction) {
case "in":
if (this.config.colorEnabled) {
icon.style += ";color: #96FF96";
}
icon.className = "fas fa-level-down-alt";
break;
case "out":
if (this.config.colorEnabled) {
icon.style += ";color: #BCDDFF";
}
icon.className = "fas fa-level-up-alt"
break;
}
iconWrapper.appendChild(icon)
callWrapper.appendChild(iconWrapper);
}
//Set caller of row
var caller = document.createElement("td");
caller.innerHTML = call;
caller.className = "title bright";
callWrapper.appendChild(caller);
//Set time of row
var time = document.createElement("td");
time.innerHTML = moment.utc(moment(moment()).diff(moment(calls[call]))).format("HH:mm:ss");
time.className = "time light xsmall";
callWrapper.appendChild(time);
//Add to wrapper
wrapper.appendChild(callWrapper);
}
return wrapper;
}
});