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