Reverted package name to 'altosui' from 'AltosUI'
[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 import org.altusmetrum.AltosLib.*;
31
32 public class AltosDisplayThread extends Thread {
33
34         Frame                   parent;
35         IdleThread              idle_thread;
36         AltosVoice              voice;
37         AltosFlightReader       reader;
38         int                     crc_errors;
39         AltosFlightDisplay      display;
40
41         void show_internal(AltosState state, int crc_errors) {
42                 if (state != null)
43                         display.show(state, crc_errors);
44         }
45
46         void show_safely(AltosState in_state, int in_crc_errors) {
47                 final AltosState state = in_state;
48                 final int crc_errors = in_crc_errors;
49                 Runnable r = new Runnable() {
50                                 public void run() {
51                                         try {
52                                                 show_internal(state, crc_errors);
53                                         } catch (Exception ex) {
54                                         }
55                                 }
56                         };
57                 SwingUtilities.invokeLater(r);
58         }
59
60         void reading_error_internal() {
61                 JOptionPane.showMessageDialog(parent,
62                                               String.format("Error reading from \"%s\"", reader.name),
63                                               "Telemetry Read Error",
64                                               JOptionPane.ERROR_MESSAGE);
65         }
66
67         void reading_error_safely() {
68                 Runnable r = new Runnable() {
69                                 public void run() {
70                                         try {
71                                                 reading_error_internal();
72                                         } catch (Exception ex) {
73                                         }
74                                 }
75                         };
76                 SwingUtilities.invokeLater(r);
77         }
78
79         class IdleThread extends Thread {
80
81                 boolean started;
82                 private AltosState state;
83                 int     reported_landing;
84                 int     report_interval;
85                 long    report_time;
86
87                 public synchronized void report(boolean last) {
88                         if (state == null)
89                                 return;
90
91                         /* reset the landing count once we hear about a new flight */
92                         if (state.state < Altos.ao_flight_drogue)
93                                 reported_landing = 0;
94
95                         /* Shut up once the rocket is on the ground */
96                         if (reported_landing > 2) {
97                                 return;
98                         }
99
100                         /* If the rocket isn't on the pad, then report height */
101                         if (Altos.ao_flight_drogue <= state.state &&
102                             state.state < Altos.ao_flight_landed &&
103                             state.range >= 0)
104                         {
105                                 voice.speak("Height %s, bearing %s %d, elevation %d, range %s.\n",
106                                             AltosConvert.height.say(state.height),
107                                             state.from_pad.bearing_words(
108                                                     AltosGreatCircle.BEARING_VOICE),
109                                             (int) (state.from_pad.bearing + 0.5),
110                                             (int) (state.elevation + 0.5),
111                                             AltosConvert.distance.say(state.range));
112                         } else if (state.state > Altos.ao_flight_pad) {
113                                 voice.speak(AltosConvert.height.say_units(state.height));
114                         } else {
115                                 reported_landing = 0;
116                         }
117
118                         /* If the rocket is coming down, check to see if it has landed;
119                          * either we've got a landed report or we haven't heard from it in
120                          * a long time
121                          */
122                         if (state.state >= Altos.ao_flight_drogue &&
123                             (last ||
124                              System.currentTimeMillis() - state.report_time >= 15000 ||
125                              state.state == Altos.ao_flight_landed))
126                         {
127                                 if (Math.abs(state.baro_speed) < 20 && state.height < 100)
128                                         voice.speak("rocket landed safely");
129                                 else
130                                         voice.speak("rocket may have crashed");
131                                 if (state.from_pad != null)
132                                         voice.speak("Bearing %d degrees, range %s.",
133                                                     (int) (state.from_pad.bearing + 0.5),
134                                                     AltosConvert.distance.say_units(state.from_pad.distance));
135                                 ++reported_landing;
136                                 if (state.state != Altos.ao_flight_landed) {
137                                         state.state = Altos.ao_flight_landed;
138                                         show_safely(state, 0);
139                                 }
140                         }
141                 }
142
143                 long now () {
144                         return System.currentTimeMillis();
145                 }
146
147                 void set_report_time() {
148                         report_time = now() + report_interval;
149                 }
150
151                 public void run () {
152                         try {
153                                 for (;;) {
154                                         set_report_time();
155                                         for (;;) {
156                                                 voice.drain();
157                                                 synchronized (this) {
158                                                         long    sleep_time = report_time - now();
159                                                         if (sleep_time <= 0)
160                                                                 break;
161                                                         wait(sleep_time);
162                                                 }
163                                         }
164                                         report(false);
165                                 }
166                         } catch (InterruptedException ie) {
167                                 try {
168                                         voice.drain();
169                                 } catch (InterruptedException iie) { }
170                         }
171                 }
172
173                 public synchronized void notice(AltosState new_state, boolean spoken) {
174                         AltosState old_state = state;
175                         state = new_state;
176                         if (!started && state.state > Altos.ao_flight_pad) {
177                                 started = true;
178                                 start();
179                         }
180
181                         if (state.state < Altos.ao_flight_drogue)
182                                 report_interval = 10000;
183                         else
184                                 report_interval = 20000;
185                         if (old_state != null && old_state.state != state.state) {
186                                 report_time = now();
187                                 this.notify();
188                         } else if (spoken)
189                                 set_report_time();
190                 }
191
192                 public IdleThread() {
193                         state = null;
194                         reported_landing = 0;
195                         report_interval = 10000;
196                 }
197         }
198
199         boolean tell(AltosState state, AltosState old_state) {
200                 boolean ret = false;
201                 if (old_state == null || old_state.state != state.state) {
202                         voice.speak(state.data.state());
203                         if ((old_state == null || old_state.state <= Altos.ao_flight_boost) &&
204                             state.state > Altos.ao_flight_boost) {
205                                 voice.speak("max speed: %s.",
206                                             AltosConvert.speed.say_units(state.max_speed + 0.5));
207                                 ret = true;
208                         } else if ((old_state == null || old_state.state < Altos.ao_flight_drogue) &&
209                                    state.state >= Altos.ao_flight_drogue) {
210                                 voice.speak("max height: %s.",
211                                             AltosConvert.height.say_units(state.max_height + 0.5));
212                                 ret = true;
213                         }
214                 }
215                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
216                         if (state.gps_ready) {
217                                 voice.speak("GPS ready");
218                                 ret = true;
219                         }
220                         else if (old_state != null) {
221                                 voice.speak("GPS lost");
222                                 ret = true;
223                         }
224                 }
225                 old_state = state;
226                 return ret;
227         }
228
229         public void run() {
230                 boolean         interrupted = false;
231                 String          line;
232                 AltosState      state = null;
233                 AltosState      old_state = null;
234                 boolean         told;
235
236                 idle_thread = new IdleThread();
237
238                 try {
239                         for (;;) {
240                                 try {
241                                         AltosRecord record = reader.read();
242                                         if (record == null)
243                                                 break;
244                                         old_state = state;
245                                         state = new AltosState(record, state);
246                                         reader.update(state);
247                                         show_safely(state, crc_errors);
248                                         told = tell(state, old_state);
249                                         idle_thread.notice(state, told);
250                                 } catch (ParseException pp) {
251                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
252                                 } catch (AltosCRCException ce) {
253                                         ++crc_errors;
254                                         show_safely(state, crc_errors);
255                                 }
256                         }
257                 } catch (InterruptedException ee) {
258                         interrupted = true;
259                 } catch (IOException ie) {
260                         reading_error_safely();
261                 } finally {
262                         if (!interrupted)
263                                 idle_thread.report(true);
264                         reader.close(interrupted);
265                         idle_thread.interrupt();
266                         try {
267                                 idle_thread.join();
268                         } catch (InterruptedException ie) {}
269                 }
270         }
271
272         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
273                 parent = in_parent;
274                 voice = in_voice;
275                 display = in_display;
276                 reader = in_reader;
277                 display.reset();
278         }
279 }