Apama 10.7.2 | Connecting Apama Applications to External Components | Standard Connectivity Plug-ins | The Cumulocity IoT Transport Connectivity Plug-in | Using operations | Creating a new operation
 
Creating a new operation
send com.apama.cumulocity.Operation("","<SOURCE>","<STATUS>",
{"c8y_Message":<any> {<any>"text":<any>"Hello Cumulocity device"}} )
to com.apama.cumulocity.Operation.SEND_CHANNEL;
Where
*<SOURCE> is the source of the operation (same as the ManagedObject identifier).
*<STATUS> is the status of the operation. This can be PENDING.
Sending operations 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 Operation.SUBSCRIBE_CHANNEL channel. You will need to subscribe to the Operation.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 an operation:
using com.apama.cumulocity.Operation;
using com.apama.cumulocity.ObjectCommitFailed;
using com.apama.cumulocity.ObjectCommitted;

monitor Test_Operations {

action onload {
monitor.subscribe(Operation.SUBSCRIBE_CHANNEL);
Operation op := new Operation;
string name := "CreateOperation";
op.source := "7104835";
op.status := "PENDING";
op.params :=
{"c8y_Meassage":any(dictionary<any,any>,
{any(string,"text"):
any(string,"Hello Cumulocity device")
})
};

integer reqId := com.apama.cumulocity.Util.generateReqId();
// Create a new Operation in Cumulocity, ensuring that a response is
// returned.
send op.withChannelResponse(reqId, new dictionary<string, string>) to
Operation.SEND_CHANNEL;

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

// If the Operation 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;
}
}
}