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.
23 import java.text.ParseException;
24 import java.util.concurrent.LinkedBlockingQueue;
27 * This creates a thread to capture telemetry data and write it to
30 class AltosLog implements Runnable {
32 LinkedBlockingQueue<AltosLine> input_queue;
33 LinkedBlockingQueue<String> pending_queue;
39 private void close_log_file() {
40 if (log_file != null) {
43 } catch (IOException io) {
51 if (log_thread != null) {
52 log_thread.interrupt();
57 boolean open (AltosRecord telem) throws IOException {
58 AltosFile a = new AltosFile(telem);
60 System.out.printf("open %s\n", a.toString());
61 log_file = new FileWriter(a, true);
62 if (log_file != null) {
63 while (!pending_queue.isEmpty()) {
65 String s = pending_queue.take();
68 } catch (InterruptedException ie) {
73 return log_file != null;
78 AltosRecord previous = null;
80 AltosLine line = input_queue.take();
81 if (line.line == null)
84 AltosRecord telem = AltosTelemetry.parse(line.line, previous);
85 if (telem.serial != 0 && telem.flight != 0 &&
86 (telem.serial != serial || telem.flight != flight || log_file == null))
89 serial = telem.serial;
90 flight = telem.flight;
91 System.out.printf("Opening telem %d %d\n", serial, flight);
95 } catch (ParseException pe) {
96 } catch (AltosCRCException ce) {
98 if (log_file != null) {
99 log_file.write(line.line);
100 log_file.write('\n');
103 pending_queue.put(line.line);
105 } catch (InterruptedException ie) {
106 } catch (IOException ie) {
111 public AltosLog (AltosSerial s) {
112 pending_queue = new LinkedBlockingQueue<String> ();
113 input_queue = new LinkedBlockingQueue<AltosLine> ();
114 s.add_monitor(input_queue);
118 log_thread = new Thread(this);