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