using com.apama.cumulocity.Alarm; using com.apama.cumulocity.FindAlarm; using com.apama.cumulocity.FindAlarmResponse; using com.apama.cumulocity.FindAlarmResponseAck; /** An object that holds query parameters. */ event AlarmFilter { /** Param to indicate how many entries of the collection * are to be retrieved from Cumulocity IoT. */ constant string PAGE_SIZE := "pageSize"; /** Param to search for alarms based on the type. */ constant string TYPE := "type"; /** Param to fetch alarms based on the device identifier. */ constant string SOURCE := "source"; /** Param to fetch alarms based on the status.*/ constant string STATUS := "status"; /** Param to fetch alarms based on the severity. */ constant string SEVERITY := "severity"; /** Search for alarms from a specific date. */ constant string FROM_DATE := "fromDate"; /** Search for alarms up to a specific date .*/ constant string TO_DATE := "toDate"; /** Create and return a new AlarmFilter object. */ static action init() returns AlarmFilter { return new AlarmFilter; } /** Request with pageSize. */ action withPageSize(integer pageSize) returns AlarmFilter { return addParam(PAGE_SIZE, pageSize.toString()); } /** Filter based on Type. */ action byType(string type) returns AlarmFilter { return addParam(TYPE, type); } /** Filter based on Source. */ action bySource(string source) returns AlarmFilter { return addParam(SOURCE, source); } /** Filter based on Status. */ action byStatus(string status) returns AlarmFilter { return addParam(STATUS, status); } /** Filter based on Severity. */ action bySeverity(string severity) returns AlarmFilter { return addParam(SEVERITY, severity); } /** Search for alarms from a specific date. */ action fromDate(float fromDate) returns AlarmFilter { return addParam(FROM_DATE, fromDate.toString()); } /** Search for alarms up to a specific date. */ action toDate(float toDate) returns AlarmFilter { return addParam(TO_DATE, toDate.toString()); } /** Add a query parameter. */ action addParam(string paramName, string paramValue) returns AlarmFilter { params.add(paramName, paramValue); return self; } /** Return query parameters. */ action getParameters() returns dictionary { return params; } /** Query parameters. */ dictionary params; } /** Helper to query for alarms. */ event AlarmLookupHelper { /** Callback for successful responses. */ action responseCallback; /** Callback for completion acknowledgement. */ action responseCompletedCallback; /** Create and return a new AlarmLookupHelper object. */ static action init() returns AlarmLookupHelper { return new AlarmLookupHelper; } /** Register query response callback. */ action withResponseCallback(action responseCb) returns AlarmLookupHelper { responseCallback := responseCb; return self; } /** Register query completed callback. */ action withResponseCompletedCallback(action responseCompletedCb) returns AlarmLookupHelper { responseCompletedCallback := responseCompletedCb; return self; } /** Simple request without any params to fetch available alarms. */ action get() returns integer { return getWithFilter(AlarmFilter.init()); } /** Query for alarms based on a filter. */ action getWithFilter(AlarmFilter filter) returns integer { /** Subscribe to FindAlarmResponse.CHANNEL to listen for responses. */ monitor.subscribe(FindAlarmResponse.CHANNEL); /** Unique request identifier. */ integer reqId := integer.getUnique(); /** Listen for matching responses. */ on all FindAlarmResponse(reqId=reqId) as resp and not FindAlarmResponseAck(reqId=reqId) { ifpresent responseCallback { responseCallback(reqId, resp.alarm); } } /** Listen for request completed acknowledgement. */ on FindAlarmResponseAck(reqId=reqId) { monitor.unsubscribe(FindAlarmResponse.CHANNEL); ifpresent responseCompletedCallback { responseCompletedCallback(reqId); } } /** Send request to find available alarms. */ send FindAlarm(reqId, filter.getParameters()) to FindAlarm.CHANNEL; return reqId; } } /** * Query for Alarm(s) based on type. * * This application sends a request to query for Alarms of type * "UnavailabilityAlarm" and creates listeners to listen for 0 or more * FindAlarmResponse events followed by the FindAlarmResponseAck completion * acknowledgement event. */ monitor FindAlarmSample { constant string ALARM_TYPE := "UnavailabilityAlarm"; action onload() { /* Create a filter to lookup alarms based on type. */ AlarmFilter filter := AlarmFilter.init().byType(ALARM_TYPE); /** Query for alarms. */ integer reqId := AlarmLookupHelper.init() .withResponseCallback(queryResponse) .withResponseCompletedCallback(queryCompleted) .getWithFilter(filter); } /** Receive each individual response. */ action queryResponse(integer reqId, Alarm alarm) { log "Find Alarm Response : " + alarm.toString() at DEBUG; } /** Request completed acknowledgement. */ action queryCompleted(integer reqId) { log "Find Alarm Request completed " + reqId.toString() at DEBUG; } }