altos: Complain about sensor self-test errors only in idle mode
[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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.altoslib_3;
19
20 import java.io.*;
21 import java.util.concurrent.*;
22
23
24 public class AltosIdleMonitor extends Thread {
25         AltosLink               link;
26         AltosIdleMonitorListener        listener;
27
28         AltosIdleFetch          fetch;
29
30         boolean                 remote;
31         double                  frequency;
32         String                  callsign;
33
34         AltosListenerState      listener_state;
35         AltosConfigData         config_data;
36         AltosGPS                gps;
37
38         int AltosRSSI() throws TimeoutException, InterruptedException {
39                 link.printf("s\n");
40                 String line = link.get_reply_no_dialog(5000);
41                 if (line == null)
42                         throw new TimeoutException();
43                 String[] items = line.split("\\s+");
44                 if (items.length < 2)
45                         return 0;
46                 if (!items[0].equals("RSSI:"))
47                         return 0;
48                 int rssi = Integer.parseInt(items[1]);
49                 return rssi;
50         }
51
52         void start_link() throws InterruptedException, TimeoutException {
53                 if (remote) {
54                         link.set_radio_frequency(frequency);
55                         link.set_callsign(callsign);
56                         link.start_remote();
57                 } else
58                         link.flush_input();
59         }
60
61         void stop_link() throws InterruptedException, TimeoutException {
62                 if (remote)
63                         link.stop_remote();
64         }
65
66         void update_state(AltosState state) throws InterruptedException, TimeoutException {
67                 boolean         worked = false;
68
69                 try {
70                         start_link();
71                         fetch.update_state(state);
72                         worked = true;
73                 } finally {
74                         stop_link();
75                         if (worked) {
76                                 if (remote)
77                                         state.set_rssi(link.rssi(), 0);
78                                 listener_state.battery = link.monitor_battery();
79                         }
80                 }
81         }
82
83         public void set_frequency(double in_frequency) {
84                 frequency = in_frequency;
85                 link.abort_reply();
86         }
87
88         public void set_callsign(String in_callsign) {
89                 callsign = in_callsign;
90                 link.abort_reply();
91         }
92
93         public void abort() throws InterruptedException {
94                 while (isAlive()) {
95                         interrupt();
96                         link.abort_reply();
97                         Thread.sleep(100);
98                 }
99                 join();
100         }
101
102         public void run() {
103                 AltosState state = new AltosState();
104                 try {
105                         while (!link.has_error) {
106                                 try {
107                                         link.config_data();
108                                         update_state(state);
109                                         listener.update(state, listener_state);
110                                 } catch (TimeoutException te) {
111                                 }
112                                 Thread.sleep(1000);
113                         }
114                 } catch (InterruptedException ie) {
115                 }
116                 try {
117                         link.close();
118                 } catch (InterruptedException ie) {
119                 }
120         }
121
122         public AltosIdleMonitor(AltosIdleMonitorListener in_listener, AltosLink in_link, boolean in_remote)
123                 throws FileNotFoundException, InterruptedException, TimeoutException {
124                 listener = in_listener;
125                 link = in_link;
126                 remote = in_remote;
127                 listener_state = new AltosListenerState();
128                 fetch = new AltosIdleFetch(link);
129         }
130 }