altos: Shuffle LCO functions around, add telelco first cut
[fw/altos] / altoslib / AltosLog.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.io.*;
21 import java.lang.*;
22 import java.util.*;
23 import java.text.ParseException;
24 import java.util.concurrent.LinkedBlockingQueue;
25
26 /*
27  * This creates a thread to capture telemetry data and write it to
28  * a log file
29  */
30 class AltosLog implements Runnable {
31
32         LinkedBlockingQueue<AltosLine>  input_queue;
33         LinkedBlockingQueue<String>     pending_queue;
34         int                             serial;
35         int                             flight;
36         FileWriter                      log_file;
37         Thread                          log_thread;
38         AltosFile                       file;
39
40         private void close_log_file() {
41                 if (log_file != null) {
42                         try {
43                                 log_file.close();
44                         } catch (IOException io) {
45                         }
46                         log_file = null;
47                 }
48         }
49
50         void close() {
51                 close_log_file();
52                 if (log_thread != null) {
53                         log_thread.interrupt();
54                         log_thread = null;
55                 }
56         }
57
58         File file() {
59                 return file;
60         }
61
62         boolean open (AltosRecord telem) throws IOException {
63                 AltosFile       a = new AltosFile(telem);
64
65                 log_file = new FileWriter(a, true);
66                 if (log_file != null) {
67                         while (!pending_queue.isEmpty()) {
68                                 try {
69                                         String s = pending_queue.take();
70                                         log_file.write(s);
71                                         log_file.write('\n');
72                                 } catch (InterruptedException ie) {
73                                 }
74                         }
75                         log_file.flush();
76                         file = a;
77                 }
78                 return log_file != null;
79         }
80
81         public void run () {
82                 try {
83                         AltosRecord     previous = null;
84                         for (;;) {
85                                 AltosLine       line = input_queue.take();
86                                 if (line.line == null)
87                                         continue;
88                                 try {
89                                         AltosRecord     telem = AltosTelemetry.parse(line.line, previous);
90                                         if (telem.serial != 0 && telem.flight != 0 &&
91                                             (telem.serial != serial || telem.flight != flight || log_file == null))
92                                         {
93                                                 close_log_file();
94                                                 serial = telem.serial;
95                                                 flight = telem.flight;
96                                                 open(telem);
97                                         }
98                                         previous = telem;
99                                 } catch (ParseException pe) {
100                                 } catch (AltosCRCException ce) {
101                                 }
102                                 if (log_file != null) {
103                                         log_file.write(line.line);
104                                         log_file.write('\n');
105                                         log_file.flush();
106                                 } else
107                                         pending_queue.put(line.line);
108                         }
109                 } catch (InterruptedException ie) {
110                 } catch (IOException ie) {
111                 }
112                 close();
113         }
114
115         public AltosLog (AltosLink link) {
116                 pending_queue = new LinkedBlockingQueue<String> ();
117                 input_queue = new LinkedBlockingQueue<AltosLine> ();
118                 link.add_monitor(input_queue);
119                 serial = -1;
120                 flight = -1;
121                 log_file = null;
122                 log_thread = new Thread(this);
123                 log_thread.start();
124         }
125 }