altoslib: Mark listener as 'not running' on EOF.
authorKeith Packard <keithp@keithp.com>
Sat, 7 Feb 2015 22:43:11 +0000 (14:43 -0800)
committerKeith Packard <keithp@keithp.com>
Sat, 7 Feb 2015 22:43:11 +0000 (14:43 -0800)
This adds a 'running' member to the AltosListenerState class, and when
the replay reader reaches EOF, marks the listener as no longer
running.

AltosUI and TeleGPS now display 'done' in the 'Age' field when this
occurs, to let the user know that the replay is over.

Also make sure that the display timers are stopped when this happens,
or when the window is closed.

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosListenerState.java
altosui/AltosFlightStatus.java
altosui/AltosFlightUI.java
altosuilib/AltosDisplayThread.java
telegps/TeleGPS.java
telegps/TeleGPSDisplayThread.java
telegps/TeleGPSStatus.java

index ed1f0f192ef53f1429fc8f7f7d1d268275d6f70d..bba2183057083afb9f6ed4e5bf03daffdb22cdc5 100644 (file)
@@ -22,9 +22,11 @@ import java.io.*;
 public class AltosListenerState implements Serializable {
        public int      crc_errors;
        public double   battery;
+       public boolean  running;
 
        public AltosListenerState() {
                crc_errors = 0;
                battery = AltosLib.MISSING;
+               running = true;
        }
 }
