Search code examples
javascriptibm-mobilefirstworklight-geolocation

Create geo triggers dynamically worklight


I am implementing geofence in worklight but I want to create geo triggers dynamically. I saw this post and tried her code but it's not working for me. I used fake location and entered a geofence but the callback function was not triggered. Here's my code. branch_data_g is a json string containing the longitude and latitudes.

function getFirstPositionAndTrack() {
    WL.Logger.info("getfirstpositionandtrack function");
    // use GPS to get the user's location
    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

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

    // get the user's current position
    WL.Device.Geo.acquirePosition(
            function(pos) {
                WL.Logger.debug("acquired position");   
                WL.Logger.debug("Longitude: " + pos.coords.longitude);
                WL.Logger.debug("Latitude: " + pos.coords.latitude);

                var triggers = new Object();
                triggers.Geo = {};

                var trigger_events = generateTrigger();

                triggers.Geo = trigger_events;              

                WL.Device.startAcquisition({ Geo: geoPolicy }, triggers, { Geo: alertOnGeoAcquisitionErr } );
            },
            function(geoErr) {
                alertOnGeoAcquisitionErr(geoErr);
                // try again:
                getFirstPositionAndTrack();
            },
            geoPolicy.Geo
        ); 
}

function generateTrigger() {
    var parsed_data = JSON.parse(branch_data_g);
    WL.Logger.info("generatetrigger function: " + parsed_data.branches.length);
    var trigger_events = new Object();
    WL.Logger.info("sample: " + parsed_data.branches[0].latitude);
    for(var i = 0; i < parsed_data.branches.length; i++) {
        var trigger = {
                type: "Enter",
                circle: {
                    longitude: parsed_data.branches[i].longitude,
                    latitude: parsed_data.branches[i].latitude,
                    radius: 100
                },
                callback: function() {
                    WL.Logger.info("Enter branch");
                    WL.Client.transmitEvent({ branch: "enter branch"}, true);
                }
        };
        trigger_events["branch"+i] = trigger;
    }

    return trigger_events;
}

Here's my code in my adapter:

function eventHandler() {
    //nothing to do here..
}

WL.Server.setEventHandlers([
    WL.Server.createEventHandler({branch: 'enter branch'}, eventHandler)
]);

Am I missing something? Thanks in advance!


Solution

  • Did you call WL.Client.connect first? Note that WL.Client.transmitEvent won't send events until a connection has been established at least once.

    I take it you are checking the callback by looking for the the WL.Logger.info messages. Otherwise, I don't see you doing anything on the server side (although the event handling could still be logged in the raw reports database).

    As well, you may want to try a DwellInside trigger (with dwellingTime set to 0) as opposed to an Enter; Enter requires that you start outside the area and then enter inside it.