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