Dependency updates, some code cleaned up

This commit is contained in:
Christian Kuster
2019-09-21 11:56:22 +02:00
parent d1eac84058
commit 32b516e302
10 changed files with 125 additions and 99 deletions

View File

@ -3,31 +3,32 @@
* Created by kc on 04.04.16.
*/
const _ = require('lodash');
const _ = require('lodash');
// usually located in /usr/bin/ but as it could be at another location, allow all found in PATH (but we're in trouble
// when the default location, /usr/bin/ is not in the PATH!). GitHub issue #1
const tool = 'iwlist';
const cmdLine = tool + ' scan';
const tool = 'iwlist';
const cmdLine = tool + ' scan';
const detector = tool + ' --help';
const macRegex = /([0-9a-zA-Z]{1}[0-9a-zA-Z]{1}[:]{1}){5}[0-9a-zA-Z]{1}[0-9a-zA-Z]{1}/;
const cellRegex = /Cell [0-9]{2,} - Address:/;
/**
* Parsing the output of iwlist, tool having a lot of different faces :-(
* @param str output of the tool
* @param callback
*/
function parseOutput(str, callback) {
var err = null;
var wifis = [];
let err = null;
let wifis = [];
try {
var blocks = str.split(cellRegex);
let blocks = str.split(cellRegex);
blocks.forEach(block => {
var network = {};
var lines = block.split('\n');
let network = {};
let lines = block.split('\n');
if (macRegex.exec(lines[0])) {
// First line is the mac address (always! (?))
network.mac = lines[0].trim();
@ -46,22 +47,21 @@ function parseOutput(str, callback) {
else if (_.startsWith(line.trim(), 'Channel:')) {
network.channel = parseInt(_.trim(line, ' )').split(/:/)[1]);
}
// Another ugly thing, the signal which can have different formats, even worse als
// having different identifiers
else if (line.indexOf('Signal level') > -1) {
if (line.indexOf('Quality') > -1) {
// This is a "Quality=40/70 Signal level=-70 dBm" line
network.rssi = parseInt(line.substr(line.indexOf('Signal level') + 13), 10);
}
else {
} else {
// This is a "Signal level=60/100" line
var elements = line.split('=');
let elements = line.split('=');
elements.forEach(e => {
if (e.indexOf('/') > 0) {
// that's our part
var parts = e.split('/');
var level = Math.floor(100 * parseInt(parts[0], 10) / parseInt(parts[1], 10));
let parts = e.split('/');
let level = Math.floor(100 * parseInt(parts[0], 10) / parseInt(parts[1], 10));
network.rssi = level / 2 - 100;
}
})