-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-SpeedtestTracker.js
executable file
·260 lines (230 loc) · 6.68 KB
/
MMM-SpeedtestTracker.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/* global Module */
/* Magic Mirror
* Module: MMM-SpeedtestTracker
*
* By
* MIT Licensed.
*/
Module.register("MMM-SpeedtestTracker", {
defaults: {
showDownload: true,
showUpload: true,
showPing: true,
showServerName: false,
showDate: false,
updateInterval: 60000,
retryDelay: 5000,
dateOptions: {
localeMatcher: "best fit",
timeZone: "Europe/Berlin",
hour12: false, // oder true für 12-Stunden-Format
formatMatcher: "best fit",
weekday: undefined,
era: undefined,
year: "numeric",
month: "long",
day: "numeric",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
timeZoneName: "short",
},
speedFormatOptions: {
style: "unit",
unit: "megabit-per-second",
unitDisplay: "short",
notation: "standard",
minimumFractionDigits: 0,
maximumFractionDigits: 0,
},
pingFormatOptions: {
style: "unit",
unit: "millisecond",
unitDisplay: "short",
minimumFractionDigits: 0,
maximumFractionDigits: 0,
},
},
requiresVersion: "2.1.0", // Required version of MagicMirror
start: function () {
var self = this;
var dataRequest = null;
var dataNotification = null;
//Flag for check if module is loaded
this.loaded = false;
if (this.config.url) {
// Schedule update timer.
this.getData();
setInterval(function () {
self.updateDom();
}, this.config.updateInterval);
} else {
Log.error(self.name, "No URL defined");
}
},
/*
* getData
* function example return data and show it in the module wrapper
* get a URL request
*
*/
getData: function () {
var self = this;
var urlApi = this.config.url + "/api/speedtest/latest";
var retry = true;
var dataRequest = new XMLHttpRequest();
dataRequest.open("GET", urlApi, true);
dataRequest.onreadystatechange = function () {
Log.debug(self.name, this.readyState);
if (this.readyState === 4) {
Log.debug(self.name, this.status);
if (this.status === 200) {
self.processData(JSON.parse(this.response));
} else if (this.status === 401) {
self.updateDom(self.config.animationSpeed);
Log.error(self.name, this.status);
retry = false;
} else {
Log.error(self.name, "Could not load data.");
}
if (retry) {
self.scheduleUpdate(self.loaded ? -1 : self.config.retryDelay);
}
}
};
dataRequest.send();
},
/* scheduleUpdate()
* Schedule next update.
*
* argument delay number - Milliseconds before next update.
* If empty, this.config.updateInterval is used.
*/
scheduleUpdate: function (delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
nextLoad = nextLoad;
var self = this;
setTimeout(function () {
self.getData();
}, nextLoad);
},
/*
getHeader: function() {
return "Speedtest Tracker";
},
*/
getDom: function () {
var self = this;
// create element wrapper for show into the module
var wrapper = document.createElement("div");
wrapper.classList.add("mmm-speedtest-tracker");
// If this.dataRequest is not empty
if (this.dataRequest) {
let row1 = document.createElement("div");
let row2 = document.createElement("div");
row2.classList.add("xsmall", "dimmed");
//download
if (this.config.showDownload) {
row1.appendChild(
this.createMeasurementElement(
this.dataRequest.data.download,
["fa-solid", "fa-download"],
this.config.speedFormatOptions
)
);
}
// upload
if (this.config.showUpload) {
row1.appendChild(
this.createMeasurementElement(
this.dataRequest.data.upload,
["fa-solid", "fa-upload"],
this.config.speedFormatOptions
)
);
}
// ping
if (this.config.showPing) {
row1.appendChild(
this.createMeasurementElement(
this.dataRequest.data.ping,
["fa-regular", "fa-clock"],
this.config.pingFormatOptions
)
);
}
// server
if (this.config.showServerName) {
row2.appendChild(this.createTextElement(this.dataRequest.data.server_name, ["fa-solid", "fa-server"], []));
}
// date
if (this.config.showDate) {
let date = new Date(this.dataRequest.data.updated_at);
let dateText = date.toLocaleString(config.locale, this.config.dateFromatOptions);
row2.appendChild(this.createTextElement(dateText, ["fa-solid", "fa-clock-rotate-left"], []));
}
if (row1.hasChildNodes()) {
wrapper.appendChild(row1);
}
if (row2.hasChildNodes()) {
wrapper.appendChild(row2);
}
}
if (!wrapper.hasChildNodes()) {
wrapper.innerText = "No data";
}
return wrapper;
},
getScripts: function () {
return [];
},
getStyles: function () {
return ["MMM-SpeedtestTracker.css"];
},
// Load translations files
getTranslations: function () {
//FIXME: This can be load a one file javascript definition
return {
en: "translations/en.json",
es: "translations/es.json",
};
},
createMeasurementElement: function (value, faIconClasses, formatOptions) {
let text = new Intl.NumberFormat(config.locale, formatOptions).format(value);
return this.createTextElement(text, faIconClasses, ["bright"]);
},
createTextElement: function (text, faIconClasses, textClasses) {
let iconWrapper = document.createElement("i");
iconWrapper.classList.add(...faIconClasses);
let textWraper = document.createElement("span");
textWraper.classList.add(...textClasses);
let downloadText = document.createTextNode(text);
textWraper.appendChild(downloadText);
let wrapper = document.createElement("span");
wrapper.appendChild(iconWrapper);
wrapper.appendChild(textWraper);
return wrapper;
},
processData: function (data) {
var self = this;
this.dataRequest = data;
if (this.loaded === false) {
self.updateDom(self.config.animationSpeed);
}
this.loaded = true;
// the data if load
// send notification to helper
this.sendSocketNotification("MMM-SpeedtestTracker-NOTIFICATION_TEST", data);
},
// socketNotificationReceived from helper
socketNotificationReceived: function (notification, payload) {
if (notification === "MMM-SpeedtestTracker-NOTIFICATION_TEST") {
// set dataNotification
this.dataNotification = payload;
this.updateDom();
}
},
});