-
Notifications
You must be signed in to change notification settings - Fork 1
/
speedtest.js
57 lines (46 loc) · 1.13 KB
/
speedtest.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
var speedTest = require('speedtest-net');
var firebase = require("firebase");
firebase.initializeApp({
databaseURL: "https://netchecker-e53aa.firebaseio.com"
});
const LOOP_TIME = 10000;
var testIsRunning = false;
var firebase_url;
var testLoop;
function test() {
var sp = null;
var log = {};
testIsRunning = true;
sp = speedTest({ maxTime: 5000 });
sp.on('data', (data) => {
data.date = new Date().toString();
firebase.database().ref(firebase_url).push({
result: data
});
});
return sp;
}
function startLoop(config) {
firebase_url = "/" + config.user + "/" + config.network + "/tests";
testLoop = setInterval(() => {
console.log('Testing...');
test();
}, config.time || LOOP_TIME);
}
function stopLoop() {
clearInterval(testLoop);
}
function getResults() {
return new Promise((resolve, reject) => {
var db = firebase.database();
var ref = db.ref(firebase_url);
ref.on("value", (snapshot) => {
resolve(snapshot.val());
}, (errorObject) => {
reject("The read failed: " + errorObject.code);
});
});
}
module.exports.test = test;
module.exports.getResults = getResults;
module.exports.startLoop = startLoop;