UNPKG

domotz-remote-pawn

Version:

Domotz Agent

156 lines (132 loc) 6.36 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * * Created by Andrea Azzara <a.azzara@domotz.com> on 20/02/2020. */ module.exports.factory = function (resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var q = resourceLocator.q; var fast = resourceLocator.networkTools.fast; function formatOutput(fastResult) { return { raw: null, server: { url: 'FAST', sponsor: 'FAST', url2: 'FAST', location: { siteName: 'FAST' }, }, 'speed.upload': fastResult.upload, 'speed.download': fastResult.download, MaxRTT: '0', MinRTT: '0', rttsec: fastResult.latency, }; } return { formatOutput: formatOutput, performSpeedTest: function () { var deferred = q.defer(); var previousState = null; var onProgress = function (progress) { if (progress.testType === 'latency') { // latency test reports result in ms in the 'value' field myConsole.debug('Progress: ' + progress.value + 'ms'); } else { // download/upload tests report result in the 'speed' field var mbs = progress.speed / 1000 / 1000; myConsole.debug('Progress: ' + mbs + 'Mbps'); if (!previousState || previousState !== progress.testType) { previousState = progress.testType; deferred.notify(progress.testType); } } }; var results = {}; var onSnapshot = function (result) { var speed = (result.bytes / result.time) * 1000 * 8, mbs = speed / 1000 / 1000; myConsole.debug('Snapshot speed: ' + mbs + 'Mbps'); }; var minDuration = resourceLocator.settingsManager.getSetting('configuration.settings.speed_test.fast.min_duration', 10); var maxDuration = resourceLocator.settingsManager.getSetting('configuration.settings.speed_test.fast.max_duration', 25); var minConnections = resourceLocator.settingsManager.getSetting('configuration.settings.speed_test.fast.min_connections', 1); var maxConnections = resourceLocator.settingsManager.getSetting('configuration.settings.speed_test.fast.max_connections', 8); var testerConfig = { duration: { // the test duration will be between min/max based on stability of measurements min: minDuration, // minimum test duration max: maxDuration, // maximum test duration }, connections: { min: minConnections, // minimum number of parallel connections max: maxConnections, // maximum number of parallel connections }, https: false, // whether to run the test using https }, testerContext = { name: 'Node.js Domotz Agent', // name of the testing app. Used for logging deviceType: resourceLocator.utils.platform.getFullPlatform(), // type of the device running the test version: resourceLocator.lodash.get(resourceLocator, 'pkg.json.version'), // version of the caller app }, tester = fast.tester(testerConfig, testerContext), events = fast.event; var onCompleteLatency = function (result) { myConsole.info('Final latency: ' + result.value + 'ms'); }; var onCompleteUpload = function (result) { results.upload = result.speed; tester.off(events.END, onCompleteUpload); tester.on(events.END, onCompleteLatency); results.latency = 0; deferred.resolve(results); }; var onCompleteDownload = function (result) { results.download = result.speed; tester.off(events.END, onCompleteDownload); tester.on(events.END, onCompleteUpload); myConsole.info('Upload test start'); tester.upload(); }; var cleanUp = function () { tester.off(events.PROGRESS, onProgress); tester.off(events.SNAPSHOT, onSnapshot); tester.off(events.END, onCompleteDownload); tester.off(events.FAIL, onFail); tester.off(events.CONNECTION_FAIL, onFail); }; var onFail = function (data) { //cleanUp(); deferred.reject(JSON.stringify(data.error)); }; // set up events // events.START - indicates start of the test // events.END - indicates the end of the test // events.PROGRESS - shows intermediate aggregate speed measurement (since start) // events.SNAPSHOT - shows instant speed measurement (since last snapshot) tester.on(events.PROGRESS, onProgress); tester.on(events.SNAPSHOT, onSnapshot); tester.on(events.END, onCompleteDownload); tester.on(events.FAIL, onFail); tester.on(events.CONNECTION_FAIL, onFail); myConsole.info('Starting FAST speed test, download'); tester.download(); return deferred.promise; }, }; };