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