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