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
130
131
132
133
|
/*
* Copyright © 2016 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; either version 2 of the License, or
* (at your option) any later version.
*
* 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_13;
import java.text.*;
import java.io.*;
import java.util.concurrent.*;
public class AltosIdleReader extends AltosFlightReader {
AltosLink link;
boolean remote;
AltosCalData cal_data = null;
AltosState state = null;
AltosIdleFetch fetch;
long next_millis;
static final long report_interval = 5 * 1000;
static final long minimum_delay = 1 * 1000;
private void start_link() throws InterruptedException, TimeoutException {
if (remote) {
link.start_remote();
} else
link.flush_input();
}
private boolean stop_link() throws InterruptedException, TimeoutException {
if (remote)
link.stop_remote();
return link.reply_abort;
}
public AltosCalData cal_data() {
if (cal_data == null) {
try {
cal_data = new AltosCalData(link.config_data());
} catch (InterruptedException ie) {
} catch (TimeoutException te) {
}
if (cal_data == null)
cal_data = new AltosCalData();
}
return cal_data;
}
public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
boolean worked = false;
boolean aborted = false;
long delay = next_millis - System.currentTimeMillis();
if (delay > 0)
Thread.sleep(delay);
next_millis = System.currentTimeMillis() + report_interval;
try {
try {
start_link();
if (state == null)
state = new AltosState(cal_data());
fetch.provide_data(state);
if (!link.has_error && !link.reply_abort)
worked = true;
} catch (TimeoutException te) {
} catch (AltosUnknownProduct ue) {
worked = true;
}
} finally {
try {
aborted = stop_link();
} catch (TimeoutException te) {
aborted = true;
}
if (worked) {
if (remote) {
try {
state.set_rssi(link.rssi(), 0);
} catch (TimeoutException te) {
state.set_rssi(0, 0);
}
}
}
}
long finish = System.currentTimeMillis();
if (next_millis - finish < minimum_delay)
next_millis = finish + minimum_delay;
return state;
}
public void close(boolean interrupted) {
try {
link.close();
} catch (InterruptedException ie) {
}
}
public void set_frequency(double frequency) throws InterruptedException, TimeoutException {
link.set_radio_frequency(frequency);
}
public void save_frequency() {
AltosPreferences.set_frequency(link.serial, link.frequency);
}
public void set_callsign(String callsign) throws InterruptedException, TimeoutException {
link.set_callsign(callsign);
}
public AltosIdleReader (AltosLink link, boolean remote)
throws IOException, InterruptedException, TimeoutException {
this.link = link;
this.remote = remote;
this.next_millis = System.currentTimeMillis();
fetch = new AltosIdleFetch(link);
}
}
|