altosdroid: initial implementation of telemetry logging.
[fw/altos] / altosui / AltosDisplayThread.java
index 3e719130a6a7c67475f41d98a8740b9904e66e01..f7a1d03eb2ad7b1bfa0f9e8f6d6bc667eef23ddc 100644 (file)
 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.*;
 
 public class AltosDisplayThread extends Thread {
 
        Frame                   parent;
        IdleThread              idle_thread;
        AltosVoice              voice;
-       String                  name;
        AltosFlightReader       reader;
        int                     crc_errors;
        AltosFlightDisplay      display;
 
-       synchronized void show(AltosState state, int crc_errors) {
+       void show_internal(AltosState state, int crc_errors) {
                if (state != null)
                        display.show(state, crc_errors);
        }
 
+       void show_safely(AltosState in_state, int in_crc_errors) {
+               final AltosState state = in_state;
+               final int crc_errors = in_crc_errors;
+               Runnable r = new Runnable() {
+                               public void run() {
+                                       try {
+                                               show_internal(state, crc_errors);
+                                       } catch (Exception ex) {
+                                       }
+                               }
+                       };
+               SwingUtilities.invokeLater(r);
+       }
+
+       void reading_error_internal() {
+               JOptionPane.showMessageDialog(parent,
+                                             String.format("Error reading from \"%s\"", reader.name),
+                                             "Telemetry Read Error",
+                                             JOptionPane.ERROR_MESSAGE);
+       }
+
+       void reading_error_safely() {
+               Runnable r = new Runnable() {
+                               public void run() {
+                                       try {
+                                               reading_error_internal();
+                                       } catch (Exception ex) {
+                                       }
+                               }
+                       };
+               SwingUtilities.invokeLater(r);
+       }
+
        class IdleThread extends Thread {
 
                boolean started;
@@ -69,15 +96,15 @@ public class AltosDisplayThread extends Thread {
                            state.state < Altos.ao_flight_landed &&
                            state.range >= 0)
                        {
-                               voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
-                                           (int) (state.height + 0.5),
-                        state.from_pad.bearing_words(
-                            AltosGreatCircle.BEARING_VOICE),
+                               voice.speak("Height %s, bearing %s %d, elevation %d, range %s.\n",
+                                           AltosConvert.height.say(state.height),
+                                           state.from_pad.bearing_words(
+                                                   AltosGreatCircle.BEARING_VOICE),
                                            (int) (state.from_pad.bearing + 0.5),
                                            (int) (state.elevation + 0.5),
-                                           (int) (state.range + 0.5));
+                                           AltosConvert.distance.say(state.range));
                        } else if (state.state > Altos.ao_flight_pad) {
-                               voice.speak("%d meters", (int) (state.height + 0.5));
+                               voice.speak(AltosConvert.height.say_units(state.height));
                        } else {
                                reported_landing = 0;
                        }
@@ -96,13 +123,13 @@ public class AltosDisplayThread extends Thread {
                                else
                                        voice.speak("rocket may have crashed");
                                if (state.from_pad != null)
-                                       voice.speak("Bearing %d degrees, range %d meters.",
+                                       voice.speak("Bearing %d degrees, range %s.",
                                                    (int) (state.from_pad.bearing + 0.5),
-                                                   (int) (state.from_pad.distance + 0.5));
+                                                   AltosConvert.distance.say_units(state.from_pad.distance));
                                ++reported_landing;
                                if (state.state != Altos.ao_flight_landed) {
                                        state.state = Altos.ao_flight_landed;
-                                       show(state, 0);
+                                       show_safely(state, 0);
                                }
                        }
                }
@@ -169,13 +196,13 @@ public class AltosDisplayThread extends Thread {
                        voice.speak(state.data.state());
                        if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
                            state.state > Altos.ao_flight_boost) {
-                               voice.speak("max speed: %d meters per second.",
-                                           (int) (state.max_speed + 0.5));
+                               voice.speak("max speed: %s.",
+                                           AltosConvert.speed.say_units(state.max_speed + 0.5));
                                ret = true;
                        } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
                                   state.state >= Altos.ao_flight_drogue) {
-                               voice.speak("max height: %d meters.",
-                                           (int) (state.max_height + 0.5));
+                               voice.speak("max height: %s.",
+                                           AltosConvert.height.say_units(state.max_height + 0.5));
                                ret = true;
                        }
                }
@@ -195,14 +222,13 @@ public class AltosDisplayThread extends Thread {
 
        public void run() {
                boolean         interrupted = false;
-               String          line;
+               //String                line;
                AltosState      state = null;
                AltosState      old_state = null;
                boolean         told;
 
                idle_thread = new IdleThread();
 
-               display.reset();
                try {
                        for (;;) {
                                try {
@@ -212,23 +238,20 @@ public class AltosDisplayThread extends Thread {
                                        old_state = state;
                                        state = new AltosState(record, state);
                                        reader.update(state);
-                                       show(state, crc_errors);
+                                       show_safely(state, crc_errors);
                                        told = tell(state, old_state);
                                        idle_thread.notice(state, told);
                                } catch (ParseException pp) {
                                        System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
                                } catch (AltosCRCException ce) {
                                        ++crc_errors;
-                                       show(state, crc_errors);
+                                       show_safely(state, crc_errors);
                                }
                        }
                } catch (InterruptedException ee) {
                        interrupted = true;
                } catch (IOException ie) {
-                       JOptionPane.showMessageDialog(parent,
-                                                     String.format("Error reading from \"%s\"", name),
-                                                     "Telemetry Read Error",
-                                                     JOptionPane.ERROR_MESSAGE);
+                       reading_error_safely();
                } finally {
                        if (!interrupted)
                                idle_thread.report(true);
@@ -245,5 +268,6 @@ public class AltosDisplayThread extends Thread {
                voice = in_voice;
                display = in_display;
                reader = in_reader;
+               display.reset();
        }
 }