altosdroid: reload previous log file at connect time
authorKeith Packard <keithp@keithp.com>
Sun, 31 Aug 2014 05:28:15 +0000 (00:28 -0500)
committerKeith Packard <keithp@keithp.com>
Sun, 31 Aug 2014 05:28:15 +0000 (00:28 -0500)
Use the saved logfile to re-load the previous state at startup time.

Signed-off-by: Keith Packard <keithp@keithp.com>
altosdroid/src/org/altusmetrum/AltosDroid/TelemetryReader.java

index 0c437f87a7fd3f20513b2dfb9bb197e75622c04c..bec518516f9113ac7b2c5749ba39fd872b97c947 100644 (file)
@@ -39,9 +39,17 @@ public class TelemetryReader extends Thread {
        AltosLink   link;
        AltosState  state = null;
 
        AltosLink   link;
        AltosState  state = null;
 
+       AltosFlightReader       stacked;
+
        LinkedBlockingQueue<AltosLine> telemQueue;
 
        public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
        LinkedBlockingQueue<AltosLine> telemQueue;
 
        public AltosState read() throws ParseException, AltosCRCException, InterruptedException, IOException {
+               if (stacked != null) {
+                       state = stacked.read();
+                       if (state != null)
+                               return state;
+                       stacked = null;
+               }
                AltosLine l = telemQueue.take();
                if (l.line == null)
                        throw new IOException("IO error");
                AltosLine l = telemQueue.take();
                if (l.line == null)
                        throw new IOException("IO error");
@@ -56,6 +64,10 @@ public class TelemetryReader extends Thread {
 
        public void close() {
                state = null;
 
        public void close() {
                state = null;
+               if (stacked != null) {
+                       stacked.close(false);
+                       stacked = null;
+               }
                link.remove_monitor(telemQueue);
                link = null;
                telemQueue.clear();
                link.remove_monitor(telemQueue);
                link = null;
                telemQueue.clear();
@@ -84,9 +96,10 @@ public class TelemetryReader extends Thread {
                }
        }
 
                }
        }
 
-       public TelemetryReader (AltosLink in_link, Handler in_handler) {
+       public TelemetryReader (AltosLink in_link, Handler in_handler, AltosFlightReader in_stacked) {
                link    = in_link;
                handler = in_handler;
                link    = in_link;
                handler = in_handler;
+               stacked = in_stacked;
 
                state = null;
                telemQueue = new LinkedBlockingQueue<AltosLine>();
 
                state = null;
                telemQueue = new LinkedBlockingQueue<AltosLine>();
@@ -101,4 +114,21 @@ public class TelemetryReader extends Thread {
                        close();
                }
        }
                        close();
                }
        }
+
+       private static AltosFlightReader existing_data(AltosLink link) {
+               if (link == null)
+                       return null;
+
+               File    file = AltosPreferences.logfile(link.serial);
+               if (file != null) {
+                       AltosStateIterable      iterable = AltosStateIterable.iterable(file);
+                       if (iterable != null)
+                               return new AltosReplayReader(iterable.iterator(), file, false);
+               }
+               return null;
+       }
+
+       public TelemetryReader(AltosLink link, Handler handler) {
+               this(link, handler, existing_data(link));
+       }
 }
 }