docs: Document altosui "Graph Data" button
[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
26 /*
27  * This creates a thread to capture telemetry data and write it to
28  * a log file
29  */
30 class AltosLog implements Runnable {
31
32         LinkedBlockingQueue<AltosLine>  input_queue;
33         LinkedBlockingQueue<String>     pending_queue;
34         int                             serial;
35         int                             flight;
36         FileWriter                      log_file;
37         Thread                          log_thread;
38
39         private void close_log_file() {
40                 if (log_file != null) {
41                         try {
42                                 log_file.close();
43                         } catch (IOException io) {
44                         }
45                         log_file = null;
46                 }
47         }
48
49         void close() {
50                 close_log_file();
51                 if (log_thread != null) {
52                         log_thread.interrupt();
53                         log_thread = null;
54                 }
55         }
56
57         boolean open (AltosTelemetry telem) throws IOException {
58                 AltosFile       a = new AltosFile(telem);
59
60                 log_file = new FileWriter(a, true);
61                 if (log_file != null) {
62                         while (!pending_queue.isEmpty()) {
63                                 try {
64                                         String s = pending_queue.take();
65                                         log_file.write(s);
66                                         log_file.write('\n');
67                                 } catch (InterruptedException ie) {
68                                 }
69                         }
70                         log_file.flush();
71                 }
72                 return log_file != null;
73         }
74
75         public void run () {
76                 try {
77                         for (;;) {
78                                 AltosLine       line = input_queue.take();
79                                 if (line.line == null)
80                                         continue;
81                                 try {
82                                         AltosTelemetry  telem = new AltosTelemetry(line.line);
83                                         if (telem.serial != serial || telem.flight != flight || log_file == null) {
84                                                 close_log_file();
85                                                 serial = telem.serial;
86                                                 flight = telem.flight;
87                                                 open(telem);
88                                         }
89                                 } catch (ParseException pe) {
90                                 } catch (AltosCRCException ce) {
91                                 }
92                                 if (log_file != null) {
93                                         log_file.write(line.line);
94                                         log_file.write('\n');
95                                         log_file.flush();
96                                 } else
97                                         pending_queue.put(line.line);
98                         }
99                 } catch (InterruptedException ie) {
100                 } catch (IOException ie) {
101                 }
102                 close();
103         }
104
105         public AltosLog (AltosSerial s) {
106                 pending_queue = new LinkedBlockingQueue<String> ();
107                 input_queue = new LinkedBlockingQueue<AltosLine> ();
108                 s.add_monitor(input_queue);
109                 serial = -1;
110                 flight = -1;
111                 log_file = null;
112                 log_thread = new Thread(this);
113                 log_thread.start();
114         }
115 }