-
Notifications
You must be signed in to change notification settings - Fork 12
/
MMM-network-signal.js
124 lines (115 loc) · 4.15 KB
/
MMM-network-signal.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
/**
* Magic Mirror
* Module: MMM-network-signal
*
* By PoOwAa https://github.com/PoOwAa/MMM-network-signal
* MIT Licensed.
*/
Module.register("MMM-network-signal", {
// Default module config
defaults: {
updateInterval: 1000 * 5, // check network every seconds
maxTimeout: 1000, // maximum timeout
animationSpeed: 1000 * 0.25, // fade effect
initialLoadDelay: 1000 * 3, // first check delay
server: "8.8.8.8", // Server to check network connection. Default 8.8.8.8 is a Google DNS server
showMessage: true,
thresholds: {
strong: 50,
medium: 150,
weak: 500,
},
flexDirection: 'row', // set to 'row' to display the row in left-to-right mode, 'row-reverse' to display the row in right-to-left mode
scale: 0.45 // scale for the icon, must be greater than 0
},
getTranslations: function() {
return {
da: "translations/da.json",
de: "translations/de.json",
en: "translations/en.json",
es: "translations/es.json",
fr: "translations/fr.json",
it: "translations/it.json"
};
},
start: function() {
Log.info("Starting module: " + this.name);
const self = this;
setTimeout(() => {
self.pingTest();
setInterval(() => {
self.pingTest();
}, self.config.updateInterval); // Actual loop timing
}, self.config.initialLoadDelay); // First delay
},
getDom: function() {
const content = document.createElement("div");
content.style = `display: flex;flex-direction: ${this.config.flexDirection};justify-content: space-between; align-items: center`;
const wifiSign = document.createElement("img");
wifiSign.style = `transform:scale(${this.config.scale})`;
if (this.config.showMessage)
{
var connStatus = document.createElement("p");
connStatus.style = "text-align:center;font-size:0.65em";
}
// Changing icon
switch (true) {
// Fast ping, strong signal
case this.ping < this.config.thresholds.strong:
wifiSign.src = this.file("icons/3.png");
if (this.config.showMessage)
{
connStatus.innerHTML = this.translate("excellent")
}
break;
// Medium ping, medium signal
case this.ping < this.config.thresholds.medium:
wifiSign.src = this.file("icons/2.png");
if (this.config.showMessage)
{
connStatus.innerHTML = this.translate("good")
}
break;
// Slow ping, weak signal
case this.ping < this.config.thresholds.weak:
wifiSign.src = this.file("icons/1.png");
if (this.config.showMessage)
{
connStatus.innerHTML = this.translate("normal")
}
break;
// Ultraslow ping, better if "no signal"
case this.ping > this.config.thresholds.weak:
wifiSign.src = this.file("icons/0.png");
if (this.config.showMessage)
{
connStatus.innerHTML = this.translate("bad")
}
break;
// No actual ping, maybe just searching for signal
default:
wifiSign.src = this.file("icons/loading.gif");
break;
}
if (this.config.showMessage)
{
content.appendChild(connStatus);
}
content.appendChild(wifiSign);
return content;
},
// Send socket notification, to start pinging the server
pingTest: function() {
this.sendSocketNotification("MMM_NETWORKSIGNAL_CHECK_SIGNAL", {
config: this.config,
});
},
// Handle socket answer
socketNotificationReceived: function(notification, payload) {
// Care only own socket answers
if (notification === "MMM_NETWORKSIGNAL_RESULT_PING") {
this.ping = payload;
this.updateDom(this.config.animationSpeed);
}
},
});