altos/test: Adjust CRC error rate after FEC fix
[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         AltosLogTrace                   trace;
41
42         private void trace(String format, Object ... arguments) {
43                 if (trace != null)
44                         trace.trace(format, arguments);
45         }
46
47         private void close_log_file() {
48                 if (log_file != null) {
49                         try {
50                                 log_file.close();
51                         } catch (IOException io) {
52                         }
53                         log_file = null;
54                 }
55         }
56
57         public void close() {
58                 link.remove_monitor(input_queue);
59                 close_log_file();
60                 if (log_thread != null) {
61                         log_thread.interrupt();
62                         log_thread = null;
63                 }
64         }
65
66         public File file() {
67                 return file;
68         }
69
70         boolean open (AltosCalData cal_data) throws IOException, InterruptedException {
71                 trace("open serial %d flight %d receiver_serial %d",
72                       cal_data.serial,
73                       cal_data.flight,
74                       cal_data.receiver_serial);
75
76                 AltosFile       a = new AltosFile(cal_data);
77
78                 trace("open file %s\n", a.getPath());
79
80                 try {
81                         log_file = new FileWriter(a, true);
82                 } catch (IOException ie) {
83                         log_file = null;
84                         trace("open file failed\n");
85                         if (trace != null)
86                                 trace.open_failed(a);
87                 }
88                 if (log_file != null) {
89                         trace("open file success\n");
90                         while (!pending_queue.isEmpty()) {
91                                 String s = pending_queue.take();
92                                 log_file.write(s);
93                                 log_file.write('\n');
94                         }
95                         log_file.flush();
96                         file = a;
97                         AltosPreferences.set_logfile(link.serial, file);
98                 }
99                 return log_file != null;
100         }
101
102         public void run () {
103                 trace("log run start\n");
104                 try {
105                         AltosConfigData receiver_config = link.config_data();
106                         AltosCalData    cal_data = new AltosCalData();
107                         AltosState      state = null;
108                         cal_data.set_receiver_serial(receiver_config.serial);
109                         for (;;) {
110                                 AltosLine       line = input_queue.take();
111                                 if (line.line == null)
112                                         continue;
113                                 try {
114                                         AltosTelemetry  telem = AltosTelemetry.parse(line.line);
115                                         if (state == null)
116                                                 state = new AltosState(cal_data);
117                                         telem.provide_data(state);
118
119                                         if (cal_data.serial != serial ||
120                                             cal_data.flight != flight ||
121                                             log_file == null)
122                                         {
123                                                 close_log_file();
124                                                 serial = cal_data.serial;
125                                                 flight = cal_data.flight;
126                                                 state = null;
127                                                 if (cal_data.serial != AltosLib.MISSING &&
128                                                     cal_data.flight != AltosLib.MISSING)
129                                                         open(cal_data);
130                                         }
131                                 } catch (ParseException pe) {
132                                 } catch (AltosCRCException ce) {
133                                 }
134                                 if (log_file != null) {
135                                         log_file.write(line.line);
136                                         log_file.write('\n');
137                                         log_file.flush();
138                                 } else
139                                         pending_queue.put(line.line);
140                         }
141                 } catch (InterruptedException ie) {
142                         trace("interrupted exception\n");
143                 } catch (TimeoutException te) {
144                         trace("timeout exception\n");
145                 } catch (IOException ie) {
146                         trace("io exception %s message %s\n", ie.toString(), ie.getMessage());
147                 }
148                 trace("log run done\n");
149                 close();
150         }
151
152         public AltosLog (AltosLink link, AltosLogTrace trace) {
153                 pending_queue = new LinkedBlockingQueue<String> ();
154                 input_queue = new LinkedBlockingQueue<AltosLine> ();
155                 link.add_monitor(input_queue);
156                 serial = -1;
157                 flight = -1;
158                 this.trace = trace;
159                 this.link = link;
160                 log_file = null;
161                 log_thread = new Thread(this);
162                 log_thread.start();
163         }
164
165         public AltosLog (AltosLink link) {
166                 this(link, null);
167         }
168 }