altoslib,altosuilib: Bump library version numbers
[fw/altos] / altoslib / AltosIdleReader.java
1 /*
2  * Copyright © 2016 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altoslib_12;
20
21 import java.text.*;
22 import java.io.*;
23 import java.util.concurrent.*;
24
25 public class AltosIdleReader extends AltosFlightReader {
26         AltosLink       link;
27         boolean         remote;
28         AltosCalData    cal_data = null;
29         AltosState      state = null;
30         AltosIdleFetch  fetch;
31         long            next_millis;
32         static final long       report_interval = 5 * 1000;
33         static final long       minimum_delay = 1 * 1000;
34
35         private void start_link() throws InterruptedException, TimeoutException {
36                 if (remote) {
37                         link.start_remote();
38                 } else
39                         link.flush_input();
40         }
41
42         private boolean stop_link() throws InterruptedException, TimeoutException {
43                 if (remote)
44                         link.stop_remote();
45                 return link.reply_abort;
46         }
47
48         public AltosCalData cal_data() {
49                 if (cal_data == null) {
50                         try {
51                                 cal_data = new AltosCalData(link.config_data());
52                         } catch (InterruptedException ie) {
53                         } catch (TimeoutException te) {
54                         }
55                         if (cal_data == null)
56                                 cal_data = new AltosCalData();
57                 }
58                 return cal_data;
59         }
60
61         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
62                 boolean worked = false;
63                 boolean aborted = false;
64
65                 long    delay = next_millis - System.currentTimeMillis();
66
67                 if (delay > 0)
68                         Thread.sleep(delay);
69                 next_millis = System.currentTimeMillis() + report_interval;
70                 try {
71                         try {
72                                 start_link();
73                                 if (state == null)
74                                         state = new AltosState(cal_data());
75                                 fetch.provide_data(state, state.cal_data);
76                                 if (!link.has_error && !link.reply_abort)
77                                         worked = true;
78                         } catch (TimeoutException te) {
79                         } catch (AltosUnknownProduct ue) {
80                                 worked = true;
81                         }
82                 } finally {
83                         try {
84                                 aborted = stop_link();
85                         } catch (TimeoutException te) {
86                                 aborted = true;
87                         }
88                         if (worked) {
89                                 if (remote) {
90                                         try {
91                                                 state.set_rssi(link.rssi(), 0);
92                                         } catch (TimeoutException te) {
93                                                 state.set_rssi(0, 0);
94                                         }
95                                 }
96                         }
97                 }
98
99                 long    finish = System.currentTimeMillis();
100
101                 if (next_millis - finish < minimum_delay)
102                         next_millis = finish + minimum_delay;
103
104                 return state;
105         }
106
107         public void close(boolean interrupted) {
108                 try {
109                         link.close();
110                 } catch (InterruptedException ie) {
111                 }
112         }
113
114         public void set_frequency(double frequency) throws InterruptedException, TimeoutException {
115                 link.set_radio_frequency(frequency);
116         }
117
118         public void save_frequency() {
119                 AltosPreferences.set_frequency(link.serial, link.frequency);
120         }
121
122         public void set_callsign(String callsign) throws InterruptedException, TimeoutException {
123                 link.set_callsign(callsign);
124         }
125
126         public AltosIdleReader (AltosLink link, boolean remote)
127                 throws IOException, InterruptedException, TimeoutException {
128                 this.link = link;
129                 this.remote = remote;
130                 this.next_millis = System.currentTimeMillis();
131                 fetch = new AltosIdleFetch(link);
132         }
133 }