Merge branch 'master' of ssh://git.gag.com/scm/git/fw/altos
[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; 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 org.altusmetrum.altosuilib_7;
19
20 import java.awt.*;
21 import javax.swing.*;
22 import java.io.*;
23 import java.text.*;
24 import org.altusmetrum.altoslib_7.*;
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 < AltosLib.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 (AltosLib.ao_flight_drogue <= state.state &&
91                             state.state < AltosLib.ao_flight_landed &&
92                             state.from_pad != null &&
93                             state.range >= 0)
94                         {
95                                 voice.speak("Height %s, bearing %s %d, elevation %d, range %s.\n",
96                                             AltosConvert.height.say(state.height()),
97                                             state.from_pad.bearing_words(
98                                                     AltosGreatCircle.BEARING_VOICE),
99                                             (int) (state.from_pad.bearing + 0.5),
100                                             (int) (state.elevation + 0.5),
101                                             AltosConvert.distance.say(state.range));
102                         } else if (state.state > AltosLib.ao_flight_pad && state.height() != AltosLib.MISSING) {
103                                 voice.speak(AltosConvert.height.say_units(state.height()));
104                         } else {
105                                 reported_landing = 0;
106                         }
107
108                         /* If the rocket is coming down, check to see if it has landed;
109                          * either we've got a landed report or we haven't heard from it in
110                          * a long time
111                          */
112                         if (state.state != AltosLib.ao_flight_stateless &&
113                             state.state >= AltosLib.ao_flight_drogue &&
114                             (last ||
115                              System.currentTimeMillis() - state.received_time >= 15000 ||
116                              state.state == AltosLib.ao_flight_landed))
117                         {
118                                 if (Math.abs(state.speed()) < 20 && state.height() < 100)
119                                         voice.speak("rocket landed safely");
120                                 else
121                                         voice.speak("rocket may have crashed");
122                                 if (state.from_pad != null)
123                                         voice.speak("Bearing %d degrees, range %s.",
124                                                     (int) (state.from_pad.bearing + 0.5),
125                                                     AltosConvert.distance.say_units(state.from_pad.distance));
126                                 ++reported_landing;
127                                 if (state.state != AltosLib.ao_flight_landed) {
128                                         state.state = AltosLib.ao_flight_landed;
129                                         show_safely();
130                                 }
131                         }
132                 }
133
134                 long now () {
135                         return System.currentTimeMillis();
136                 }
137
138                 void set_report_time() {
139                         report_time = now() + report_interval;
140                 }
141
142                 public void run () {
143                         try {
144                                 for (;;) {
145                                         if (reader.has_monitor_battery()) {
146                                                 listener_state.battery = reader.monitor_battery();
147                                                 show_safely();
148                                         }
149                                         set_report_time();
150                                         for (;;) {
151                                                 voice.drain();
152                                                 synchronized (this) {
153                                                         long    sleep_time = report_time - now();
154                                                         if (sleep_time <= 0)
155                                                                 break;
156                                                         wait(sleep_time);
157                                                 }
158                                         }
159
160                                         report(false);
161                                 }
162                         } catch (InterruptedException ie) {
163                                 try {
164                                         voice.drain();
165                                 } catch (InterruptedException iie) { }
166                         }
167                 }
168
169                 public synchronized void notice(boolean spoken) {
170                         if (old_state != null && old_state.state != state.state) {
171                                 report_time = now();
172                                 this.notify();
173                         } else if (spoken)
174                                 set_report_time();
175                 }
176
177                 public IdleThread() {
178                         reported_landing = 0;
179                         report_interval = 10000;
180                 }
181         }
182
183         synchronized boolean tell() {
184                 boolean ret = false;
185                 if (old_state == null || old_state.state != state.state) {
186                         if (state.state != AltosLib.ao_flight_stateless)
187                                 voice.speak(state.state_name());
188                         if ((old_state == null || old_state.state <= AltosLib.ao_flight_boost) &&
189                             state.state > AltosLib.ao_flight_boost) {
190                                 if (state.max_speed() != AltosLib.MISSING)
191                                         voice.speak("max speed: %s.",
192                                                     AltosConvert.speed.say_units(state.max_speed() + 0.5));
193                                 ret = true;
194                         } else if ((old_state == null || old_state.state < AltosLib.ao_flight_drogue) &&
195                                    state.state >= AltosLib.ao_flight_drogue) {
196                                 if (state.max_height() != AltosLib.MISSING)
197                                         voice.speak("max height: %s.",
198                                                     AltosConvert.height.say_units(state.max_height() + 0.5));
199                                 ret = true;
200                         }
201                 }
202                 if (old_state == null || old_state.gps_ready != state.gps_ready) {
203                         if (state.gps_ready) {
204                                 voice.speak("GPS ready");
205                                 ret = true;
206                         }
207                         else if (old_state != null) {
208                                 voice.speak("GPS lost");
209                                 ret = true;
210                         }
211                 }
212                 old_state = state;
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                                         reader.update(state);
232                                         show_safely();
233                                         told = tell();
234                                         idle_thread.notice(told);
235                                 } catch (ParseException pp) {
236                                         System.out.printf("Parse error: %d \"%s\"\n", pp.getErrorOffset(), pp.getMessage());
237                                 } catch (AltosCRCException ce) {
238                                         ++listener_state.crc_errors;
239                                         show_safely();
240                                 }
241                         }
242                 } catch (InterruptedException ee) {
243                         interrupted = true;
244                 } catch (IOException ie) {
245                         reading_error_safely();
246                 } finally {
247                         if (!interrupted)
248                                 idle_thread.report(true);
249                         reader.close(interrupted);
250                         idle_thread.interrupt();
251                         try {
252                                 idle_thread.join();
253                         } catch (InterruptedException ie) {}
254                 }
255         }
256
257         public AltosDisplayThread(Frame in_parent, AltosVoice in_voice, AltosFlightDisplay in_display, AltosFlightReader in_reader) {
258                 listener_state = new AltosListenerState();
259                 parent = in_parent;
260                 voice = in_voice;
261                 display = in_display;
262                 reader = in_reader;
263                 display.reset();
264         }
265 }