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; version 2 of the License.
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.
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.
18 package org.altusmetrum.altoslib_2;
22 import java.util.concurrent.*;
25 * This creates a thread to capture telemetry data and write it to
28 public class AltosLog implements Runnable {
30 LinkedBlockingQueue<AltosLine> input_queue;
31 LinkedBlockingQueue<String> pending_queue;
40 private void close_log_file() {
41 if (log_file != null) {
44 } catch (IOException io) {
52 if (log_thread != null) {
53 log_thread.interrupt();
62 boolean open (AltosState state) throws IOException, InterruptedException {
63 AltosFile a = new AltosFile(state);
65 log_file = new FileWriter(a, true);
66 if (log_file != null) {
67 while (!pending_queue.isEmpty()) {
68 String s = pending_queue.take();
75 return log_file != null;
80 AltosState state = new AltosState();
81 AltosConfigData receiver_config = link.config_data();
82 state.set_receiver_serial(receiver_config.serial);
84 AltosLine line = input_queue.take();
85 if (line.line == null)
88 AltosTelemetry telem = AltosTelemetry.parse(line.line);
89 state = state.clone();
90 telem.update_state(state);
91 if (state.serial != serial || state.flight != flight || log_file == null)
94 serial = state.serial;
95 flight = state.flight;
96 if (state.serial != AltosLib.MISSING && state.flight != AltosLib.MISSING)
99 } catch (ParseException pe) {
100 } catch (AltosCRCException ce) {
102 if (log_file != null) {
103 log_file.write(line.line);
104 log_file.write('\n');
107 pending_queue.put(line.line);
109 } catch (InterruptedException ie) {
110 } catch (TimeoutException te) {
111 } catch (IOException ie) {
116 public AltosLog (AltosLink link) {
117 pending_queue = new LinkedBlockingQueue<String> ();
118 input_queue = new LinkedBlockingQueue<AltosLine> ();
119 link.add_monitor(input_queue);
124 log_thread = new Thread(this);