Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X
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();
}
}