-
Notifications
You must be signed in to change notification settings - Fork 10
/
MMM-kalliope.js
114 lines (90 loc) · 3.27 KB
/
MMM-kalliope.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
/* global Module */
class Message {
constructor(text) {
this.text = text;
this.timestamp = new Date();
}
}
Module.register('MMM-kalliope',{
// list of messages to print on the screen
messages: [],
// Default module config.
defaults: {
max: 5,
keep_seconds: 5,
title: "Kalliope"
},
start: function() {
// need to connect to the node helper
this.sendSocketNotification("CONNECT", null);
console.log("Starting module: " + this.name);
//Update DOM every minute so that the time of the call updates and calls get removed after a certain time
setInterval(() => {
this.updateDom();
}, 1000);
// only clean old messages if keep_seconds is set
if (this.config.keep_seconds > 0){
setInterval(() => {
this.cleanOldMesssage();
}, 1000);
}
},
cleanOldMesssage: function() {
var currentDate = new Date();
for(var i = 0; i < this.messages.length; i++){
var dif = currentDate.getTime() - this.messages[i].timestamp.getTime();
var secondsFromCurrentDateToMessageDate = dif / 1000;
var secondsBetweenDates = Math.abs(secondsFromCurrentDateToMessageDate);
// delete the message if to old
if (secondsBetweenDates > this.config.keep_seconds){
this.messages.splice(i, 1);
}
}
},
// Override dom generator
getDom: function() {
var wrapper = document.createElement("div");
if (this.messages.length == 0){
wrapper.innerHTML = "";
return wrapper
}
var title = document.createElement("div");
title.className = "light small dimmed";
title.innerHTML = this.config.title;
wrapper.appendChild(title);
var table = document.createElement("table");
for(var i = 0; i < this.messages.length; i++){
var row = document.createElement("tr");
table.appendChild(row);
var messageCell = document.createElement("td");
messageCell.innerHTML = this.messages[i].text
row.appendChild(messageCell);
}
wrapper.appendChild(table);
return wrapper;
},
socketNotificationReceived: function(notification, payload) {
// console.log(this.name + " received a socket notification: " + notification + " - Payload: " + payload);
if (notification == "KALLIOPE"){
// create new message object
var newMessage = new Message(payload);
this.messages.push(newMessage);
// clean old messages if list is too long
while(this.messages.length > this.config.max){
this.messages.shift();
}
}else{
// forward the notification to all modules
this.sendNotification(notification, payload);
}
},
notificationReceived: function(notification, payload, sender) {
if (sender) {
console.log(this.name + " received a module notification: " + notification
+ " from sender: " + sender.name);
console.log(payload);
} else {
Log.log(this.name + " received a system notification: " + notification);
}
}
});