domotz-remote-pawn
Version:
Domotz Agent
71 lines (61 loc) • 2.54 kB
JavaScript
/** 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 timeout = message.payload.timeout;
var maxAttempts = message.payload.maximum_attempts;
var enable_ookla = typeof resourceLocator.configuration.enable_ookla == 'undefined' ? true : resourceLocator.configuration.enable_ookla;
if (enable_ookla) {
var ooklaServers = resourceLocator.networkTools.ooklaServers;
} else {
var ndtServers = resourceLocator.networkTools.ndtServers;
}
return {
describe: function () {
return "detectNearestNDTServer - " +
"Timeout: " + timeout + 'secs, ' +
'Maximum number of attempts: ' + maxAttempts;
},
execute: function (callback) {
if (enable_ookla) {
ooklaServers.fetch().
then(function (servers) {
return ooklaServers.findNearestResponding(servers, timeout,
maxAttempts);
}).
then(callback).
catch(function (error) {
callback(null, error);
});
} else {
ndtServers.fetch().
then(function (servers) {
return ndtServers.findNearestResponding(servers, timeout,
maxAttempts);
}).
then(callback).
catch(function (error) {
callback(null, error);
});
}
}
};
};
module.exports.command = detectNearestNDTServer;