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: Get Precipitation by a given latitude and Longitude

No ratings

This Groovy script gets the weather forecast for a given lat/long by calling an external web service.

Use in an Expression rule like this:

If: something

Then: SetDataItem ("precipitation", round(ExecuteCustomObject ("GetPrecipitation", location) ))

This sets the dataitem "precipitation" to the value returned by this script.

Parameters:

Variable Name               Display Name
location                         localtion (lat, lon)

import org.apache.commons.httpclient.methods.*

import org.apache.commons.httpclient.*

import java.text.SimpleDateFormat

def location = parameters.location.toString()

def locparts = location.split(',')

def lat = locparts[0]

def lon = locparts[1]

def hostname = " www.weather.gov"

def url = "/forecasts/xml/sample_products/browser_interface/ndfdXMLclient.php"

String ndfdElement = "pop12" // see http://www.weather.gov/forecasts/xml/docs/elementInputNames.php

def perceptTimeFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss");

def cal = Calendar.getInstance();

Date startDate = cal.getTime()

cal.add(Calendar.HOUR,12)

Date endDate = cal.getTime()

def client = new HttpClient ()

HostConfiguration host = client.getHostConfiguration()

host.setHost(hostname, 80, "http")

GetMethod get = new GetMethod (url)

NameValuePair [] params = new NameValuePair [6]

params[0] = new NameValuePair ("lat", lat);

params[1] = new NameValuePair ("lon", lon);

params[2] = new NameValuePair ("product", 'time-series');

params[3] = new NameValuePair ("begin", perceptTimeFormat.format(startDate));

params[4] = new NameValuePair ("end", perceptTimeFormat.format(endDate));

params[5] = new NameValuePair (ndfdElement, ndfdElement);

get.setQueryString(params)

client.executeMethod(host, get);

message = "Status:" + get.getStatusText()

content = get.getResponseBodyAsString()

get.releaseConnection()

// parse result XML and compute average

def dwml = new XmlSlurper ().parseText(content)

readings = dwml.data.parameters."probability-of-precipitation".value.collect { Integer.parseInt(it.toString()) }

average = readings.sum() / readings.size()

//logger.info "Expected precipitation for $location is $readings"

return readings[0]

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