-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmain.js
581 lines (479 loc) · 23.4 KB
/
main.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
'use strict';
/*
* Created with @iobroker/create-adapter v1.21.1
*/
// The adapter-core module gives you access to the core ioBroker functions
// you need to create an adapter
const utils = require('@iobroker/adapter-core');
const { type } = require('os');
// Load speedtest-net thank you @ddsol !
const state_attr = require(__dirname + '/lib/state_attr.js');
const speedTest = require('speedtest-net');
// Declare used varaibles
let run_test = null, test_running = false, down_ready = null, up_ready = null;
let intervall_time = null, timer = null, stop_timer = null, test_duration = null;
let test_server_id = null, test_server_url = null, running_mode;
let server_id = null, server_url = null;
// const fs = require("fs");
class WebSpeedy extends utils.Adapter {
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
constructor(options) {
super({
...options,
name: 'web-speedy',
});
this.on('ready', this.onReady.bind(this));
this.on('stateChange', this.onStateChange.bind(this));
this.on('unload', this.onUnload.bind(this));
}
/**
* Is called when databases are connected and adapter received configuration.
*/
async onReady() {
// Reset the connection & statuus indicators during startup
this.setState('info.connection', false, true);
this.setNotRunning(true);
// Create state to manually run test & sped indicators
this.create_state('running_download', 'running_download', false);
this.create_state('running_upload', 'running_upload', false);
// Subscribe to configuration states
this.subscribeStates('test_best');
this.subscribeStates('test_by_ID');
this.subscribeStates('test_by_URL');
this.subscribeStates('test_duration');
this.subscribeStates('test_auto_modus');
this.subscribeStates('test_specific_id');
this.subscribeStates('test_specific_url');
this.subscribeStates('test_auto_intervall');
// Shedule automated execution
await this.intervall_runner();
// Initial run to get best-servers and run a first test creating all wanted data-points
// Ignore if no automated intervall time is set
if (intervall_time !== 0) {
this.log.info('Web Speedy startet, getting list of closest servers ');
await this.test_run(0); // Initial run on best Server
this.log.info('Automated scan every : ' + intervall_time + ' minutes :-)');
}
}
async test_run(run_type, best_id) {
// Get configuration of max duration time for scans
const duration_time = await this.getStateAsync('test_duration');
if (!duration_time || (duration_time.val * 1000) < 5000) {
this.log.warn('Invalid value set for test duration, ignoring value and set to default : 15 seconds');
test_duration = 15000;
await this.setStateAsync('test_duration', { val: 15, ack: true });
} else {
test_duration = duration_time.val * 1000;
}
// Select running mode, run default in case of error during selection
try {
switch (run_type) {
case (0):
// Run test on Best Server found
this.log.info('Run test on on Best Server found');
run_test = speedTest({ maxTime: test_duration });
break;
case (1):
// Get configuration of pecific server id if configured for scan
server_id = await this.getStateAsync('test_specific_id');
if (server_id !== null && server_id !== undefined) {
test_server_id = server_id.val;
// Run test on configured server by id
if (best_id) { test_server_id = best_id; }
this.log.info('Run test on configured server by id : ' + test_server_id);
run_test = speedTest({ maxTime: test_duration, serverId: test_server_id.toString() });
// run_test = speedTest({maxTime: test_duration, serverId: test_server_id});
} else {
this.log.warn('Error Case 1 selecting specific server, running Best_Server mode');
run_test = speedTest({ maxTime: test_duration });
}
break;
case (2):
// Get configuration of pecific server id if configured for scan
server_url = await this.getStateAsync('test_specific_url');
if (server_url !== null && server_url !== undefined) {
test_server_url = server_url.val;
// Run test on configured server by url
this.log.info('Run test on configured server by url : ' + test_server_url);
run_test = speedTest({ maxTime: test_duration, serversUrl: test_server_url.toString() });
} else {
this.log.warn('Error Case 2 selecting specific server, running Best_Server mode');
run_test = speedTest({ maxTime: test_duration });
}
break;
default:
this.log.warn('Error No Case selecting specific server, running Best_Server mode');
run_test = speedTest({ maxTime: test_duration });
}
} catch (error) {
this.log.warn('Error selecting specific server, running Best_Server mode : ' + error);
run_test = speedTest({ maxTime: test_duration });
}
this.log.info('The speed test has been started and will take at maximum ' + (test_duration / 1000) + ' seconds for a single test run');
// Fired when an error occurs. The error is written to log when received.
run_test.on('error', err => {
this.log.error(err);
// Reset all states to non-running state
this.setState('info.connection', false, true);
this.setNotRunning(); // Reset all status indicators to not-running
});
// Fired when module has been triggered providing configuration
run_test.on('config', config => {
this.log.debug('Configuration info : ' + JSON.stringify(config));
// Set all states to running state
this.setRunning();
});
// Monitor download progress %
run_test.on('downloadprogress', progress => {
this.log.debug('Download progress : ' + progress);
this.create_state('running_download_progress', 'running_download_progress', progress);
});
// Monitor upload progress %
run_test.on('uploadprogress', progress => {
this.log.debug('Upload progress : ' + progress);
this.create_state('running_upload_progress', 'running_upload_progress', progress);
});
// Fired when download is finished
run_test.on('downloadspeed', speed => {
this.setState('running_download', false, true);
this.setState('running_download_speed', 0, true);
this.log.debug('Download speed : ' + (speed * 125).toFixed(2));
});
// Fired when upload is finished
run_test.on('uploadspeed', speed => {
this.setState('running_upload', false, true);
this.setState('running_upload_speed', 0, true);
this.log.debug('Upload speed : ' + (speed * 125).toFixed(2));
});
// Monitor download speed kb/s
run_test.on('downloadspeedprogress', speed => {
this.log.debug('Download speed (in progress) : ' + (speed * 125).toFixed(2) + ' kb/s');
if (down_ready !== true) {
down_ready = true;
this.setState('running_download', true, true);
}
this.create_state('running_download_speed', 'running_upload_speed', parseFloat((speed * 125).toFixed(2)));
});
// Monitor upload speed kb/s
run_test.on('uploadspeedprogress', speed => {
this.log.debug('Upload speed (in progress) : ' + (speed * 125).toFixed(2) + ' kb/s');
if (up_ready !== true) {
up_ready = true;
this.setState('running_upload', true, true);
}
this.create_state('running_upload_speed', 'running_upload_speed', parseFloat((speed * 125).toFixed(2)));
});
// Execute when data is publish at test-end (not working)
// run_test.on('result', url => {
// if (!url) {
// this.log.error('Could not successfully post test results.');
// } else {
// this.log.info('Test result url:', url);
// }
// });
// Get best server results and write to states and create drop-down menu to run specific test
run_test.on('bestservers', serverlist => {
this.setRunning();
this.log.debug('Closest servers : ' + JSON.stringify(serverlist));
// Create channel for list of closest servers with all details
this.extendObject('Closest_servers', {
type: 'channel',
common: {
name: 'Closest servers pinged at last test',
},
native: {},
});
// Write Server details to states
try {
// Loop array of server
for (const i in serverlist) {
this.log.debug('Closest server : ' + i + ' : ' + JSON.stringify(serverlist[i]));
// Create Server Channels
this.extendObject('Closest_servers.' + i, {
type: 'channel',
common: {
name: 'Closest server ' + i,
},
native: {},
});
// Create and write information to states
for (const x in serverlist[i]) {
this.create_state('Closest_servers.' + i + '.' + x, x, serverlist[i][x]);
}
}
// Create state to run test on specific server with drop-down menu(top 5 retrieved from scan)
this.extendObject('test_specific', {
type: 'state',
common: {
name: 'Run test on selected server',
type: 'mixed',
role: 'state',
write: true,
states: {
[serverlist[0]['id']]: 'name : ' + serverlist[0]['sponsor'] + ' Last Ping : ' + serverlist[0]['bestPing'],
[serverlist[1]['id']]: 'name : ' + serverlist[1]['sponsor'] + ' Last Ping : ' + serverlist[1]['bestPing'],
[serverlist[2]['id']]: 'name : ' + serverlist[2]['sponsor'] + ' Last Ping : ' + serverlist[2]['bestPing'],
[serverlist[3]['id']]: 'name : ' + serverlist[3]['sponsor'] + ' Last Ping : ' + serverlist[3]['bestPing'],
[serverlist[4]['id']]: 'name : ' + serverlist[4]['sponsor'] + ' Last Ping : ' + serverlist[4]['bestPing'],
},
},
native: {},
});
this.setState('test_specific', { val: '', ack: true });
this.subscribeStates('test_specific');
} catch (error) {
this.log.error(error);
}
this.log.info('Closest server found, running test');
});
// Get all test-result data (to much information, ignoring results)
// eslint-disable-next-line no-unused-vars
run_test.on('done', dataOverload => {
// this.log.info('Speed test finished, result : ' + JSON.stringify(dataOverload));
this.log.info('The speed test has been completed successfully.');
this.setNotRunning(); // Reset all status indicators to not-running
});
// Fired at test end providing JSON-array with all data
run_test.on('data', data => {
this.log.debug('Test Result Data : ' + JSON.stringify(data));
// Create channle for test-results
this.extendObject('Results', {
type: 'channel',
common: {
name: 'Test results of latest run',
},
native: {},
});
// Write Speed-test results to state
this.log.debug('Test results : ' + JSON.stringify(data));
this.create_state('Results.Last_Run', 'Last_Run_Timestamp', new Date().toString());
for (const i in data) {
// Create channel for each cathegorie
this.extendObject('Results.' + i, {
type: 'channel',
common: {
name: i,
},
native: {},
});
// Loop data-array and write values to states
for (const x in data[i]) {
switch (x) {
// Make propper calculation for download speed in Mbit and MByte
case ('download'):
this.create_state('Results.' + i + '.download_MB', 'download_MB', (data[i][x] / 8));
this.create_state('Results.' + i + '.download_Mb', 'download_Mb', data[i][x]);
break;
case ('upload'):
this.create_state('Results.' + i + '.upload_MB', 'upload_MB', (data[i][x] / 8));
this.create_state('Results.' + i + '.upload_Mb', 'upload_Mb', data[i][x]);
break;
default:
// Write alle regular states without conversion
if (x === 'lon' || x === 'lat') { // latitude and longtitude == type string!
this.create_state('Results.' + i + '.' + x, x, (data[i][x]).toString());
} else {
this.create_state('Results.' + i + '.' + x, x, data[i][x]);
};
}
}
}
this.setNotRunning(); // Reset all status indicators to not-running
});
}
// Intervall timer to run automated tests
async intervall_runner() {
// Get intervall time configuration
intervall_time = await this.getStateAsync('test_auto_intervall');
// Propper handling of shedule, if NULL set to default (30 minutes)
if (!intervall_time || (intervall_time && intervall_time.val === null)) {
await this.setStateAsync('test_auto_intervall', { val: 30, ack: true });
intervall_time = 30;
} else if (intervall_time && intervall_time.val !== null) {
intervall_time = intervall_time.val;
}
// Get intervall running mode configuration
const test_auto_modus = await this.getStateAsync('test_auto_modus');
// Propper handling of shedule, if NULL set to default (Best Server)
if (!test_auto_modus) {
this.log.warn('Invalid value set for auto modus, ignoring value and set to default');
running_mode = 0; // Run on best server found
await this.setStateAsync('test_auto_modus', { val: 0, ack: true });
} else {
running_mode = test_auto_modus.val;
}
this.log.debug('Start timer with : ' + (intervall_time * 60000));
// Disable time if test_auto_intervall is set to 0
if (intervall_time !== 0) {
// Reset timer (if running) and start new one
if (timer) { clearTimeout(timer); timer = null; }
timer = setTimeout(() => {
this.log.info('Execute timer with : ' + (intervall_time * 60000) + ' Currently running : ' + test_running);
if (!test_running) {
this.test_run(running_mode);
}
// Restart intervall at run
this.intervall_runner();
// Set timer, minutes to milliseconds
}, (intervall_time * 60000));
} else {
// 0 intervall time configured, disabling auto shedule
if (timer) { clearTimeout(timer); timer = null; }
this.log.warn('!!! Automated test disabled !!!');
}
}
// Handle configuration changes and test-start trigger
onStateChange(id, state) {
this.log.debug('State Change event : ' + id + ' value : ' + JSON.stringify(state));
// Check if valid state change is received with not-acknowledged value
if (state && state.ack === false) {
// Get state name
const deviceId = id.split('.');
const device = deviceId[2];
// Only handle cases for different states if not test is currenlty running
if (!test_running) {
switch (device) {
case ('test_best'):
this.test_run(0);
this.setRunning();
this.setState('test_best', { ack: true });
this.log.info('Manuel test startet on best server');
break;
case ('test_by_ID'):
this.test_run(1);
this.setRunning();
this.log.info('Manuel test startet for specific server ID');
this.setState('test_by_ID', { ack: true });
break;
case ('test_by_URL'):
this.test_run(2);
this.setRunning();
this.log.info('Manuel test startet for specific server URL');
this.setState('test_by_ID', { ack: true });
break;
case ('test_specific'):
this.test_run(1, state.val);
this.setRunning();
this.log.info('Manuel test startet for best avaiable server ID : ' + state.val);
this.setState('test_specific', { val: null, ack: true });
break;
case ('test_specific_id'):
// Acknowledge ID change
this.log.info('Test pecific server ID changed');
this.setState('test_specific_id', { ack: true });
break;
case ('test_specific_url'):
// Acknowledge ID change
this.log.info('Test pecific server URL changed');
this.setState('test_specific_id', { ack: true });
break;
case ('test_auto_intervall'):
this.intervall_runner();
this.setState('test_auto_intervall', { ack: true });
this.log.info('Automated scan changed to every : ' + state.val + ' minutes');
break;
case ('test_auto_modus'):
this.intervall_runner();
this.setState('test_auto_modus', { ack: true });
this.log.info('Automated scan mode changed.');
break;
case ('test_duration'):
this.setState('test_duration', { ack: true });
this.log.info('Maximum test duration changed to : ' + state.val + ' seconds');
break;
default:
}
}
}
}
// Set all status indicators to running state
setRunning() {
if (!test_running) {
this.setState('info.connection', true, true);
this.setState('running', true, true);
test_running = true;
}
}
// Set all status indicators to not-running state
setNotRunning(start) {
// Different delay times for adapter start and running mode
let delay_time = null;
if (start === true) {
delay_time = 10;
} else {
delay_time = 5000;
}
// a little delay to ensure all backend processes are finished
if (stop_timer) { clearTimeout(stop_timer); stop_timer = null; }
if (start !== true) {
stop_timer = setTimeout(() => {
this.setState('running', false, true);
this.setState('running_download_speed', 0, true);
this.setState('running_upload_speed', 0, true);
this.setState('running_download', false, true);
this.setState('running_upload', false, true);
down_ready = false;
up_ready = false;
test_running = false;
}, delay_time);
}
}
async create_state(state, name, value) {
this.log.debug('Create_state called for : ' + state + ' with value : ' + value);
try {
// Try to get details from state lib, if not use defaults. throw warning if states is not known in attribute list
if ((state_attr[name] === undefined)) { this.log.warn('State attribute definition missing for + ' + name); }
const writable = (state_attr[name] !== undefined) ? state_attr[name].write || false : false;
const state_name = (state_attr[name] !== undefined) ? state_attr[name].name || name : name;
const role = (state_attr[name] !== undefined) ? state_attr[name].role || 'state' : 'state';
const type = (state_attr[name] !== undefined) ? state_attr[name].type || 'mixed' : 'mixed';
const unit = (state_attr[name] !== undefined) ? state_attr[name].unit || '' : '';
this.log.debug('Write value : ' + writable);
await this.extendObjectAsync(state, {
type: 'state',
common: {
name: state_name,
role: role,
type: type,
unit: unit,
write: writable
},
native: {},
});
// Only set value if input != null
if (value !== null) { await this.setState(state, { val: value, ack: true }); }
// Subscribe on state changes if writable
if (writable === true) { this.subscribeStates(state); }
} catch (error) {
this.log.error('Create state error = ' + error);
}
}
/**
* Is called when adapter shuts down - callback has to be called under any circumstances!
* @param {() => void} callback
*/
onUnload(callback) {
try {
this.log.info('cleaned everything up...');
// clear running timers
if (timer) { clearTimeout(timer); timer = null; }
if (stop_timer) { clearTimeout(stop_timer); stop_timer = null; }
callback();
} catch (e) {
callback();
}
}
}
// @ts-ignore parent is a valid property on module
if (module.parent) {
// Export the constructor in compact mode
/**
* @param {Partial<ioBroker.AdapterOptions>} [options={}]
*/
module.exports = (options) => new WebSpeedy(options);
} else {
// otherwise start the instance directly
new WebSpeedy();
}