Creating measurement fragments
You can send a single fragment to Cumulocity IoT to create a single-fragment measurement.
Example of creating a measurement fragment:
using com.apama.cumulocity.MeasurementFragment;
using com.apama.cumulocity.ObjectCommitFailed;
using com.apama.cumulocity.ObjectCommitted;
monitor Test {
string deviceId; // Where this is populated from the actual device Id.
float timestamp; // Where this is populated from the timestamp of the
// device.
action onload {
MeasurementFragment mf := new MeasurementFragment;
mf.type := "test_measurement";
mf.source := deviceId;
mf.time := timestamp;
mf.valueFragment := "c8y_speed";
mf.valueSeries := "speedX";
mf.value := 12.0;
mf.unit := "km/hr";
integer reqId := com.apama.cumulocity.Util.generateReqId();
send mf to MeasurementFragment.SEND_CHANNEL;
}
}
Where
source is the source of the measurement.
time is the time at which the measurement was taken.
type is the type of the measurement.
valueFragment is the name of the fragment of the measurement fragment.
valueSeries is the name of the series of the measurement fragment.
value is the value from the sensor.
unit is the units the reading is in, for example, mm, lux, km/hr.
Sending measurement fragments requesting a response and setting headers
When creating a new object, it is recommended that you use the withChannelResponse action. This allows your application to receive a response on completion on the MeasurementFragment.SUBSCRIBE_CHANNEL channel. You will need to subscribe to the MeasurementFragment.SUBSCRIBE_CHANNEL channel first. The response can be one of two possibilities:
ObjectCommitted This includes the reqId which is the identifier of the original request, the Id which is the identifier of the newly created or updated object, and the actual object in JSON form.
ObjectCommitFailed This includes the reqId which is the identifier of the original request, the statusCode which is the HTTP status code of the failure, and the body which is the content of the response from the API (this might be in HTML format).
When using withChannelResponse, it allows the ability to set headers. This can be used, for example, to determine what processing mode Cumulocity IoT will use as shown in the example below.
Example of creating a measurement fragment:
using com.apama.cumulocity.MeasurementFragment;
using com.apama.cumulocity.ObjectCommitFailed;
using com.apama.cumulocity.ObjectCommitted;
monitor Test {
string deviceId; // Where this is populated from the actual device Id.
string timestamp; // Where this is populated from the timestamp of the
// device.
action onload {
monitor.subscribe(MeasurementFragment.SUBSCRIBE_CHANNEL);
MeasurementFragment mf := new MeasurementFragment;
mf.type := "test_measurement";
mf.source := deviceId;
mf.time := timestamp;
mf.valueFragment := "c8y_speed";
mf.valueSeries := "speedX";
mf.value := 12.0;
mf.unit := "km/hr";
integer reqId := com.apama.cumulocity.Util.generateReqId();
// Create a new Measurement in Cumulocity from a single
// MeasurementFragment, ensuring that a response is returned
// and the processing mode, indicating how to process the request, sent
// to Cumulocity is Transient.
send mf.withChannelResponse(reqId, { "X-Cumulocity-Processing-Mode":
"Transient" }) to MeasurementFragment.SEND_CHANNEL;
// If the Measurement creation succeeded do something with the returned
// object or id.
on ObjectCommitted(reqId=reqId) as c and not
ObjectCommitFailed(reqId=reqId){
log "New measurement successfully created " + c.toString() at INFO;
}
// If the Measurement creation failed, log an error.
on ObjectCommitFailed(reqId=reqId) as cfail and not
ObjectCommitted(reqId=reqId){
log "Creating a new measurement failed " + cfail.toString()
at ERROR;
}
}
}