Axeda Groovy Script: alarm Object

    When an Expression Rule of type Alarm, AlarmExtendedDataChange, AlarmSeverityChange or AlarmStateChange calls a Groovy script, the script is provided with the implicit object alarm.  This example shows how the alarm object can be used.

     

    This Expression Rule uses the CountAlarmsSinceHours Groovy script to check the number of alarms in the past number of hours, and escalate the alarm if more than three alarms have occurred:

     

    IF    ExecuteCustomObject("CountAlarmsSinceHours", 1) < 3

    THEN SetAlarmState("ACKNOWLEDGED", "Less than three alarms in the past hour")

    ELSE  SetAlarmState("ESCALATED", "THREE OR MORE alarms in the past hour")

     

    Here is the definition of the CountAlarmsSinceHours Groovy script.  The script uses the parameter 'hours' passed from the expression rule and the implicit object 'alarm'.  It returns the number of times the current alarm has occurred within 'hours' hours.

     

    import com.axeda.drm.sdk.Context
    import com.axeda.drm.sdk.data.HistoricalAlarmFinder
    import java.util.Calendar
    import com.axeda.common.sdk.jdbc.DateQuery
    
    // get Date object for an hour ago
    Calendar cal = Calendar.getInstance()
    cal.add(Calendar.HOUR, -parameters.hours.toInteger())
    Date sinceTime = cal.getTime()
    
    HistoricalAlarmFinder findAlarms = new HistoricalAlarmFinder (Context.create())
    findAlarms.device = alarm.device
    findAlarms.setAlarmName(alarm.name)
    findAlarms.date = DateQuery.after(sinceTime)
    List matchingAlarms = findAlarms.findAll()