]> git.gag.com Git - fw/altos/blob - altoslib/AltosIdleMonitor.java
altos/lpcxpresso: Update for 11u24 board
[fw/altos] / altoslib / AltosIdleMonitor.java
1 /*
2  * Copyright © 2010 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_14;
20
21 import java.io.*;
22 import java.util.concurrent.*;
23
24
25 public class AltosIdleMonitor extends Thread {
26         AltosLink               link;
27         AltosIdleMonitorListener        listener;
28
29         AltosIdleFetch          fetch;
30
31         boolean                 remote;
32         boolean                 close_on_exit;
33         double                  frequency = AltosLib.MISSING;
34         String                  callsign;
35
36         AltosState              state;
37         AltosListenerState      listener_state;
38         AltosConfigData         config_data;
39         AltosGPS                gps;
40
41         void start_link() throws InterruptedException, TimeoutException {
42                 if (remote) {
43                         link.set_radio_frequency(frequency);
44                         link.set_callsign(callsign);
45                         link.start_remote();
46                 } else
47                         link.flush_input();
48         }
49
50         boolean stop_link() throws InterruptedException, TimeoutException {
51                 if (remote)
52                         link.stop_remote();
53                 return link.reply_abort;
54         }
55
56         boolean provide_data() throws InterruptedException, TimeoutException, AltosUnknownProduct {
57                 boolean         worked = false;
58                 boolean         aborted = false;
59
60                 try {
61                         start_link();
62                         link.config_data();
63                         if (state == null)
64                                 state = new AltosState(new AltosCalData(link.config_data()));
65                         fetch.provide_data(state);
66                         if (frequency != AltosLib.MISSING)
67                                 state.set_frequency(frequency);
68                         if (!link.has_error && !link.reply_abort)
69                                 worked = true;
70                 } finally {
71                         aborted = stop_link();
72                         if (worked) {
73                                 if (remote)
74                                         state.set_rssi(link.rssi(), 0);
75                                 listener_state.battery = link.monitor_battery();
76                         }
77                 }
78                 return aborted;
79         }
80
81         public void set_frequency(double in_frequency) {
82                 frequency = in_frequency;
83                 link.abort_reply();
84         }
85
86         public void set_callsign(String in_callsign) {
87                 callsign = in_callsign;
88                 link.abort_reply();
89         }
90
91         public void abort() throws InterruptedException {
92                 while (isAlive()) {
93                         interrupt();
94                         link.abort_reply();
95                         Thread.sleep(100);
96                 }
97                 join();
98         }
99
100         public void run() {
101                 state = null;
102                 try {
103                         for (;;) {
104                                 try {
105                                         provide_data();
106                                         listener.update(state, listener_state);
107                                 } catch (TimeoutException te) {
108                                 } catch (AltosUnknownProduct ae) {
109                                         listener.error(String.format("Unknown product \"%s\"", ae.product));
110                                 }
111                                 if (link.has_error || link.reply_abort) {
112                                         listener.failed();
113                                         break;
114                                 }
115                                 Thread.sleep(1000);
116                         }
117                 } catch (InterruptedException ie) {
118                 }
119                 if (close_on_exit) {
120                         try {
121                                 link.close();
122                         } catch (InterruptedException ie) {
123                         }
124                 }
125         }
126
127         public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote, boolean in_close_on_exit) {
128                 listener = in_listener;
129                 link = in_link;
130                 remote = in_remote;
131                 close_on_exit = in_close_on_exit;
132                 listener_state = new AltosListenerState();
133                 fetch = new AltosIdleFetch(link);
134         }
135
136         public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote) {
137                 this(in_listener, in_link, in_remote, true);
138         }
139 }
140