decodes.tsdb.alarm.xml.AlarmXio Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of opendcs Show documentation
Show all versions of opendcs Show documentation
A collection of software for aggregatting and processing environmental data such as from NOAA GOES satellites.
The newest version!
/*
* $Id$
*
* Copyright 2017 Cove Software, LLC. All rights reserved.
*
* $Log$
* Revision 1.2 2019/03/05 20:47:42 mmaloney
* Support new table names for ALARM
*
* Revision 1.1 2019/03/05 14:53:01 mmaloney
* Checked in partial implementation of Alarm classes.
*
* Revision 1.5 2018/03/23 20:12:20 mmaloney
* Added 'Enabled' flag for process and file monitors.
*
* Revision 1.4 2017/05/17 20:37:38 mmaloney
* First working version.
*
* Revision 1.3 2017/03/30 20:55:20 mmaloney
* Alarm and Event monitoring capabilities for 6.4 added.
*
* Revision 1.2 2017/03/21 12:17:11 mmaloney
* First working XML and SQL I/O.
*
*/
package decodes.tsdb.alarm.xml;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.TimeZone;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
import ilex.util.Logger;
import ilex.util.TextUtil;
import ilex.xml.DomHelper;
import ilex.xml.XmlOutputStream;
import decodes.db.DataType;
import decodes.db.Site;
import decodes.db.SiteName;
import decodes.sql.DbKey;
import decodes.tsdb.alarm.AlarmEvent;
import decodes.tsdb.alarm.AlarmGroup;
import decodes.tsdb.alarm.AlarmScreening;
import decodes.tsdb.alarm.AlarmLimitSet;
import decodes.tsdb.alarm.EmailAddr;
import decodes.tsdb.alarm.FileMonitor;
import decodes.tsdb.alarm.ProcessMonitor;
import decodes.tsdb.xml.CompXioTags;
import decodes.tsdb.xml.DbXmlException;
/**
XML Input/Output for Alarm Meta Data.
*/
public class AlarmXio
{
private String module = "AlarmXio";
private String filename;
public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
static { sdf.setTimeZone(TimeZone.getTimeZone("UTC")); }
public AlarmXio()
{
}
/**
* Reads an openDCS 6.6 alarm xml file and returns it in an AlarmFile object
* @param filename
* @return
* @throws DbXmlException
*/
public AlarmFile readAlarmFile(String filename)
throws DbXmlException
{
AlarmFile ret = new AlarmFile();
Logger.instance().debug1(module + " reading alarm file '" + filename + "'");
this.filename = filename;
Document doc;
try
{
doc = DomHelper.readFile(module, filename);
}
catch(ilex.util.ErrorException ex)
{
throw new DbXmlException(ex.toString());
}
Node rootel = doc.getDocumentElement();
if (rootel.getNodeName().equalsIgnoreCase(AlarmXioTags.AlarmGroup))
{
// Legacy file contains a single group definition
AlarmGroup ag = readAlarmGroup(rootel);
ret.getGroups().add(ag);
}
else if (rootel.getNodeName().equalsIgnoreCase(AlarmXioTags.AlarmDefinitions))
{
// Walk the tree, which can contain multiple AlarmGroup or AlarmScreening elements
NodeList children = rootel.getChildNodes();
for(int i=0; children != null && i>> process monitor " + name);
NodeList children = node.getChildNodes();
for(int i=0; children != null && i 0)
xos.writeElement(AlarmXioTags.MaxFiles, AlarmXioTags.hint,
(fm.getMaxFilesHint() == null ? "" : fm.getMaxFilesHint()),
"" + fm.getMaxFiles());
if (fm.getMaxSize() > 0)
xos.writeElement(AlarmXioTags.MaxSize, AlarmXioTags.hint,
(fm.getMaxSizeHint() == null ? "" : fm.getMaxSizeHint()),
"" + fm.getMaxSize());
if (fm.getMaxLMT() != null && fm.getMaxLMT().trim().length() > 0)
xos.writeElement(AlarmXioTags.MaxLMT, AlarmXioTags.hint,
(fm.getMaxLMTHint() == null ? "" : fm.getMaxLMTHint()),
fm.getMaxLMT());
xos.writeElement(AlarmXioTags.enabled, "" + fm.isEnabled());
xos.endElement(AlarmXioTags.FileMonitor);
}
for(ProcessMonitor pm : grp.getProcessMonitors())
{
xos.startElement(AlarmXioTags.ProcessMonitor, AlarmXioTags.name, pm.getProcName());
xos.writeElement(AlarmXioTags.enabled, "" + pm.isEnabled());
for(AlarmEvent def : pm.getDefs())
{
xos.writeElement(AlarmXioTags.AlarmDef, AlarmXioTags.priority,
priority2string(def.getPriority()), def.getPattern());
}
xos.endElement(AlarmXioTags.ProcessMonitor);
}
xos.endElement(AlarmXioTags.AlarmGroup);
}
private String priority2string(int pri)
{
switch(pri)
{
case Logger.E_INFORMATION: return "INFO";
case Logger.E_WARNING: return "WARNING";
case Logger.E_FAILURE: return "FAILURE";
case Logger.E_FATAL: return "FATAL";
default: return "ANY";
}
}
/**
* Writes to an output stream. Does not close stream after fininshing.
* @param grp
* @param os
* @throws IOException
*/
public void writeXML(ArrayList screenings, ArrayList groups, OutputStream os)
throws IOException
{
XmlOutputStream xos = new XmlOutputStream(os, AlarmXioTags.AlarmDefinitions);
xos.writeXmlHeader();
xos.startElement(AlarmXioTags.AlarmDefinitions);
decodes.db.Database decDb = decodes.db.Database.getDb();
// Write the alarm groups first.
for(AlarmGroup grp : groups)
writeXML(grp, xos);
for(AlarmScreening as : screenings)
{
xos.startElement(AlarmXioTags.AlarmScreening, AlarmXioTags.name, as.getScreeningName());
if (!DbKey.isNull(as.getSiteId()))
{
Site site = decDb.siteList.getSiteById(as.getSiteId());
if (site != null)
for(SiteName sn : site.getNameArray())
xos.writeElement(CompXioTags.siteName, CompXioTags.nameType, sn.getNameType(),
sn.getNameValue());
else
Logger.instance().warning("Alarm Screening with id=" + as.getScreeningId()
+ " and name='" + as.getScreeningName() + "' has an invalid site with id="
+ as.getSiteId() + " -- ignored.");
}
if (!DbKey.isNull(as.getDatatypeId()))
{
DataType dt = decDb.dataTypeSet.getById(as.getDatatypeId());
if (dt != null)
{
xos.writeElement(CompXioTags.dataType,
CompXioTags.standard, dt.getStandard(),
CompXioTags.code, dt.getCode(), null);
}
else
Logger.instance().warning("Alarm Screening with id=" + as.getScreeningId()
+ " and name='" + as.getScreeningName() + "' has an invalid datatype with id="
+ as.getDatatypeId() + " -- ignored.");
}
if (as.getStartDateTime() != null)
xos.writeElement(AlarmXioTags.startDateTime, sdf.format(as.getStartDateTime()));
Date lmt = as.getLastModified();
xos.writeElement(AlarmXioTags.lastModified, sdf.format(lmt != null ? lmt : new Date()));
xos.writeElement(AlarmXioTags.enabled, "" + as.isEnabled());
if (as.getAppInfo() != null)
xos.writeElement(AlarmXioTags.AppName, as.getAppInfo().getAppName());
if (!DbKey.isNull(as.getAlarmGroupId()))
{
boolean found = false;
for(AlarmGroup grp : groups)
if (as.getAlarmGroupId().equals(grp.getAlarmGroupId()))
{
xos.writeElement(AlarmXioTags.alarmGroupName, grp.getName());
found = true;
}
if (!found)
Logger.instance().warning("Alarm Screening with id=" + as.getScreeningId()
+ " and name='" + as.getScreeningName() + "' has an invalid AlarmGroup with id="
+ as.getAlarmGroupId() + " -- ignored.");
}
if (as.getDescription() != null)
xos.writeElement(AlarmXioTags.desc, as.getDescription());
NumberFormat nf = NumberFormat.getNumberInstance();
nf.setGroupingUsed(false);
nf.setMaximumFractionDigits(6);
for(AlarmLimitSet als : as.getLimitSets())
{
if (als.getSeasonName() == null)
xos.startElement(AlarmXioTags.AlarmLimitSet);
else
xos.startElement(AlarmXioTags.AlarmLimitSet, AlarmXioTags.seasonName, als.getSeasonName());
if (als.getRejectHigh() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.rejectHigh, nf.format(als.getRejectHigh()));
if (als.getCriticalHigh() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.criticalHigh, nf.format(als.getCriticalHigh()));
if (als.getWarningHigh() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.warningHigh, nf.format(als.getWarningHigh()));
if (als.getWarningLow() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.warningLow, nf.format(als.getWarningLow()));
if (als.getCriticalLow() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.criticalLow, nf.format(als.getCriticalLow()));
if (als.getRejectLow() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.rejectLow, nf.format(als.getRejectLow()));
if (als.getStuckDuration() != null)
{
xos.writeElement(AlarmXioTags.stuckDuration, als.getStuckDuration());
xos.writeElement(AlarmXioTags.stuckTolerance,
nf.format(als.getStuckTolerance() != AlarmLimitSet.UNASSIGNED_LIMIT ?
als.getStuckTolerance() : 0.0));
if (als.getMinToCheck() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.stuckMinToCheck, nf.format(als.getMinToCheck()));
if (als.getMaxGap() != null)
xos.writeElement(AlarmXioTags.stuckMaxGap, als.getMaxGap());
}
if (als.getRocInterval() != null)
{
xos.writeElement(AlarmXioTags.rocInterval, als.getRocInterval());
if (als.getRejectRocHigh() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.rejectRocHigh, nf.format(als.getRejectRocHigh()));
if (als.getCriticalRocHigh() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.criticalRocHigh, nf.format(als.getCriticalRocHigh()));
if (als.getWarningRocHigh() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.warningRocHigh, nf.format(als.getWarningRocHigh()));
if (als.getWarningRocLow() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.warningRocLow, nf.format(als.getWarningRocLow()));
if (als.getCriticalRocLow() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.criticalRocLow, nf.format(als.getCriticalRocLow()));
if (als.getRejectRocLow() != AlarmLimitSet.UNASSIGNED_LIMIT)
xos.writeElement(AlarmXioTags.rejectRocLow, nf.format(als.getRejectRocLow()));
}
if (als.getMissingPeriod() != null && als.getMissingInterval() != null)
{
xos.writeElement(AlarmXioTags.missingPeriod, als.getMissingPeriod());
xos.writeElement(AlarmXioTags.missingInterval, als.getMissingInterval());
xos.writeElement(AlarmXioTags.missingMaxValues, "" + als.getMaxMissingValues());
}
if (als.getHintText() != null)
xos.writeElement(AlarmXioTags.hint, als.getHintText());
xos.endElement(AlarmXioTags.AlarmLimitSet);
}
xos.endElement(AlarmXioTags.AlarmScreening);
}
xos.endElement(AlarmXioTags.AlarmDefinitions);
}
/**
* Test main. Reads XML into objects and then converts back to XML to stdout.
* @param args 1 arg filename
*/
public static void main(String args[])
throws Exception
{
if (args.length == 0)
{
System.err.println("Usage: java ... AlarmXio ");
System.exit(1);
}
AlarmXio xio = new AlarmXio();
AlarmFile alarmFile = xio.readAlarmFile(args[0]);
xio.writeXML(alarmFile.getScreenings(), alarmFile.getGroups(), System.out);
}
}