From a4b60759904ffdf61558e119e823abe77df6bb5b Mon Sep 17 00:00:00 2001
From: ulrich
Date: Thu, 01 Apr 2021 15:05:15 +0000
Subject: [PATCH] start angepasst

---
 src/de/uhilger/avdirektor/OMXLogLeser.java |  160 +++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 137 insertions(+), 23 deletions(-)

diff --git a/src/de/uhilger/avdirektor/OMXLogLeser.java b/src/de/uhilger/avdirektor/OMXLogLeser.java
index 80b2765..91ff199 100644
--- a/src/de/uhilger/avdirektor/OMXLogLeser.java
+++ b/src/de/uhilger/avdirektor/OMXLogLeser.java
@@ -7,6 +7,13 @@
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.logging.Level;
 import java.util.logging.Logger;
 
 /**
@@ -16,36 +23,143 @@
 public class OMXLogLeser {
   
   private static final Logger logger = Logger.getLogger(OMXLogLeser.class.getName());
+  
+  public static final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
+  
+  private static final long MILLIS = (long) 1000;
+  
+  private static final long MINSEC = (long) 60;
+  
+  
+  public String logDirLesen(File logDir) throws IOException, FileNotFoundException, ParseException {
+    Blocks blocks = new Blocks();
+    File[] files = logDir.listFiles();
+    for(File file : files) {
+      lesen(file, blocks);
+    }
+    return blocks.getTimeString();
+  }
       
-  public String lesen(File logfile) throws FileNotFoundException, IOException {    
+  /**
+   * 
+   * <p>Die Spieldauer laut OMX-Log ergibt sich aus dem Zeitstempel des 
+   * letzten Eintrags abzueglich des Zeitstempels des 
+   * ersten Eintrags abzueglich der Pausen</p>
+   * 
+   * <p>Eine Pause ist der Zeitraum vom Zeitstempel eines Eintrags mit 
+   * <code>Keyboard: character p (0x70)</code> bis zum Zeitstempel des 
+   * n&auml;chsten Eintrags mit <code>Keyboard: character p</code></p>
+   * 
+   * 
+   * @param logfile
+   * @param blocks
+   * @return  die Spieldauer als String im Format H:MM:SS
+   * @throws FileNotFoundException
+   * @throws IOException
+   * @throws ParseException 
+   */
+  public void lesen(File logfile, Blocks blocks) throws FileNotFoundException, IOException, ParseException {    
+    boolean inPause = false;
+    logger.info("Starting to parse log..");
+    Date parseStart = new Date();
+    long lineCount = 0;
     InputStream is = new FileInputStream(logfile);
     BufferedReader r = new BufferedReader(new InputStreamReader(is));
     String firstLine = r.readLine();
+    ++lineCount;
     String lastLine = "";
-    if(firstLine != null) {
-      long size = logfile.length();
-      long pos = size - (long) 1000;
-      logger.info("Size: " + size + ", Pos: " + pos); // 2.341.930 Bytes
-      long skipped = r.skip(pos);
-      logger.info("skipped: " + skipped);
-      String line = r.readLine();
-      while(line != null) {
-        lastLine = line;
-        logger.info(lastLine);
-        line = r.readLine();
+    String line = r.readLine();
+    //Blocks blocks = new Blocks();
+    Block currentBlock = new Block();
+    while(line != null) {
+      ++lineCount;
+      if(currentBlock.getStart() == null) {
+        currentBlock.setStart(line.substring(0, 8));
       }
+      if(line.contains("Keyboard: character p")) {
+        if(inPause) {
+          currentBlock.setStart(line.substring(0, 8));          
+        } else {
+          currentBlock.setEnd(line.substring(0, 8));
+          blocks.add(currentBlock);
+          currentBlock = new Block();
+        }
+        inPause = !inPause;
+      }
+      lastLine = line;
+      line = r.readLine();
     }
-    r.close();
-    is.close();
-    StringBuilder sb = new StringBuilder();
-    sb.append("\r\n---");
-    sb.append("\r\nfirst line:\r\n");
-    sb.append(firstLine);
-    sb.append("\r\n\r\nlastLine\r\n");
-    sb.append(lastLine);
-    String lines = sb.toString();
-    logger.info(lines);
-    return lines;
+    currentBlock.setEnd(lastLine.substring(0, 8));
+    blocks.add(currentBlock);
+    Date parseEnd = new Date();
+    long timeMillis = parseEnd.getTime() - parseStart.getTime();
+    long timeSeconds = timeMillis / MILLIS;
+    long secMillis = timeSeconds * MILLIS;
+    long restMillis = timeMillis - secMillis;
+    logger.log(Level.INFO, 
+            "{0} lines parsed in {1} seconds and {2} milliseconds.", 
+            new Object[]{lineCount, timeSeconds, restMillis});
+    //return blocks.getTimeString();
   }
   
+  class Block {
+    private String start = null;
+    private String end = null;
+    
+    public long getDuration() throws ParseException {
+      Date startDate = sdf.parse(start);
+      Date endDate = sdf.parse(end);
+      return endDate.getTime() - startDate.getTime();
+    }
+
+    public String getStart() {
+      return start;
+    }
+
+    public void setStart(String start) {
+      //logger.log(Level.FINER, "Start: ''{0}''", start);
+      this.start = start;
+    }
+
+    public String getEnd() {
+      return end;
+    }
+
+    public void setEnd(String end) {
+      //logger.log(Level.FINER, "End: ''{0}''", end);
+      this.end = end;
+    }
+    
+    
+  }
+  
+  class Blocks {
+    
+    private final List<Block> blocks = new ArrayList();
+    
+    public void add(Block block) {
+      blocks.add(block);
+    }
+    
+    public long getDuration() throws ParseException {
+      long duration = (long) 0;
+      Iterator<Block> i = blocks.iterator();
+      while(i.hasNext()) {
+        Block b = i.next();
+        duration += b.getDuration();
+      }
+      return duration;
+    }
+    
+    public String getTimeString() throws ParseException {
+      long diff = getDuration();
+      long h = diff / MILLIS / MINSEC / MINSEC;
+      long hmillis = h * MINSEC * MINSEC  * MILLIS;
+      long m = (diff - hmillis) / MILLIS / MINSEC;
+      long mmillis = m * MINSEC * MILLIS;
+      long s = (diff - hmillis - mmillis) / MILLIS;
+      //logger.log(Level.FINER, "{0}:{1}:{2}", new Object[]{h, m, s});
+      return h + ":" + m + ":" + s;
+    }
+  }
 }

--
Gitblit v1.9.3