Add version numbers to java libraries
[fw/altos] / altoslib / AltosTelemetryReader.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_1;
19
20 import java.text.*;
21 import java.io.*;
22 import java.util.concurrent.*;
23
24 public class AltosTelemetryReader extends AltosFlightReader {
25         AltosLink       link;
26         AltosLog        log;
27         AltosRecord     previous;
28         double          frequency;
29         int             telemetry;
30
31         LinkedBlockingQueue<AltosLine> telem;
32
33         public AltosRecord read() throws InterruptedException, ParseException, AltosCRCException, IOException {
34                 AltosLine l = telem.take();
35                 if (l.line == null)
36                         throw new IOException("IO error");
37                 AltosRecord     next = AltosTelemetry.parse(l.line, previous);
38                 previous = next;
39                 return next;
40         }
41
42         public void flush() {
43                 telem.clear();
44         }
45
46         public void reset() {
47                 previous = null;
48                 flush();
49         }
50
51         public void close(boolean interrupted) {
52                 link.remove_monitor(telem);
53                 log.close();
54                 link.close();
55         }
56
57         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
58                 frequency = in_frequency;
59                 link.set_radio_frequency(frequency);
60         }
61
62         public boolean supports_telemetry(int telemetry) {
63
64                 try {
65                         /* Version 1.0 or later firmware supports all telemetry formats */
66                         if (link.config_data().compare_version("1.0") >= 0)
67                                 return true;
68
69                         /* Version 0.9 firmware only supports 0.9 telemetry */
70                         if (link.config_data().compare_version("0.9") >= 0) {
71                                 if (telemetry == AltosLib.ao_telemetry_0_9)
72                                         return true;
73                                 else
74                                         return false;
75                         }
76
77                         /* Version 0.8 firmware only supports 0.8 telemetry */
78                         if (telemetry == AltosLib.ao_telemetry_0_8)
79                                 return true;
80                         else
81                                 return false;
82                 } catch (InterruptedException ie) {
83                         return true;
84                 } catch (TimeoutException te) {
85                         return true;
86                 }
87         }
88
89         public void save_frequency() {
90                 AltosPreferences.set_frequency(link.serial, frequency);
91         }
92
93         public void set_telemetry(int in_telemetry) {
94                 telemetry = in_telemetry;
95                 link.set_telemetry(telemetry);
96         }
97
98         public void save_telemetry() {
99                 AltosPreferences.set_telemetry(link.serial, telemetry);
100         }
101
102         public void set_monitor(boolean monitor) {
103                 link.set_monitor(monitor);
104         }
105
106         public File backing_file() {
107                 return log.file();
108         }
109
110         public AltosTelemetryReader (AltosLink in_link)
111                 throws IOException, InterruptedException, TimeoutException {
112                 link = in_link;
113                 try {
114                         log = new AltosLog(link);
115                         name = link.name;
116                         previous = null;
117                         telem = new LinkedBlockingQueue<AltosLine>();
118                         frequency = AltosPreferences.frequency(link.serial);
119                         set_frequency(frequency);
120                         telemetry = AltosPreferences.telemetry(link.serial);
121                         set_telemetry(telemetry);
122                         link.add_monitor(telem);
123                 } catch (TimeoutException e) {
124                         close(true);
125                         throw(e);
126                 } catch (InterruptedException e) {
127                         close(true);
128                         throw(e);
129                 }
130         }
131 }