Package de.desy.tine.alarmUtils

Source Code of de.desy.tine.alarmUtils.TAlarmSystem

package de.desy.tine.alarmUtils;

import de.desy.tine.addrUtils.TSrvEntry;
import de.desy.tine.client.*;
import de.desy.tine.dataUtils.*;
import de.desy.tine.definitions.*;
import de.desy.tine.queryUtils.*;
import de.desy.tine.server.alarms.*;
import de.desy.tine.server.logger.DbgLog;
import de.desy.tine.types.*;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.Vector;

/**
* TAlarmSystem is a utility class with numerous static methods for access
* alarm data from either the central alarm system of the local
* alarm subsystems of targeted servers. 
*
* @author duval
*/
/**
* @author duval
*
*/
public class TAlarmSystem
{
  private static final int nice_query_size = 100;
  private static NAME16II[] almSysBuf = new NAME16II[nice_query_size];
  private static NAME32[] srvNamesBuf = new NAME32[nice_query_size];
  private static Hashtable<String,NAME16II[]> ctxInfoTbl = new Hashtable<String, NAME16II[]>();
  private static Hashtable<String,CasSrvInfo> srvInfoTbl = new Hashtable<String,CasSrvInfo>();
  /**
   * Disables the specific alarm given for the given server at the CAS
   *
   * The alarm will be disabled for all registered devices on the given server.
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param alarmCode is the alarm code of the alarm which should be disabled
   * at the Central Alarm Server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int disableAlarm(String context,String server,int alarmCode)
  {
    int[] ac = {alarmCode};
    return disableAlarms(context,server,ac);
  }
  /**
   * Disables the specific alarm given for the given server and device at the CAS
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param device is the targeted device.  If device is null or an empty string,
   * then alarms with the given alarm code will be disabled for ALL devices
   * from the specified server.
   * @param alarmCode is the alarm code of the alarm which should be disabled
   * at the Central Alarm Server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int disableAlarm(String context,String server,String device, int alarmCode)
  {
    int[] ac = {alarmCode};
    if (device != null) server += "/"+device;
    return disableAlarms(context,server,ac);
  }
  /**
   * Disables the specific alarm given for the given server and device at the CAS, but
   * only for the current active instance of the alarm.  Once the active alarm has
   * terminated any new alarms for the device and code specified will NOT be disabled!
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param device is the targeted device.  If device is null or an empty string,
   * then alarms with the given alarm code will be disabled for ALL devices
   * from the specified server.
   * @param alarmCode is the alarm code of the alarm which should be disabled
   * at the Central Alarm Server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int disableCurrentAlarm(String context,String server,String device, int alarmCode)
  {
    NAME64DBLDBL[] ndd = new NAME64DBLDBL[] {new NAME64DBLDBL(device,alarmCode,1)};
    return disableAlarms(context,server,ndd);
  }
  /**
   * Disables the specific alarm given for the given server and device at the CAS and
   * provide a comment (reason) for the disabling action.
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param device is the targeted device.  If device is null or an empty string,
   * then alarms with the given alarm code will be disabled for ALL devices
   * from the specified server.
   * @param alarmCode is the alarm code of the alarm which should be disabled
   * at the Central Alarm Server
   * @param cmt is an annotation to the disabling action.
   *
   * @return 0 on success or a TINE Error code
   */
  public static int disableAlarm(String context,String server,String device, int alarmCode,String cmt)
  {
    return disableAlarm(context,server,device,alarmCode,cmt,null);
  }
  public static int disableAlarm(String context,String server,String device, int alarmCode,String cmt,String caller)
  {
    int[] ac = {alarmCode};
    if (device != null) server += "/"+device;
    int cc = disableAlarms(context,server,ac);
    if (cc != 0) return cc;
    cc = annotateDisablingAction(context, server, device, alarmCode, cmt, caller);
    return cc;
  }
  /**
   * Disables the alarms in the given list for the given server at the CAS
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param alarmCodes is a simple array of those alarm codes which should be disabled
   * at the Central Alarm Server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int disableAlarms(String context,String server,int[] alarmCodes)
  {
    if (context == null || context.length() == 0) return TErrorList.illegal_context;
    if (server == null || server.length() == 0) return TErrorList.invalid_keyword;
    if (alarmCodes == null) return TErrorList.invalid_parameter;
    TDataType din = new TDataType(alarmCodes);
    String dev = "/" + context + "/CAS/" + server;
    TLink tl = new TLink(dev,"DisableAlarm",null,din,TAccess.CA_WRITE);
    return tl.executeAndClose(500);
  }
  public static int disableAlarms(String context,String server,NAME64I[] alarms)
  {
    if (context == null || context.length() == 0) return TErrorList.illegal_context;
    if (server == null || server.length() == 0) return TErrorList.invalid_keyword;
    if (alarms == null) return TErrorList.invalid_parameter;
    TDataType din = new TDataType(alarms);
    String dev = "/" + context + "/CAS/" + server;
    TLink tl = new TLink(dev,"DisableAlarm",null,din,TAccess.CA_WRITE);
    return tl.executeAndClose(500);
  }
  public static int disableAlarms(String context,String server,NAME64DBLDBL[] alarms)
  {
    if (context == null || context.length() == 0) return TErrorList.illegal_context;
    if (server == null || server.length() == 0) return TErrorList.invalid_keyword;
    if (alarms == null) return TErrorList.invalid_parameter;
    TDataType din = new TDataType(alarms);
    String dev = "/" + context + "/CAS/" + server;
    TLink tl = new TLink(dev,"DisableAlarm",null,din,TAccess.CA_WRITE);
    return tl.executeAndClose(500);
  }
  /**
   * Determines whether the specified disabled alarm is currently in an active state
   * (and hence whether enabling the alarm would result in an alarm state).
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param device is the targeted device. A null string checks for all devices.
   * @param alarmCode is the targeted alarm code.
   *
   * @return true if the current alarm state for the disable alarm is active.  If the
   * given alarm is not disabled then this method returns false.
   */
  public static boolean isDisabledAlarmActive(String context,String server,String device,int alarmCode)
  {
    String dev = "/" + context + "/CAS/" + server;
    if (device != null) dev += "/" + device;
    boolean[] bv = new boolean[1];
    TDataType dout = new TDataType(bv);
    TDataType din = new TDataType(new int[]{alarmCode});
    TLink tl = new TLink(dev,"DisabledAlarmIsActive",dout,din,TAccess.CA_READ);
    int cc = tl.executeAndClose(500);
    if (cc != 0) return false;
    return bv[0];
  }
  public static boolean isDisabledAlarmActive(String context,String server,int alarmCode)
  {
    return isDisabledAlarmActive(context,server,null,alarmCode);
  }
  public static int annotateDisablingAction(String context,String server,String device,int alarmCode,String cmt,String caller)
  {
    if (context == null || context.length() == 0) return TErrorList.illegal_context;
    if (server == null || server.length() == 0) return TErrorList.invalid_keyword;
    if (device == null) device = "ALL";
    if (cmt == null) return TErrorList.argument_list_error;
    TDataType din = new TDataType(cmt);
    String dev = "/"+context+"/CAS/"+server+"/"+device+":"+alarmCode;
    TLinkFactory thisFactory = TLinkFactory.getInstance();
    String usr = thisFactory.getUserName();
    if (caller == null || caller.length() == 0) caller = usr;
    thisFactory.setUserName(caller);
    TLink tl = new TLink(dev,"DisabledAlarmComment",null,din,TAccess.CA_WRITE);
    int cc = tl.executeAndClose(500);
    thisFactory.setUserName(usr);
    return cc;
  }
  public static int annotateDisablingAction(String context,String server,String device,int alarmCode,String cmt)
  {
    return annotateDisablingAction(context,server,device,alarmCode,cmt,null);
  }
  public static String getDisablingActionAnnotation(String context,String server,String device,int alarmCode)
  {
    if (context == null || context.length() == 0) return null;
    if (server == null || server.length() == 0) return null;
    if (device == null) device = "ALL";
    TDataType dout = new TDataType(256,TFormat.CF_TEXT);
    String dev = "/"+context+"/CAS/"+server+"/"+device+":"+alarmCode;
    TLink tl = new TLink(dev,"DisabledAlarmComment",dout,null,TAccess.CA_READ);
    int cc = tl.executeAndClose(500);
    if (cc != 0) return null;
    char[] cmt = new char[256];
    dout.getData(cmt);
    return new String(cmt);
  }
  public static int[] getDisabledAlarmCodes(String context,String server)
  {
    if (context == null || context.length() == 0) return null;
    if (server == null || server.length() == 0) return null;
    int[] alarmCodes = new int[100];
    TDataType dout = new TDataType(alarmCodes);
    String dev = "/" + context + "/CAS/" + server;
    TLink tl = new TLink(dev,"DisableAlarm",dout,null,TAccess.CA_WRITE);
    int cc = tl.executeAndClose(500);
    return cc == 0 ? alarmCodes : null;
  }
  /**
   * Gets the disabled alarm list for the given context and alarm system
   *
   * @param context is the TINE context
   * @param alarmSystem is the targeted alarm system
   *
   * @return the current list of disabled alarm message types or NULL if no alarms have been disabled.
   */
  public static TAlarmMessage[] getDisabledAlarms(String context,String alarmSystem)
  {
    if (context == null || context.length() == 0) return null;
    String dev = "/"+context+"/CAS/SYS:"+alarmSystem;
    TAlarmMessage[] ams = null;
    ams = new TAlarmMessage[256];
    for (int i=0; i<256; i++) ams[i] = new TAlarmMessage();
    TDataType amst = new TDataType(ams);
    TLink tl = new TLink(dev,"DisableAlarm",amst,null,TAccess.CA_READ);
    int cc = tl.execute(1000, true);
    tl.close();
    if (cc != 0) return null;
    int nret = amst.getCompletionLength();
    if (nret < 256)
    {
      TAlarmMessage[] a = new TAlarmMessage[nret];
      for (int i=0; i<nret; i++) a[i] = ams[i];
      ams = a;
    }   
    return cc == 0 ? ams : null;
  }
  /**
   * Enables the specific alarm given list for the given server at the CAS
   *
   * Using this call will enable the given alarm code for ALL registered
   * devices on the given server.
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param alarmCode is the alarm code of the alarm which should be enabled
   * at the Central Alarm Server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int enableAlarm(String context,String server,int alarmCode)
  {
    int[] ac = {alarmCode};
    return enableAlarms(context,server,ac);
  }
  /**
   * Enables the specific alarm given list for the given server and device
   * at the CAS
   *
   * @param context is the TINE context.
   * @param server is the targeted server.
   * @param device is the targeted device. If device is null or an empty string,
   * then alarms with the given alarm code will be enabled for ALL devices
   * from the specified server.
   * @param alarmCode is the alarm code of the alarm which should be enabled
   * at the Central Alarm Server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int enableAlarm(String context,String server,String device,int alarmCode)
  {
    int[] ac = {alarmCode};
    if (device != null) server += "/"+device;
    return enableAlarms(context,server,ac);
  }
  /**
   * Enables the alarms in the given list for the given server at the CAS
   *
   * @param context is the TINE context
   * @param server is the targeted server
   * @param alarmCodes is a simple array of those alarm codes which should be enabled
   * at the Central Alarm Server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int enableAlarms(String context,String server,int[] alarmCodes)
  {
    if (context == null || context.length() == 0) return TErrorList.illegal_context;
    if (server == null || server.length() == 0) return TErrorList.invalid_keyword;
    if (alarmCodes == null) return TErrorList.invalid_parameter;
    TDataType din = new TDataType(alarmCodes);
    String dev = "/" + context + "/CAS/" + server;
    TLink tl = new TLink(dev,"EnableAlarm",null,din,TAccess.CA_WRITE);
    int cc = tl.execute(500, true);
    tl.close();
    return cc;
  }
  public static int enableAlarms(String context,String server,NAME64I[] alarms)
  {
    if (context == null || context.length() == 0) return TErrorList.illegal_context;
    if (server == null || server.length() == 0) return TErrorList.invalid_keyword;
    if (alarms == null) return TErrorList.invalid_parameter;
    TDataType din = new TDataType(alarms);
    String dev = "/" + context + "/CAS/" + server;
    TLink tl = new TLink(dev,"EnableAlarm",null,din,TAccess.CA_WRITE);
    int cc = tl.execute(500, true);
    tl.close();
    return cc;
  }
  /**
   * Enables all alarms for the given server at the CAS
   *
   * @param context is the TINE context
   * @param server is the targeted server
   *
   * @return 0 on success or a TINE Error code
   */
  public static int enableAlarms(String context,String server)
  {
    if (context == null || context.length() == 0) return TErrorList.illegal_context;
    if (server == null || server.length() == 0) return TErrorList.invalid_keyword;
    String dev = "/" + context + "/CAS/" + server;
    TLink tl = new TLink(dev,"EnableAlarm",null,null,TAccess.CA_WRITE);
    int cc = tl.execute(500, true);
    tl.close();
    return cc;
  }
  /**
   * Gets the alarm systems available in the given context
   *
   * This method gets the available alarm systems in the given context
   * as a simple string array.  For the mapping of alarm system names to
   * alarm system numbers use getAlarmSystemsMap
   * 
   * @param context [input] is the TINE context
   *
   * @return a string array containing the available alarm systems
   * @see getAlarmSystemsMap, getAlarmSystem
   */
  public static String[] getAlarmSystems(String context)
  {
    return getAlarmSystems(context,false);
  }
  public static String[] getAlarmSystems(String context,boolean acquireAll)
  {
    NAME16II[] n16ii = getAlarmSystemsMap(context,acquireAll);
    if (n16ii == null || n16ii.length == 0) return null;
    String[] as = new String[n16ii.length];
    for (int i=0; i<n16ii.length; i++) as[i] = n16ii[i].getName();
    return as;
  }
  public static String[] getAlarmSystemsViewerColumn(String context,int column)
  {
    return getAlarmSystemsViewerColumn(context,column,false);
  }
  public static String[] getAlarmSystemsViewerColumn(String context,int column,boolean acquireAll)
  {
    NAME16II[] n16ii = getAlarmSystemsMap(context,acquireAll);
    if (n16ii == null || n16ii.length == 0) return null;
    int[] nsys = new int[3];
    for (int i=0; i<n16ii.length; i++)
    {
      if (n16ii[i].i2val < 1 || n16ii[i].i2val > 3) continue;
      nsys[n16ii[i].i2val-1]++;
    }
    int n = nsys[0];
    if (nsys[1] > n) n = nsys[1];
    if (nsys[2] > n) n = nsys[2];
    if (n < 1) n = n16ii.length;
    String[] as = new String[n];
    for (int i=0,k=0; i<n16ii.length && k<n; i++)
    {
      if (n16ii[i].i2val != column) continue;
      as[k++] = n16ii[i].getName();
    }
    return as;
  }
  /**
   * Gets the alarm systems available in the given context
   *
   * This method gets the available alarm systems in the given context
   * as an array of NAME16I pairs, which gives a mapping of the alarm system
   * name (NAME16 part) to the alarm system number (integer part).
   * 
   * @param context [input] is the TINE context
   *
   * @return a NAME16I array containing the available alarm systems
   * @see getAlarmSystems, getAlarmSystem
   */
  public static synchronized NAME16II[] getAlarmSystemsMap(String context)
  {
    return getAlarmSystemsMap(context,false);
  }
  /**
   * Gets the alarm systems available in the given context
   *
   * This method gets the available alarm systems in the given context
   * as an array of NAME16I pairs, which gives a mapping of the alarm system
   * name (NAME16 part) to the alarm system number (integer part).
   * 
   * @param context [input] is the TINE context
   * @param acquireAll [input] if 'true' will also return alarm subsystems marked as 'hidden'
   *
   * @return a NAME16I array containing the available alarm systems
   * @see getAlarmSystems, getAlarmSystem
   */
  public static synchronized NAME16II[] getAlarmSystemsMap(String context,boolean acquireAll)
  {
    if (context == null) return null;
    NAME16II[] as;
    String key = acquireAll ? context + ".ALL" : context;
    if ((as=ctxInfoTbl.get(key)) != null) return as;
    TDataType ast = new TDataType(almSysBuf);
    String dev = "/" + context + "/CAS";
    String prp = acquireAll ? "ALARMSYSTEMS.ALL" : "ALARMSYSTEMS";
    TLink tl = new TLink(dev,prp,ast,null,TAccess.CA_READ);
    int cc = tl.execute(500, true);
    tl.close();
    if (cc == 0)
    {
      int n = ast.getCompletionLength();
      if (n > 0)
      {
        as = new NAME16II[n];
        for (int i=0; i<n; i++) as[i] = new NAME16II(almSysBuf[i]);
        if (as != null) ctxInfoTbl.put(key, as);
        return as;
      }
    }
    return null;
  }
  /**
   * Gets the alarm system corresponding the the alarm system number
   * and context given.
   *
   * This method gets the alarm system corresponding the the alarm system number
   * and context given.
   * 
   * @param context [input] is the TINE context
   * @param alarmSystemNumber [input] is the specified alarm system number for which
   * the system name is desired.
   *
   * @return a String containing the corresponding alarm system name.
   * @see getAlarmSystems, getAlarmSystemsMap
   */
  public static String getAlarmSystem(String context,int alarmSystemNumber)
  {
    NAME16II[] as = getAlarmSystemsMap(context,true);
    if (as != null)
    {
      for (int i=0; i<as.length; i++)
      {
        if (as[i].i1val == alarmSystemNumber) return as[i].getName();
      }
    }
    return new String("alarm system " + alarmSystemNumber);
  }
  public static int getAlarmSystemNumber(String context,String alarmSystem)
  {
    NAME16II[] as = getAlarmSystemsMap(context,true);
    if (as != null && alarmSystem != null)
    {
      for (int i=0; i<as.length; i++)
      {
        if (as[i].name.compareToIgnoreCase(alarmSystem) == 0)
          return as[i].i1val;
      }
    }
    return 0;
  }
  public static String[] getAlarmServerContexts()
  {
    return TQuery.getContexts("CAS");
  }
  public static String[] getAlarmServerContexts(String importance)
  {
    return TQuery.getContexts("CAS",importance);
  }
  public static String[] getAlarmServerServers(String context,String alarmSystem)
  {
    if (context == null) return null;
    TDataType srvt = new TDataType(srvNamesBuf);
    String srv = "/" + context + "/CAS";
    String dev = srv + "/" + (alarmSystem != null ? "SYS:"+alarmSystem : "#0");
    String prp = alarmSystem != null && alarmSystem.compareToIgnoreCase("ALL") != 0 ? "Server4AlmSystem" : "SERVERS";

    TLink tl = new TLink(dev,prp,srvt,null,TAccess.CA_READ);
    int cc = tl.execute(500, true);
    tl.close();
    if (cc == 0)
    {
      int n = srvt.getCompletionLength();
      if (n > 0)
      {
        String[] as = new String[n];
        for (int i=0; i<n; i++) as[i] = srvNamesBuf[i].getName();
        return as;
      }
    }
    return null;
  }
  public static CasSrvInfo getAlarmServerServerInfo(String context,String server)
  {
    if (context == null || server == null) return null;
    String key = "/" + context + "/" + server;
    CasSrvInfo[] cq = new CasSrvInfo[1];
    cq[0] = srvInfoTbl.get(key);
    if (cq[0] == null)
    {
      cq[0] = new CasSrvInfo();
      TDataType srvt = new TDataType(cq);
      String srv = "/" + context + "/CAS/" + server; 
      TLink tl = new TLink(srv,"SERVERINFO",srvt,null,TAccess.CA_READ);
      int cc = tl.execute(500, true);
      tl.close();
      if (cc == 0)
      {
        srvInfoTbl.put(key, cq[0]);
      }
    }
    return cq[0];
  }
  /**
   * Gets the number of recent alarms in the time interval (up to 2 hours)
   *
   * This method gets the number of recent alarms in the time interval given.
   * The time interval can be within the most recent 2 hours or
   * a values of -(depth) and -1 can be passed to signal the most recent 2 hour
   * period.
   * 
   * @param context is the TINE context
   * @param server is the targeted server (null => Central alarm server)
   * @param alarmSystem is the targeted alarm sub system
   * @param start is the UTC start time (or a negative depth in milliseconds)
   * @param stop is the UTC stop time (or -1 if start is the depth)
   * @param severity is the lower limit for the severity filter
   *
   * @return the number of alarms in the time interval
   * @see monitorNumberOfAlarms
   */
  public static int getNumberOfAlarms(String context,String server,String alarmSystem,long start,long stop,int severity)
  {
    return getNumberOfAlarms(context,server,alarmSystem,start,stop,severity,false);
  }
  /**
   * Gets the number of recent alarms in the time interval (up to 2 hours)
   *
   * This method gets the number of recent alarms in the time interval given.
   * The time interval can be within the most recent 2 hours or
   * a values of -(depth) and -1 can be passed to signal the most recent 2 hour
   * period.  One can alternatively include terminated alarms or not.
   * 
   * @param context is the TINE context
   * @param server is the targeted server (null => Central alarm server)
   * @param alarmSystem is the targeted alarm sub system
   * @param start is the UTC start time (or a negative depth in milliseconds)
   * @param stop is the UTC stop time (or -1 if start is the depth)
   * @param severity is the lower limit for the severity filter
   * @param includeTerminated specifies whether to include terminated alarms or not
   *
   * @return the number of alarms in the time interval
   * @see monitorNumberOfAlarms
   */
  public static int getNumberOfAlarms(String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated)
  {
    return getNumberOfAlarms(context,server,alarmSystem,start,stop,severity,includeTerminated,false);
  }
  public static int getNumberOfAlarms(String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated,boolean acquireAll)
  {
    if (context == null) return -TErrorList.argument_list_error;
    int[] stst = new int[3];
    stst[0] = (int)(start / 1000);
    stst[1] = (int)(stop / 1000);
    stst[2] = severity;
    TDataType sst;
    int[] nalms;
    String srv = server == null ? "CAS" : server;
    String dev = "/" + context + "/" + srv;
    String prp;
    int nindex = 0;
    if (srv.compareToIgnoreCase("CAS") == 0)
    {
      sst = new TDataType(stst);
      nalms = new int[2];
      prp = includeTerminated ? "NUMALARMS.RAW" : "NUMALARMS";
      if (acquireAll) prp += ".ALL";
      if (alarmSystem != null)
      {
        dev += alarmSystem.startsWith("Server:") ? alarmSystem.substring(7) : "/SYS:" + alarmSystem;
      }
      nindex = includeTerminated ? 0 : 1; // the number of non-terminated alarms
    }
    else
    {
      sst = new TDataType();
      nalms = new int[5];
      prp = "NALARMS";
    }
    TDataType nat = new TDataType(nalms);
    TLink tl = new TLink(dev,prp,nat,sst,TAccess.CA_READ);
    int cc = tl.execute(500, true);
    tl.close();
    int nalm = 0;
    if (cc == 0) nalm = nalms[nindex];
    return nalm;
  }
  public static int getNumberOfAlarms(String context,String alarmSystem,long start,long stop,int severity)
  {
    return getNumberOfAlarms(context,null,alarmSystem,start,stop,severity);
  }
  /**
   * Monitors the recent alarms in the time interval (up to 2 hours)
   *
   * This method monitors the recent alarms and calls the AlarmHandler from the 
   * AlarmMonitor provided.  Calling this method only makes sense in a persistent application
   * (e.g. not a command line application).  The handler will be fired upon
   * any change of alarm information within the most recent 2 hour interval.
   * When the handler is called, the instance of the Monitor is passed, which
   * can be queried for all relevant alarms and their properties.
   * 
   * @param context is the TINE context
   * @param server is the targeted server (null => Central alarm server)
   * @param alarmSystem is the targeted alarm sub system
   * @param severity is the lower limit for the severity filter
   * @param almMonitor is a AlarmMonitor object whose AlarmHandler is to be fired upon change of data
   *
   * @return a TLink object (or null if not successful)
   * @see getNumberOfAlarms, TLink.close()
   */
  public static TLink monitorAlarms(String context,String server,String alarmSystem,int severity,AlarmMonitor almMonitor)
  {
    return monitorAlarms(context,server,alarmSystem,severity,almMonitor,false);   
  }
  public static TLink monitorAlarms(String context,String server,String alarmSystem,int severity,AlarmMonitor almMonitor,boolean includeTerminated)
  {
    return monitorAlarms(context,server,alarmSystem,severity,almMonitor,includeTerminated,false);
  }
  public static TLink monitorAlarms(String context,String server,String alarmSystem,int severity,AlarmMonitor almMonitor,boolean includeTerminated,boolean acquireAll)
  {
    if (context == null) return null;
    int[] stst = new int[3];
    stst[0] = -7200;
    stst[1] = -1;
    stst[2] = severity;
    TDataType sst;
    int[] nalms;
    String srv = server == null ? "CAS" : server;
    String dev = "/" + context + "/" + srv;
    String prp;
    if (srv.compareToIgnoreCase("CAS") == 0)
    {
      sst = new TDataType(stst);
      nalms = new int[3];
      prp = includeTerminated ? "NUMALARMS.RAW" : "NUMALARMS";
      if (acquireAll) prp += ".ALL";
      if (alarmSystem != null)
      {
        dev += alarmSystem.startsWith("Server:") ? alarmSystem.substring(7) : "/SYS:" + alarmSystem;
      }
    }
    else
    {
      sst = new TDataType();
      nalms = new int[5];
      prp = "NALARMS";
    }
    TDataType nat = new TDataType(nalms);
    TLink tl = new TLink(dev,prp,nat,sst,TAccess.CA_READ);
   // tl.setNotificationTolerance(0, 0); // this will suppress heartbeats
    //if (includeTerminated) includeTerminated = false; // force this ?
    AlarmMonitorCallback amcb = new AlarmMonitorCallback(context,server,alarmSystem,severity,includeTerminated,acquireAll);
    int id = tl.attach(TMode.CM_POLL, amcb, 1000);
    if (id < 0)
    {
      tl.close();
      return null;
    }
    if (almMonitor != null)
    {
      almMonitor.setAmcb(amcb);
      amcb.setAlarmMonitor(almMonitor);
    }
    return tl;
  }
  /**
   * Gets the alarm message structure array
   *
   * This method gets the alarm message structure array from either
   * the Central Alarm Server (server == null) or the targeted server.
   * 
   * @param context [input] is the TINE context
   * @param server [input] is the targeted server if given.  To get alarms from the
   * Central Alarm Server, this parameter should be null.
   * @param alarmSystem [input] is the targeted alarm system and is only valid for
   * call to the Central Alarm Server.  alarmSystem can be a registered alarm system or
   * refer to a specific server when given in the form "Server:<server name>"
   * @param start [input] is the start time for the search as a UTC long
   * @param stop [input] is the stop time for the search as a UTC long
   * @param severity [input] is the minimum severity for the search
   *
   * @return an array of Alarm Messages for the input given.
   *
   */
  public static TAlarmMessage[] getAlarms(String context,String server,String alarmSystem,long start,long stop,int severity)
  {
    return getAlarms(context,server,alarmSystem,start,stop,severity,false);   
  }
  /**
   * Gets the alarm message structure array
   *
   * This method gets the alarm message structure array from either
   * the Central Alarm Server (server == null) or the targeted server.
   * One can alternatively include terminated alarms or not
   * 
   * @param context [input] is the TINE context
   * @param server [input] is the targeted server if given.  To get alarms from the
   * Central Alarm Server, this parameter should be null.
   * @param alarmSystem [input] is the targeted alarm system and is only valid for
   * call to the Central Alarm Server.  alarmSystem can be a registered alarm system or
   * refer to a specific server when given in the form "Server:<server name>"
   * @param start [input] is the start time for the search as a UTC long
   * @param stop [input] is the stop time for the search as a UTC long
   * @param severity [input] is the minimum severity for the search
   * @param includeTerminated specifies whether terminated alarms are to be included or not
   *
   * @return an array of Alarm Messages for the input given.
   *
   */
  public static synchronized TAlarmMessage[] getAlarms(String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated)
  {
    return getAlarms(context,server,alarmSystem,start,stop,severity,includeTerminated,false);
  }
  public static synchronized TAlarmMessage[] getAlarms(String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated,boolean acquireAll)
  {
    long tnow = System.currentTimeMillis();
    boolean callArchive = start < (tnow - 10800000);
    if (context == null) return null;
    String srv = server == null ? "CAS" : server;
    String dev = "/" + context + "/" + srv;
    String prp;
    if (srv.compareToIgnoreCase("CAS") == 0)
    {
      prp = includeTerminated ? "SYSALARMS.RAW" : "SYSALARMS";
      if (acquireAll) prp += ".ALL";
      if (alarmSystem != null && alarmSystem.compareToIgnoreCase("ALL") != 0)
      {
        dev += alarmSystem.startsWith("Server:") ? alarmSystem.substring(7) : "/SYS:" + alarmSystem;
      }
      else
      {
        dev += "/ALL";
      }
    }
    else
    {
      prp = "ALARMS";
      callArchive = false;
    }
    int n = getNumberOfAlarms(context, server, alarmSystem, start, stop, severity,includeTerminated,acquireAll);
    TAlarmMessage[] ams = null;
    if (n > 0)
    { // something found in the recent list
      ams = new TAlarmMessage[n];
      for (int i=0; i<n; i++) ams[i] = new TAlarmMessage();
      TDataType amst = new TDataType(ams);
      int[] stst = new int[3];
      stst[0] = (int)(start / 1000);
      stst[1] = (int)(stop / 1000);
      stst[2] = severity;
      TDataType sst = new TDataType(stst);
      TLink tl = new TLink(dev,prp,amst,sst,TAccess.CA_READ);
      int cc = tl.execute(500, true);
      tl.close();
      if (cc != 0) return null;
      int nret = amst.getCompletionLength();
      if (nret < n)
      {
        TAlarmMessage[] a = new TAlarmMessage[nret];
        for (int i=0; i<nret; i++) a[i] = ams[i];
        ams = a;
      }
      Arrays.sort(ams);
    }
    if (callArchive)
    {
      TAlarmMessage[] aa = getArchivedAlarms(context, alarmSystem, start, stop, severity);
      if (aa != null)
      { // join these
        if (ams == null)
        { // no recent alarms
          ams = aa;
        }
        else
        { // join the two sets
          TAlarmMessage[] a = new TAlarmMessage[aa.length + ams.length];
          for (int i=0; i<ams.length; i++) a[i] = ams[i];
          for (int i=0; i<aa.length; i++) a[ams.length+i] = aa[i];
          ams = a;
        }
      }
    }
    return ams;
  }
  public static synchronized TAlarmMessage[] getAlarms(int numAlarms, String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated)
  {
    return getAlarms(numAlarms, context,server,alarmSystem,start,stop,severity,includeTerminated,false);
  }
  public static synchronized TAlarmMessage[] getAlarms(int numAlarms, String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated,boolean acquireAll)
  {
    if (context == null) return null;
    String srv = server == null ? "CAS" : server;
    String dev = "/" + context + "/" + srv;
    String prp;
    if (srv.compareToIgnoreCase("CAS") == 0)
    {
      prp = includeTerminated ? "SYSALARMS.RAW" : "SYSALARMS";
      if (acquireAll) prp += ".ALL";
      if (alarmSystem != null && alarmSystem.compareToIgnoreCase("ALL") != 0)
      {
        dev += alarmSystem.startsWith("Server:") ? alarmSystem.substring(7) : "/SYS:" + alarmSystem;
      }
      else
      {
        dev += "/ALL";
      }
    }
    else
    {
      prp = "ALARMS";
    }
    int n = numAlarms;
    while (n > 800 && stop > start)
    { // take drastic measures
      n = 800; //n /= 2;
      start -= 1200000 ; //+= (stop-start)/2;
    }
    TAlarmMessage[] ams = null;
    if (n > 0)
    { // something found in the recent list
      ams = new TAlarmMessage[n];
      for (int i=0; i<n; i++) ams[i] = new TAlarmMessage();
      TDataType amst = new TDataType(ams);
      int[] stst = new int[3];
      stst[0] = (int)(start / 1000);
      stst[1] = (int)(stop / 1000);
      stst[2] = severity;
      TDataType sst = new TDataType(stst);
      TLink tl = new TLink(dev,prp,amst,sst,TAccess.CA_READ);
      int cc = TErrorList.connection_timeout;
      for (int i=0; i<2; i++)
      {
        if ((cc=tl.execute(500, true)) == 0) break;
      }
      tl.close();
      if (cc != 0)
      {
        DbgLog.log("getAlarms","getAlarms for " + dev + " failed : " + TErrorList.getErrorString(cc));
        return null;
      }
      int nret = amst.getCompletionLength();
      if (nret == 0) return null;
      if (nret < n)
      {
        TAlarmMessage[] a = new TAlarmMessage[nret];
        for (int i=0; i<nret; i++) a[i] = ams[i];
        ams = a;
      }
      Arrays.sort(ams);
    }
    return ams;
  }
  public static synchronized TAlarmMessage[] getAlarmsAsync(int numAlarms, String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated,TLinkCallback alarmsCallback)
  {
    return getAlarmsAsync(numAlarms, context,server,alarmSystem,start,stop,severity,includeTerminated,false,alarmsCallback);
  }
  public static synchronized TAlarmMessage[] getAlarmsAsync(int numAlarms, String context,String server,String alarmSystem,long start,long stop,int severity,boolean includeTerminated,boolean acquireAll,TLinkCallback alarmsCallback)
  {
    if (start > 0) start--; // always backup to ensure a complete second's worth
    if (context == null) return null;
    String srv = server == null ? "CAS" : server;
    String dev = "/" + context + "/" + srv;
    String prp;
    if (srv.compareToIgnoreCase("CAS") == 0)
    {
      prp = includeTerminated ? "SYSALARMS.RAW" : "SYSALARMS";
      if (acquireAll) prp += ".ALL";
      if (alarmSystem != null && alarmSystem.compareToIgnoreCase("ALL") != 0)
      {
        dev += alarmSystem.startsWith("Server:") ? alarmSystem.substring(7) : "/SYS:" + alarmSystem;
      }
      else
      {
        dev += "/ALL";
      }
    }
    else
    {
      prp = "ALARMS";
    }
    int n = numAlarms;
    if (n > 1000 && stop > start)
    { // take drastic measures
      if (TLinkFactory.debugLevel > 0)
        DbgLog.log("getAlarmsAsync","getAlarms for "+dev+" : "+n+" alarms over last 2 hours");
      if (stop > start + 1200000)
      { // application start up ?
        if (TLinkFactory.debugLevel > 0)
          DbgLog.log("getAlarmsAsync","acquire last 20 minutes -> truncate call to 1000 alarms above severity 7");
        start = stop - 1200000; // just take the last 20 minutes
        n = 1000;
        severity = 7;
      }
      else
      { // stop-start likely to be 1 second
        n = 100 * (int)(stop-start)/1000;
        if (TLinkFactory.debugLevel > 0)
          DbgLog.log("getAlarmsAsync","acquire last "+(stop-start)/1000+"seconds -> truncate call to "+n+" alarms");
      }
    }
    // one last check as the CAS cannot send more than 1000 at a time ...
    if (n > 1000) n = 1000;
    TAlarmMessage[] ams = null;
    if (n > 0)
    { // something found in the recent list
      ams = new TAlarmMessage[n];
      for (int i=0; i<n; i++) ams[i] = new TAlarmMessage();
      TDataType amst = new TDataType(ams);
      int[] stst = new int[3];
      stst[0] = (int)(start / 1000);
      stst[1] = (int)(stop / 1000);
      stst[2] = severity;
      TDataType sst = new TDataType(stst);
      TLink tl = new TLink(dev,prp,amst,sst,TAccess.CA_READ);
      int lid = tl.attach(TMode.CM_SINGLE, alarmsCallback, 1000);
      if (lid < 0)
      {
        DbgLog.log("getAlarmsAsync","getAlarms for " + dev + " failed : " + TErrorList.getErrorString(-lid));
        return null;
      }
    }
    return ams;
  }
  /**
   * Gets the alarm message structure array from the CAS with extended text
   *
   * This method gets the alarm message structure array from
   * the Central Alarm Server.
   * One can alternatively include terminated alarms or not
   * 
   * @param context [input] is the TINE context
   * @param alarmSystem [input] is the targeted alarm system and is only valid for
   * call to the Central Alarm Server.  alarmSystem can be a registered alarm system or
   * refer to a specific server when given in the form "Server:<server name>"
   * @param start [input] is the start time for the search as a UTC long
   * @param stop [input] is the stop time for the search as a UTC long
   * @param severity [input] is the minimum severity for the search
   * @param includeTerminated specifies whether terminated alarms are to be included or not
   *
   * @return an array of Alarm Messages for the input given.
   *
   */
  public static TCASAlarmMessage[] getAlarmsWithText(String context,String alarmSystem,long start,long stop,int severity,boolean includeTerminated)
  {
    return getAlarmsWithText(context,alarmSystem,start,stop,severity,includeTerminated,false);
  }
  public static TCASAlarmMessage[] getAlarmsWithText(String context,String alarmSystem,long start,long stop,int severity,boolean includeTerminated,boolean acquireAll)
  {
    long tnow = System.currentTimeMillis();
    boolean callArchive = start < (tnow - 10800000);
    if (context == null) return null;
    String srv = "CAS";
    String dev = "/" + context + "/" + srv;
    String prp;
    if (srv.compareToIgnoreCase("CAS") == 0)
    {
      prp = includeTerminated ? "SYSALARMS.RAW" : "SYSALARMS";
      if (acquireAll) prp += ".ALL";
      if (alarmSystem != null && alarmSystem.compareToIgnoreCase("ALL") != 0)
      {
        dev += alarmSystem.startsWith("Server:") ? alarmSystem.substring(7) : "/SYS:" + alarmSystem;
      }
      else
      {
        dev += "/ALL";
      }
    }
    else
    {
      prp = "ALARMS";
      callArchive = false;
    }
    int n = getNumberOfAlarms(context, srv, alarmSystem, start, stop, severity);
    TCASAlarmMessage[] ams = null;
    if (n > 0)
    { // something found in the recent list
      ams = new TCASAlarmMessage[n];
      for (int i=0; i<n; i++) ams[i] = new TCASAlarmMessage();
      TDataType amst = new TDataType(ams);
      int[] stst = new int[3];
      stst[0] = (int)(start / 1000);
      stst[1] = (int)(stop / 1000);
      stst[2] = severity;
      TDataType sst = new TDataType(stst);
      TLink tl = new TLink(dev,prp,amst,sst,TAccess.CA_READ);
      int cc = tl.execute(500, true);
      tl.close();
      if (cc != 0)
      {
        DbgLog.log("getAlarmsWithText","getAlarmsWithText for " + dev + " failed : " + TErrorList.getErrorString(cc));
        return null;
      }
      int nret = amst.getCompletionLength();
      if (nret < n)
      {
        TCASAlarmMessage[] a = new TCASAlarmMessage[nret];
        for (int i=0; i<nret; i++) a[i] = ams[i];
        ams = a;
      }
      Arrays.sort(ams);
    }
    if (callArchive)
    {
      TAlarmMessage[] aa = getArchivedAlarms(context, alarmSystem, start, stop, severity);
      if (aa != null)
      { // join these
        TCASAlarmMessage[] xaa = new TCASAlarmMessage[aa.length];
        if (ams == null)
        { // no recent alarms
          for (int i=0; i<xaa.length; i++)
            xaa[i] = new TCASAlarmMessage(aa[i]);
          ams = xaa;
        }
        else
        { // join the two sets
          TCASAlarmMessage[] a = new TCASAlarmMessage[aa.length + ams.length];
          for (int i=0; i<ams.length; i++) a[i] = ams[i];
          for (int i=0; i<aa.length; i++)
            a[ams.length+i] = new TCASAlarmMessage(aa[i]);
          ams = a;
        }
      }
    }
    return ams;
  }
  /**
   * Gets the alarm message structure array
   *
   * This method gets the alarm message structure array from
   * the Central Alarm Server.
   * 
   * @param context [input] is the TINE context
   * @param alarmSystem [input] is the targeted alarm system
   * and can refer to a registered alarm system or
   * to a specific server when given in the form "Server:<server name>"
   * @param start [input] is the start time for the search as a UTC long
   * @param stop [input] is the stop time for the search as a UTC long
   * @param severity [input] is the minimum severity for the search
   *
   * @return an array of Alarm Messages for the input given.
   *
   */
  public static TAlarmMessage[] getAlarms(String context,String alarmSystem,long start,long stop,int severity)
  {
    return getAlarms(context,null,alarmSystem,start,stop,severity);
  }
  /**
   * Gets the alarm message structure array from the targeted server
   *
   * This method gets the alarm message structure array from
   * the targeted server.
   * 
   * @param context [input] is the TINE context
   * @param server [input] is the targeted server name
   * @param start [input] is the start time for the search as a UTC long
   * @param stop [input] is the stop time for the search as a UTC long
   * @param severity [input] is the minimum severity for the search
   *
   * @return an array of Alarm Messages for the input given.
   *
   */
  public static TAlarmMessage[] getAlarmsFromServer(String context,String server,long start,long stop,int severity)
  {
    return getAlarms(context,server,null,start,stop,severity);
  }
  /**
   * Gets the alarm definition structure array
   *
   * This method gets the alarm definition structure array as registered at the
   * Central Alarm Server
   * 
   * @param context is the TINE context
   * @param server is the targeted server.
   * @param alarmCode is the targeted alarm code. Passing a value less
   * than zero will acquire all known alarm definitions.
   *
   * @return an array list of alarm definitions for the input given.
   *
   */
  public static TAlarmDefinition[] getAlarmDefinitions(String context,String server,int alarmCode)
  {
    if (context == null || server == null) return null;
    if (alarmCode > 0 && TAlarm.isLinkErrorAlarm(alarmCode))
      alarmCode = TErrorList.link_error;
    String dev = "/" + context + "/CAS/" + server;
    String prp = "SYSALMDEFS";
    int n = nice_query_size;
    TAlarmDefinition[] d = new TAlarmDefinition[n];
    for (int i=0; i<n; i++) d[i] = new TAlarmDefinition();
    TDataType adst = new TDataType(d);
    TDataType act;
    if (alarmCode > 0)
    {
      int[] almcod = new int[1];
      almcod[0] = TAlarm.getBaseCode(alarmCode);
      act = new TDataType(almcod);
    }
    else
    {
      act = new TDataType();
    }
    TLink tl = new TLink(dev,prp,adst,act,TAccess.CA_READ);
    int cc = tl.execute(500, true);
    tl.close();
    n = adst.getCompletionLength();
    TAlarmDefinition[] ads = null;
    if (cc == TErrorList.illegal_device)
    { // then try the server itself ....
      prp = "ALMDEFS";
      dev = "/"+context+"/"+server+"/*";
      try
      {
        tl = new TLink(dev,prp,adst,null,TAccess.CA_READ);
        cc = tl.execute(500, true);
        tl.close();
        n = adst.getCompletionLength();
        if (cc == 0 && alarmCode > 0 && n > 0)
        {
          boolean found = false;
          for (int i=0; i<n; i++)
          {
            if (alarmCode == d[i].getAlarmCode())
            {
              d[0] = d[i]; n = 1; found = true;
              break;
            }
          }
          if (!found) return null;
        }
      } catch (Exception ignore) { return null; }
    }
    if (cc != 0) return null;
    if (n == 0 && alarmCode > 0)
    {
      if (TAlarm.isLinkErrorAlarm(alarmCode)) alarmCode = TErrorList.link_error;
      if (TAlarm.isDiskSpaceAlarm(alarmCode)) alarmCode = alarmCode & 0xff;
      d[0] = (TAlarmDefinition)TAlarmStockAlarm.getStockAlarmDefinitions().get(Integer.toString(alarmCode));
      if (d[0] != null) n = 1;
    }
    ads = new TAlarmDefinition[n];
    for (int i=0; i<n; i++) ads[i] = d[i];
    return ads;
  }
  /**
   * Gets the alarm definition structure array
   *
   * This method gets the alarm definition structure array as registered at the
   * Central Alarm Server for the given server.
   * 
   * @param context is the TINE context
   * @param server is the targeted server.
   *
   * @return an array list of alarm definitions for the input given.
   *
   */
  public static TAlarmDefinition[] getAlarmDefinitions(String context,String server)
  {
    return getAlarmDefinitions(context,server,-1);
  }
  /**
   * Gets the alarm definition structure array from the targeted server
   *
   * This method gets the alarm definition structure array directly from the targeted
   * server.
   * 
   * @param context [input] is the TINE context
   * @param server [input] is the targeted server.
   *
   * @return an array of Alarm Definitions from the targeted server.
   *
   */
  public static TAlarmDefinition[] getAlarmDefinitionsFromServer(String context,String server)
  {
    if (context == null || server == null) return null;
    String dev = "/" + context + "/" + server;
    String prp = "ALMDEFS";
    int n = nice_query_size;
    TAlarmDefinition[] d = new TAlarmDefinition[n];
    for (int i=0; i<n; i++) d[i] = new TAlarmDefinition();
    TDataType adst = new TDataType(d);
    TLink tl = new TLink(dev,prp,adst,null,TAccess.CA_READ);
    int cc = tl.execute(500, true);
    tl.close();
    if (cc != 0) return null;
    n = adst.getCompletionLength();
    TAlarmDefinition[] ads = new TAlarmDefinition[n];
    for (int i=0; i<n; i++) ads[i] = d[i];
    return ads;
  }
  private static Vector<TAlarmMessage> getArchivedAlarmsVector(String context,String alarmSystem,String server,long start,long stop,int severity,boolean acquireAll)
  {
    int n = 0, cc;
    if (server == null) return null;
    CasSrvInfo si = getAlarmServerServerInfo(context,server);
    if (si == null || si.getArchiveExtension().length() == 0) return null;
    String ext = si.getArchiveExtension();
    String dev = "/" + context + "/CAS.ARCHIVE/" + ext; 
    int[] nevn = new int[1];
    TDataType dout = new TDataType(nevn);
    int[] stst = new int[2];
    stst[0] = (int)(start / 1000);
    stst[1] = (int)(stop / 1000);
    TDataType sst = new TDataType(stst);
    // count the CAS 'event sets' for the time interval
    TLink tl = new TLink(dev,"QUERY.NFILES",dout,sst,TAccess.CA_READ);
    cc = tl.execute(500, true);
    tl.close();
    if (cc != 0) return null;
    if ((n=nevn[0]) <= 0) return null;
    // get the 'event sets' for the interval
    NAME8[] evn = new NAME8[n];
    dout = new TDataType(evn);
    tl = new TLink(dev,"QUERY.FILES",dout,sst,TAccess.CA_READ);
    int to = n > 5000 ? 3000 : 1000;
    cc = tl.execute(to, true);
    tl.close();
    if (cc != 0) return null;   
    int alarmSystemNumber = getAlarmSystemNumber(context, alarmSystem);
    TAlarmMessage[] ams;
    Vector<TAlarmMessage> v = new Vector<TAlarmMessage>();
    int asys = 0;
    for (int i=0; i<evn.length; i++)
    {
      ams = getArchivedAlarmsFromCAS(context,ext,evn[i].getName(),"#0",acquireAll);
      if (ams == null) continue;
      Arrays.sort(ams);
      for (n=0; n<ams.length; n++)
      {
        if (ams[n].getAlarmSeverity() < severity) continue;
        asys = ams[n].getAlarmSystem();
        if (acquireAll) asys = TAlarm.unhideAlarmSystem(asys);
        if (alarmSystemNumber != 0 &&
            asys != alarmSystemNumber) continue;
        v.addElement(ams[n]);
      }
    }
    return v;
  }
  public static TAlarmMessage[] getArchivedServerAlarms(String context,String server,long start,long stop,int severity)
  {
    return getArchivedServerAlarms(context,server,start,stop,severity,false);
  }
  public static TAlarmMessage[] getArchivedServerAlarms(String context,String server,long start,long stop,int severity,boolean acquireAll)
  {
    Vector<TAlarmMessage> v = getArchivedAlarmsVector(context,null,server,start,stop,severity,acquireAll);
    return v != null ? (TAlarmMessage[]) v.toArray(new TAlarmMessage[v.size()]) : null;
  }
  public static TAlarmMessage[] getArchivedServerAlarms(String context,String alarmSystem,String server,long start,long stop,int severity)
  {
    return getArchivedServerAlarms(context,alarmSystem,server,start,stop,severity,false);
  }
  public static TAlarmMessage[] getArchivedServerAlarms(String context,String alarmSystem,String server,long start,long stop,int severity,boolean acquireAll)
  {
    Vector<TAlarmMessage> v = getArchivedAlarmsVector(context,alarmSystem,server,start,stop,severity,acquireAll);
    return v != null ? (TAlarmMessage[]) v.toArray(new TAlarmMessage[v.size()]) : null;
  }
  public static TAlarmMessage[] getArchivedAlarms(String context,String alarmSystem,long start,long stop,int severity)
  {
    return getArchivedAlarms(context,alarmSystem,start,stop,severity,false);
  }
  public static TAlarmMessage[] getArchivedAlarms(String context,String alarmSystem,long start,long stop,int severity,boolean acquireAll)
  {
    String[] srvs = getAlarmServerServers(context,alarmSystem);
    if (srvs == null || srvs.length == 0) return null;
    Vector[] v = new Vector[srvs.length];
    int[] idx = new int[srvs.length];
    int nalms = 0;
    for (int i=0; i<srvs.length; i++)
    {
      v[i] = getArchivedAlarmsVector(context,alarmSystem,srvs[i],start,stop,severity,acquireAll);
      if (v[i] == null)
      {
        idx[i] = -1;
        continue;
      }
      idx[i] = v[i].size() - 1;
      nalms += v[i].size();
    }
    if (nalms == 0) return null;
    TAlarmMessage[] ams = new TAlarmMessage[nalms];
    TAlarmMessage[] aa;
    int off = 0, siz;
    for (int i=0; i<v.length; i++)
    {
      if (v[i] == null) continue;
      siz = v[i].size();
      aa = (TAlarmMessage[]) v[i].toArray(new TAlarmMessage[siz]);
      System.arraycopy(aa, 0, ams, off, siz);
      off += siz;
    }
    Arrays.sort(ams);

//    TAlarmMessage a;
//    // sort descending amounts to splicing the already sorted server sets:
//    long t, at;
//    int grp;
//    for (int n=0; n<nalms; n++)
//    {
//      grp = -1; t = 0;
//      for (int i=0; i<srvs.length; i++)
//      { // search for most recent
//        if (idx[i] < 0) continue;
//        a = (TAlarmMessage) v[i].elementAt(idx[i]);
//        at = a.getTimeStamp();
//        if (at > t)
//        {
//          t = at;
//          grp = i;
//        }
//      }
//      if (grp < 0) break;
//      ams[n] = (TAlarmMessage) v[grp].elementAt(idx[grp]);
//      idx[grp]--;
//    }
    return ams;
  }
  private static int sessionId = -1;
  private static String sessionEvent = null;
  private static String sessionGroup = null;
  private static int getSessionId(String context,String extension,String event)
  {
    if (extension == null || event == null) return -1;
    String srv = extension;
    String dev = "/" + context + "/CAS.ARCHIVE/" + srv; 
    boolean isNew = false;
    int cc = 0;
    if (sessionId < 0) isNew = true;
    else if (sessionEvent == null || sessionEvent.compareTo(event) != 0) isNew = true;
    else if (sessionGroup == null || sessionGroup.compareTo(extension) != 0) isNew = true;
    if (isNew)
    { // close the old session if there
      short[] id = new short[1];
      id[0] = (short)sessionId;
      TDataType din = new TDataType(id);
      TLink tl;
      if (sessionId > 0)
      {
        tl = new TLink(dev,"CLOSE",null,din,TAccess.CA_READ);
        cc = tl.execute(500, true);
        tl.close();
        if (cc != 0) return -1;
      }
      // now call "OPEN"
      NAME8[] e = new NAME8[1];
      e[0] = new NAME8(event);
      din = new TDataType(e);
      TDataType dout = new TDataType(id);
      tl = new TLink(dev,"OPEN",dout,din,TAccess.CA_READ);
      cc = tl.execute(500,true);
      tl.close();
      if (cc != 0) return -1;
      sessionId = id[0]; // the new session id
    }
    return sessionId;
  }
  private static synchronized TAlarmMessage[] getArchivedAlarmsFromCAS(String context,String extension,String event,String device,boolean acquireAll)
  {
    int sid = getSessionId(context, extension, event);
    int evn = Integer.parseInt(event, 16);
    ApcBlk[] apc = new ApcBlk[1];
    String prp = acquireAll ? "NUMALARMS.ALL" : "NUMALARMS";
    apc[0] = new ApcBlk(sid,evn,context,"CAS",device,prp,1,TFormat.CF_SHORT,"");
    TDataType din = new TDataType(apc);
    int[] nalm = new int[1];
    TDataType dout = new TDataType(nalm);
    String dev = "/" + context + "/CAS.ARCHIVE/" + device; 
    TLink tl = new TLink(dev,"READDATA",dout,din,TAccess.CA_READ);
    int cc = tl.execute(500, true);
    tl.close();
    if (cc != 0) return null; //TODO: exception

    int n = nalm[0];
    // TODO: what is a reasonable cap on n ?
    int to = n > 5000 ? 3000 : 1000;
    prp = acquireAll ? "SYSALARMS.ALL" : "SYSALARMS";
    apc[0] = new ApcBlk(sid,evn,context,"CAS",device,prp,n,TFormat.CF_STRUCT,"AMSr4");
    din.putData(apc);
    TAlarmMessage[] a = new TAlarmMessage[n];
    for (int i=0; i<n; i++) a[i] = new TAlarmMessage();
    dout = new TDataType(a);
    tl = new TLink(dev,"READDATA",dout,din,TAccess.CA_READ);
    cc = tl.execute(to, true);
    tl.close();
    if (cc != 0) return null; //TODO: exception
    return a;
  }
  public static int getArchivedNumberOfAlarms(String context,String server,String alarmSystem,long start,long stop)
  {
    int n = 0, cc;
    String[] srvs;
    if (server != null && server.compareToIgnoreCase("CAS") != 0)
    { // just this server
      srvs = new String[1];
      srvs[0] = server;
    }
    else
    {
      srvs = getAlarmServerServers(context,alarmSystem);
    }
    if (srvs == null || srvs.length == 0) return 0;
    int nalms = 0;
    for (int i=0; i<srvs.length; i++)
    {
      CasSrvInfo si = getAlarmServerServerInfo(context,srvs[i]);
      if (si == null || si.getArchiveExtension().length() == 0) continue;
      String ext = si.getArchiveExtension();
      String dev = "/" + context + "/CAS.ARCHIVE/" + ext; 
      int[] nevn = new int[1];
      TDataType dout = new TDataType(nevn);
      int[] stst = new int[2];
      stst[0] = (int)(start / 1000);
      stst[1] = (int)(stop / 1000);
      TDataType sst = new TDataType(stst);
      // count the CAS 'event sets' for the time interval
      TLink tl = new TLink(dev,"QUERY.NFILES",dout,sst,TAccess.CA_READ);
      cc = tl.execute(500, true);
      tl.close();
      if (cc != 0) break;
      if ((n=nevn[0]) <= 0) continue;
      // get the 'event sets' for the interval
      NAME8[] evn = new NAME8[n];
      dout = new TDataType(evn);
      tl = new TLink(dev,"QUERY.FILES",dout,sst,TAccess.CA_READ);
      int to = n > 5000 ? 3000 : 1000;
      cc = tl.execute(to, true);
      tl.close();
      if (cc != 0) break;
      n = 0; // now count the stored alarms
      for (int k=0; k<evn.length; k++)
      {
        n += getArchivedNumberOfAlarmsFromCAS(context,ext,evn[k].getName(),"#0");
      }
      nalms += n;
    }
    return nalms;
  }
  private static synchronized int getArchivedNumberOfAlarmsFromCAS(String context,String extension,String event,String device)
  {
    int sid = getSessionId(context, extension, event);
    int evn = Integer.parseInt(event, 16);
    ApcBlk[] apc = new ApcBlk[1];
    apc[0] = new ApcBlk(sid,evn,context,"CAS",device,"NUMALARMS",1,TFormat.CF_SHORT,"");
    TDataType din = new TDataType(apc);
    int[] nalm = new int[1];
    TDataType dout = new TDataType(nalm);
    String dev = "/" + context + "/CAS.ARCHIVE/" + device; 
    TLink tl = new TLink(dev,"READDATA",dout,din,TAccess.CA_READ);
    int cc = tl.execute(500, true);
    tl.close();
    if (cc != 0) return 0; //TODO: exception   
    return nalm[0];
  }
  public static int addToWatchTable(
      String context,String server, String property, String device,
      int datasiz,int datafmt,int severity,float hiThreshold,float loThreshold)
  {
    TSrvEntry srv = new TSrvEntry(device,server,context);   
    TAlarmWatchEntry[] awe = new TAlarmWatchEntry[1];
    TAlarmWatchThreshold awt = new TAlarmWatchThreshold(5,hiThreshold,(float)(hiThreshold*0.9),loThreshold,(float)(loThreshold*0.9));
    awe[0] = new TAlarmWatchEntry(srv.getEqmName(),device, property, datasiz, datafmt, TArrayType.AT_UNKNOWN,0,severity,awt);
    String dev = "/"+context+"/"+server;
    TLink lnk = new TLink(dev,"ALMWATCHTBL",null,new TDataType(awe),TAccess.CA_WRITE);
    return lnk.executeAndClose();
  }
  public static int setAlarmDefinition(String context,String server,TAlarmDefinition almDefinition)
  {
    if (context == null || server == null || almDefinition == null)
      return TErrorList.argument_list_error;
    String dev = "/" + context + "/" + server;
    String prp = "ALMDEFS";
    TAlarmDefinition[] d = new TAlarmDefinition[1];
    d[0] = almDefinition;
    TLink tl = new TLink(dev,prp,null,new TDataType(d),TAccess.CA_WRITE);
    return tl.executeAndClose();
  }
}
TOP

Related Classes of de.desy.tine.alarmUtils.TAlarmSystem

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.