package de.desy.tine.histUtils;
import java.io.IOException;
import de.desy.tine.client.TLink;
import de.desy.tine.dataUtils.TDataType;
import de.desy.tine.definitions.TAccess;
import de.desy.tine.definitions.TErrorList;
public final class TArchiver
{
private static final int number_stock_filters = 10;
private static final int nice_query_size = 1000;
private static final int filter_table_size = 32;
private static DAQRec[] recs = new DAQRec[nice_query_size];
private static DAQPrm[] prms = new DAQPrm[nice_query_size/10];
private static DAQFltr[] fltrs = new DAQFltr[filter_table_size];
private static boolean isInitialized = false;
private static void initStructs()
{
if (isInitialized) return;
for (int i=0; i<nice_query_size; i++)
{
recs[i] = new DAQRec();
}
for (int i=0; i<nice_query_size/10; i++)
{
prms[i] = new DAQPrm();
}
for (int i=0; i<filter_table_size; i++)
{
fltrs[i] = new DAQFltr();
}
isInitialized = true;
}
public static DAQRec[] getDAQRecords(String context) throws IOException
{
if (context == null) return null;
if (!isInitialized) initStructs();
for (DAQRec r : recs) r.clear();
TDataType dtrecs = new TDataType(recs);
String dname = "/" + context + "/HISTORY";
TLink tl = new TLink(dname,"RECORD.INFO",dtrecs,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
int len = dtrecs.getCompletionLength();
DAQRec[] results = new DAQRec[len];
for (int i=0; i<len; i++) results[i] = new DAQRec(recs[i]);
return results;
}
public static int setDAQRecords(String context,DAQRec[] recs) throws IOException
{
if (context == null || recs == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
TDataType dtrecs = new TDataType(recs);
String dname = "/" + context + "/HISTORY";
TLink tl = new TLink(dname,"RECORD.INFO",null,dtrecs,TAccess.CA_WRITE);
int cc = tl.executeAndClose(2000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static DAQPrm[] getDAQKeyInfo(String context,String keyFile) throws IOException
{
if (context == null || keyFile == null) return null;
if (!isInitialized) initStructs();
for (DAQPrm p : prms) p.clear();
TDataType dtprms = new TDataType(prms);
String dname = "/" + context + "/HISTORY/"+keyFile;
TLink tl = new TLink(dname,"KEYWORD.INFO",dtprms,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
int len = dtprms.getCompletionLength();
DAQPrm[] results = new DAQPrm[len];
for (int i=0; i<len; i++) results[i] = new DAQPrm(prms[i]);
return results;
}
public static int setDAQKeyInfo(String context,String keyFile,DAQPrm[] prms) throws IOException
{
if (context == null || keyFile == null || prms == null)
return TErrorList.argument_list_error;
if (context.length() == 0 || keyFile.length() == 0 || prms.length == 0)
return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
TDataType dtprms = new TDataType(prms);
if (!keyFile.endsWith(".csv")) keyFile += ".csv";
String dname = "/" + context + "/HISTORY/"+keyFile;
TLink tl = new TLink(dname,"KEYWORD.INFO",null,dtprms,TAccess.CA_WRITE);
int cc = tl.executeAndClose(2000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static TDataType getDAQRecInput(String context,DAQRec rec) throws IOException
{
if (context == null || rec == null) return null;
if (!isInitialized) initStructs();
TDataType dtinpt = new TDataType(rec.getInputLength(),(short)rec.getInputFormat());
String dname = "/" + context + "/HISTORY/"+rec.getInputFileName();
TLink tl = new TLink(dname,"KEYWORD.INPT",dtinpt,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
return dtinpt;
}
public static int setDAQRecInput(String context,DAQRec rec,TDataType din) throws IOException
{
if (context == null || rec == null || din == null)
return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
String dname = "/" + context + "/HISTORY/"+rec.getInputFileName();
TLink tl = new TLink(dname,"KEYWORD.INPT",null,din,TAccess.CA_WRITE);
int cc = tl.executeAndClose(2000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static DAQFltr[] getDAQFilterTable(String context) throws IOException
{
if (context == null) return null;
if (!isInitialized) initStructs();
for (DAQFltr f : fltrs) f.clear();
TDataType dtfltrs = new TDataType(fltrs);
String dname = "/" + context + "/HISTORY";
TLink tl = new TLink(dname,"FILTERTABLE",dtfltrs,null,TAccess.CA_READ);
if (tl.executeAndClose(1000) != 0) throw new IOException(tl.getLastError());
int len = dtfltrs.getCompletionLength();
DAQFltr[] results = new DAQFltr[len];
for (int i=0; i<len; i++) results[i] = new DAQFltr(fltrs[i]);
return results;
}
public static int setDAQFilterTable(String context,DAQFltr[] fltrs) throws IOException
{
if (context == null || fltrs == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
TDataType dtfltrs = new TDataType(fltrs);
String dname = "/" + context + "/HISTORY";
TLink tl = new TLink(dname,"FILTERTABLE",null,dtfltrs,TAccess.CA_WRITE);
int cc = tl.executeAndClose(2000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public int getFilterOptionFromString(DAQFltr[] fltrs,String filterString)
{
int v = 0;
if (fltrs == null || filterString == null) return 0;
for (int i = 0; i<fltrs.length; i++)
{
if (filterString.contains(fltrs[i].getTag()))
{
v += (1<<i);
}
}
return v;
}
public static String getFilterOptionAsString(DAQFltr[] fltrs,int filterOptions)
{
String filterString = "";
if (fltrs == null || filterOptions == 0) return filterString;
int b = 0;
for (int i = 0; i<fltrs.length; i++)
{
b = 1 << i;
if ((filterOptions & b) == b)
{
if (filterString.length() > 0) filterString += "|";
filterString += fltrs[i].getTag();
}
}
return filterString;
}
public static int reloadDatabase(String context) throws IOException
{
if (context == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
String dname = "/" + context + "/HISTORY";
TLink tl = new TLink(dname,"DB.RELOAD",null,null,TAccess.CA_WRITE);
int cc = tl.executeAndClose(3000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
if (cc != 0) return cc;
dname = "/" + context + "/ARCHIVER";
tl = new TLink(dname,"DB.RELOAD",null,null,TAccess.CA_WRITE);
cc = tl.executeAndClose(3000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static int updateServer(String context) throws IOException
{
if (context == null) return TErrorList.argument_list_error;
if (!isInitialized) initStructs();
String dname = "/" + context + "/HISTORY";
TLink tl = new TLink(dname,"RESTART",null,null,TAccess.CA_WRITE);
int cc = tl.executeAndClose(3000);
if (cc == TErrorList.connection_timeout || cc == TErrorList.link_timeout)
throw new IOException(tl.getLastError());
return cc;
}
public static int getNumberStockFilters()
{
return number_stock_filters;
}
}