/
node_helper.js
124 lines (110 loc) · 3.54 KB
/
node_helper.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
/*
* Module: MMM-SpeedTest
*
* @bugsounet
*/
var NodeHelper = require('node_helper');
var _loadLib = require("./components/loadLibraries.js") // <-- main code for loading libraries
let log = (...args) => { /* do nothing */ }
module.exports = NodeHelper.create({
start: function(){
this.lib = { error: 0 }
this.config = null
this.interval = null
},
socketNotificationReceived: function(notification, payload){
if (notification == "INIT") this.initialize(payload)
},
initialize: async function(payload) {
console.log("[SPEED] MMM-SpeedTest Version:", require('./package.json').version)
this.config= payload
this.config.update= this.getUpdateTime(this.config.update)
if (this.config.debug) log = (...args) => { console.log("[SPEED]", ...args) }
await _loadLib.load(this) // <-- try to load sensible library (speedtest-net)
if (this.lib.error) return // <-- verify if no errors // if error stop module !
this.check() // <--- execute speed check
},
check: async function() {
log("[SPEED] Check SpeedTest")
try {
var Check = await this.lib.speedtest({
"serverId": this.config.server.preferedId ? this.config.server.preferedId : null,
"acceptLicense": this.config.server.acceptLicense,
"acceptGdpr": this.config.server.acceptGdpr,
"progress": (data) => this.progress(data)
})
} catch (err) {
console.log("[SPEED]", err.message)
} finally {
if (Check) {
log("Result:", Check)
this.sendSocketNotification("DATA", Check)
log("Done")
}
}
this.scheduleUpdate()
},
progress: function(data) {
switch (data.type) {
case "download":
this.sendSocketNotification("DOWNLOAD", this.oToMbps(data.download.bandwidth))
log("Download:", this.oToMbps(data.download.bandwidth), "Mbps")
break
case "upload":
this.sendSocketNotification("UPLOAD", this.oToMbps(data.upload.bandwidth))
log("Upload:", this.oToMbps(data.upload.bandwidth), "Mbps")
break
case "ping":
this.sendSocketNotification("PING", data.ping.latency)
log("Ping:", data.ping.latency, "ms")
break
}
},
/** update process **/
scheduleUpdate: function() {
if (this.config.update < 60*1000) this.config.update = 60*1000
clearInterval(this.interval)
log("Update Programmed")
this.interval = setInterval(() => {
log("Update...")
this.check()
}, this.config.update)
},
/** convert h m s to ms **/
getUpdateTime: function(str) {
let ms = 0, time, type, value
let time_list = ('' + str).split(' ').filter(v => v != '' && /^(\d{1,}\.)?\d{1,}([wdhms])?$/i.test(v))
for(let i = 0, len = time_list.length; i < len; i++){
time = time_list[i]
type = time.match(/[wdhms]$/i)
if(type){
value = Number(time.replace(type[0], ''))
switch(type[0].toLowerCase()){
case 'w':
ms += value * 604800000
break
case 'd':
ms += value * 86400000
break
case 'h':
ms += value * 3600000
break
case 'm':
ms += value * 60000
break
case 's':
ms += value * 1000
break
}
} else if(!isNaN(parseFloat(time)) && isFinite(time)){
ms += parseFloat(time)
}
}
return ms
},
/** Convert octect to Mbps [Match with Speedtest web result] **/
oToMbps: function(value) {
if (!value) return 0
return (value * 0.000008).toFixed(2)
}
});