forked from ronny3050/internet-monitor
-
Notifications
You must be signed in to change notification settings - Fork 5
/
node_helper.js
78 lines (70 loc) · 2.11 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
/**
* Created by debayan on 7/23/16.
*/
const Log = require("logger");
const NodeHelper = require("node_helper");
const speedtest = require("speedtest-net");
module.exports = NodeHelper.create({
start () {
Log.log(`${this.name} helper started ...`);
this.config = null;
this.interval = null;
},
socketNotificationReceived (notification, payload) {
if (notification === "Start") {
this.config = payload;
}
if (notification === "Check") {
this.checkSpeed();
}
},
async checkSpeed () {
Log.log("Checking speed...");
let Check;
try {
Check = await speedtest({serverId: this.config.serverId,
acceptLicense: true,
acceptGdpr: true,
progress: (data) => this.handleProgressEvent(data)});
} catch (err) {
Log.error("[internet-monitor]", err.message);
} finally {
if (Check) {
Log.debug("Result: ", Check);
this.sendSocketNotification("data", Check);
Log.debug("Done");
}
} // end try
this.scheduleUpdate();
},
handleProgressEvent (data) {
switch (data.type) {
case "download":
Log.debug(`Examining download statistic: ${data.download.bandwidth}`);
this.sendSocketNotification("downloadSpeedProgress", this.oToMbps(data.download.bandwidth));
break;
case "upload":
Log.debug(`Examining upload statistic: ${data.upload.bandwidth}`);
this.sendSocketNotification("uploadSpeedProgress", this.oToMbps(data.upload.bandwidth));
break;
case "ping":
Log.debug(`Examining ping statistic:${data.ping.latency}`);
this.sendSocketNotification("ping", Math.round(data.ping.latency));
}
},
// Convert octect to Mbps [Match with Speedtest web result]
oToMbps (value) {
if (!value) {
return 0;
}
return (value * 0.000008).toFixed(2);
},
/** update process **/
scheduleUpdate () {
if (this.config.updateInterval < 60 * 1000) {
this.config.updateInterval = 60 * 1000;
}
clearInterval(this.interval);
this.interval = setInterval(() => this.checkSpeed(), this.config.updateInterval);
}
});