From 3a65297d3cc9e729c9518403d73527c10fd5ea08 Mon Sep 17 00:00:00 2001 From: ebner Date: Fri, 2 Dec 2011 09:04:57 +0100 Subject: [PATCH] Improved CRLOGIC readout --- ch.psi.fda/pom.xml | 2 +- .../ch/psi/fda/core/loops/cr/CrlogicLoop.java | 108 +++++++++++++----- .../home/scans/templates/cscan2d-crlogic.xml | 2 +- 3 files changed, 84 insertions(+), 28 deletions(-) diff --git a/ch.psi.fda/pom.xml b/ch.psi.fda/pom.xml index 099c379..866f588 100644 --- a/ch.psi.fda/pom.xml +++ b/ch.psi.fda/pom.xml @@ -3,7 +3,7 @@ 4.0.0 ch.psi fda - 1.1.28 + 1.1.29 diff --git a/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/cr/CrlogicLoop.java b/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/cr/CrlogicLoop.java index a84ff8e..4acb9b7 100644 --- a/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/cr/CrlogicLoop.java +++ b/ch.psi.fda/src/main/java/ch/psi/fda/core/loops/cr/CrlogicLoop.java @@ -67,6 +67,12 @@ public class CrlogicLoop implements ActionLoop { */ private boolean dataGroup = false; + boolean keepTmpFiles = true; + + private BlockingQueue readQueue = new LinkedBlockingQueue(); + private volatile boolean stopReadoutThread = false; + private Thread readoutThread; + // Constants /** @@ -182,7 +188,12 @@ public class CrlogicLoop implements ActionLoop { logger.info("Wait until file is written [lock file: " + lockfile.getCanonicalPath() + "]"); // Wait until file is created while ((!tmpFile.exists()) || lockfile.exists()) { - Thread.sleep(100); + try{ + Thread.sleep(100); + } + catch(InterruptedException e){ + abort=true; + } if(abort){ // If abort is issued while waiting for data immediately return without // trying to read the data @@ -194,12 +205,15 @@ public class CrlogicLoop implements ActionLoop { BufferedReader in = new BufferedReader(inreader); String line; boolean firstline = true; + int linecount=0; + int mcounter=0; - boolean wasInRangeBefore = false; +// boolean wasInRangeBefore = false; boolean discardAnyway = false; while (true) { line = in.readLine(); + linecount++; if (line == null) { break; } else { @@ -255,17 +269,20 @@ public class CrlogicLoop implements ActionLoop { message.getData().add(val); } - // Use this to filter out motor retry movements at the end of the scan - wasInRangeBefore = wasInRangeBefore | use; - if(!use && wasInRangeBefore){ - discardAnyway=true; - // Optimization - terminate read loop once range is left - break; - } + // Does not work if zigzag, ... +// // Use this to filter out motor retry movements at the end of the scan +// wasInRangeBefore = wasInRangeBefore | use; +// if(!use && wasInRangeBefore){ +// discardAnyway=true; +// // Optimization - terminate read loop once range is left +// logger.info("Terminate read loop because point is outside range"); +// break; +// } // Filter data if(use && !discardAnyway){ dataQueue.put(message); + mcounter++; } @@ -275,8 +292,12 @@ public class CrlogicLoop implements ActionLoop { in.close(); inreader.close(); + logger.info("Lines read: "+linecount+" Messages generated (after filtering): "+mcounter); + // Remove temporary file - tmpFile.delete(); + if(!keepTmpFiles){ + tmpFile.delete(); + } } else{ // TODO - File in local file system @@ -538,22 +559,24 @@ public class CrlogicLoop implements ActionLoop { motortemplate.getBacklashDistance().setValue(backupBacklash); } - - // Read data - Thread t = new Thread(new Runnable() { - - @Override - public void run() { - try { - collectData(smbShare, tmpFileName); - } catch (InterruptedException e) { - throw new RuntimeException("Unable to read CRLOGIC raw data file",e); - } catch (IOException e) { - throw new RuntimeException("Unable to read CRLOGIC raw data file",e); - } - } - }); - t.start(); + // Request read of data file + readQueue.put(tmpFileName); + +// // Read data +// Thread t = new Thread(new Runnable() { +// +// @Override +// public void run() { +// try { +// collectData(smbShare, tmpFileName); +// } catch (InterruptedException e) { +// throw new RuntimeException("Unable to read CRLOGIC raw data file",e); +// } catch (IOException e) { +// throw new RuntimeException("Unable to read CRLOGIC raw data file",e); +// } +// } +// }); +// t.start(); if(zigZag){ // Swap start and end @@ -598,6 +621,10 @@ public class CrlogicLoop implements ActionLoop { */ @Override public void destroy() { + stopReadoutThread = true; + readoutThread.interrupt(); + // TODO eventually interrupt readout thread + try { ChannelBeanFactory.getFactory().destroyChannelBeans(template); @@ -617,6 +644,35 @@ public class CrlogicLoop implements ActionLoop { */ @Override public void prepare() { + + stopReadoutThread = false; + // Start readout Thread + readoutThread = new Thread(new Runnable() { + + @Override + public void run() { + while(!stopReadoutThread){ + String file; + try { + file = readQueue.take(); + } catch (InterruptedException e) { + break; + } + // TODO Read file and + try { + collectData(smbShare, file); + } catch (InterruptedException e) { + throw new RuntimeException("Unable to read CRLOGIC raw data file",e); + } catch (IOException e) { + throw new RuntimeException("Unable to read CRLOGIC raw data file",e); + } + + } + + } + }); + readoutThread.start(); + try{ // Connect crlogic channels template = new CrlogicChannelsTemplate(); diff --git a/ch.psi.fda/src/test/resources/home/scans/templates/cscan2d-crlogic.xml b/ch.psi.fda/src/test/resources/home/scans/templates/cscan2d-crlogic.xml index 8770396..a191c2e 100644 --- a/ch.psi.fda/src/test/resources/home/scans/templates/cscan2d-crlogic.xml +++ b/ch.psi.fda/src/test/resources/home/scans/templates/cscan2d-crlogic.xml @@ -21,7 +21,7 @@ - 3 + 30