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_11;
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;
41 private void close_log_file() {
42 if (log_file != null) {
45 } catch (IOException io) {
52 link.remove_monitor(input_queue);
54 if (log_thread != null) {
55 log_thread.interrupt();
64 boolean open (AltosState state) throws IOException, InterruptedException {
65 AltosFile a = new AltosFile(state);
67 log_file = new FileWriter(a, true);
68 if (log_file != null) {
69 while (!pending_queue.isEmpty()) {
70 String s = pending_queue.take();
76 AltosPreferences.set_logfile(link.serial, file);
78 return log_file != null;
83 AltosState state = new AltosState();
84 AltosConfigData receiver_config = link.config_data();
85 state.set_receiver_serial(receiver_config.serial);
87 AltosLine line = input_queue.take();
88 if (line.line == null)
91 AltosTelemetry telem = AltosTelemetry.parse(line.line);
92 state = state.clone();
93 telem.update_state(state);
94 if (state.serial != serial || state.flight != flight || log_file == null)
97 serial = state.serial;
98 flight = state.flight;
99 if (state.serial != AltosLib.MISSING && state.flight != AltosLib.MISSING)
102 } catch (ParseException pe) {
103 } catch (AltosCRCException ce) {
105 if (log_file != null) {
106 log_file.write(line.line);
107 log_file.write('\n');
110 pending_queue.put(line.line);
112 } catch (InterruptedException ie) {
113 } catch (TimeoutException te) {
114 } catch (IOException ie) {
119 public AltosLog (AltosLink link) {
120 pending_queue = new LinkedBlockingQueue<String> ();
121 input_queue = new LinkedBlockingQueue<AltosLine> ();
122 link.add_monitor(input_queue);
127 log_thread = new Thread(this);