package org.hwo.pulscounter; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.PrintStream; import java.lang.reflect.Field; import java.net.InetAddress; import java.net.UnknownHostException; import java.text.DateFormat; import java.util.Arrays; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.logging.Level; import java.util.logging.LogManager; import java.util.logging.Logger; import java.util.prefs.Preferences; import org.hwo.beacon.Beacon; import org.hwo.beacon.Beacon.BeaconSender; import org.hwo.csv.CSV; import org.hwo.io.SerialPortExeption; import org.hwo.os.OsDetect; import org.hwo.os.OsDetect.OsType; import org.hwo.pulscounter.application.InspectorApplication; import org.hwo.pulscounter.service.PulsCounterService; import org.hwo.pulscounter.ui.PulsCounterWindow; import org.hwo.rpc.json.RPCAdapter; import org.hwo.rpc.simple.SimpleRPCServer; import org.hwo.rpc.simple.SimpleRPCService; public class PulsCounter { static private Preferences prefs; public static Preferences getPrefs() { return prefs; } static private PulsCounterService localService; public static PulsCounterService getLocalService(){ return localService; } static private InspectorApplication inspectorApplication; static public InspectorApplication getInspectorApplication() { if (inspectorApplication == null) inspectorApplication = new InspectorApplication(); return inspectorApplication; } static String logFile; static boolean serverMode = false; static boolean guiMode = true; static boolean batchMode = false; static boolean batchConvert = false; static boolean startLocalService = false; static String batchFilename = null; static String networkName = null; static String batchConvertTarget = null; static boolean debugMode = false; public static void main(String args[]) { File cwd = new File("."); /* System.err.println("Starting on OS: " + System.getProperty("os.name")); System.err.println("Current Directory: " + cwd.getAbsolutePath()); System.err.println("Library Path: " + System.getProperty("java.library.path")); */ Logger rootLogger = LogManager.getLogManager().getLogger(""); if (rootLogger != null){ rootLogger.setLevel(Level.SEVERE); } prefs = Preferences.userRoot(); if (OsDetect.getOperatingSystem() == OsType.LINUX) { // System.err.println("Linux erkannt. Patche java.library.path"); // System.setProperty("java.library.path",String.format(".:%s",System.getProperty("java.library.path"))); Field fieldSysPath; try { fieldSysPath = ClassLoader.class.getDeclaredField( "sys_paths" ); fieldSysPath.setAccessible( true ); fieldSysPath.set( null, null ); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } // System.err.println("Library Path: " + System.getProperty("java.library.path")); } /* TODO: Load serverMode default from local preferences */ startLocalService = prefs.getBoolean("pulscounter.local.enabled", false); Iterator options = Arrays.asList(args).iterator(); while (options.hasNext()) { String no = options.next(); if (no.equals("-s")) { serverMode = true; guiMode = false; } else if (no.equals("-bo")) { serverMode = false; startLocalService = false; } else if (no.equals("-b")) { batchMode = true; } else if (no.equals("-c")) { batchConvert = true; batchConvertTarget = options.next(); } else if (no.equals("-f")){ batchFilename = options.next(); } else if (no.equals("-n")){ networkName = options.next(); startLocalService = false; } else if (no.equals("-l")){ logFile = options.next(); } else if (no.equals("-u")){ startLocalService = true; } else if (no.equals("-d")){ debugMode = true; } } if (logFile != null){ try { PrintStream logStream = new PrintStream(new FileOutputStream(logFile, true)); System.setErr(logStream); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (batchMode) guiMode = false; localService = new PulsCounterService(); if (startLocalService){ localService.start(); } if (batchMode) { batchrun(); } else if (guiMode){ PulsCounterWindow window = new PulsCounterWindow(); window.setVisible(true); } if (batchConvert) { batchConvert(batchFilename,batchConvertTarget); } } private static void batchrun(){ if (networkName == null) { // TODO: Use UUID from preferences to select network service } else { Beacon clientBeacon = new Beacon(44556); clientBeacon.setClientOnly(true); clientBeacon.start(); for (int i=0;i<120;i++){ if (clientBeacon.getSenderByName(networkName) != null) break; try { Thread.sleep(100); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } BeaconSender sender = clientBeacon.getSenderByName(networkName); if (sender == null) { System.err.println("Networknode: " + networkName + " is offline."); clientBeacon.exit(); return; } IPulsCounter ipc = createProxyFromBeaconSender(sender); Date d = new Date(); String time = String.format("%04d-%02d-%02d %02d:%02d:%02d",d.getYear()+1900,d.getMonth()+1,d.getDate(),d.getHours(),d.getMinutes(),d.getSeconds()); try { CounterChannel[] channels = new CounterChannel[ ipc.getChannels() ]; int[] values = ipc.getChannelCounters(); try { File csvfile = new File(batchFilename); boolean newfile = !csvfile.exists(); BufferedWriter writer = new BufferedWriter(new FileWriter(csvfile,true)); if (newfile){ writer.write("Zeitpunkt"); for (int i=0;i0) writer.write("\t"); writer.write(String.format("%d",values[i])); } writer.write("\r\n"); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } catch (SerialPortExeption sex){ System.err.println(sex); sex.printStackTrace(); } catch (Exception e) { System.err.println(e); e.printStackTrace(); } clientBeacon.exit(); } } public static void batchConvert(String source,String target){ CSV csv = new CSV(); csv.setSeparator('\t'); csv.readFromFile(source); for (int i=csv.getRecords().size()-1;i>1;--i){ for (int j=1;j