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