-
Notifications
You must be signed in to change notification settings - Fork 1
/
speed.js
41 lines (35 loc) · 1.04 KB
/
speed.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
/**
* Created by lukedavis on 1/20/17.
*/
const speedTest = require('speedtest-net');
/**
* Creates instance of Speed Node for use with Node-Red https://nodered.org
*
* @param RED - The RED instance passed in as part of the Node Red life cycle
*/
module.exports = function(RED) {
function SpeedNode(config) {
var node = this;
var speed = {};
RED.nodes.createNode(this, config);
this.on('input', msg => {
speed = speedTest({maxTime: 5000});
speed.on('downloadprogress', progress => {
node.status({fill: "yellow", shape: "dot", text: progress + " %"});
});
speed.on('data', data => {
msg.payload.speedResults = data;
node.status({fill: "green", shape: "dot", text: data.speeds.download + " Mbps"});
// Since this is the final callback we care about, remove speed instance
speed = null;
node.send(msg);
});
speed.on('error', error => {
node.error(error);
// We need to , remove speed instance
speed = null;
});
});
}
RED.nodes.registerType("node-red-contrib-speed-test", SpeedNode);
};