altosui: Move telemetry reader &c to altoslib
authorKeith Packard <keithp@keithp.com>
Tue, 3 Jan 2012 06:35:41 +0000 (22:35 -0800)
committerKeith Packard <keithp@keithp.com>
Sun, 3 Jun 2012 02:37:26 +0000 (19:37 -0700)
Move all of the device and file reading code into altoslib

Signed-off-by: Keith Packard <keithp@keithp.com>
16 files changed:
altosui/AltosFile.java [deleted file]
altosui/AltosFlightReader.java [deleted file]
altosui/AltosLog.java [deleted file]
altosui/AltosReplayReader.java [deleted file]
altosui/AltosScanUI.java
altosui/AltosSerial.java
altosui/AltosTelemetryReader.java [deleted file]
altosui/AltosUI.java
altosui/Makefile.am
altosui/altoslib/Makefile.am
altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java [new file with mode: 0644]
altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java [new file with mode: 0644]
altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java
altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java [new file with mode: 0644]
altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java [new file with mode: 0644]
altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java [new file with mode: 0644]

diff --git a/altosui/AltosFile.java b/altosui/AltosFile.java
deleted file mode 100644 (file)
index 4cf7de3..0000000
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * Copyright © 2010 Keith Packard <keithp@keithp.com>
- *
- * 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 altosui;
-
-import java.lang.*;
-import java.io.File;
-import java.util.*;
-import org.altusmetrum.AltosLib.*;
-
-class AltosFile extends File {
-
-       public AltosFile(int year, int month, int day, int serial, int flight, String extension) {
-               super (AltosUIPreferences.logdir(),
-                      String.format("%04d-%02d-%02d-serial-%03d-flight-%03d.%s",
-                                    year, month, day, serial, flight, extension));
-       }
-
-       public AltosFile(int serial, int flight, String extension) {
-               this(Calendar.getInstance().get(Calendar.YEAR),
-                    Calendar.getInstance().get(Calendar.MONTH) + 1,
-                    Calendar.getInstance().get(Calendar.DAY_OF_MONTH),
-                    serial,
-                    flight,
-                    extension);
-       }
-
-       public AltosFile(AltosRecord telem) {
-               this(telem.serial, telem.flight, "telem");
-       }
-}
diff --git a/altosui/AltosFlightReader.java b/altosui/AltosFlightReader.java
deleted file mode 100644 (file)
index 1ac9f84..0000000
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * Copyright © 2010 Keith Packard <keithp@keithp.com>
- *
- * 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 altosui;
-
-import java.lang.*;
-import java.text.*;
-import java.io.*;
-import java.util.concurrent.*;
-import org.altusmetrum.AltosLib.*;
-
-public class AltosFlightReader {
-       String name;
-
-       int serial;
-
-       void init() { }
-
-       AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException { return null; }
-
-       void close(boolean interrupted) { }
-
-       void set_frequency(double frequency) throws InterruptedException, TimeoutException { }
-
-       void save_frequency() { }
-
-       void set_telemetry(int telemetry) { }
-
-       void save_telemetry() { }
-
-       void update(AltosState state) throws InterruptedException { }
-
-       boolean supports_telemetry(int telemetry) { return false; }
-
-       File backing_file() { return null; }
-}
diff --git a/altosui/AltosLog.java b/altosui/AltosLog.java
deleted file mode 100644 (file)
index 740f0be..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Copyright © 2010 Keith Packard <keithp@keithp.com>
- *
- * 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 altosui;
-
-import java.io.*;
-import java.lang.*;
-import java.util.*;
-import java.text.ParseException;
-import java.util.concurrent.LinkedBlockingQueue;
-import org.altusmetrum.AltosLib.*;
-
-/*
- * This creates a thread to capture telemetry data and write it to
- * a log file
- */
-class AltosLog implements Runnable {
-
-       LinkedBlockingQueue<AltosLine>  input_queue;
-       LinkedBlockingQueue<String>     pending_queue;
-       int                             serial;
-       int                             flight;
-       FileWriter                      log_file;
-       Thread                          log_thread;
-       AltosFile                       file;
-
-       private void close_log_file() {
-               if (log_file != null) {
-                       try {
-                               log_file.close();
-                       } catch (IOException io) {
-                       }
-                       log_file = null;
-               }
-       }
-
-       void close() {
-               close_log_file();
-               if (log_thread != null) {
-                       log_thread.interrupt();
-                       log_thread = null;
-               }
-       }
-
-       File file() {
-               return file;
-       }
-
-       boolean open (AltosRecord telem) throws IOException {
-               AltosFile       a = new AltosFile(telem);
-
-               System.out.printf("open %s\n", a.toString());
-               log_file = new FileWriter(a, true);
-               if (log_file != null) {
-                       while (!pending_queue.isEmpty()) {
-                               try {
-                                       String s = pending_queue.take();
-                                       log_file.write(s);
-                                       log_file.write('\n');
-                               } catch (InterruptedException ie) {
-                               }
-                       }
-                       log_file.flush();
-                       file = a;
-               }
-               return log_file != null;
-       }
-
-       public void run () {
-               try {
-                       AltosRecord     previous = null;
-                       for (;;) {
-                               AltosLine       line = input_queue.take();
-                               if (line.line == null)
-                                       continue;
-                               try {
-                                       AltosRecord     telem = AltosTelemetry.parse(line.line, previous);
-                                       if (telem.serial != 0 && telem.flight != 0 &&
-                                           (telem.serial != serial || telem.flight != flight || log_file == null))
-                                       {
-                                               close_log_file();
-                                               serial = telem.serial;
-                                               flight = telem.flight;
-                                               open(telem);
-                                       }
-                                       previous = telem;
-                               } catch (ParseException pe) {
-                               } catch (AltosCRCException ce) {
-                               }
-                               if (log_file != null) {
-                                       log_file.write(line.line);
-                                       log_file.write('\n');
-                                       log_file.flush();
-                               } else
-                                       pending_queue.put(line.line);
-                       }
-               } catch (InterruptedException ie) {
-               } catch (IOException ie) {
-               }
-               close();
-       }
-
-       public AltosLog (AltosSerial s) {
-               pending_queue = new LinkedBlockingQueue<String> ();
-               input_queue = new LinkedBlockingQueue<AltosLine> ();
-               s.add_monitor(input_queue);
-               serial = -1;
-               flight = -1;
-               log_file = null;
-               log_thread = new Thread(this);
-               log_thread.start();
-       }
-}
diff --git a/altosui/AltosReplayReader.java b/altosui/AltosReplayReader.java
deleted file mode 100644 (file)
index f92c032..0000000
+++ /dev/null
@@ -1,62 +0,0 @@
-/*
- * Copyright © 2010 Keith Packard <keithp@keithp.com>
- *
- * 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 altosui;
-
-import java.awt.*;
-import java.awt.event.*;
-import javax.swing.*;
-import javax.swing.filechooser.FileNameExtensionFilter;
-import javax.swing.table.*;
-import java.io.*;
-import java.util.*;
-import java.text.*;
-import java.util.prefs.*;
-import java.util.concurrent.LinkedBlockingQueue;
-import org.altusmetrum.AltosLib.*;
-
-/*
- * Open an existing telemetry file and replay it in realtime
- */
-
-public class AltosReplayReader extends AltosFlightReader {
-       Iterator<AltosRecord>   iterator;
-       File    file;
-
-       public AltosRecord read() {
-               if (iterator.hasNext())
-                       return iterator.next();
-               return null;
-       }
-
-       public void close (boolean interrupted) {
-       }
-
-       void update(AltosState state) throws InterruptedException {
-               /* Make it run in realtime after the rocket leaves the pad */
-               if (state.state > Altos.ao_flight_pad)
-                       Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
-       }
-
-       public File backing_file() { return file; }
-
-       public AltosReplayReader(Iterator<AltosRecord> in_iterator, File in_file) {
-               iterator = in_iterator;
-               file = in_file;
-               name = file.getName();
-       }
-}
index 1be8aa26063219bbfd54d19bf2e522124ccdcfba..44eeda6dcc83ff228c21efe0415a1bf4f7de5bfa 100644 (file)
@@ -208,7 +208,7 @@ public class AltosScanUI
        }
        
        void next() throws InterruptedException, TimeoutException {
-               reader.serial.set_monitor(false);
+               reader.set_monitor(false);
                Thread.sleep(100);
                ++frequency_index;
                if (frequency_index >= frequencies.length ||
@@ -224,7 +224,7 @@ public class AltosScanUI
                }
                set_frequency();
                set_label();
-               reader.serial.set_monitor(true);
+               reader.set_monitor(true);
        }
 
 
