altosdroid: reload previous log file at connect time
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryReader.java
index c47e4942040dd911a1d44f4e3b05f7df33dc145e..bec518516f9113ac7b2c5749ba39fd872b97c947 100644 (file)
-/*\r
- * Copyright © 2011 Keith Packard <keithp@keithp.com>\r
- * Copyright © 2012 Mike Beattie <mike@ethernal.org>\r
- *\r
- * This program is free software; you can redistribute it and/or modify\r
- * it under the terms of the GNU General Public License as published by\r
- * the Free Software Foundation; version 2 of the License.\r
- *\r
- * This program is distributed in the hope that it will be useful, but\r
- * WITHOUT ANY WARRANTY; without even the implied warranty of\r
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
- * General Public License for more details.\r
- *\r
- * You should have received a copy of the GNU General Public License along\r
- * with this program; if not, write to the Free Software Foundation, Inc.,\r
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
- */\r
-\r
-\r
-package org.altusmetrum.AltosDroid;\r
-\r
-import java.text.*;\r
-import java.io.*;\r
-import java.util.concurrent.*;\r
-import android.util.Log;\r
-import android.os.Handler;\r
-\r
-import org.altusmetrum.AltosLib.*;\r
-\r
-\r
-public class TelemetryReader extends Thread {\r
-\r
-       private static final String TAG = "TelemetryReader";\r
-\r
-       int         crc_errors;\r
-\r
-       Handler     handler;\r
-\r
-       AltosLink   link;\r
-       AltosRecord previous;\r
-\r
-       LinkedBlockingQueue<AltosLine> telem;\r
-\r
-       public AltosRecord read() throws ParseException, AltosCRCException, InterruptedException, IOException {\r
-               AltosLine l = telem.take();\r
-               if (l.line == null)\r
-                       throw new IOException("IO error");\r
-               AltosRecord     next = AltosTelemetry.parse(l.line, previous);\r
-               previous = next;\r
-               return next;\r
-       }\r
-\r
-       public void close() {\r
-               previous = null;\r
-               link.remove_monitor(telem);\r
-               link = null;\r
-               telem.clear();\r
-               telem = null;\r
-       }\r
-\r
-       public void run() {\r
-               AltosState  state = null;\r
-\r
-               try {\r
-                       for (;;) {\r
-                               try {\r
-                                       AltosRecord record = read();\r
-                                       if (record == null)\r
-                                               break;\r
-                                       state = new AltosState(record, state);\r
-\r
-                                       handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();\r
-                               } catch (ParseException pp) {\r
-                                       Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));\r
-                               } catch (AltosCRCException ce) {\r
-                                       ++crc_errors;\r
-                               }\r
-                       }\r
-               } catch (InterruptedException ee) {\r
-               } catch (IOException ie) {\r
-               } finally {\r
-                       close();\r
-               }\r
-       }\r
-\r
-       public TelemetryReader (AltosLink in_link, Handler in_handler) {\r
-               link    = in_link;\r
-               handler = in_handler;\r
-\r
-               previous = null;\r
-               telem = new LinkedBlockingQueue<AltosLine>();\r
-               link.add_monitor(telem);\r
-       }\r
-}\r
+/*
+ * Copyright © 2011 Keith Packard <keithp@keithp.com>
+ * Copyright © 2012 Mike Beattie <mike@ethernal.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+
+package org.altusmetrum.AltosDroid;
+
+import java.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+import android.util.Log;
+import android.os.Handler;
+
+import org.altusmetrum.altoslib_5.*;
+
+
+public class TelemetryReader extends Thread {
+
+       private static final String TAG = "TelemetryReader";
+
+       int         crc_errors;
+
+       Handler     handler;
+
+       AltosLink   link;
+       AltosState  state = null;
+
+       AltosFlightReader       stacked;
+
+       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");
+               AltosTelemetry telem = AltosTelemetryLegacy.parse(l.line);
+               if (state == null)
+                       state = new AltosState();
+               else
+                       state = state.clone();
+               telem.update_state(state);
+               return state;
+       }
+
+       public void close() {
+               state = null;
+               if (stacked != null) {
+                       stacked.close(false);
+                       stacked = null;
+               }
+               link.remove_monitor(telemQueue);
+               link = null;
+               telemQueue.clear();
+               telemQueue = null;
+       }
+
+       public void run() {
+               AltosState  state = null;
+
+               try {
+                       while (telemQueue != null) {
+                               try {
+                                       state = read();
+                                       handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();
+                               } catch (ParseException pp) {
+                                       Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));
+                               } catch (AltosCRCException ce) {
+                                       ++crc_errors;
+                                       handler.obtainMessage(TelemetryService.MSG_CRC_ERROR, new Integer(crc_errors)).sendToTarget();
+                               }
+                       }
+               } catch (InterruptedException ee) {
+               } catch (IOException ie) {
+               } finally {
+                       close();
+               }
+       }
+
+       public TelemetryReader (AltosLink in_link, Handler in_handler, AltosFlightReader in_stacked) {
+               link    = in_link;
+               handler = in_handler;
+               stacked = in_stacked;
+
+               state = null;
+               telemQueue = new LinkedBlockingQueue<AltosLine>();
+               link.add_monitor(telemQueue);
+               try {
+                       link.set_radio_frequency(AltosPreferences.frequency(link.serial));
+                       link.set_telemetry(AltosLib.ao_telemetry_standard);
+                       link.set_telemetry_rate(AltosPreferences.telemetry_rate(link.serial));
+               } catch (InterruptedException ee) {
+                       close();
+               } catch (TimeoutException te) {
+                       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));
+       }
+}