2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 package org.altusmetrum.altoslib_12;
23 import java.util.concurrent.*;
25 public class AltosIdleReader extends AltosFlightReader {
28 AltosCalData cal_data = null;
29 AltosState state = null;
32 static final long report_interval = 5 * 1000;
33 static final long minimum_delay = 1 * 1000;
35 private void start_link() throws InterruptedException, TimeoutException {
42 private boolean stop_link() throws InterruptedException, TimeoutException {
45 return link.reply_abort;
48 public AltosCalData cal_data() {
49 if (cal_data == null) {
51 cal_data = new AltosCalData(link.config_data());
52 } catch (InterruptedException ie) {
53 } catch (TimeoutException te) {
56 cal_data = new AltosCalData();
61 public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
62 boolean worked = false;
63 boolean aborted = false;
65 long delay = next_millis - System.currentTimeMillis();
69 next_millis = System.currentTimeMillis() + report_interval;
74 state = new AltosState(cal_data());
75 fetch.provide_data(state);
76 if (!link.has_error && !link.reply_abort)
78 } catch (TimeoutException te) {
79 } catch (AltosUnknownProduct ue) {
84 aborted = stop_link();
85 } catch (TimeoutException te) {
91 state.set_rssi(link.rssi(), 0);
92 } catch (TimeoutException te) {
99 long finish = System.currentTimeMillis();
101 if (next_millis - finish < minimum_delay)
102 next_millis = finish + minimum_delay;
107 public void close(boolean interrupted) {
110 } catch (InterruptedException ie) {
114 public void set_frequency(double frequency) throws InterruptedException, TimeoutException {
115 link.set_radio_frequency(frequency);
118 public void save_frequency() {
119 AltosPreferences.set_frequency(link.serial, link.frequency);
122 public void set_callsign(String callsign) throws InterruptedException, TimeoutException {
123 link.set_callsign(callsign);
126 public AltosIdleReader (AltosLink link, boolean remote)
127 throws IOException, InterruptedException, TimeoutException {
129 this.remote = remote;
130 this.next_millis = System.currentTimeMillis();
131 fetch = new AltosIdleFetch(link);