]> git.gag.com Git - fw/altos/blob - altoslib/AltosLog.java
altos: oops. Fix syntax
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_14;
20
21 import java.io.*;
22 import java.text.*;
23 import java.util.concurrent.*;
24
25 /*
26  * This creates a thread to capture telemetry data and write it to
27  * a log file
28  */
29 public class AltosLog implements Runnable {
30
31         LinkedBlockingQueue<AltosLine>  input_queue;
32         LinkedBlockingQueue<String>     pending_queue;
33         int                             serial;
34         int                             flight;
35         int                             receiver_serial;
36         FileWriter                      log_file;
37         Thread                          log_thread;
38         AltosFile                       file;
39         AltosLink                       link;
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         public void close() {
52                 link.remove_monitor(input_queue);
53                 close_log_file();
54                 if (log_thread != null) {
55                         log_thread.interrupt();
56                         log_thread = null;
57                 }
58         }
59
60         public File file() {
61                 return file;
62         }
63
64         boolean open (AltosCalData cal_data) throws IOException, InterruptedException {
65                 AltosFile       a = new AltosFile(cal_data);
66
67                 log_file = new FileWriter(a, true);
68                 if (log_file != null) {
69                         while (!pending_queue.isEmpty()) {
70                                 String s = pending_queue.take();
71                                 log_file.write(s);
72                                 log_file.write('\n');
73                         }
74                         log_file.flush();
75                         file = a;
76                         AltosPreferences.set_logfile(link.serial, file);
77                 }
78                 return log_file != null;
79         }
80
81         public void run () {
82                 try {
83                         AltosConfigData receiver_config = link.config_data();
84                         AltosCalData    cal_data = new AltosCalData();
85                         AltosState      state = null;
86                         cal_data.set_receiver_serial(receiver_config.serial);
87                         for (;;) {
88                                 AltosLine       line = input_queue.take();
89                                 if (line.line == null)
90                                         continue;
91                                 try {
92                                         AltosTelemetry  telem = AltosTelemetry.parse(line.line);
93                                         if (state == null)
94                                                 state = new AltosState(cal_data);
95                                         telem.provide_data(state);
96
97                                         if (cal_data.serial != serial ||
98                                             cal_data.flight != flight ||
99                                             log_file == null)
100                                         {
101                                                 close_log_file();
102                                                 serial = cal_data.serial;
103                                                 flight = cal_data.flight;
104                                                 state = null;
105                                                 if (cal_data.serial != AltosLib.MISSING &&
106                                                     cal_data.flight != AltosLib.MISSING)
107                                                         open(cal_data);
108                                         }
109                                 } catch (ParseException pe) {
110                                 } catch (AltosCRCException ce) {
111                                 }
112                                 if (log_file != null) {
113                                         log_file.write(line.line);
114                                         log_file.write('\n');
115                                         log_file.flush();
116                                 } else
117                                         pending_queue.put(line.line);
118                         }
119                 } catch (InterruptedException ie) {
120                 } catch (TimeoutException te) {
121                 } catch (IOException ie) {
122                 }
123                 close();
124         }
125
126         public AltosLog (AltosLink link) {
127                 pending_queue = new LinkedBlockingQueue<String> ();
128                 input_queue = new LinkedBlockingQueue<AltosLine> ();
129                 link.add_monitor(input_queue);
130                 serial = -1;
131                 flight = -1;
132                 this.link = link;
133                 log_file = null;
134                 log_thread = new Thread(this);
135                 log_thread.start();
136         }
137 }