2017-10-21 56 views
1

J'ai une application de géolocalisation pour un appareil Firefox OS (ZTE Open v1.1), qui diffuse à un serveur Web les détails de son emplacement.L'application ne se diffuse pas lorsque des traces de réveil sont ajoutées

Cependant, mon code ne s'exécute pas si je minimise l'application et éteins l'écran.

Je pensais ajouter les requestWakeLocks suivants au code trierait le problème, mais il ne semble pas aider:

var lock = window.navigator.requestWakeLock('gps'); 
var lock = window.navigator.requestWakeLock('wifi'); 

Avez-vous une idée de ce que je fais mal?

code:

function init() { 
    document.addEventListener("DOMContentLoaded", watchPosition, false); // to test on web browser 
    //document.addEventListener("deviceready", watchPosition, false); // deviceready is a cordova event 
} 

/* ---------------------------------- Local Variables ---------------------------------- */ 
var checkPeriodically; 
var watchPositionOutput = document.getElementById('watchPositionOutput'); 
var ajaxVars; // HTTP POST data values 

/* ---------------------------------- Local Functions ---------------------------------- */ 
function watchPosition() { 

    var lock = window.navigator.requestWakeLock('gps'); // FireFox-OS only - keeps the gps active when screen is off 
    var lock = window.navigator.requestWakeLock('wifi'); 
    checkPeriodically = setInterval(checkTime, 10000); 
    var watchID = navigator.geolocation.watchPosition(onSuccess, onError, options); 

    var options = { 
     enableHighAccuracy: true, 
    } 

    function onSuccess(position) { 
     ajaxVars = 
      "lt=" +  position.coords.latitude + 
      "&lg=" + position.coords.longitude + 
      "&ac=" + position.coords.accuracy + 
      "&sp=" + position.coords.speed + 
      "&ts=" + position.timestamp + 
      "&sec=SEC_TOKEN"; 

     var dt = new Date(position.timestamp); 
     date_time = 
      dt.getFullYear() + '-' + 
      (dt.getMonth() + 1) + '-' + 
      dt.getDate() + ' ' + 
      dt.getHours() + ':' + 
      dt.getMinutes() + ':' + 
      dt.getSeconds(); 

     watchPositionOutput.innerHTML = 
      'Latitude: ' + position.coords.latitude + '<br>' + 
      'Longitude: ' + position.coords.longitude + '<br>' + 
      'Accuracy: ' + position.coords.accuracy + '<br>' + 
      'Speed: '  + position.coords.speed  + '<br>' + 
      'Timestamp: ' + date_time     + '<br>'; 
    } 

    function onError(error) { 
     watchPositionOutput.innerHTML = 'code: ' + error.code + '<br>' +'message: ' + error.message + '<br>'; 
    } 

} 

// update the server with location data 
function ajax_post(postData){ 
    // when there is no data in postData 
    if (typeof(postData) === 'undefined') { return false; } // exit the function 

    var req = new XMLHttpRequest(); // Create XMLHttpRequest object 

    var url = "http://example.com/locate-me/post.php"; 
    //var url = "http://localhost/locate-me/post.php"; 

    req.open("POST", url, true); 

    // Set content type header info for sending url encoded variables in the request 
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 

    // Access the onreadystatechange event for the XMLHttpRequest object 
    req.onreadystatechange = function() { 
     if(req.readyState == 4 && req.status == 200) { 
      var return_data = req.responseText; // return whatever php echos 
      var date_time = new Date(return_data * 1000); // php is currently returning the time (timestamp) 
      document.getElementById("status").innerHTML = "Server time: " + date_time; 
     } 
    } 
    // Send data to PHP, and wait for response to update the status div 
    req.send(postData); // execute the request 
    document.getElementById("status").innerHTML = "processing..."; 
} 

// schedule to post the position data to a php script during certain times on certain days 
function checkTime(){ 
    // for example a day (day 0 == Sun) between 06:00 abd 23:45 
    var d = new Date(); 
    var today = d.getDay(); 
    var hms = d.getHours()+":"+d.getMinutes(); 
    // mon - thurs 
    if((today === 1 || today === 2 || today === 3 || today === 4) && hms > "10:23" && hms < "15:40") { 
     ajax_post(ajaxVars); 
    } 
    // friday 
    else if(today === 5 && hms > "13:00" && hms < "13:40") { 
     ajax_post(ajaxVars); 
    } 
    // testing: run all the time 
    else if(today < 7) { 
     ajax_post(ajaxVars); 
    } 
    else { 
     document.getElementById("status").innerHTML = "Data not scheduled to be posted to the server yet"; 
    } 
} 

init(); 

Répondre