Report multiple Resources in a single Event and Stream

In the examples from the previous topics, data from each Resource was routed to a different cloud Stream.

Depending on how you plan to handle the data on the cloud side, it can be beneficial to send most of your data in a single Stream.

The following diagram depicts how to accomplish this. In this Octave implementation, which builds off of that in Report multiple Resources independently to the Cloud, a single Edge Action has been created.

Here, thermometer Events are routed to the Edge Action through the Edge Action's Observation, but humidity Events are read by the Edge Action' s logic, by invoking the Octave Edge Action Runner API Datahub.read(). Wind speed Events are now routed to a Stream Observation, and the Edge Action gets their events by Datahub.query().

The Edge Action then processes data from all three Resources and sends a single Event to the cloud.

1772

πŸ“˜

Key Takeways

  • In order to send Resource data in a single Event, you can use an Edge Action to collect the data and create the Event with the resource payload that you want
  • One resource and Edge Action Observation are used to trigger the Edge Action
  • Other Resources can be read from the Edge Action:
    • directly via a call to Datahub.Read('resource') to get the latest value of a Resource.
    • by calling an Observation via Datahub.query('observation'), thus benefiting from the Observation characteristics (filter, buffering, min/max/mean).
  • The Event sent from the Edge Action will be routed to the device/:default Stream.

The following is an example of an Edge Action to handle this implementation:

function(event) {

return {
  "cl://": [{																								
    "temperature":  event.value,														
    "humidity":     Datahub.read('/humidity/value',0).value,
    "wind_speed":   Datahub.query('wind_speed','mean',10)
    }]
};

}

The following shows what the Event looks like in the device/:default Stream:

{
 "elems": {
    "humidity": 31.3,
    "temperature": 37.4,
    "wind_speed": 17.7
  }
}

πŸ‘

Learning more:

  • In order to report Events on a periodic basis rather than on an actual measurable Resource, a similar approach can be taken with 'util/counter/value' as the input Resource to trigger the Edge Action.
  • By directing the Edge Action output to sf:// instead of cl:// the Events will be handled by Store and Forward and sent according to its flushing rules.
  • if you want to use a similar approach but send the data to another a Stream other than device/:default, you can write out the Edge Action's output to a JSON Virtual Resource and set a Cloud Stream Observation on that Virtual Resource.