From: Keith Packard Date: Tue, 3 Jan 2012 06:35:41 +0000 (-0800) Subject: altosui: Move telemetry reader &c to altoslib X-Git-Tag: 1.0.9.6~110 X-Git-Url: https://git.gag.com/?p=fw%2Faltos;a=commitdiff_plain;h=5270a0f1416baef5fde08547c6c98d97f973ae95 altosui: Move telemetry reader &c to altoslib Move all of the device and file reading code into altoslib Signed-off-by: Keith Packard --- diff --git a/altosui/AltosFile.java b/altosui/AltosFile.java deleted file mode 100644 index 4cf7de3c..00000000 --- a/altosui/AltosFile.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright © 2010 Keith Packard - * - * 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 index 1ac9f848..00000000 --- a/altosui/AltosFlightReader.java +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright © 2010 Keith Packard - * - * 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 index 740f0be6..00000000 --- a/altosui/AltosLog.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright © 2010 Keith Packard - * - * 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 input_queue; - LinkedBlockingQueue 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 (); - input_queue = new LinkedBlockingQueue (); - 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 index f92c0328..00000000 --- a/altosui/AltosReplayReader.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright © 2010 Keith Packard - * - * 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 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 in_iterator, File in_file) { - iterator = in_iterator; - file = in_file; - name = file.getName(); - } -} diff --git a/altosui/AltosScanUI.java b/altosui/AltosScanUI.java index 1be8aa26..44eeda6d 100644 --- a/altosui/AltosScanUI.java +++ b/altosui/AltosScanUI.java @@ -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 { diff --git a/altosui/AltosSerial.java b/altosui/AltosSerial.java index cc384586..54cdcba7 100644 --- a/altosui/AltosSerial.java +++ b/altosui/AltosSerial.java @@ -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 index dc7e4a75..00000000 --- a/altosui/AltosTelemetryReader.java +++ /dev/null @@ -1,120 +0,0 @@ -/* - * Copyright © 2010 Keith Packard - * - * 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 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(); - 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); - } -} diff --git a/altosui/AltosUI.java b/altosui/AltosUI.java index 25c6c36b..538f8734 100644 --- a/altosui/AltosUI.java +++ b/altosui/AltosUI.java @@ -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) { diff --git a/altosui/Makefile.am b/altosui/Makefile.am index 270fe114..2946890b 100644 --- a/altosui/Makefile.am +++ b/altosui/Makefile.am @@ -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 \ diff --git a/altosui/altoslib/Makefile.am b/altosui/altoslib/Makefile.am index 40ec3af8..2ddd24e6 100644 --- a/altosui/altoslib/Makefile.am +++ b/altosui/altoslib/Makefile.am @@ -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 index 00000000..d2e4f2f7 --- /dev/null +++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFile.java @@ -0,0 +1,44 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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 index 00000000..3fdea469 --- /dev/null +++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosFlightReader.java @@ -0,0 +1,49 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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; } +} diff --git a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java index 49585975..9b80e916 100644 --- a/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java +++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLink.java @@ -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 index 00000000..08c45ca8 --- /dev/null +++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosLog.java @@ -0,0 +1,126 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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 input_queue; + LinkedBlockingQueue 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 (); + input_queue = new LinkedBlockingQueue (); + 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 index 00000000..1585f9eb --- /dev/null +++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosReplayReader.java @@ -0,0 +1,56 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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 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 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 index 00000000..2cc2822a --- /dev/null +++ b/altosui/altoslib/src/org/altusmetrum/AltosLib/AltosTelemetryReader.java @@ -0,0 +1,118 @@ +/* + * Copyright © 2010 Keith Packard + * + * 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 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(); + frequency = AltosPreferences.frequency(link.serial); + set_frequency(frequency); + telemetry = AltosPreferences.telemetry(link.serial); + set_telemetry(telemetry); + link.add_monitor(telem); + } +}