@@ -312,7 +312,7 @@ public class AltosScanUI
                if (device == null)
                        return false;
                try {
-                       reader = new AltosTelemetryReader(device);
+                       reader = new AltosTelemetryReader(new AltosSerial(device));
                        set_frequency();
                        set_telemetry();
                        try {
index cc384586f50f1366413cfb0625979350c4d9b888..54cdcba7643b52b22524a2a175267d3a2dc43cfb 100644 (file)
@@ -396,6 +396,7 @@ public class AltosSerial extends AltosLink implements Runnable {
                device = in_device;
                frame = null;
                serial = device.getSerial();
+               name = device.toShortString();
                open();
        }
 }
diff --git a/altosui/AltosTelemetryReader.java b/altosui/AltosTelemetryReader.java
deleted file mode 100644 (file)
index dc7e4a7..0000000
+++ /dev/null
@@ -1,120 +0,0 @@
-/*
- * Copyright © 2010 Keith Packard <keithp@keithp.com>
- *
- * 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 altosui;
-
-import java.lang.*;
-import java.text.*;
-import java.io.*;
-import java.util.concurrent.*;
-import org.altusmetrum.AltosLib.*;
-
-class AltosTelemetryReader extends AltosFlightReader {
-       AltosDevice     device;
-       AltosSerial     serial;
-       AltosLog        log;
-       AltosRecord     previous;
-       double          frequency;
-       int             telemetry;
-
-       LinkedBlockingQueue<AltosLine> telem;
-
-       AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
-               AltosLine l = telem.take();
-               if (l.line == null)
-                       throw new IOException("IO error");
-               AltosRecord     next = AltosTelemetry.parse(l.line, previous);
-               previous = next;
-               return next;
-       }
-
-       void flush() {
-               telem.clear();
-       }
-
-       void close(boolean interrupted) {
-               serial.remove_monitor(telem);
-               log.close();
-               serial.close();
-       }
-
-       public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
-               frequency = in_frequency;
-               serial.set_radio_frequency(frequency);
-       }
-
-       public boolean supports_telemetry(int telemetry) {
-
-               try {
-                       /* Version 1.0 or later firmware supports all telemetry formats */
-                       if (serial.config_data().compare_version("1.0") >= 0)
-                               return true;
-
-                       /* Version 0.9 firmware only supports 0.9 telemetry */
-                       if (serial.config_data().compare_version("0.9") >= 0) {
-                               if (telemetry == Altos.ao_telemetry_0_9)
-                                       return true;
-                               else
-                                       return false;
-                       }
-
-                       /* Version 0.8 firmware only supports 0.8 telemetry */
-                       if (telemetry == Altos.ao_telemetry_0_8)
-                               return true;
-                       else
-                               return false;
-               } catch (InterruptedException ie) {
-                       return true;
-               } catch (TimeoutException te) {
-                       return true;
-               }
-       }
-
-       void save_frequency() {
-               AltosPreferences.set_frequency(device.getSerial(), frequency);
-       }
-
-       void set_telemetry(int in_telemetry) {
-               telemetry = in_telemetry;
-               serial.set_telemetry(telemetry);
-       }
-
-       void save_telemetry() {
-               AltosPreferences.set_telemetry(device.getSerial(), telemetry);
-       }
-
-       File backing_file() {
-               return log.file();
-       }
-
-       public AltosTelemetryReader (AltosDevice in_device)
-               throws FileNotFoundException, AltosSerialInUseException, IOException, InterruptedException, TimeoutException {
-               device = in_device;
-               serial = new AltosSerial(device);
-               log = new AltosLog(serial);
-               name = device.toShortString();
-               previous = null;
-
-               telem = new LinkedBlockingQueue<AltosLine>();
-               frequency = AltosPreferences.frequency(device.getSerial());
-               set_frequency(frequency);
-               telemetry = AltosPreferences.telemetry(device.getSerial());
-               set_telemetry(telemetry);
-               serial.set_callsign(AltosUIPreferences.callsign());
-               serial.add_monitor(telem);
-       }
-}
index 25c6c36b7845c374c9da499a61517b6b6e00f863..538f87348639a00195323db2e5ce987a8b777ce9 100644 (file)
@@ -48,7 +48,7 @@ public class AltosUI extends AltosFrame {
 
        void telemetry_window(AltosDevice device) {
                try {
-                       AltosFlightReader reader = new AltosTelemetryReader(device);
+                       AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
                        if (reader != null)
                                new AltosFlightUI(voice, reader, device.getSerial());
                } catch (FileNotFoundException ee) {
index 270fe1149a98342360135d06b0de584e65f1a646..2946890befbcb1f45964628a2c1769c8700406b9 100644 (file)
@@ -49,12 +49,10 @@ altosui_JAVA = \
        AltosIMU.java \
        AltosMag.java \
        AltosEepromSelect.java \
-       AltosFile.java \
        AltosFlash.java \
        AltosFlashUI.java \
        AltosFlightDisplay.java \
        AltosFlightInfoTableModel.java \
-       AltosFlightReader.java \
        AltosFlightStats.java \
        AltosFlightStatsTable.java \
        AltosFlightStatus.java \
@@ -74,12 +72,9 @@ altosui_JAVA = \
        AltosLanded.java \
        AltosLed.java \
        AltosLights.java \
-       AltosLog.java \
        AltosPad.java \
        AltosUIPreferences.java \
        AltosReader.java \
-       AltosTelemetryReader.java \
-       AltosReplayReader.java \
        AltosRomconfig.java \
        AltosRomconfigUI.java \
        AltosScanUI.java \
@@ -90,7 +85,6 @@ altosui_JAVA = \
        AltosSiteMapPreload.java \
        AltosSiteMapCache.java \
        AltosSiteMapTile.java \
-       AltosTelemetryReader.java \
        AltosUI.java \
        AltosUIListener.java \
        AltosFrame.java \
index 40ec3af8e730ff0c3c18f8f8926b8d155ca10d37..2ddd24e6099d97d0826a43207f2c871393674a85 100644 (file)
@@ -19,21 +19,26 @@ AltosLib_JAVA = \
        $(SRC)/AltosEepromLog.java \
        $(SRC)/AltosEepromRecord.java \
        $(SRC)/AltosEepromTeleScience.java \
+       $(SRC)/AltosFile.java \
+       $(SRC)/AltosFlightReader.java \
        $(SRC)/AltosFrequency.java \
        $(SRC)/AltosGPS.java \
        $(SRC)/AltosGPSSat.java \
        $(SRC)/AltosGreatCircle.java \
        $(SRC)/AltosLine.java \
        $(SRC)/AltosLink.java \
+       $(SRC)/AltosLog.java \
        $(SRC)/AltosParse.java \
        $(SRC)/AltosPreferences.java \
        $(SRC)/AltosRecordCompanion.java \
        $(SRC)/AltosRecordIterable.java \
        $(SRC)/AltosRecord.java \
+       $(SRC)/AltosReplayReader.java \
        $(SRC)/AltosState.java \
        $(SRC)/AltosTelemetry.java \
        $(SRC)/AltosTelemetryIterable.java \
        $(SRC)/AltosTelemetryMap.java \
+       $(SRC)/AltosTelemetryReader.java \
        $(SRC)/AltosTelemetryRecordCompanion.java \
        $(SRC)/AltosTelemetryRecordConfiguration.java \
        $(SRC)/AltosTelemetryRecordGeneral.java \
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java
new file mode 100644 (file)
index 0000000..d2e4f2f
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * 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.AltosLib; 
+
+import java.lang.*;
+import java.io.File;
+import java.util.*;
+
+public class AltosFile extends File {
+
+       public AltosFile(int year, int month, int day, int serial, int flight, String extension) {
+               super (AltosPreferences.logdir(),
+                      String.format("%04d-%02d-%02d-serial-%03d-flight-%03d.%s",
+                                    year, month, day, serial, flight, extension));
+       }
+
+       public AltosFile(int serial, int flight, String extension) {
+               this(Calendar.getInstance().get(Calendar.YEAR),
+                    Calendar.getInstance().get(Calendar.MONTH) + 1,
+                    Calendar.getInstance().get(Calendar.DAY_OF_MONTH),
+                    serial,
+                    flight,
+                    extension);
+       }
+
+       public AltosFile(AltosRecord telem) {
+               this(telem.serial, telem.flight, "telem");
+       }
+}
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java
new file mode 100644 (file)
index 0000000..3fdea46
--- /dev/null
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * 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.AltosLib;
+
+import java.lang.*;
+import java.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+
+public class AltosFlightReader {
+       public String name;
+
+       public int serial;
+
+       public void init() { }
+
+       public AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException { return null; }
+
+       public void close(boolean interrupted) { }
+
+       public void set_frequency(double frequency) throws InterruptedException, TimeoutException { }
+
+       public void save_frequency() { }
+
+       public void set_telemetry(int telemetry) { }
+
+       public void save_telemetry() { }
+
+       public void update(AltosState state) throws InterruptedException { }
+
+       public boolean supports_telemetry(int telemetry) { return false; }
+
+       public File backing_file() { return null; }
+}
index 49585975f649acab42af33317d1b0618b6fc2db4..9b80e9166f473241855eaa28cbf56e0d373f4089 100644 (file)
@@ -24,6 +24,8 @@ import java.util.*;
 import java.text.*;
 
 public abstract class AltosLink {
+       public abstract void print(String data);
+       public abstract void close();
 
        public static boolean debug = false;
        public static void set_debug(boolean in_debug) { debug = in_debug; }
@@ -43,8 +45,6 @@ public abstract class AltosLink {
                        set_monitor(false);
        }
 
-       public abstract void print(String data);
-
        public void printf(String format, Object ... arguments) {
                String  line = String.format(format, arguments);
                if (debug)
@@ -207,6 +207,7 @@ public abstract class AltosLink {
 
        public boolean remote;
        public int serial;
+       public String name;
 
        public void start_remote() throws TimeoutException, InterruptedException {
                if (debug)
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java
new file mode 100644 (file)
index 0000000..08c45ca
--- /dev/null
@@ -0,0 +1,126 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * 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.AltosLib;
+
+import java.io.*;
+import java.lang.*;
+import java.util.*;
+import java.text.ParseException;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/*
+ * This creates a thread to capture telemetry data and write it to
+ * a log file
+ */
+class AltosLog implements Runnable {
+
+       LinkedBlockingQueue<AltosLine>  input_queue;
+       LinkedBlockingQueue<String>     pending_queue;
+       int                             serial;
+       int                             flight;
+       FileWriter                      log_file;
+       Thread                          log_thread;
+       AltosFile                       file;
+
+       private void close_log_file() {
+               if (log_file != null) {
+                       try {
+                               log_file.close();
+                       } catch (IOException io) {
+                       }
+                       log_file = null;
+               }
+       }
+
+       void close() {
+               close_log_file();
+               if (log_thread != null) {
+                       log_thread.interrupt();
+                       log_thread = null;
+               }
+       }
+
+       File file() {
+               return file;
+       }
+
+       boolean open (AltosRecord telem) throws IOException {
+               AltosFile       a = new AltosFile(telem);
+
+               System.out.printf("open %s\n", a.toString());
+               log_file = new FileWriter(a, true);
+               if (log_file != null) {
+                       while (!pending_queue.isEmpty()) {
+                               try {
+                                       String s = pending_queue.take();
+                                       log_file.write(s);
+                                       log_file.write('\n');
+                               } catch (InterruptedException ie) {
+                               }
+                       }
+                       log_file.flush();
+                       file = a;
+               }
+               return log_file != null;
+       }
+
+       public void run () {
+               try {
+                       AltosRecord     previous = null;
+                       for (;;) {
+                               AltosLine       line = input_queue.take();
+                               if (line.line == null)
+                                       continue;
+                               try {
+                                       AltosRecord     telem = AltosTelemetry.parse(line.line, previous);
+                                       if (telem.serial != 0 && telem.flight != 0 &&
+                                           (telem.serial != serial || telem.flight != flight || log_file == null))
+                                       {
+                                               close_log_file();
+                                               serial = telem.serial;
+                                               flight = telem.flight;
+                                               open(telem);
+                                       }
+                                       previous = telem;
+                               } catch (ParseException pe) {
+                               } catch (AltosCRCException ce) {
+                               }
+                               if (log_file != null) {
+                                       log_file.write(line.line);
+                                       log_file.write('\n');
+                                       log_file.flush();
+                               } else
+                                       pending_queue.put(line.line);
+                       }
+               } catch (InterruptedException ie) {
+               } catch (IOException ie) {
+               }
+               close();
+       }
+
+       public AltosLog (AltosLink link) {
+               pending_queue = new LinkedBlockingQueue<String> ();
+               input_queue = new LinkedBlockingQueue<AltosLine> ();
+               link.add_monitor(input_queue);
+               serial = -1;
+               flight = -1;
+               log_file = null;
+               log_thread = new Thread(this);
+               log_thread.start();
+       }
+}
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java
new file mode 100644 (file)
index 0000000..1585f9e
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * 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.AltosLib;
+
+import java.io.*;
+import java.util.*;
+import java.text.*;
+import java.util.prefs.*;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/*
+ * Open an existing telemetry file and replay it in realtime
+ */
+
+public class AltosReplayReader extends AltosFlightReader {
+       Iterator<AltosRecord>   iterator;
+       File    file;
+
+       public AltosRecord read() {
+               if (iterator.hasNext())
+                       return iterator.next();
+               return null;
+       }
+
+       public void close (boolean interrupted) {
+       }
+
+       public void update(AltosState state) throws InterruptedException {
+               /* Make it run in realtime after the rocket leaves the pad */
+               if (state.state > AltosLib.ao_flight_pad)
+                       Thread.sleep((int) (Math.min(state.time_change,10) * 1000));
+       }
+
+       public File backing_file() { return file; }
+
+       public AltosReplayReader(Iterator<AltosRecord> in_iterator, File in_file) {
+               iterator = in_iterator;
+               file = in_file;
+               name = file.getName();
+       }
+}
diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java
new file mode 100644 (file)
index 0000000..2cc2822
--- /dev/null
@@ -0,0 +1,118 @@
+/*
+ * Copyright © 2010 Keith Packard <keithp@keithp.com>
+ *
+ * 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.AltosLib;
+
+import java.lang.*;
+import java.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+
+public class AltosTelemetryReader extends AltosFlightReader {
+       AltosLink       link;
+       AltosLog        log;
+       AltosRecord     previous;
+       double          frequency;
+       int             telemetry;
+
+       LinkedBlockingQueue<AltosLine> telem;
+
+       public AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
+               AltosLine l = telem.take();
+               if (l.line == null)
+                       throw new IOException("IO error");
+               AltosRecord     next = AltosTelemetry.parse(l.line, previous);
+               previous = next;
+               return next;
+       }
+
+       public void flush() {
+               telem.clear();
+       }
+
+       public void close(boolean interrupted) {
+               link.remove_monitor(telem);
+               log.close();
+               link.close();
+       }
+
+       public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
+               frequency = in_frequency;
+               link.set_radio_frequency(frequency);
+       }
+
+       public boolean supports_telemetry(int telemetry) {
+
+               try {
+                       /* Version 1.0 or later firmware supports all telemetry formats */
+                       if (serial.config_data().compare_version("1.0") >= 0)
+                               return true;
+
+                       /* Version 0.9 firmware only supports 0.9 telemetry */
+                       if (serial.config_data().compare_version("0.9") >= 0) {
+                               if (telemetry == Altos.ao_telemetry_0_9)
+                                       return true;
+                               else
+                                       return false;
+                       }
+
+                       /* Version 0.8 firmware only supports 0.8 telemetry */
+                       if (telemetry == Altos.ao_telemetry_0_8)
+                               return true;
+                       else
+                               return false;
+               } catch (InterruptedException ie) {
+                       return true;
+               } catch (TimeoutException te) {
+                       return true;
+               }
+       }
+
+       public void save_frequency() {
+               AltosPreferences.set_frequency(link.serial, frequency);
+       }
+
+       public void set_telemetry(int in_telemetry) {
+               telemetry = in_telemetry;
+               link.set_telemetry(telemetry);
+       }
+
+       public void save_telemetry() {
+               AltosPreferences.set_telemetry(link.serial, telemetry);
+       }
+
+       public void set_monitor(boolean monitor) {
+               link.set_monitor(monitor);
+       }
+
+       public File backing_file() {
+               return log.file();
+       }
+
+       public AltosTelemetryReader (AltosLink in_link)
+               throws IOException, InterruptedException, TimeoutException {
+               log = new AltosLog(link);
+               name = link.name;
+               previous = null;
+               telem = new LinkedBlockingQueue<AltosLine>();
+               frequency = AltosPreferences.frequency(link.serial);
+               set_frequency(frequency);
+               telemetry = AltosPreferences.telemetry(link.serial);
+               set_telemetry(telemetry);
+               link.add_monitor(telem);
+       }
+}