altosui, telegps, altosdroid: say ground distance, not total range
[fw/altos] / altosuilib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_11;
20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.io.*;
24 import java.text.*;
25 import org.altusmetrum.altoslib_11.*;
26
27 public class AltosDisplayThread extends Thread {
28
29         Frame                   parent;
30         IdleThread              idle_thread;
31         AltosVoice              voice;
32         AltosFlightReader       reader;
33         AltosState              state;
34         int                     old_state = AltosLib.ao_flight_invalid;
35         boolean                 old_gps_ready = false;
36         AltosListenerState      listener_state;
37         AltosFlightDisplay      display;
38
39         synchronized void show_safely() {
40                 final AltosState my_state = state;
41                 final AltosListenerState my_listener_state = listener_state;
42                 Runnable r = new Runnable() {
43                                 public void run() {
44                                         try {
45                                                 display.show(my_state, my_listener_state);
46                                         } catch (Exception ex) {
47                                         }
48                                 }
49                         };
50                 SwingUtilities.invokeLater(r);
51         }
52
53         void reading_error_internal() {
54                 JOptionPane.showMessageDialog(parent,
55                                               String.format("Error reading from \"%s\"", reader.name),
56                                               "Telemetry Read Error",
57                                               JOptionPane.ERROR_MESSAGE);
58         }
59
60         void reading_error_safely() {
61                 Runnable r = new Runnable() {
62                                 public void run() {
63                                         try {
64                                                 reading_error_internal();
65                                         } catch (Exception ex) {
66                                         }
67                                 }
68                         };
69                 SwingUtilities.invokeLater(r);
70         }
71
72         class IdleThread extends Thread {
73
74                 boolean started;
75                 int     reported_landing;
76                 int     report_interval;
77                 long    report_time;
78
79                 public synchronized void report(boolean last) {
80                         if (state == null)
81                                 return;
82
83                         /* reset the landing count once we hear about a new flight */
84                         if (state.state() < AltosLib.ao_flight_drogue)
85                                 reported_landing = 0;
86
87                         /* Shut up once the rocket is on the ground */
88                         if (reported_landing > 2) {
89                                 return;
90                         }
91
92                         /* If the rocket isn't on the pad, then report height */
93                         if (AltosLib.ao_flight_drogue <= state.state() &&
94                             state.state() < AltosLib.ao_flight_landed &&
95                             state.from_pad != null &&
96                             state.range >= 0)
97                         {
98                                 voice.speak("Height %s, bearing %s %d, elevation %d, distance %s.\n",
99                                             AltosConvert.height.say(state.height()),
100                                             state.from_pad.bearing_words(
101                                                     AltosGreatCircle.BEARING_VOICE),
102                                             (int) (state.from_pad.bearing + 0.5),
103                                             (int) (state.elevation + 0.5),
104                                             AltosConvert.distance.say(state.distance));
105                         } else if (state.state() > AltosLib.ao_flight_pad && state.height() != AltosLib.MISSING) {
106                                 voice.speak(AltosConvert.height.say_units(state.height()));
107                         } else {
108                                 reported_landing = 0;
109                         }
110
111                         /* If the rocket is coming down, check to see if it has landed;
112                          * either we've got a landed report or we haven't heard from it in
113                          * a long time
114                          */
115                         if (state.state() != AltosLib.ao_flight_stateless &&
116                             state.state() >= AltosLib.ao_flight_drogue &&
117                             (last ||
118                              System.currentTimeMillis() - state.received_time >= 15000 ||
119                              state.state() == AltosLib.ao_flight_landed))
120                         {
121                                 if (Math.abs(state.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, distance %s.",
127                                                     (int) (state.from_pad.bearing + 0.5),
128                                                     AltosConvert.distance.say_units(state.from_pad.distance));
129                                 ++reported_landing;
130                         }
131                 }
132
133                 long now () {
134                         return System.currentTimeMillis();
135                 }
136
137                 void set_report_time() {
138                         report_time = now() + report_interval;
139                 }
140
141                 public void run () {
142                         try {
143                                 for (;;) {
144                                         if (reader.has_monitor_battery()) {
145                                                 listener_state.battery = reader.monitor_battery();
146                                                 show_safely();
147                                         }
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
159                                         report(false);
160                                 }
161                         } catch (InterruptedException ie) {
162                                 try {
163                                         voice.drain();
164                                 } catch (InterruptedException iie) { }
165                         }
166                 }
167
168                 public synchronized void notice(boolean spoken) {
169                         if (old_state != state.state()) {
170                                 report_time = now();
171                                 this.notify();
172                         } else if (spoken)
173                                 set_report_time();
174                 }
175
176                 public IdleThread() {
177                         reported_landing = 0;
178                         report_interval = 10000;
179                 }
180         }
181
182         synchronized boolean tell() {
183                 boolean ret = false;
184                 if (old_state != state.state()) {
185                         if (state.state() != AltosLib.ao_flight_stateless)
186                                 voice.speak(state.state_name());
187                         if ((old_state == AltosLib.ao_flight_invalid || old_state <= AltosLib.ao_flight_boost) &&
188                             state.state() > AltosLib.ao_flight_boost) {
189                                 if (state.max_speed() != AltosLib.MISSING)
190                                         voice.speak("max speed: %s.",
191                                                     AltosConvert.speed.say_units(state.max_speed() + 0.5));
192                                 ret = true;
193                         } else if ((old_state == AltosLib.ao_flight_invalid || old_state < AltosLib.ao_flight_drogue) &&
194                                    state.state() >= AltosLib.ao_flight_drogue) {
195                                 if (state.max_height() != AltosLib.MISSING)
196                                         voice.speak("max height: %s.",
197                                                     AltosConvert.height.say_units(state.max_height() + 0.5));
198                                 ret = true;
199                         }
200                 }
201                 if (old_gps_ready != state.gps_ready) {
202                         if (state.gps_ready) {
203                                 voice.speak("GPS ready");
204                                 ret = true;
205                         }
206                         else if (old_gps_ready) {
207                                 voice.speak("GPS lost");
208                                 ret = true;
209                         }
210                 }
211                 old_state = state.state();
212                 old_gps_ready = state.gps_ready;
213                 return ret;
214         }
215
216         public void run() {
217                 boolean         interrupted = false;
218                 boolean         told;
219
220                 idle_thread = new IdleThread();
221                 idle_thread.start();
222
223                 try {
224                         for (;;) {
225                                 try {
226                                         state = reader.read();
227                                         if (state == null) {
228                                                 listener_state.running = false;
229                                                 break;
230                                         }
231                                         show_safely();
232                                         told = tell();
233                                         idle_thread.notice(told);
234                                 } catch (ParseException pp) {
235                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
236                                 } catch (AltosCRCException ce) {
237                                         ++listener_state.crc_errors;
238                                         show_safely();
239                                 }
240                         }
241                 } catch (InterruptedException ee) {
242                         interrupted = true;
243                 } catch (IOException ie) {
244                         reading_error_safely();
245                 } finally {
246                         if (!interrupted)
247                                 idle_thread.report(true);
248                         reader.close(interrupted);
249                         idle_thread.interrupt();
250                         try {
251                                 idle_thread.join();
252                         } catch (InterruptedException ie) {}
253                 }
254         }
255
256         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
257                 listener_state = new AltosListenerState();
258                 parent = in_parent;
259                 voice = in_voice;
260                 display = in_display;
261                 reader = in_reader;
262                 display.reset();
263         }
264 }