UNPKG

domotz-remote-pawn

Version:

Domotz Agent

83 lines (76 loc) 3.23 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/>. **/ /** * Given the list of NDT servers sorted by distance, returns the nearest responding. * Created by Iacopo Papalini <ipapalini@domotz.com> on 16/09/15. */ var detectNearestNDTServer = function (message, resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var getSpeedTestSuite = resourceLocator.networkTools.speedTest.getSpeedTestSuite; var SPEED_TEST_SUITES = resourceLocator.networkTools.speedTest.SPEED_TEST_SUITES; var timeout = message.payload.timeout; var maxAttempts = message.payload.maximum_attempts; var ooklaServers = resourceLocator.networkTools.ooklaServers; var ndtServers = resourceLocator.networkTools.ndtServers; return { describe: function () { return 'detectNearestNDTServer - ' + 'Timeout: ' + timeout + 'secs, ' + 'Maximum number of attempts: ' + maxAttempts; }, execute: function (callback) { var speedTestSuite = getSpeedTestSuite(); if (speedTestSuite === SPEED_TEST_SUITES.MLAB) { myConsole.debug('MLAB'); callback({ name: 'MLAB', sponsor: 'MLAB', url2: '', id: '-1', location: { siteName: 'MLAB' }, }); } else if (speedTestSuite === SPEED_TEST_SUITES.NDT) { ndtServers .fetch() .then(function (servers) { return ndtServers.findNearestResponding(servers, timeout, maxAttempts); }) .then(callback) .catch(function (error) { callback(null, error); }); } else if (speedTestSuite === SPEED_TEST_SUITES.FAST) { callback({ name: 'FAST', sponsor: 'FAST', url2: '', id: '-1', location: { siteName: 'FAST' }, }); } else { ooklaServers .fetch() .then(function (servers) { return ooklaServers.findNearestResponding(servers); }) .then(callback) .catch(function (error) { callback(null, error); }); } }, }; }; module.exports.command = detectNearestNDTServer;