Skip to content

Commit

Permalink
Merge pull request #356 from prey/wifi-retry
Browse files Browse the repository at this point in the history
Wifi location strategy retry
  • Loading branch information
javo authored Apr 5, 2018
2 parents 1d1af8d + 8caf681 commit d13c447
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 29 deletions.
9 changes: 9 additions & 0 deletions lib/agent/providers/geo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ var strategies = require('./strategies'),
hooks = require('./../../hooks'),
logger = require('../../common').logger.prefix('geo');

var attempt = 0,
MAX_ATTEMPS = 3;

function log_error(err, strategy) {
logger.debug("Error getting location using " + strategy + " strategy: " + err);
}
Expand All @@ -25,11 +28,17 @@ exports.fetch_location = function(cb) {
}

function wifi_cb(err, res) {
attempt++;
logger.debug("Location via wifi strategy, attempt: " + attempt);
if (err) {
log_error(err, "wifi");
if (attempt < MAX_ATTEMPS)
return strategies.wifi(wifi_cb);
attempt = 0;
return strategies.geoip(geoip_cb);
}

attempt = 0;
return cb(null, res);
}

Expand Down
6 changes: 4 additions & 2 deletions lib/agent/providers/geo/strategies.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ function wifi(cb) {
return process_response(stdout.geolocation, cb);

if (!stdout.endpoint) return cb(new Error("No location endpoint available"));
var url = stdout.endpoint.url;
var url = stdout.endpoint.url,
provider = stdout.endpoint.provider;

var options = {
user_agent: stdout.endpoint['user-agent'],
Expand All @@ -86,7 +87,8 @@ function wifi(cb) {
var geolocation = stdout;
var loc_data = {
"geolocation": geolocation,
"wifiAccessPoints": aps
"wifiAccessPoints": aps,
"provider": provider
}

// Send the new location info and process it
Expand Down
39 changes: 17 additions & 22 deletions lib/agent/providers/geo/win32/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var common = require('./../../../common');

var geo;

function load_geo() {
Expand All @@ -15,30 +13,27 @@ function load_geo() {
}

exports.get_location = function(cb) {
// Force networks re-scan to avoid empty APs list
common.system.scan_networks(function() {
var geo = load_geo();
var geo = load_geo();

if (!geo.Geolocator)
return cb(new Error('Unable to load geolocation module: ' + geo.message))
if (!geo.Geolocator)
return cb(new Error('Unable to load geolocation module: ' + geo.message))

function locate() {
var loc = new geo.Geolocator();
function locate() {
var loc = new geo.Geolocator();

loc.getGeopositionAsync(function(err, res) {
if (err || !res.coordinate)
return cb(err || new Error('Unable to get location.'))
loc.getGeopositionAsync(function(err, res) {
if (err || !res.coordinate)
return cb(err || new Error('Unable to get location.'))

var obj = {
lat: res.coordinate.latitude,
lng: res.coordinate.longitude,
accuracy: res.coordinate.accuracy
}
var obj = {
lat: res.coordinate.latitude,
lng: res.coordinate.longitude,
accuracy: res.coordinate.accuracy
}

cb(null, obj);
});
}
cb(null, obj);
});
}

locate();
})
locate();
}
12 changes: 8 additions & 4 deletions lib/system/windows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
var path = require('path'),
wmic = require('wmic'),
os = require('os'),
exec = require('child_process').exec,
cp = require('child_process'),
exec = cp.exec,
spawn = cp.spawn,
os_name = process.platform.replace('win32', 'windows');

// add windows bin path to env
Expand Down Expand Up @@ -60,9 +62,11 @@ exports.reconnect = function(cb) {

exports.scan_networks = function(cb) {
var cmd_path = bin_path('wlanscan.exe');
exec('"' + cmd_path + '" /triggerscan', function() {
setTimeout(cb, 2000);
})

var child = spawn(cmd_path, ['/triggerscan'], {});
child.on('exit', function() {
cb();
});
}

function bin_path(executable) {
Expand Down
2 changes: 1 addition & 1 deletion test/lib/agent/providers/geo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe('geoloc fallback', function() {
err.should.equal(err);
should(res).not.exist;
stubs.native.calledOnce.should.equal(true);
stubs.wifi.calledOnce.should.equal(true);
stubs.wifi.calledThrice.should.equal(true);
stubs.geoip.calledOnce.should.equal(true);
done();
});
Expand Down

0 comments on commit d13c447

Please sign in to comment.