cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X

Axeda Groovy Script: mobileLocation object

No ratings

When an Expression Rule of type MobileLocation calls a Groovy script, the script is provided with the implicit object mobileLocation.  This example shows how the mobileLocation object can be used.

This Expression Rule calls the Groovy script 'getAddress' to retrieve the location and translate it into a street address:

Type:  MobileLocation

IF:      some condition e.g. true

THEN:  SetDataItem("location", str(ExecuteCustomObject("getAddress")))

The 'getAddress' script uses the mobileLocation object to retrieve the asset's reported location, and then calls a REST service to translate a given latitude and longitude to a street address.  The street address is returned.

import groovyx.net.http.RESTClient

String rmdHostname =  "http://ws.geonames.org";

if (mobileLocation != null)

{

rmd = new RESTClient(rmdHostname);

try {

      def resp = rmd.get( path: 'findNearestAddress',

                      query:[lat:mobileLocation.lat , lng:mobileLocation.lng] )

       streetnum = resp.data.address.streetNumber.text()

       street = resp.data.address.street.text()

       town = resp.data.address.adminName2.text()

       state = resp.data.address.adminCode1.text()

       postalcode = resp.data.address.postalcode.text()

    

      return streetnum + " " + street + " " +  town + " " + state + " " + postalcode

} catch (groovyx.net.http.HttpResponseException e) {

    e.printStackTrace();

    }

}

Version history
Last update:
‎May 26, 2016 05:43 PM
Updated by:
Labels (2)