domotz-remote-pawn
Version:
Domotz Agent
69 lines (59 loc) • 2.3 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/>.
**/
var mtr = function (message, resourceLocator) {
const time = resourceLocator.utils.time;
const mtr = resourceLocator.networkTools.mtr;
const scriptsConf = resourceLocator.configuration.external_scripts;
const engineClient = resourceLocator.engineRequest;
const count = message.payload.count || scriptsConf.number_of_packets_for_mtr;
const destination = message.payload.destination || scriptsConf.remote_host_for_mtr_computation;
var results = {
stats: {
start: new Date(),
end: null,
elapsed_milliseconds: null
},
configuration: {
count: count,
destination: destination
},
mtr: null
};
function copyResults(mtrInfo) {
results.stats.end = new Date();
results.stats.elapsed_milliseconds = time.getDiff(results.stats);
results.mtr = mtr.mtrInfoToDTO(mtrInfo);
return mtrInfo;
}
return {
describe: function () {
return message.correlationId + " - MTR message; count: " + count;
},
execute: function (callback) {
mtr.getMtrInfo(destination, count).
then(copyResults).
then(function finalize(items) {
callback(results);
return engineClient.sendWithPromise('/network/mtr', 'POST', items);
}).
catch(function (err) {
callback(null, err);
});
}
};
};
module.exports.command = mtr;