var delayInSecs = 20;
var maxStarts = Math.round(3600 / delayInSecs); // Stop tracking after 1 hour of inactivity, disabled now
var startCounter = 0;
var lastTimestampSent = 0;
var lastLatitude = 0;
var lastLongitude = 0;
var lastMessage = 'ok';
var epsilon = 0.00001; // less than 1-10 meters in the current zone of the world
// After the device wakes up from sleep mode and the courier opens the application tab in the browser,
// all missed starts of the tracking method (trackPoint) will be executed in bulk.
// In this case, the coordinates in all these launches will be the same.
// Manually create a send blocking flag so that there are no extra packages to the server with the same coordinates.
var locked = false;
function trackEvent(orderId, orderNumber, message, refererMethod)
{
var params = $.extend(true, {}, baseParams); // Clone base params
params.dataType = 'event';
params.orderId = orderId;
params.orderNumber = orderNumber;
params.timestamp = Math.round(+new Date() / 1000); // In seconds
params.message = message;
params.referer = refererMethod;
$.post('tracker.php', { json:JSON.stringify(params) });
}
function trackOrderDelivered(orderId, orderNumber, isDelivered) // Delivery message must be sent without blocking
{
var params = $.extend(true, {}, baseParams); // Clone base params
params.orderId = orderId;
params.orderNumber = orderNumber;
params.timestamp = Math.round(+new Date() / 1000); // In seconds
params.referer = "setOrderDelivered";
var redirectUrl = "list.php?setstatus=delivered&order=" + orderId; // Redirect to process a status change
if (isDelivered == false) {
params.referer = "setOrderUndelivered";
redirectUrl = "list.php?setstatus=ontheway&order=" + orderId;
}
if (!navigator.geolocation) {
// Navigator.geolocation is not supported, send error message without coordinates
params.dataType = 'error';
params.message = 'not-supported';
lastMessage = 'not-supported';
$.post('tracker.php', { json:JSON.stringify(params) });
document.location.href = redirectUrl;
} else
navigator.geolocation.getCurrentPosition(
function (position) {
params.dataType = 'delivery';
params.message = 'ok';
params.latitude = position.coords.latitude;
params.longitude = position.coords.longitude;
params.accuracy = position.coords.accuracy;
lastLatitude = params.latitude;
lastLongitude = params.longitude;
lastMessage = 'ok';
$.post('tracker.php', { json:JSON.stringify(params) });
document.location.href = redirectUrl;
},
function(x) {
// Unable to get device coordinates, send error message without coordinates
params.dataType = 'error';
params.message = 'unable-or-block';
lastMessage = 'unable-or-block';
$.post('tracker.php', { json:JSON.stringify(params) });
document.location.href = redirectUrl;
}
);
// You can’t do one start of the $.post method here,
// otherwise the tracker will get zero coordinates due to the delay in receiving coordinates from the device
}
function trackPoint() {
// Sending to the server is allowed?
if (!locked)
// Always block at the beginning, and after sending we unblock
locked = true;
else {
console.log("locked");
return;
}
// Stop tracking after maxStarts, disabled now (1 == 0)
if (1 == 0 && startCounter >= maxStarts) {
clearInterval(trackIntervalID);
locked = false;
return;
} else
startCounter++;
var params = $.extend(true, {}, baseParams);
params.timestamp = Math.round(+new Date() / 1000); // In seconds
// After the device wakes up from sleep mode and the courier opens the application tab in the browser,
// all missed starts of the tracking method (trackPoint) will be executed in bulk.
// With delayed starts, all timestamps will be the same, the coordinates will be the same, the last.
// There is no sense in this array of coordinates, we send only the last record to the server.
console.log('#' + startCounter + ' CHECK: ts ' + params.timestamp + ' last ' + lastTimestampSent + ' (ts - last < delay) ? => ' + (params.timestamp - lastTimestampSent < delayInSecs));
if (params.timestamp - lastTimestampSent < delayInSecs) {
locked = false;
return;
}
if (!navigator.geolocation) {
params.dataType = 'error';
params.message = 'not-supported';
if (params.message == lastMessage) {
console.log('#' + startCounter + ' Locked! params.message: [' + params.message + '] lastMessage: [' + lastMessage + ']');
locked = false;
return;
}
lastMessage = 'not-supported';
$.post('tracker.php', { json:JSON.stringify(params) });
var trackerAlert = document.getElementById('tracker-alert');
trackerAlert.style.display = "block";
} else
navigator.geolocation.getCurrentPosition(
function (position) {
params.dataType = 'point';
params.message = 'ok';
params.latitude = position.coords.latitude;
params.longitude = position.coords.longitude;
params.accuracy = position.coords.accuracy;
// Check if the coordinates match
var deltaLatitude = Math.abs(params.latitude - lastLatitude);
var deltaLongitude = Math.abs(params.longitude - lastLongitude);
if (deltaLatitude < epsilon && deltaLongitude < epsilon) {
console.log('#' + startCounter + ' Locked! deltaLatitude: [' + deltaLatitude + '] deltaLongitude: [' + deltaLongitude + '] epsilon: [' + epsilon + ']');
locked = false;
return;
}
lastLatitude = params.latitude;
lastLongitude = params.longitude;
lastMessage = 'ok';
$.post('tracker.php', { json:JSON.stringify(params) });
},
function(x) {
params.dataType = 'error';
params.message = 'unable-or-block';
if (params.message == lastMessage) {
console.log('#' + startCounter + ' Locked! params.message: [' + params.message + '] lastMessage: [' + lastMessage + ']');
locked = false;
return;
}
lastMessage = 'unable-or-block';
$.post('tracker.php', { json:JSON.stringify(params) });
var trackerAlert = document.getElementById('tracker-alert');
trackerAlert.style.display = "block";
}
);
lastTimestampSent = params.timestamp;
locked = false; // Remove the lock, sending the following packages to the server is allowed
console.log('#' + startCounter + ' ' + new Date() + ' lastTS: ' + lastTimestampSent + ' => trackPoint OK');
}
trackPoint();
var trackIntervalID = setInterval(trackPoint, delayInSecs * 1000);