index 7e7efa64d91a96621b7256c6905e6c676eb00085..a847d884067147339a7fa47438a151837809dd9d 100644 (file)
@@ -229,10 +229,14 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay
                long    last_secs = -1;
 
                void show(AltosState state, AltosListenerState listener_state) {
-                       long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
-                       if (secs != last_secs) {
-                               value.setText(String.format("%d", secs));
-                               last_secs = secs;
+                       if (listener_state.running) {
+                               long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
+                               if (secs != last_secs) {
+                                       value.setText(String.format("%d", secs));
+                                       last_secs = secs;
+                               }
+                       } else {
+                               value.setText("done");
                        }
                }
 
@@ -276,6 +280,8 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay
                flight_state.show(state, listener_state);
                rssi.show(state, listener_state);
                last_packet.show(state, listener_state);
+               if (!listener_state.running)
+                       stop();
        }
 
        public int height() {
@@ -285,6 +291,22 @@ public class AltosFlightStatus extends JComponent implements AltosFlightDisplay
 
        public String getName() { return "Flight Status"; }
 
+       AltosFlightStatusUpdate status_update;
+       javax.swing.Timer       timer;
+
+       public void start(AltosFlightStatusUpdate status_update) {
+               this.status_update = status_update;
+               timer = new javax.swing.Timer(100, status_update);
+               timer.start();
+       }
+
+       public void stop() {
+               if (timer != null) {
+                       timer.stop();
+                       timer = null;
+               }
+       }
+
        public AltosFlightStatus() {
                layout = new GridBagLayout();
 
index 6af345ea909068c09fdf1cb887badd0e7532b540..d7c8223ef88cc1485f9820f98e2a928106d2cf96 100644 (file)
@@ -97,6 +97,7 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
 
        public void show(AltosState state, AltosListenerState listener_state) {
                status_update.saved_state = state;
+               status_update.saved_listener_state = listener_state;
 
                if (state == null)
                        state = new AltosState();
@@ -335,9 +336,14 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
                AltosUIPreferences.register_font_listener(this);
                AltosPreferences.register_units_listener(this);
 
+               status_update = new AltosFlightStatusUpdate(flightStatus);
+
+               flightStatus.start(status_update);
+
                addWindowListener(new WindowAdapter() {
                                @Override
                                public void windowClosing(WindowEvent e) {
+                                       flightStatus.stop();
                                        disconnect();
                                        setVisible(false);
                                        dispose();
@@ -353,10 +359,6 @@ public class AltosFlightUI extends AltosUIFrame implements AltosFlightDisplay {
 
                thread = new AltosDisplayThread(this, voice, this, reader);
 
-               status_update = new AltosFlightStatusUpdate(flightStatus);
-
-               new javax.swing.Timer(100, status_update).start();
-
                thread.start();
        }
 
index 6b6e03e7372baa86c778f52e7946ee54ef914dca..627088bcddfc7c6ee6aa598362628979a9765877 100644 (file)
@@ -224,8 +224,10 @@ public class AltosDisplayThread extends Thread {
                        for (;;) {
                                try {
                                        state = reader.read();
-                                       if (state == null)
+                                       if (state == null) {
+                                               listener_state.running = false;
                                                break;
+                                       }
                                        reader.update(state);
                                        show_safely();
                                        told = tell();
index a4b221e814b5444df1630fb46cd82fb15273b580..41f881726af92761a5d3bb477ad4481114955a9f 100644 (file)
@@ -67,7 +67,6 @@ public class TeleGPS
 
        TeleGPSStatus           telegps_status;
        TeleGPSStatusUpdate     status_update;
-       javax.swing.Timer       status_timer;
 
        JTabbedPane             pane;
 
@@ -147,6 +146,7 @@ public class TeleGPS
        public void show(AltosState state, AltosListenerState listener_state) {
                try {
                        status_update.saved_state = state;
+                       status_update.saved_listener_state = listener_state;
 
                        if (state == null)
                                state = new AltosState();
@@ -178,11 +178,7 @@ public class TeleGPS
        void disconnect() {
                setTitle("TeleGPS");
                stop_display();
-               if (status_timer != null) {
-                       status_timer.stop();
-                       status_timer = null;
-                       status_update = null;
-               }
+               telegps_status.stop();
 
                telegps_status.disable_receive();
                disable_frequency_menu();
@@ -404,8 +400,7 @@ public class TeleGPS
        public void set_reader(AltosFlightReader reader, AltosDevice device) {
                status_update = new TeleGPSStatusUpdate(telegps_status);
 
-               status_timer = new javax.swing.Timer(100, status_update);
-               status_timer.start();
+               telegps_status.start(status_update);
 
                setTitle(String.format("TeleGPS %s", reader.name));
                thread = new TeleGPSDisplayThread(this, voice(), this, reader);
index 18b8d9fcd7fe29d0bd086756da27b8277286b07c..6305bf2f6929a61372b02800e2ec078c63a6c666 100644 (file)
@@ -168,8 +168,10 @@ public class TeleGPSDisplayThread extends Thread {
                        for (;;) {
                                try {
                                        state = reader.read();
-                                       if (state == null)
+                                       if (state == null) {
+                                               listener_state.running = false;
                                                break;
+                                       }
                                        reader.update(state);
                                        show_safely();
                                        told = tell();
index 1d4415d6064d400444891ea769c3a0643a3ac0ea..1eeb7ed5de718e72a50aee8696c411c44e03a568 100644 (file)
@@ -179,11 +179,15 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay {
                long    last_secs = -1;
 
                void show(AltosState state, AltosListenerState listener_state) {
-                       long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
-
-                       if (secs != last_secs) {
-                               value.setText(String.format("%d", secs));
-                               last_secs = secs;
+                       if (listener_state.running) {
+                               long secs = (System.currentTimeMillis() - state.received_time + 500) / 1000;
+
+                               if (secs != last_secs) {
+                                       value.setText(String.format("%d", secs));
+                                       last_secs = secs;
+                               }
+                       } else {
+                               value.setText("done");
                        }
                }
 
@@ -232,6 +236,8 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay {
                flight.show(state, listener_state);
                rssi.show(state, listener_state);
                last_packet.show(state, listener_state);
+               if (!listener_state.running)
+                       stop();
        }
 
        public int height() {
@@ -239,6 +245,22 @@ public class TeleGPSStatus extends JComponent implements AltosFlightDisplay {
                return d.height;
        }
 
+       TeleGPSStatusUpdate     status_update;
+       javax.swing.Timer       timer;
+
+       public void start(TeleGPSStatusUpdate status_update) {
+               this.status_update = status_update;
+               timer = new javax.swing.Timer(100, status_update);
+               timer.start();
+       }
+
+       public void stop() {
+               if (timer != null) {
+                       timer.stop();
+                       timer = null;
+               }
+       }
+
        public TeleGPSStatus() {
                layout = new GridBagLayout();