Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error reconnecting too quickly geofences #766

Merged
merged 5 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions lib/agent/triggers/control-zones/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ var emitter,
retryTimeOut,
checking = false;

let timerActionStart;

var push_event = function(type, zone_id, coords) {
coords.id = zone_id.id;
var data = {
Expand Down Expand Up @@ -90,9 +92,11 @@ var check_zones = function() {
// distance comes in KM, so we transform to M to compare

if (distance * 1000 > zone.radius) { // outside

storage.do('update', { type: 'geofences', id: zone.id, columns: 'state', values: 'outside' }, (err) => {
if (err) logger.error(err);
if (err) {
logger.error(err);
return;
}
if (zone.state == 'inside' && zone.direction !== 'in') {
logger.info('Device left the geofence ' + zone.name + '! Notifying')
hooks.trigger('geofencing_out', zone.id)
Expand All @@ -101,11 +105,12 @@ var check_zones = function() {
checking = false;
zone.state = 'outside';
});

} else { // inside

storage.do('update', { type: 'geofences', id: zone.id, columns: 'state', values: 'inside' }, (err) => {
if (err) logger.error(err);
if (err) {
logger.error(err);
return;
}
if ((zone.state == 'outside' && zone.direction !== 'out') || zone.state == null) {
logger.info('Device got inside the geofence ' + zone.name + '! Notifying')
hooks.trigger('geofencing_in', zone.id)
Expand All @@ -114,7 +119,6 @@ var check_zones = function() {
checking = false;
zone.state = 'inside';
});

}
});
}
Expand Down Expand Up @@ -160,12 +164,15 @@ exports.start = function(opts, cb) {
hooks.on('connected', function() {
clearTimeout(retryTimeOut)
attempts = 0;

action.start();
if (timerActionStart) clearTimeout(timerActionStart);
timerActionStart = setTimeout(() => {
action.start();
}, 1000 * 15);
})

// No need to keep checking the location until it's connected again
hooks.on('disconnected', function() {
if (timerActionStart) clearTimeout(timerActionStart);
clearTimeout(retryTimeOut)
attempts = 0;
})
Expand Down
6 changes: 6 additions & 0 deletions test/lib/agent/providers/geo/geonative_strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var helpers = require('./../../../../helpers'),
geo = {
darwin: require(join(geo_path, 'darwin')),
linux: require(join(geo_path, 'linux')),
win32: require(join(geo_path, 'win32')),
win: require(join(geo_path, 'win32')),
},
geonative_strat = helpers.load('providers/geo/strategies').native;
Expand All @@ -26,6 +27,11 @@ describe('native geoloc', function () {
}

var successful_location = {
win32: {
lat: 38.707163,
lng: -9.135517,
accuracy: 140000.0
},
win: {
lat: 38.707163,
lng: -9.135517,
Expand Down