-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-WebHookAlerts.js
executable file
·131 lines (109 loc) · 3.82 KB
/
MMM-WebHookAlerts.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
/* global Module */
/*
This code was originally written by JC21 https://github.com/jc21/MMM-IFTTT and modified by P J Tewkesbury
*/
Module.register('MMM-WebHookAlerts', {
/**
* Module config defaults
*/
defaults: {
displaySeconds: 60,
fadeSpeed: 3000,
effect: "slide", // scale|slide|genie|jelly|flip|bouncyflip|exploader
alert_effect: "jelly", // scale|slide|genie|jelly|flip|bouncyflip|exploader
display_time: 3500, // time a notification is displayed in seconds
position: "center",
welcome_message: false // shown at startup
},
/**
* @var {Object}
*/
currentNotification: null,
/**
* @var {Integer}
*/
currentTimeout: null,
/**
* Starting of the module
*/
start: function () {
Log.info('[' + this.name + '] Starting');
this.sendSocketNotification('START', this.config);
if (this.config.effect === "slide") {
this.config.effect = `${this.config.effect}-${this.config.position}`;
}
},
getScripts() {
return ["notificationFx.js"];
},
getStyles: function () {
return ['WebHookAlert.css', "font-awesome.css", this.file(`./styles/notificationFx.css`), this.file(`./styles/${this.config.position}.css`)];
},
getTemplate(type) {
return `templates/${type}.njk`;
},
async showNotification(notification) {
const message = await this.renderMessage("notification", notification);
new NotificationFx({
message,
layout: "growl",
effect: this.config.effect,
ttl: notification.timer || this.config.display_time
}).show();
},
/**
* @param {String} notification
* @param {Object} payload
*/
socketNotificationReceived: function (notification, payload) {
if (notification === 'WEBHOOKALERTS_NOTIFICATION') {
let fadeSpeed = this.config.fadeSpeed;
if (payload && typeof payload.fadeSpeed !== 'undefined') {
fadeSpeed = payload.fadeSpeed;
}
// this.currentNotification = payload;
// this.updateDom(fadeSpeed);
this.sendNotification('SCREEN_WAKEUP', true);
console.log(`WebHook ${payload.message}`);
console.log(`WebHook ${payload.title}`);
this.showNotification({ type: "notification", title: payload.title, message: payload.message, timer: payload.displaySeconds * 1000 });
// this.sendNotification("SHOW_ALERT", {type: "notification", title:payload.title, message: payload.message, timer : payload.displaySeconds * 1000});
if (payload.sound !== undefined)
this.sendNotification('PLAY_SOUND', payload.sound);
}
},
hideAlert(sender, close = true) {
// Dismiss alert and remove from this.alerts
if (this.alerts[sender.name]) {
this.alerts[sender.name].dismiss(close);
delete this.alerts[sender.name];
// Remove overlay
if (!Object.keys(this.alerts).length) {
this.toggleBlur(false);
}
}
},
renderMessage(type, data) {
return new Promise((resolve) => {
this.nunjucksEnvironment().render(this.getTemplate(type), data, function (err, res) {
if (err) {
Log.error("Failed to render alert", err);
}
resolve(res);
});
});
},
toggleBlur(add = false) {
const method = add ? "add" : "remove";
const modules = document.querySelectorAll(".module");
for (const module of modules) {
module.classList[method]("alert-blur");
}
},
// /**
// * @returns {*}
// */
getDom: function () {
return null;
}
});