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