altoslib: Add AltosIdleReader
[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; 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_11;
19
20 import java.text.*;
21 import java.io.*;
22 import java.util.concurrent.*;
23
24 public class AltosIdleReader extends AltosFlightReader {
25         AltosLink       link;
26         boolean         remote;
27         AltosState      state = null;
28         AltosIdleFetch  fetch;
29         long            next_millis;
30         static final long       report_interval = 5 * 1000;
31         static final long       minimum_delay = 1 * 1000;
32
33         private void start_link() throws InterruptedException, TimeoutException {
34                 if (remote) {
35                         link.start_remote();
36                 } else
37                         link.flush_input();
38         }
39
40         private boolean stop_link() throws InterruptedException, TimeoutException {
41                 if (remote)
42                         link.stop_remote();
43                 return link.reply_abort;
44         }
45
46         public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
47                 boolean worked = false;
48                 boolean aborted = false;
49
50                 if (state == null)
51                         state = new AltosState();
52                 else
53                         state = state.clone();
54
55                 long    delay = next_millis - System.currentTimeMillis();
56
57                 if (delay > 0)
58                         Thread.sleep(delay);
59                 next_millis = System.currentTimeMillis() + report_interval;
60                 try {
61                         try {
62                                 start_link();
63                                 fetch.update_state(state);
64                                 if (!link.has_error && !link.reply_abort)
65                                         worked = true;
66                         } catch (TimeoutException te) {
67                         } catch (AltosUnknownProduct ue) {
68                                 worked = true;
69                         }
70                 } finally {
71                         try {
72                                 aborted = stop_link();
73                         } catch (TimeoutException te) {
74                                 aborted = true;
75                         }
76                         if (worked) {
77                                 if (remote) {
78                                         try {
79                                                 state.set_rssi(link.rssi(), 0);
80                                         } catch (TimeoutException te) {
81                                                 state.set_rssi(0, 0);
82                                         }
83                                 }
84                         }
85                 }
86
87                 long    finish = System.currentTimeMillis();
88
89                 if (next_millis - finish < minimum_delay)
90                         next_millis = finish + minimum_delay;
91
92                 return state;
93         }
94
95         public void close(boolean interrupted) {
96                 try {
97                         link.close();
98                 } catch (InterruptedException ie) {
99                 }
100         }
101
102         public void set_frequency(double frequency) throws InterruptedException, TimeoutException {
103                 link.set_radio_frequency(frequency);
104         }
105
106         public void save_frequency() {
107                 AltosPreferences.set_frequency(link.serial, link.frequency);
108         }
109
110         public void set_callsign(String callsign) throws InterruptedException, TimeoutException {
111                 link.set_callsign(callsign);
112         }
113
114         public AltosIdleReader (AltosLink link, boolean remote)
115                 throws IOException, InterruptedException, TimeoutException {
116                 this.link = link;
117                 this.remote = remote;
118                 this.next_millis = System.currentTimeMillis();
119                 fetch = new AltosIdleFetch(link);
120         }
121 }