diff options
Diffstat (limited to 'altoslib')
-rw-r--r-- | altoslib/AltosFlightReader.java | 8 | ||||
-rw-r--r-- | altoslib/AltosLog.java | 1 | ||||
-rw-r--r-- | altoslib/AltosPreferences.java | 27 | ||||
-rw-r--r-- | altoslib/AltosReplayReader.java | 11 | ||||
-rw-r--r-- | altoslib/AltosStateIterable.java | 14 | ||||
-rw-r--r-- | altoslib/AltosTelemetryReader.java | 31 |
6 files changed, 85 insertions, 7 deletions
diff --git a/altoslib/AltosFlightReader.java b/altoslib/AltosFlightReader.java index c0565e88..be103838 100644 --- a/altoslib/AltosFlightReader.java +++ b/altoslib/AltosFlightReader.java @@ -21,16 +21,16 @@ import java.text.*; import java.io.*; import java.util.concurrent.*; -public class AltosFlightReader { +public abstract class AltosFlightReader { public String name; public int serial; - public void init() { } + public void init() {} - public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException { return null; } + public abstract AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException; - public void close(boolean interrupted) { } + public abstract void close(boolean interrupted); public void set_frequency(double frequency) throws InterruptedException, TimeoutException { } diff --git a/altoslib/AltosLog.java b/altoslib/AltosLog.java index 1cac6b52..28116e3d 100644 --- a/altoslib/AltosLog.java +++ b/altoslib/AltosLog.java @@ -72,6 +72,7 @@ public class AltosLog implements Runnable { } log_file.flush(); file = a; + AltosPreferences.set_logfile(link.serial, file); } return log_file != null; } diff --git a/altoslib/AltosPreferences.java b/altoslib/AltosPreferences.java index 72cfeb4b..0e91e4f4 100644 --- a/altoslib/AltosPreferences.java +++ b/altoslib/AltosPreferences.java @@ -38,6 +38,9 @@ public class AltosPreferences { /* telemetry rate format preference name */ public final static String telemetryRatePreferenceFormat = "RATE-%d"; + /* log file format preference name */ + public final static String logfilePreferenceFormat = "LOGFILE-%d"; + /* voice preference name */ public final static String voicePreference = "VOICE"; @@ -83,6 +86,9 @@ public class AltosPreferences { /* Telemetry rate (map serial to telemetry format) */ public static Hashtable<Integer, Integer> telemetry_rates; + /* Log file (map serial to logfile name) */ + public static Hashtable<Integer, File> logfiles; + /* Voice preference */ public static boolean voice; @@ -300,6 +306,27 @@ public class AltosPreferences { } } + public static void set_logfile(int serial, File new_logfile) { + synchronized(backend) { + logfiles.put(serial, new_logfile); + backend.putString(String.format(logfilePreferenceFormat, serial), new_logfile.getPath()); + flush_preferences(); + } + } + + public static File logfile(int serial) { + synchronized(backend) { + if (logfiles.containsKey(serial)) + return logfiles.get(serial); + String logfile_string = backend.getString(String.format(logfilePreferenceFormat, serial), null); + if (logfile_string == null) + return null; + File logfile = new File(logfile_string); + logfiles.put(serial, logfile); + return logfile; + } + } + public static void set_scanning_telemetry(int new_scanning_telemetry) { synchronized (backend) { scanning_telemetry = new_scanning_telemetry; diff --git a/altoslib/AltosReplayReader.java b/altoslib/AltosReplayReader.java index 15093af1..2864e02a 100644 --- a/altoslib/AltosReplayReader.java +++ b/altoslib/AltosReplayReader.java @@ -27,6 +27,7 @@ import java.util.*; public class AltosReplayReader extends AltosFlightReader { Iterator<AltosState> iterator; File file; + boolean real_time; public AltosState read() { if (iterator.hasNext()) @@ -39,16 +40,22 @@ public class AltosReplayReader extends AltosFlightReader { 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 && state.time_change > 0) + if (real_time && state.state > AltosLib.ao_flight_pad && state.time_change > 0) Thread.sleep((int) (Math.min(state.time_change,10) * 1000)); state.set_received_time(System.currentTimeMillis()); } public File backing_file() { return file; } - public AltosReplayReader(Iterator<AltosState> in_iterator, File in_file) { + public AltosReplayReader(Iterator<AltosState> in_iterator, File in_file, + boolean in_real_time) { iterator = in_iterator; file = in_file; + real_time = in_real_time; name = file.getName(); } + + public AltosReplayReader(Iterator<AltosState> in_iterator, File in_file) { + this(in_iterator, in_file, false); + } } diff --git a/altoslib/AltosStateIterable.java b/altoslib/AltosStateIterable.java index f7cd424d..4154b71c 100644 --- a/altoslib/AltosStateIterable.java +++ b/altoslib/AltosStateIterable.java @@ -26,4 +26,18 @@ public abstract class AltosStateIterable implements Iterable<AltosState> { } public abstract void write(PrintStream out); + + public static AltosStateIterable iterable(File file) { + FileInputStream in; + try { + in = new FileInputStream(file); + } catch (Exception e) { + System.out.printf("Failed to open file '%s'\n", file); + return null; + } + if (file.getName().endsWith("telem")) + return new AltosTelemetryFile(in); + else + return new AltosEepromFile(in); + } } diff --git a/altoslib/AltosTelemetryReader.java b/altoslib/AltosTelemetryReader.java index 5ed50134..8803e19f 100644 --- a/altoslib/AltosTelemetryReader.java +++ b/altoslib/AltosTelemetryReader.java @@ -28,10 +28,17 @@ public class AltosTelemetryReader extends AltosFlightReader { int telemetry; int telemetry_rate; AltosState state = null; + AltosFlightReader stacked; LinkedBlockingQueue<AltosLine> telem; public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException { + if (stacked != null) { + state = stacked.read(); + if (state != null) + return state; + stacked = null; + } AltosLine l = telem.take(); if (l.line == null) throw new IOException("IO error"); @@ -53,6 +60,12 @@ public class AltosTelemetryReader extends AltosFlightReader { } public void close(boolean interrupted) { + + if (stacked != null) { + stacked.close(interrupted); + stacked = null; + } + link.remove_monitor(telem); log.close(); try { @@ -148,9 +161,10 @@ public class AltosTelemetryReader extends AltosFlightReader { return link.monitor_battery(); } - public AltosTelemetryReader (AltosLink in_link) + public AltosTelemetryReader (AltosLink in_link, AltosFlightReader in_stacked) throws IOException, InterruptedException, TimeoutException { link = in_link; + stacked = in_stacked; boolean success = false; try { log = new AltosLog(link); @@ -169,4 +183,19 @@ public class AltosTelemetryReader extends AltosFlightReader { close(true); } } + + private static AltosFlightReader existing_data(AltosLink link) { + 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 AltosTelemetryReader(AltosLink link) + throws IOException, InterruptedException, TimeoutException { + this(link, existing_data(link)); + } } |