Main Page | Class Hierarchy | Class List | File List | Class Members | Related Pages

DaqToRbnb.java

00001 00011 package org.nees.daq; 00012 00013 import java.io.IOException; 00014 import java.net.UnknownHostException; 00015 import java.net.Socket; 00016 import java.util.Hashtable; 00017 import java.util.Calendar; 00018 import java.util.GregorianCalendar; 00019 import com.rbnb.sapi.*; 00020 00021 import com.rbnb.utility.ArgHandler; //for argument parsing 00022 import com.rbnb.utility.RBNBProcess; //alternative to System.exit, so 00023 //don't bring down servlet engine 00024 //import COM.Creare.Utility.ArgHandler; //for argument parsing 00025 //import COM.Creare.Utility.RBNBProcess; //alternative to System.exit, so 00026 //don't bring down servlet engine 00027 00037 public class DaqToRbnb { 00038 00039 private ControlPort controlPort = null; 00040 private DataThread dataThread = null; 00041 private ChannelEntry[] channels = new ChannelEntry[0]; 00042 00043 private static final String DEFAULT_DAQ_SERVER = "localhost"; 00044 private static final int DEFAULT_DAQ_CONTROL_PORT = 55055; 00045 private static final int DEFAULT_DAQ_DATA_PORT = 55056; 00046 00047 private String daqServerName = DEFAULT_DAQ_SERVER; 00048 private int daqControlPort = DEFAULT_DAQ_CONTROL_PORT; 00049 private int daqDataPort = DEFAULT_DAQ_DATA_PORT; 00050 00051 private static final String DEFAULT_RBNB_SERVER = "localhost"; 00052 private static final int DEFAULT_RBNB_PORT = 3333; 00053 private static final String DEFAULT_RBNB_SOURCE_NAME = "FromDAQ"; 00054 00055 private String rbnbServerName = DEFAULT_RBNB_SERVER; 00056 private int rbnbServerPort = DEFAULT_RBNB_PORT; 00057 private String rbnbSourceName = DEFAULT_RBNB_SOURCE_NAME; 00058 00059 private static final int DEFAULT_CACHE_SIZE=100; 00060 private int cacheSize = DEFAULT_CACHE_SIZE; 00061 00062 private Socket controlSocket = null; 00063 private Socket dataSocket = null; 00064 00065 private Source source; 00066 00067 private double timeOffset = 0.0; // in seconds 00068 00086 public static final void main(String[] args) 00087 { 00088 00089 System.out.println("*************************************************"); 00090 System.out.println("* This version of the code is obsoleate, please *"); 00091 System.out.println("* use org.nees.rbnb.DaqToRbnb instead. Also, be *"); 00092 System.out.println("* aware that the parameters may have changed. *"); 00093 System.out.println("*************************************************"); 00094 00095 return; 00096 /* 00097 try 00098 { 00099 DaqToRbnb control = new DaqToRbnb(args); 00100 control.startDaqConnections(); // note both connects must be made first 00101 control.connectToDaqControl(); 00102 control.buildChannels(); 00103 control.start(); 00104 } 00105 catch (Exception e) 00106 { 00107 e.printStackTrace(); 00108 } 00109 */ 00110 } 00111 00116 public DaqToRbnb(String[] args) 00117 { 00118 computeDefaultTimeOffset(); 00119 parseArgs(args); 00120 } 00121 00124 private void computeDefaultTimeOffset() { 00125 Calendar calendar = new GregorianCalendar(); 00126 long tz = calendar.get(Calendar.ZONE_OFFSET); 00127 long dt = calendar.get(Calendar.DST_OFFSET); 00128 System.out.println("Default time: Time Zone offset: " 00129 + (-((double)(tz/1000))/(60.0*60.0))); // in hours 00130 System.out.println("Defalut time: Daylight Savings Time offest (in hours): " 00131 + (-((double)(dt/1000))/(60.0*60.0))); // in hours 00132 // Time zone offset 00133 timeOffset = - (double)((tz + dt)/1000); // in seconds 00134 } 00135 00137 private void printUsage() { 00138 System.out.println("DaqToRbnb: usage is..."); 00139 System.out.println("DaqToRbnb "); 00140 System.out.println("[-q DAQ Server *" + DEFAULT_DAQ_SERVER + "] "); 00141 System.out.println("[-c DAQ Control Port *" + DEFAULT_DAQ_CONTROL_PORT + "] "); 00142 System.out.println("[-d DAQ Data Port *" + DEFAULT_DAQ_DATA_PORT + "] "); 00143 System.out.println("[-s RBNB Server *" + DEFAULT_RBNB_SERVER + "] "); 00144 System.out.println("[-p RBNB Port *" + DEFAULT_RBNB_PORT + "] "); 00145 System.out.println("[-n RBNB Source Name *" + DEFAULT_RBNB_SOURCE_NAME + "] "); 00146 System.out.println("[-z cache size *" + DEFAULT_CACHE_SIZE + "]"); 00147 double hours = timeOffset/(60.0*60.0); 00148 System.out.println("[-o time offset, floating point, hours to GMT *"+ hours +"]"); 00149 System.out.println(); 00150 } 00151 00155 private void parseArgs (String[] args) 00156 { 00157 String a; 00158 //parse args 00159 try { 00160 ArgHandler ah=new ArgHandler(args); 00161 if (ah.checkFlag('h')) { 00162 printUsage(); 00163 RBNBProcess.exit(0); 00164 } 00165 if (ah.checkFlag('q')) { 00166 a=ah.getOption('q'); 00167 if (a!=null) daqServerName=a; 00168 } 00169 if (ah.checkFlag('c')) { 00170 a=ah.getOption('c'); 00171 if (a!=null) daqControlPort=Integer.parseInt(a); 00172 } 00173 if (ah.checkFlag('d')) { 00174 a=ah.getOption('d'); 00175 if (a!=null) daqDataPort=Integer.parseInt(a); 00176 } 00177 if (ah.checkFlag('s')) { 00178 a=ah.getOption('s'); 00179 if (a!=null) rbnbServerName=a; 00180 } 00181 if (ah.checkFlag('p')) { 00182 a=ah.getOption('p'); 00183 if (a!=null) rbnbServerPort=Integer.parseInt(a); 00184 } 00185 if (ah.checkFlag('n')) { 00186 a=ah.getOption('n'); 00187 if (a!=null) rbnbSourceName=a; 00188 } 00189 if (ah.checkFlag('z')) { 00190 a=ah.getOption('z'); 00191 if (a!=null) 00192 try 00193 { 00194 Integer i = new Integer(a); 00195 int value = i.intValue(); 00196 cacheSize = value; 00197 } 00198 catch (Exception ignore) {} 00199 } 00200 if (ah.checkFlag('o')) { 00201 a=ah.getOption('o'); 00202 if (a!=null) 00203 try 00204 { 00205 double value = Double.parseDouble(a); // in hours 00206 timeOffset = (long)(value*60.0*60.0); // in seconds 00207 } 00208 catch (Exception ignore) {} 00209 } 00210 } catch (Exception e) { 00211 System.err.println("DaqToRbnb argument exception "+e.getMessage()); 00212 e.printStackTrace(); 00213 RBNBProcess.exit(0); 00214 } 00215 System.out.println("Starting DaqToRbnb with"); 00216 System.out.println(" DAQ: server = " + daqServerName + 00217 "; control port = "+ daqControlPort + "; data port = " + daqDataPort ); 00218 System.out.println(" RBNB: server = " + rbnbServerName + 00219 "; port = "+ rbnbServerPort + "; source name = " + rbnbSourceName ); 00220 System.out.println(" Time offset (in seconds) = " + timeOffset 00221 + ", which is " + timeOffset/(60.0*60.0) + " hours"); 00222 System.out.println(" Use DaqToRbnb -h to see optional parameters"); 00223 } 00224 00225 00230 private void startDaqConnections() 00231 throws UnknownHostException, IOException 00232 { 00234 controlSocket = new Socket(daqServerName,daqControlPort); 00235 00239 try 00240 { 00241 Thread.sleep(1000); 00242 } catch (Exception ignore) {} 00243 System.out.println("...Continued"); 00244 00245 dataSocket = new Socket(daqServerName,daqDataPort); 00246 } 00247 00251 private void connectToDaqControl() 00252 throws UnknownHostException, IOException 00253 { 00254 controlPort = null; 00255 00256 ControlPort port = null; 00257 00258 // use a local vaiable in case there is an exception 00259 port = new ControlPort(controlSocket); 00260 00261 System.out.println("Pause for one sec after ControlPort..."); 00262 try 00263 { 00264 Thread.sleep(1000); 00265 } catch (Exception ignore) {} 00266 System.out.println("...Continued"); 00267 00268 controlPort = port; 00269 } 00270 00275 private void buildChannels() 00276 throws IOException, SAPIException 00277 { 00278 if (controlPort == null) return; 00279 00281 dataThread = new DataThread(dataSocket); 00282 00283 source=new Source(cacheSize, "none", 0); 00284 source.OpenRBNBConnection(rbnbServerName + ":" + rbnbServerPort, rbnbSourceName); 00285 00286 String[] channelList = controlPort.getChannels(); 00287 00288 channels = new ChannelEntry[channelList.length]; 00289 00291 for (int i = 0; i < channelList.length; i++) 00292 { 00293 String name = channelList[i]; 00294 channels[i] = new ChannelEntry(name); 00295 dataThread.addListener(name, channels[i]); 00296 } 00297 00298 } 00299 00304 private void start() throws IOException 00305 { 00306 if (controlPort == null) return; 00307 00308 if (dataThread == null) return; 00309 00310 for (int i = 0; i < channels.length; i++) 00311 { 00312 channels[i].subscribe(); 00313 } 00314 00315 System.out.println("Startint data Thread"); 00316 dataThread.start(); 00317 00318 } 00319 00327 private class ChannelEntry implements DaqListener 00328 { 00329 00330 String name; 00331 ChannelMap map; 00332 boolean subscribed = false; 00333 int index; 00334 00339 ChannelEntry(String name) throws SAPIException 00340 { 00341 this.name = name; 00342 map = new ChannelMap(); 00343 index = map.Add(name); 00344 System.out.println("RBNB: Connection made to sever = " 00345 + rbnbServerName + ":" + rbnbServerPort + 00346 " as source = " + rbnbSourceName + 00347 " with channel = " + name + "."); 00348 } 00349 00351 void subscribe() throws IOException 00352 { 00353 controlPort.subscribe(name); 00354 subscribed = true; 00355 } 00356 00358 void unsubscribe() throws IOException 00359 { 00360 controlPort.unsubscribe(name); 00361 subscribed = false; 00362 } 00363 00364 /* 00365 @brief Put DAQ data into turbine 00366 00367 Implements the data listener; data arrives from DAQ and is put on RBNB. 00368 00369 @param name Channel name 00370 @param time timestamp, RBNB format, in seconds 00371 @param data DAQ datum 00372 @see DaqListener#postData 00373 @bug the timeOffset was added when it was discovered that the DAQ controller 00374 did not know about time zones and was using the system time from the machine 00375 of origin as THE TIME. 00376 */ 00377 public void postData(String name, double time, double data) 00378 throws SAPIException 00379 { 00380 if (!name.equals(this.name)) return; 00381 00382 map.PutTime(time + timeOffset, 0.0); // in seconds 00383 00384 //map.PutTimeAuto("timeofday"); 00385 double dataArray[] = new double[1]; 00386 dataArray[0] = data; 00387 map.PutDataAsFloat64(index,dataArray); 00388 // System.out.println("Put Data, " + name + ": " + data); 00389 source.Flush(map); 00390 } 00391 } 00392 }

Generated on Tue Aug 24 11:12:25 2004 for Data turbine for NEESGrid by doxygen 1.3.7