altosui: Display reader name (usually the device) when an I/O error occurs
[fw/altos] / altosui / AltosDisplayThread.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.LinkedBlockingQueue;
30
31 public class AltosDisplayThread extends Thread {
32
33         Frame                   parent;
34         IdleThread              idle_thread;
35         AltosVoice              voice;
36         AltosFlightReader       reader;
37         int                     crc_errors;
38         AltosFlightDisplay      display;
39
40         void show_internal(AltosState state, int crc_errors) {
41                 if (state != null)
42                         display.show(state, crc_errors);
43         }
44
45         void show_safely(AltosState in_state, int in_crc_errors) {
46                 final AltosState state = in_state;
47                 final int crc_errors = in_crc_errors;
48                 Runnable r = new Runnable() {
49                                 public void run() {
50                                         try {
51                                                 show_internal(state, crc_errors);
52                                         } catch (Exception ex) {
53                                         }
54                                 }
55                         };
56                 SwingUtilities.invokeLater(r);
57         }
58
59         void reading_error_internal() {
60                 JOptionPane.showMessageDialog(parent,
61                                               String.format("Error reading from \"%s\"", reader.name),
62                                               "Telemetry Read Error",
63                                               JOptionPane.ERROR_MESSAGE);
64         }
65
66         void reading_error_safely() {
67                 Runnable r = new Runnable() {
68                                 public void run() {
69                                         try {
70                                                 reading_error_internal();
71                                         } catch (Exception ex) {
72                                         }
73                                 }
74                         };
75                 SwingUtilities.invokeLater(r);
76         }
77
78         class IdleThread extends Thread {
79
80                 boolean started;
81                 private AltosState state;
82                 int     reported_landing;
83                 int     report_interval;
84                 long    report_time;
85
86                 public synchronized void report(boolean last) {
87                         if (state == null)
88                                 return;
89
90                         /* reset the landing count once we hear about a new flight */
91                         if (state.state < Altos.ao_flight_drogue)
92                                 reported_landing = 0;
93
94                         /* Shut up once the rocket is on the ground */
95                         if (reported_landing > 2) {
96                                 return;
97                         }
98
99                         /* If the rocket isn't on the pad, then report height */
100                         if (Altos.ao_flight_drogue <= state.state &&
101                             state.state < Altos.ao_flight_landed &&
102                             state.range >= 0)
103                         {
104                                 voice.speak("Height %d, bearing %s %d, elevation %d, range %d.\n",
105                                             (int) (state.height + 0.5),
106                         state.from_pad.bearing_words(
107                             AltosGreatCircle.BEARING_VOICE),
108                                             (int) (state.from_pad.bearing + 0.5),
109                                             (int) (state.elevation + 0.5),
110                                             (int) (state.range + 0.5));
111                         } else if (state.state > Altos.ao_flight_pad) {
112                                 voice.speak("%d meters", (int) (state.height + 0.5));
113                         } else {
114                                 reported_landing = 0;
115                         }
116
117                         /* If the rocket is coming down, check to see if it has landed;
118                          * either we've got a landed report or we haven't heard from it in
119                          * a long time
120                          */
121                         if (state.state >= Altos.ao_flight_drogue &&
122                             (last ||
123                              System.currentTimeMillis() - state.report_time >= 15000 ||
124                              state.state == Altos.ao_flight_landed))
125                         {
126                                 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
127                                         voice.speak("rocket landed safely");
128                                 else
129                                         voice.speak("rocket may have crashed");
130                                 if (state.from_pad != null)
131                                         voice.speak("Bearing %d degrees, range %d meters.",
132                                                     (int) (state.from_pad.bearing + 0.5),
133                                                     (int) (state.from_pad.distance + 0.5));
134                                 ++reported_landing;
135                                 if (state.state != Altos.ao_flight_landed) {
136                                         state.state = Altos.ao_flight_landed;
137                                         show_safely(state, 0);
138                                 }
139                         }
140                 }
141
142                 long now () {
143                         return System.currentTimeMillis();
144                 }
145
146                 void set_report_time() {
147                         report_time = now() + report_interval;
148                 }
149
150                 public void run () {
151                         try {
152                                 for (;;) {
153                                         set_report_time();
154                                         for (;;) {
155                                                 voice.drain();
156                                                 synchronized (this) {
157                                                         long    sleep_time = report_time - now();
158                                                         if (sleep_time <= 0)
159                                                                 break;
160                                                         wait(sleep_time);
161                                                 }
162                                         }
163                                         report(false);
164                                 }
165                         } catch (InterruptedException ie) {
166                                 try {
167                                         voice.drain();
168                                 } catch (InterruptedException iie) { }
169                         }
170                 }
171
172                 public synchronized void notice(AltosState new_state, boolean spoken) {
173                         AltosState old_state = state;
174                         state = new_state;
175                         if (!started && state.state > Altos.ao_flight_pad) {
176                                 started = true;
177                                 start();
178                         }
179
180                         if (state.state < Altos.ao_flight_drogue)
181                                 report_interval = 10000;
182                         else
183                                 report_interval = 20000;
184                         if (old_state != null && old_state.state != state.state) {
185                                 report_time = now();
186                                 this.notify();
187                         } else if (spoken)
188                                 set_report_time();
189                 }
190
191                 public IdleThread() {
192                         state = null;
193                         reported_landing = 0;
194                         report_interval = 10000;
195                 }
196         }
197
198         boolean tell(AltosState state, AltosState old_state) {
199                 boolean ret = false;
200                 if (old_state == null || old_state.state != state.state) {
201                         voice.speak(state.data.state());
202                         if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
203                             state.state > Altos.ao_flight_boost) {
204                                 voice.speak("max speed: %d meters per second.",
205                                             (int) (state.max_speed + 0.5));
206                                 ret = true;
207                         } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
208                                    state.state >= Altos.ao_flight_drogue) {
209                                 voice.speak("max height: %d meters.",
210                                             (int) (state.max_height + 0.5));
211                                 ret = true;
212                         }
213                 }
214                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
215                         if (state.gps_ready) {
216                                 voice.speak("GPS ready");
217                                 ret = true;
218                         }
219                         else if (old_state != null) {
220                                 voice.speak("GPS lost");
221                                 ret = true;
222                         }
223                 }
224                 old_state = state;
225                 return ret;
226         }
227
228         public void run() {
229                 boolean         interrupted = false;
230                 String          line;
231                 AltosState      state = null;
232                 AltosState      old_state = null;
233                 boolean         told;
234
235                 idle_thread = new IdleThread();
236
237                 try {
238                         for (;;) {
239                                 try {
240                                         AltosRecord record = reader.read();
241                                         if (record == null)
242                                                 break;
243                                         old_state = state;
244                                         state = new AltosState(record, state);
245                                         reader.update(state);
246                                         show_safely(state, crc_errors);
247                                         told = tell(state, old_state);
248                                         idle_thread.notice(state, told);
249                                 } catch (ParseException pp) {
250                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
251                                 } catch (AltosCRCException ce) {
252                                         ++crc_errors;
253                                         show_safely(state, crc_errors);
254                                 }
255                         }
256                 } catch (InterruptedException ee) {
257                         interrupted = true;
258                 } catch (IOException ie) {
259                         reading_error_safely();
260                 } finally {
261                         if (!interrupted)
262                                 idle_thread.report(true);
263                         reader.close(interrupted);
264                         idle_thread.interrupt();
265                         try {
266                                 idle_thread.join();
267                         } catch (InterruptedException ie) {}
268                 }
269         }
270
271         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
272                 parent = in_parent;
273                 voice = in_voice;
274                 display = in_display;
275                 reader = in_reader;
276                 display.reset();
277         }
278 }