altoslib: Add tilt and pyro data to CSV export
[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_12;
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;
34         String                  callsign;
35
36         AltosListenerState      listener_state;
37         AltosConfigData         config_data;
38         AltosGPS                gps;
39
40         void start_link() throws InterruptedException, TimeoutException {
41                 if (remote) {
42                         link.set_radio_frequency(frequency);
43                         link.set_callsign(callsign);
44                         link.start_remote();
45                 } else
46                         link.flush_input();
47         }
48
49         boolean stop_link() throws InterruptedException, TimeoutException {
50                 if (remote)
51                         link.stop_remote();
52                 return link.reply_abort;
53         }
54
55         boolean provide_data(AltosDataListener listener) throws InterruptedException, TimeoutException, AltosUnknownProduct {
56                 boolean         worked = false;
57                 boolean         aborted = false;
58
59                 try {
60                         start_link();
61                         fetch.provide_data(listener);
62                         if (!link.has_error && !link.reply_abort)
63                                 worked = true;
64                 } finally {
65                         aborted = stop_link();
66                         if (worked) {
67                                 if (remote)
68                                         listener.set_rssi(link.rssi(), 0);
69                                 listener_state.battery = link.monitor_battery();
70                         }
71                 }
72                 return aborted;
73         }
74
75         public void set_frequency(double in_frequency) {
76                 frequency = in_frequency;
77                 link.abort_reply();
78         }
79
80         public void set_callsign(String in_callsign) {
81                 callsign = in_callsign;
82                 link.abort_reply();
83         }
84
85         public void abort() throws InterruptedException {
86                 while (isAlive()) {
87                         interrupt();
88                         link.abort_reply();
89                         Thread.sleep(100);
90                 }
91                 join();
92         }
93
94         public void run() {
95                 AltosState state = null;
96                 try {
97                         for (;;) {
98                                 try {
99                                         link.config_data();
100                                         if (state == null)
101                                                 state = new AltosState(new AltosCalData(link.config_data()));
102                                         provide_data(state);
103                                         listener.update(state, listener_state);
104                                 } catch (TimeoutException te) {
105                                 } catch (AltosUnknownProduct ae) {
106                                         listener.error(String.format("Unknown product \"%s\"", ae.product));
107                                 }
108                                 if (link.has_error || link.reply_abort) {
109                                         listener.failed();
110                                         break;
111                                 }
112                                 Thread.sleep(1000);
113                         }
114                 } catch (InterruptedException ie) {
115                 }
116                 if (close_on_exit) {
117                         try {
118                                 link.close();
119                         } catch (InterruptedException ie) {
120                         }
121                 }
122         }
123
124         public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote, boolean in_close_on_exit) {
125                 listener = in_listener;
126                 link = in_link;
127                 remote = in_remote;
128                 close_on_exit = in_close_on_exit;
129                 listener_state = new AltosListenerState();
130                 fetch = new AltosIdleFetch(link);
131         }
132
133         public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote) {
134                 this(in_listener, in_link, in_remote, true);
135         }
136 }
137