UNPKG

domotz-remote-pawn

Version:

Domotz Agent

75 lines (63 loc) 2.52 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 Tommaso Latini <tommaso@domotz.com> on 14/12/15. */ function getNdtServerMessage() { return { correlationId: 'EVENT', payload: { timeout: 3, maximum_attempts: 10, }, }; } function getSpeedTestMessage(server) { return { correlationId: 'EVENT', payload: { server_id: server.id, }, non_interactive: true, }; } var speedTest = function (resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var status = resourceLocator.status; if (!status.canExecuteEventHandler()) { myConsole.info('Pawn in restricted or updating mode. Cannot perform Speed Test'); return; } // "configuration" is defined at this point, but "settings" might be // undefined before 1.3.7 version. In this case "true" by default. var settings = resourceLocator.configuration.settings; var enabled = settings === undefined || settings.auto_speed_test === undefined || settings.auto_speed_test; if (!enabled) { myConsole.warn('Automated speed test is disabled'); return; } var executeCommand = resourceLocator.utils.command.execute; var join = resourceLocator.path.join; var commandNetworkRoot = join(__dirname, '..', '..', 'commands', 'network'); var ndtServerModulePath = join(commandNetworkRoot, 'detectNearestNDTServer.js'); var speedTestModulePath = join(commandNetworkRoot, 'speedTest.js'); executeCommand(ndtServerModulePath, getNdtServerMessage(), resourceLocator, function callback(server) { var speedTestMessage = getSpeedTestMessage(server); executeCommand(speedTestModulePath, speedTestMessage, resourceLocator); }); }; module.exports.handler = speedTest;