Apama 10.15.0 | Connecting Apama Applications to External Components | Standard Connectivity Plug-ins | The Cumulocity IoT Transport Connectivity Plug-in | Using managed objects | Updating a managed object
 
Updating a managed object
To enable use cases where information related to a managed object can be persisted, you can update any metadata information (such as the state) as properties of a managed object.
managedObject.params.add("<CUSTOM_PROPERTY>", <PROPERTY_VALUE>);
send managedObject to com.apama.cumulocity.ManagedObject.SEND_CHANNEL
Where
*<CUSTOM_PROPERTY> is the property that is to be added.
*<PROPERTY_VALUE> is the value for the newly added property.
Sending managed objects requesting response and setting headers
When creating a new object or updating an existing one, it is recommended that you use the withChannelResponse action. This allows your application to receive a response on completion on the ManagedObject.SUBSCRIBE_CHANNEL channel. You will need to subscribe to the ManagedObject.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 managed object:
using com.apama.cumulocity.ManagedObject;
using com.apama.cumulocity.ObjectCommitFailed;
using com.apama.cumulocity.ObjectCommitted;

monitor Test_ManagedObjects {

action onload {
monitor.subscribe(ManagedObject.SUBSCRIBE_CHANNEL);
ManagedObject mo := new ManagedObject;
mo.params.add("c8y_IsDevice", new dictionary<any, any>);
mo.name := "MyManagedObject";

mo.type := "DeviceType";
integer reqId := com.apama.cumulocity.Util.generateReqId();

// Create a new ManagedObject in Cumulocity, ensuring that a
// response is returned.
send mo.withChannelResponse(reqId, new dictionary<string, string>) to
ManagedObject.SEND_CHANNEL;

// If the ManagedObject creation succeeded do something with the
// returned object or id.
on ObjectCommitted(reqId=reqId) as c and not
ObjectCommitFailed(reqId=reqId){
log "New managed object successfully created " + c.toString()
at INFO;
}

// If the ManagedObject creation failed, log an error.
on ObjectCommitFailed(reqId=reqId) as cfail and not
ObjectCommitted(reqId=reqId){
log "Creating a new event failed " + cfail.toString() at ERROR;
}
}
}
Note: 
The following ManagedObject reference fields cannot be set using ManagedObject events and are useful for read-only purposes in these events: childDeviceIds, childAssetIds, deviceParentIds, and assetParentIds. However, these fields can be set using the Cumulocity IoT REST API which can be invoked in EPL by using GenericRequest events. For more information, see Invoking other parts of the Cumulocity IoT REST API and the information on child operations in the Cumulocity IoT OpenAPI documentation ( https://cumulocity.com/api/#tag/Child-operations).