Querying for measurements
To search for a measurement or a collection of measurements, send the com.apama.cumulocity.FindMeasurement event to Cumulocity IoT, with a unique reqId to the com.apama.cumulocity.FindMeasurement.CHANNEL channel.
The transport will then respond with zero or more com.apama.cumulocity.FindMeasurementResponse events and then one com.apama.cumulocity.FindMeasurementResponseAck event on the com.apama.cumulocity.FindMeasurementResponse.CHANNEL channel.
Example:
integer reqId := integer.incrementCounter("CumulocityRequestId");
com.apama.cumulocity.FindMeasurement request :=
new com.apama.cumulocity.FindMeasurement;
request.reqId := reqId;
// Filter based on measurement fragmentType
request.params.add("fragmentType", "c8y_MotionMeasurement");
// Subscribe to com.apama.cumulocity.FindMeasurementResponse.CHANNEL to listen
// for responses
monitor.subscribe(com.apama.cumulocity.FindMeasurementResponse.CHANNEL);
// Listen for responses based on reqId
on all com.apama.cumulocity.FindMeasurementResponse(reqId=reqId) as response
// Avoid listener leaks by terminating the listener on completion of the request
and not com.apama.cumulocity.FindMeasurementResponseAck(reqId=reqId)
{
log "Received Measurement " + response.toString() at INFO;
}
// Listen for com.apama.cumulocity.FindMeasurementResponseAck,
// this indicates that all responses have been received
on com.apama.cumulocity.FindMeasurementResponseAck(reqId=reqId)
as requestCompleted
{
log "Received all Measurement(s) for request "
+ requestCompleted.reqId.toString() at INFO;
// Request is completed and we can unsubscribe from this channel
monitor.unsubscribe(com.apama.cumulocity.FindMeasurementResponse.CHANNEL);
}
// Send request to find available measurements
send request to com.apama.cumulocity.FindMeasurement.CHANNEL;
Supported query parameters
You can provide the following query parameters with the request:
Parameter | Description |
id | Search for a measurement based on measurementId. When searching for a measurement based on Id, all the query parameters listed below are ignored. |
source | Search for measurements based on the device identifier or asset identifier of the source. |
type | Search for measurements based on the type. |
fragmentType | Search for measurements based on the fragment type. |
fromDate | Search for measurements from a start date. The date and time is provided as seconds since the epoch. |
toDate | Search for measurements within a time range. This is to be used in combination with fromDate. The date and time is provided as seconds since the epoch. |
pageSize | Indicates how many entries of the collection are to be retrieved from Cumulocity IoT. |
revert | Boolean parameter. By default, Cumulocity returns the oldest measurements first. You can reverse the order by adding the query parameter revert=true. This returns the most recent measurement first. |