Apama 10.15.0 | Connecting Apama Applications to External Components | Standard Connectivity Plug-ins | The Cumulocity IoT Transport Connectivity Plug-in | Using events | Creating a new event
 
Creating a new event
send Event("","c8y_SampleEvent","<SOURCE>", <TIME>,
"Event text",new dictionary<string,any>) to Event.SEND_CHANNEL;
Where
*<SOURCE> is the source of the event (same as the ManagedObject identifier).
*<TIME> is the time at which the event was generated.
Sending events 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 Event.SUBSCRIBE_CHANNEL channel. You will need to subscribe to the Event.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 event:
using com.apama.cumulocity.Event;
using com.apama.cumulocity.ObjectCommitFailed;
using com.apama.cumulocity.ObjectCommitted;

monitor Test_CumulocityEvents {
string timestamp; // Where this is populated from the timestamp of the
// device.

action onload {
monitor.subscribe(Event.SUBSCRIBE_CHANNEL);
Event ev := new Event;
string name := "MyEvent";
ev.type := "DoorSensor";
ev.source := "7104838";
ev.text := "Door sensor was triggered";
ev.time := currentTime;

integer reqId := com.apama.cumulocity.Util.generateReqId();
// Create a new Event in Cumulocity, ensuring that a response is
// returned
// and the processing mode, indicating how to process the request, sent
// to Cumulocity is Transient.
send ev.withChannelResponse(reqId, { "X-Cumulocity-Processing-Mode":
"Transient" }) to Event.SEND_CHANNEL;


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

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