2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_14;
23 import java.util.concurrent.*;
26 * This creates a thread to capture telemetry data and write it to
29 public class AltosLog implements Runnable {
31 LinkedBlockingQueue<AltosLine> input_queue;
32 LinkedBlockingQueue<String> pending_queue;
42 private void trace(String format, Object ... arguments) {
44 trace.trace(format, arguments);
47 private void close_log_file() {
48 if (log_file != null) {
51 } catch (IOException io) {
58 link.remove_monitor(input_queue);
60 if (log_thread != null) {
61 log_thread.interrupt();
70 boolean open (AltosCalData cal_data) throws IOException, InterruptedException {
71 trace("open serial %d flight %d receiver_serial %d",
74 cal_data.receiver_serial);
76 AltosFile a = new AltosFile(cal_data);
78 trace("open file %s\n", a.getPath());
81 log_file = new FileWriter(a, true);
82 } catch (IOException ie) {
84 trace("open file failed\n");
88 if (log_file != null) {
89 trace("open file success\n");
90 while (!pending_queue.isEmpty()) {
91 String s = pending_queue.take();
97 AltosPreferences.set_logfile(link.serial, file);
99 return log_file != null;
103 trace("log run start\n");
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);
110 AltosLine line = input_queue.take();
111 if (line.line == null)
114 AltosTelemetry telem = AltosTelemetry.parse(line.line);
116 state = new AltosState(cal_data);
117 telem.provide_data(state);
119 if (cal_data.serial != serial ||
120 cal_data.flight != flight ||
124 serial = cal_data.serial;
125 flight = cal_data.flight;
127 if (cal_data.serial != AltosLib.MISSING &&
128 cal_data.flight != AltosLib.MISSING)
131 } catch (ParseException pe) {
132 } catch (AltosCRCException ce) {
134 if (log_file != null) {
135 log_file.write(line.line);
136 log_file.write('\n');
139 pending_queue.put(line.line);
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());
148 trace("log run done\n");
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);
161 log_thread = new Thread(this);
165 public AltosLog (AltosLink link) {