Worklight Geofence错误动态不更新触发器

时间:2016-01-03 07:19:17

标签: ibm-mobilefirst geofencing

在工作灯中实施地理围栏。第一次触发地理围栏时效果很好。用户离开该区域后,我尝试再次更新触发器和startAcquisition,但在这种情况下,时间触发器不会被触发。

//display the position to the user
function displayPosition(pos) {
    $('#longitude').html('<b>Longitude:</b> ' + pos.coords.longitude);
    $('#latitude').html('<b>Latitude:</b> ' + pos.coords.latitude);
    $('#timestamp').html('<b>Timestamp:</b> ' + new Date(pos.timestamp));
}

function alertOnGeoAcquisitionErr(geoErr) {
    alert('Error acquiring geolocation (' + geoErr.code + '): ' + geoErr.message);
}
var triggers={};
    var geoPolicy = WL.Device.Geo.Profiles.LiveTracking();
    geoPolicy.timeout = 60000; // set timeout to 1 minute
    geoPolicy.maximumAge = 10000; // allow to use a position that is 10 seconds old


function getFirstPositionAndTrack() {
    alert('Click OK to proceed to acquire starting position');

    // use GPS to get the user's location

    // note: to see at high-accuracy, change RoughTracking above to LiveTracking

    // get the user's current position
    WL.Device.Geo.acquirePosition(
            function(pos) {

                displayPosition(pos);

                 triggers = generaTriggers(pos);

                startTrace({ Geo: geoPolicy },triggers,{ Geo: alertOnGeoAcquisitionErr })
            },
            function(geoErr) {
                alertOnGeoAcquisitionErr(geoErr);
                // try again:
                getFirstPositionAndTrack();
            },
            geoPolicy
        ); 
}

function onConnectSuccess(){
    // start up acquisition process
    getFirstPositionAndTrack();

}

function onConnectFailure(){


getFirstPositionAndTrack();

}

function wlCommonInit(){
    // Common initialization code goes here
    WL.Client.connect({
        onSuccess: onConnectSuccess,
        onFailure: onConnectFailure
    });

    // keep running while in background on Android; will show a notification
    WL.App.setKeepAliveInBackground(true);
}

function generaTriggers(pos){

    var triggers = {
          Geo: {
            posChange: { // display all movement
              type: "PositionChange",
              callback: function(deviceContext) {
                  displayPosition(deviceContext.Geo);
                }
            },

            leftArea: { // alert when we have left the area
              type: "Exit",
              circle: {
                longitude: pos.coords.longitude,
                latitude: pos.coords.latitude,
                radius: 200
              },
              callback: function(deviceContext) {
                alert('Left the area');
                //resetTriggers(deviceContext.Geo);
                startTrace({ Geo: geoPolicy },resetTriggers(deviceContext.Geo),{ Geo: alertOnGeoAcquisitionErr });
                //WL.Client.transmitEvent({ event: 'exit area'}, true);
              }
            },

            dwellArea: { // alert when we have stayed in the vicinity for 3 seconds
              type: "DwellInside",
              circle: {
                longitude: pos.coords.longitude,
                latitude: pos.coords.latitude,
                radius: 50
              },
              dwellingTime: 3000,
              callback: function() {
                alert('Still in the vicinity');
                //WL.Client.transmitEvent({ event: 'dwell inside area'}, true);
              }
            }
          } 
        };


    return triggers;
}

function resetTriggers(deviceContext){

//alert();
return generaTriggers(deviceContext);

}

function startTrace(policy, trigger, geoerr){
alert("trace");
                WL.Device.startAcquisition(policy, trigger, geoerr );

}

1 个答案:

答案 0 :(得分:1)

每个触发器的状态在调用startAcquisition之间保持不变;它依赖于触发器名称来标识唯一的配置,并且不会检查底层配置是否不同。

一般来说,你有2个选择。

  1. 对于您希望更新的触发器,请务必使用与当前触发器集不同的名称。请注意,如果存在应保持其内部状态的现有触发器(例如,设备是否已在配置区域中),则需要保留旧名称。
  2. 对startAcquisition进行额外的中间调用,传入一个触发器对象,删除所有要更新的触发器(删除它们),然后最终调用将使用原始名称(因为新配置将是应用)。