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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* 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 AltosIgnite {
AltosLink link;
boolean remote;
boolean link_started;
public final static int None = 0;
public final static int Apogee = 1;
public final static int Main = 2;
public final static int Unknown = 0;
public final static int Ready = 1;
public final static int Active = 2;
public final static int Open = 3;
private void start_link() throws InterruptedException, TimeoutException {
link_started = true;
if (remote)
link.start_remote();
}
private void stop_link() throws InterruptedException {
if (!link_started)
return;
link_started = false;
if (link == null)
return;
if (remote)
link.stop_remote();
}
class string_ref {
String value;
public String get() {
return value;
}
public void set(String i) {
value = i;
}
public string_ref() {
value = null;
}
}
/*
private boolean get_string(String line, String label, string_ref s) {
if (line.startsWith(label)) {
String quoted = line.substring(label.length()).trim();
if (quoted.startsWith("\""))
quoted = quoted.substring(1);
if (quoted.endsWith("\""))
quoted = quoted.substring(0,quoted.length()-1);
s.set(quoted);
return true;
} else {
return false;
}
}
*/
private int status(String status_name) {
if (status_name.equals("unknown"))
return Unknown;
if (status_name.equals("ready"))
return Ready;
if (status_name.equals("active"))
return Active;
if (status_name.equals("open"))
return Open;
return Unknown;
}
public int status(int igniter) throws InterruptedException, TimeoutException {
int status = Unknown;
if (link == null)
return status;
//string_ref status_name = new string_ref();
try {
start_link();
link.printf("t\n");
for (;;) {
String line = link.get_reply(5000);
if (line == null)
throw new TimeoutException();
String[] items = line.split("\\s+");
if (items.length < 4)
continue;
if (!items[0].equals("Igniter:"))
continue;
if (!items[2].equals("Status:"))
continue;
if (items[1].equals("drogue")) {
if (igniter == Apogee)
status = status(items[3]);
} else if (items[1].equals("main")) {
if (igniter == Main)
status = status(items[3]);
break;
}
}
} finally {
stop_link();
}
return status;
}
public static String status_string(int status) {
switch (status) {
case Unknown: return "Unknown";
case Ready: return "Ready";
case Active: return "Active";
case Open: return "Open";
default: return "Unknown";
}
}
public void fire(int igniter) throws InterruptedException {
if (link == null)
return;
try {
start_link();
switch (igniter) {
case Main:
link.printf("i DoIt main\n");
break;
case Apogee:
link.printf("i DoIt drogue\n");
break;
}
} catch (TimeoutException te) {
} finally {
stop_link();
}
}
public void close() throws InterruptedException {
stop_link();
link.close();
link = null;
}
public AltosIgnite(AltosLink in_link, boolean in_remote)
throws FileNotFoundException, TimeoutException, InterruptedException {
link = in_link;
remote = in_remote;
}
}
|