2 * Copyright © 2010 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_11;
22 import java.util.concurrent.*;
25 public class AltosIdleMonitor extends Thread {
27 AltosIdleMonitorListener listener;
32 boolean close_on_exit;
36 AltosListenerState listener_state;
37 AltosConfigData config_data;
40 void start_link() throws InterruptedException, TimeoutException {
42 link.set_radio_frequency(frequency);
43 link.set_callsign(callsign);
49 boolean stop_link() throws InterruptedException, TimeoutException {
52 return link.reply_abort;
55 boolean provide_data(AltosDataListener listener) throws InterruptedException, TimeoutException, AltosUnknownProduct {
56 boolean worked = false;
57 boolean aborted = false;
58 AltosCalData cal_data = new AltosCalData(link.config_data());
62 fetch.provide_data(listener, cal_data);
63 if (!link.has_error && !link.reply_abort)
66 aborted = stop_link();
69 listener.set_rssi(link.rssi(), 0);
70 listener_state.battery = link.monitor_battery();
76 public void set_frequency(double in_frequency) {
77 frequency = in_frequency;
81 public void set_callsign(String in_callsign) {
82 callsign = in_callsign;
86 public void abort() throws InterruptedException {
96 AltosState state = null;
102 state = new AltosState(new AltosCalData(link.config_data()));
104 listener.update(state, listener_state);
105 } catch (TimeoutException te) {
106 } catch (AltosUnknownProduct ae) {
107 listener.error(String.format("Unknown product \"%s\"", ae.product));
109 if (link.has_error || link.reply_abort) {
115 } catch (InterruptedException ie) {
120 } catch (InterruptedException ie) {
125 public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote, boolean in_close_on_exit) {
126 listener = in_listener;
129 close_on_exit = in_close_on_exit;
130 listener_state = new AltosListenerState();
131 fetch = new AltosIdleFetch(link);
134 public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote) {
135 this(in_listener, in_link, in_remote, true);