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