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