Update java library version numbers
[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_10;
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         double          frequency;
28         int             telemetry;
29         int             telemetry_rate;
30         AltosState      state = null;
31
32         LinkedBlockingQueue<AltosLine> telem;
33
34         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
35                 AltosLine l;
36                 do {
37                         l = telem.take();
38                         if (l.line == null)
39                                 throw new IOException("IO error");
40                 } while (!link.get_monitor());
41                 AltosTelemetry  telem = AltosTelemetry.parse(l.line);
42                 if (state == null)
43                         state = new AltosState();
44                 else
45                         state = state.clone();
46                 telem.update_state(state);
47                 return state;
48         }
49
50         public void flush() {
51                 telem.clear();
52         }
53
54         public void reset() {
55                 flush();
56                 state = null;
57         }
58
59         public void close(boolean interrupted) {
60
61                 link.remove_monitor(telem);
62                 log.close();
63                 try {
64                         link.close();
65                 } catch (InterruptedException ie) {
66                 }
67         }
68
69         public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
70                 frequency = in_frequency;
71                 link.set_radio_frequency(frequency);
72         }
73
74         public boolean supports_telemetry(int telemetry) {
75
76                 try {
77                         /* Version 1.0 or later firmware supports all telemetry formats */
78                         if (link.config_data().compare_version("1.0") >= 0)
79                                 return true;
80
81                         /* Version 0.9 firmware only supports 0.9 telemetry */
82                         if (link.config_data().compare_version("0.9") >= 0) {
83                                 if (telemetry == AltosLib.ao_telemetry_0_9)
84                                         return true;
85                                 else
86                                         return false;
87                         }
88
89                         /* Version 0.8 firmware only supports 0.8 telemetry */
90                         if (telemetry == AltosLib.ao_telemetry_0_8)
91                                 return true;
92                         else
93                                 return false;
94                 } catch (InterruptedException ie) {
95                         return false;
96                 } catch (TimeoutException te) {
97                         return true;
98                 }
99         }
100
101         public boolean supports_telemetry_rate(int telemetry_rate) {
102                 try {
103                         /* Version 1.4.1.1 supports all rates, older versions don't */
104                         if (link.config_data().compare_version("1.4.1.1") >= 0)
105                                 return true;
106
107                         if (telemetry_rate == AltosLib.ao_telemetry_rate_38400)
108                                 return true;
109                         else
110                                 return false;
111                 } catch (InterruptedException ie) {
112                         return false;
113                 } catch (TimeoutException te) {
114                         return true;
115                 }
116         }
117
118         public void save_frequency() {
119                 AltosPreferences.set_frequency(link.serial, frequency);
120         }
121
122         public void set_telemetry(int in_telemetry) {
123                 telemetry = in_telemetry;
124                 link.set_telemetry(telemetry);
125         }
126
127         public void save_telemetry() {
128                 AltosPreferences.set_telemetry(link.serial, telemetry);
129         }
130
131         public void set_telemetry_rate(int in_telemetry_rate) {
132                 telemetry_rate = in_telemetry_rate;
133                 link.set_telemetry_rate(telemetry_rate);
134         }
135
136         public void save_telemetry_rate() {
137                 AltosPreferences.set_telemetry_rate(link.serial, telemetry_rate);
138         }
139
140         public void set_monitor(boolean monitor) {
141                 link.set_monitor(monitor);
142         }
143
144         public File backing_file() {
145                 return log.file();
146         }
147
148         public boolean has_monitor_battery() {
149                 return link.has_monitor_battery();
150         }
151
152         public double monitor_battery() throws InterruptedException {
153                 return link.monitor_battery();
154         }
155
156         public AltosTelemetryReader (AltosLink in_link)
157                 throws IOException, InterruptedException, TimeoutException {
158                 link = in_link;
159                 boolean success = false;
160                 try {
161                         log = new AltosLog(link);
162                         name = link.name;
163                         telem = new LinkedBlockingQueue<AltosLine>();
164                         frequency = AltosPreferences.frequency(link.serial);
165                         set_frequency(frequency);
166                         telemetry = AltosPreferences.telemetry(link.serial);
167                         set_telemetry(telemetry);
168                         telemetry_rate = AltosPreferences.telemetry_rate(link.serial);
169                         set_telemetry_rate(telemetry_rate);
170                         link.add_monitor(telem);
171                         success = true;
172                 } finally {
173                         if (!success)
174                                 close(true);
175                 }
176         }
177 }