Skip to end of metadata
Go to start of metadata

For this use case, we present the Yahoo public weather service as an example of a web service that will be queried by a polling device. This can represent any web service that returns sensor data.

Query the Web Service

These steps query the web service, requesting the humidity, pressure, and temperature in Paris and New York.

  1. Open the Yahoo development portal in a browser: https://developer.yahoo.com/weather/



  2. Select JSON for the Response.
  3. Enter the following query into the YQL Query field and click Test.

    select location.city, atmosphere, item.lat, item.long, item.condition.temp from weather.forecast where woeid in (2459115,615702)

Each execution of the polling device creates two child devices (one for each city), with the following OMA resources:

  • location
  • humidity
  • pressure
  • temperature

Web Service Endpoint

{"query":{"count":2,"created":"2018-05-22T15:52:38Z","lang":"en-US","results":{"channel":[{"location":{"city":"New York"},"atmosphere":{"humidity":"66","pressure":"1019.0","rising":"0","visibility":"16.1"},"item":{"lat":"40.71455","long":"-74.007118","condition":{"temp":"64"}}},{"location":{"city":"Paris"},"atmosphere":{"humidity":"56","pressure":"1005.0","rising":"0","visibility":"16.1"},"item":{"lat":"48.85693","long":"2.3412","condition":{"temp":"69"}}}]}}}

Web Service Response

{
"query": {
  "count": 2,
  "created": "2018-05-12T18:08:36Z",
  "lang": "en-US",
  "results": {
   "channel": [
    {
     "location": {
      "city": "New York"
     },
     "atmosphere": {
      "humidity": "83",
      "pressure": "1018.0",
      "rising": "0",
      "visibility": "16.1"
     },
     "item": {
      "lat": "40.71455",
      "long": "-74.007118",
      "condition": {
       "temp": "57"
      }
     }
    },
    {
     "location": {
      "city": "Paris"
     },
     "atmosphere": {
      "humidity": "69",
      "pressure": "1004.0",
      "rising": "0",
      "visibility": "16.1"
     },
     "item": {
      "lat": "48.85693",
      "long": "2.3412",
      "condition": {
       "temp": "62"
      }
     }
    }
   ]
  }
}
}

Create a Model to Poll the Web Service

To create a LoopCloud model to poll the web service:

  1. Click the company name at the top of the LoopCloud window.
  2. Click a Project.
  3. In the navigation panel, select Configuration > Models.
  4. Select the POLL tab. See also, Model Types.
  5. Click New Model.
  6. In the CONFIG tab:
    1. Enter a Model Name.
    2. Select a Method: Get.
    3. Enter a Request URL.

      https://query.yahooapis.com/v1/public/yql?q=select location.city, atmosphere, item.lat, item.long, item.condition.temp from weather.forecast where woeid in (2459115,615702)&format=json&env=store://datatables.org/alltableswithkeys
  7. Select a Poll Interval: every hour.

    Note:

    • The interval could be set to less than an hour.
    • Keep in mind that the Yahoo web service used in this example is a public service, which has usage limits. Yahoo may restrict access if the web service is called too often.




  8. Click Test to view the JSON response:


  9. Click Save.

Configure Transformation

To configure transformation in the LoopCloud Poll Model:

  1. Select the TRANSFORMATION tab in the Device Model Editor, to configure transformation for the Yahoo weather poll model.
  2. Paste the following Groovy script into the Step 1 window and select a script type of Groovy.

    //----------------------------------------------------------------------------------------
    import com.litmusloop.common.device.Payload
    import groovy.json.JsonSlurper
    
    /**
    * in: byte[] input
    * in: OMNARegistry omnaRegistry
    * out: return List<Payload.Value>
    */
    def jsonSlurper = new JsonSlurper()
    
    // Parse the webservice response 
    def messages = jsonSlurper.parseText(new String(input))
    
    // Declare  the result variable
    List<Payload.Value> result = new ArrayList<>();
    
    // For each query.results.channel element from the response
    
    messages.query.results.channel.each {
        println it
        // temperature
        result.add(new Payload.Value(
                3303,       // OMA Temperature object ID
                "0",        // arbitrary instance ID = 0
                5700,       // Temperature Sensor Value
                "Float",    // Datatype
                it.item.condition.temp,  // value
                ["deviceId": it.location.city]  // deviceID
        ))
        // humidity
        result.add(new Payload.Value(
                3304,       // OMA Humidity object ID
                "0",        // arbitrary instance ID = 0
                5700,       // Humidity Sensor Value
                "Float",    // Datatype
                it.atmosphere.humidity,  // value
                ["deviceId": it.location.city]  // deviceID
        ))
        // pressure
        result.add(new Payload.Value(
                3315,       // OMA Pressure object ID
                "0",        // arbitrary instance ID = 0
                5700,       // Pressure Sensor Value
                "Float",    // Datatype
                it.atmosphere.pressure,  // value
                ["deviceId": it.location.city]  // deviceID
        ))
        // location latitude
        result.add(new Payload.Value(
                6,       // OMA Location object ID
                "0",        // arbitrary instance ID = 0
                0,       // latitude
                "Float",    // Datatype
                it.item.lat,  // value
                ["deviceId": it.location.city]  // deviceID
        ))
        // location  Longitude
        result.add(new Payload.Value(
                6,       // OMA Location object ID
                "0",        // arbitrary instance ID = 0
                1,       // Longitude
                "Float",    // Datatype
                it.item.long,  // value
                ["deviceId": it.location.city]  // deviceID
        ))
    }
    
    return result
    //----------------------------------------------------------------------------------------
  3. Click Test.
  4. Click Save.

Add a Device from the Poll Model

To add a LoopCloud device directly from a Poll Model, see Add a Device from a Model.

The transformation script will autocreate two additional child devices, one for each city in this example.

View Devices and Data

Once the poll has triggered data collection (polling interval was set to one hour), the poll device and two autocreated child devices can be viewed in the Devices list (Device Mgmt > Devices).

In this example:

  • Yahoo weather device is the user-created device.
  • New York device was autocreated when the Yahoo web service was polled.
  • Paris device was autocreated when the Yahoo web service was polled.

View Device Details

  1. Click the link for the New York device and select the EXPLORE tab to view the web service response.

2. Toggle the switch to view the parsed details: 

This parsed view lists all resources from the web service response.


Steps in this example:

  • No labels