altosui: Clear displayed data rows as needed.
[fw/altos] / ao-tools / altosui / AltosLog.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.io.*;
21 import java.lang.*;
22 import java.util.*;
23 import java.text.ParseException;
24 import java.util.concurrent.LinkedBlockingQueue;
25 import altosui.AltosSerial;
26 import altosui.AltosFile;
27
28 /*
29  * This creates a thread to capture telemetry data and write it to
30  * a log file
31  */
32 class AltosLog implements Runnable {
33
34         LinkedBlockingQueue<String>     input_queue;
35         LinkedBlockingQueue<String>     pending_queue;
36         int                             serial;
37         int                             flight;
38         FileWriter                      log_file;
39         Thread                          log_thread;
40
41         void close() throws IOException {
42                 if (log_file != null)
43                         log_file.close();
44         }
45
46         boolean open (AltosTelemetry telem) throws IOException {
47                 AltosFile       a = new AltosFile(telem);
48
49                 log_file = new FileWriter(a, true);
50                 if (log_file != null) {
51                         while (!pending_queue.isEmpty()) {
52                                 try {
53                                         String s = pending_queue.take();
54                                         log_file.write(s);
55                                         log_file.write('\n');
56                                 } catch (InterruptedException ie) {
57                                 }
58                         }
59                         log_file.flush();
60                 }
61                 return log_file != null;
62         }
63
64         public void run () {
65                 try {
66                         for (;;) {
67                                 String  line = input_queue.take();
68                                 try {
69                                         AltosTelemetry  telem = new AltosTelemetry(line);
70                                         if (telem.serial != serial || telem.flight != flight || log_file == null) {
71                                                 close();
72                                                 serial = telem.serial;
73                                                 flight = telem.flight;
74                                                 open(telem);
75                                         }
76                                 } catch (ParseException pe) {
77                                 }
78                                 if (log_file != null) {
79                                         log_file.write(line);
80                                         log_file.write('\n');
81                                         log_file.flush();
82                                 } else
83                                         pending_queue.put(line);
84                         }
85                 } catch (InterruptedException ie) {
86                 } catch (IOException ie) {
87                 }
88                 try {
89                         close();
90                 } catch (IOException ie) {
91                 }
92         }
93
94         public AltosLog (AltosSerial s) {
95                 pending_queue = new LinkedBlockingQueue<String> ();
96                 input_queue = new LinkedBlockingQueue<String> ();
97                 s.add_monitor(input_queue);
98                 serial = -1;
99                 flight = -1;
100                 log_file = null;
101                 log_thread = new Thread(this);
102                 log_thread.start();
103         }
104 }