altoslib: Make sure AltosFlightSeries is filled in before use
[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                 long    delay = next_millis - System.currentTimeMillis();
52
53                 if (delay > 0)
54                         Thread.sleep(delay);
55                 next_millis = System.currentTimeMillis() + report_interval;
56                 try {
57                         try {
58                                 start_link();
59                                 if (state == null)
60                                         state = new AltosState(new AltosCalData(link.config_data()));
61                                 fetch.provide_data(state, state.cal_data);
62                                 if (!link.has_error && !link.reply_abort)
63                                         worked = true;
64                         } catch (TimeoutException te) {
65                         } catch (AltosUnknownProduct ue) {
66                                 worked = true;
67                         }
68                 } finally {
69                         try {
70                                 aborted = stop_link();
71                         } catch (TimeoutException te) {
72                                 aborted = true;
73                         }
74                         if (worked) {
75                                 if (remote) {
76                                         try {
77                                                 state.set_rssi(link.rssi(), 0);
78                                         } catch (TimeoutException te) {
79                                                 state.set_rssi(0, 0);
80                                         }
81                                 }
82                         }
83                 }
84
85                 long    finish = System.currentTimeMillis();
86
87                 if (next_millis - finish < minimum_delay)
88                         next_millis = finish + minimum_delay;
89
90                 return state;
91         }
92
93         public void close(boolean interrupted) {
94                 try {
95                         link.close();
96                 } catch (InterruptedException ie) {
97                 }
98         }
99
100         public void set_frequency(double frequency) throws InterruptedException, TimeoutException {
101                 link.set_radio_frequency(frequency);
102         }
103
104         public void save_frequency() {
105                 AltosPreferences.set_frequency(link.serial, link.frequency);
106         }
107
108         public void set_callsign(String callsign) throws InterruptedException, TimeoutException {
109                 link.set_callsign(callsign);
110         }
111
112         public AltosIdleReader (AltosLink link, boolean remote)
113                 throws IOException, InterruptedException, TimeoutException {
114                 this.link = link;
115                 this.remote = remote;
116                 this.next_millis = System.currentTimeMillis();
117                 fetch = new AltosIdleFetch(link);
118         }
119 }