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