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;
21 import java.text.ParseException;
22 import java.util.concurrent.LinkedBlockingQueue;
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;
38 private void close_log_file() {
39 if (log_file != null) {
42 } catch (IOException io) {
50 if (log_thread != null) {
51 log_thread.interrupt();
60 boolean open (AltosRecord telem) throws IOException {
61 AltosFile a = new AltosFile(telem);
63 log_file = new FileWriter(a, true);
64 if (log_file != null) {
65 while (!pending_queue.isEmpty()) {
67 String s = pending_queue.take();
70 } catch (InterruptedException ie) {
76 return log_file != null;
81 AltosRecord previous = null;
83 AltosLine line = input_queue.take();
84 if (line.line == null)
87 AltosRecord telem = AltosTelemetry.parse(line.line, previous);
88 if ((telem.seen & AltosRecord.seen_flight) != 0 &&
89 (telem.serial != serial || telem.flight != flight || log_file == null))
92 serial = telem.serial;
93 flight = telem.flight;
97 } catch (ParseException pe) {
98 } catch (AltosCRCException ce) {
100 if (log_file != null) {
101 log_file.write(line.line);
102 log_file.write('\n');
105 pending_queue.put(line.line);
107 } catch (InterruptedException ie) {
108 } catch (IOException ie) {
113 public AltosLog (AltosLink link) {
114 pending_queue = new LinkedBlockingQueue<String> ();
115 input_queue = new LinkedBlockingQueue<AltosLine> ();
116 link.add_monitor(input_queue);
120 log_thread = new Thread(this);