domotz-remote-pawn
Version:
Domotz Agent
139 lines (117 loc) • 4.85 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/>.
**/
/**
*
* Created by Iacopo Papalini <ipapalini@domotz.com> on 10/09/15.
*/
module.exports.factory = function (_, procUtils, dto, number) {
var command = process.env.DOMOTZ_BIN_DIR + '/domotz_web100clt -ll ' +
'--disablemid --disablesfw -n ';
var bandWidthRegexp = /(\d+\.\d+) (.)b\/s/;
var dataRegexp = /^[^\:]+: +[0-9]+\.?[0-9]*/;
var errorRegexp = /^Connect\(\) for control socket failed/;
function gotUploadLine(item) {
return /^running [0-9]+s outbound test \(client to server\)[ \.\s]+/.test(item);
}
function gotDownloadLine(item) {
return /^running [0-9]+s inbound test \(server to client\)[ \.]+/.test(item);
}
function recordSpeedLine(item, str) {
var bandwidthValue = bandWidthRegexp.exec(item);
// conversion prefix to multiple (i.e. k => 1000)
var multiple = number.prefix2multiple(bandwidthValue[2]);
var speed = Math.floor(bandwidthValue[1] * multiple);
console.verbose('Storing %s bandwidth %s => %s', str, item, speed, {});
return speed;
}
function parseOutput(server, data) {
console.debug("Speed test successful");
var lines = data.toString().split(/\r?\n/);
var items = {raw: lines, server: server};
lines.forEach(function (item) {
// Check connect error
if (errorRegexp.test(item)) {
throw new Error('Processing NDT info for server ' +
server.service.hostname + ' failed to connect:' + item);
}
if (gotUploadLine(item) && bandWidthRegexp.test(item)) {
items['speed.upload'] = recordSpeedLine(item, 'outbound');
} else if (gotDownloadLine(item) && bandWidthRegexp.test(item)) {
items['speed.download'] = recordSpeedLine(item, 'inbound');
} else if (dataRegexp.test(item)) {
var fields = item.split(': ');
if (fields.length !== 2) {
console.warn('Fields malformed on item: %s', item, {});
} else {
items[fields[0]] = fields[1]; // putting the values to the corresponding item
console.verbose('Storing field %s with value %s',
fields[0], fields[1], {});
}
} else {
console.verbose('Discard line: ' + item);
}
});
return items;
}
return {
performSpeedTest: function (server) {
var hostname = server.service.hostname;
console.info("Launching slow web100clt on server %s", hostname);
console.debug("Command: %s %s", command, hostname);
return procUtils.exec(command + hostname)
.then(function(stdout) {
return parseOutput(server, stdout);
})
.catch(function(err) {
return {status: 500, message: err.message};
});
},
ndtInfoIntoDTO: function (ndtInfo) {
if (ndtInfo.rttsec) {
ndtInfo.rttsec *= 1000;
}
var map = {
'speed.upload': 'speed.upload',
'speed.download': 'speed.download',
'rttsec': 'speed.rtt',
'MaxRTT': 'speed.rttmax',
'MinRTT': 'speed.rttmin',
'server': 'server',
'raw': 'raw'
};
return dto.mapArrayToDTO(ndtInfo, map);
},
filterItems: function(ndtInfo) {
var items = _.pick(
ndtInfo,
'speed.upload',
'speed.download',
'rttsec',
'MinRTT',
'MaxRTT',
'server',
'raw');
items.rttmax = items.MaxRTT;
delete items.MaxRTT;
items.rttmin = items.MinRTT;
delete items.MinRTT;
items.rawdata = items.raw;
delete items.raw;
return items;
}
};
};