Apama 10.7.2 | Connecting Apama Applications to External Components | Standard Connectivity Plug-ins | The Cumulocity IoT Transport Connectivity Plug-in | Using alarms | Creating a new alarm
 
Creating a new alarm
send Alarm("","c8y_SampleAlarm","<SOURCE>",<TIME>,
"Alarm text", "<STATUS>","<SEVERITY>",1,new dictionary<string,any>) to Alarm.SEND_CHANNEL;
Where
*<SOURCE> is the source of the alarm (same as the ManagedObject identifier).
*<TIME> is the time at which the alarm was generated.
*<STATUS> is the status of the alarm. This can be ACTIVE, ACKNOWLEDGED or CLEARED.
*<SEVERITY> is the severity of the alarm. This can be CRITICAL, MAJOR, MINOR or WARNING.
Sending alarms 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 Alarm.SUBSCRIBE_CHANNEL channel. You will need to subscribe to the Alarm.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 alarm:
using com.apama.cumulocity.Alarm;
using com.apama.cumulocity.ObjectCommitFailed;
using com.apama.cumulocity.ObjectCommitted;

monitor TestCreatingAlarm {
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(Alarm.SUBSCRIBE_CHANNEL);
Alarm al := new Alarm;
string name := "MyTestAlarm";
al.status := "ACTIVE";
al.severity := "CRITICAL";
al.source := deviceId;
al.type := "c8y_TestAlarm";
al.text := "test alarm";
al.time := currentTime;

integer reqId := com.apama.cumulocity.Util.generateReqId();

// Create a new Alarm in Cumulocity, ensuring that a response is
// returned
// and the processing mode, indicating how to process the request,
// sent to Cumulocity is Transient.
send al.withChannelResponse(reqId, { "X-Cumulocity-Processing-Mode":
"Transient" }) to Alarm.SEND_CHANNEL;

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

// If the Alarm 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;
}
}
}
Alarm de-duplication
If an active or acknowledged alarm (does not work for the CLEARED status) with the same source and type exists, no new alarm is created. Instead, the existing alarm is updated by incrementing the count property, and the time property is also updated. Any other changes are ignored, and the alarm history is not updated. The first occurrence of the alarm is recorded in firstOccurenceTime.