Report multiple Resources in a single Event and Stream
In the previous examples, 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.
This is how to do so:
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 EA Observation is used to trigger the EA
- other Resources can be ready from the EA:
- directly via a 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 EA will be routed to the 'device/:default' stream
Here's a short example of an EA code handling this implementation:
function(event) {
return {
"cl://": [{
"temperature": event.value,
"humidity": Datahub.read('/humidity/value',0).value,
"wind_speed": Datahub.query('wind_speed','mean',10)
}]
};
}
and this is 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 EA
- by directing the EA output to "sf://" instead of "cl://" the Events will be handled by the S&F and sent according to its flushing rules.
- if you want to use a similar approach but send the data to another stream than 'device/:default', you can write rout EA output to a JSON VR and set a CS Observation on that VR
Updated over 4 years ago