1 Star 0 Fork 0

changhaisong/MMM-kalliope

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
MMM-kalliope.js 3.27 KB
一键复制 编辑 原始数据 按行查看 历史
Nicolas Marcq 提交于 2018-07-24 10:50 . Update MMM-kalliope.js
/* 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);
}
}
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/changhaisong/MMM-kalliope.git
git@gitee.com:changhaisong/MMM-kalliope.git
changhaisong
MMM-kalliope
MMM-kalliope
master

搜索帮助