Changed package name from altosui to AltosUI
[fw/altos] / altosui / AltosLaunch.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 AltosUI;
19
20 import java.io.*;
21 import java.util.concurrent.*;
22 import java.awt.*;
23 import java.awt.event.*;
24 import javax.swing.*;
25 import javax.swing.filechooser.FileNameExtensionFilter;
26 import javax.swing.table.*;
27 import javax.swing.event.*;
28 import org.altusmetrum.AltosLib.*;
29
30 public class AltosLaunch {
31         AltosDevice     device;
32         AltosSerial     serial;
33         boolean         serial_started;
34         int             launcher_serial;
35         int             launcher_channel;
36         int             rssi;
37
38         final static int        Unknown = -1;
39         final static int        Good = 0;
40         final static int        Bad = 1;
41
42         int             armed;
43         int             igniter;
44
45         private void start_serial() throws InterruptedException {
46                 serial_started = true;
47         }
48
49         private void stop_serial() throws InterruptedException {
50                 if (!serial_started)
51                         return;
52                 serial_started = false;
53                 if (serial == null)
54                         return;
55         }
56
57         class string_ref {
58                 String  value;
59
60                 public String get() {
61                         return value;
62                 }
63                 public void set(String i) {
64                         value = i;
65                 }
66                 public string_ref() {
67                         value = null;
68                 }
69         }
70
71         private boolean get_string(String line, String label, string_ref s) {
72                 if (line.startsWith(label)) {
73                         String  quoted = line.substring(label.length()).trim();
74
75                         if (quoted.startsWith("\""))
76                                 quoted = quoted.substring(1);
77                         if (quoted.endsWith("\""))
78                                 quoted = quoted.substring(0,quoted.length()-1);
79                         s.set(quoted);
80                         return true;
81                 } else {
82                         return false;
83                 }
84         }
85
86         public boolean status() throws InterruptedException, TimeoutException {
87                 boolean ok = false;
88                 if (serial == null)
89                         return false;
90                 string_ref status_name = new string_ref();
91                 start_serial();
92                 serial.printf("l %d %d\n", launcher_serial, launcher_channel);
93                 for (;;) {
94                         String line = serial.get_reply(20000);
95                         if (line == null)
96                                 throw new TimeoutException();
97                         if (get_string(line, "Rssi: ", status_name)) {
98                                 try {
99                                         rssi = Altos.fromdec(status_name.get());
100                                 } catch (NumberFormatException ne) {
101                                 }
102                                 break;
103                         } else if (get_string(line, "Armed: ", status_name)) {
104                                 armed = Good;
105                                 String status = status_name.get();
106                                 if (status.startsWith("igniter good"))
107                                         igniter = Good;
108                                 else if (status.startsWith("igniter bad"))
109                                         igniter = Bad;
110                                 else
111                                         igniter = Unknown;
112                                 ok = true;
113                         } else if (get_string(line, "Disarmed: ", status_name)) {
114                                 armed = Bad;
115                                 if (status_name.get().startsWith("igniter good"))
116                                         igniter = Good;
117                                 else if (status_name.get().startsWith("igniter bad"))
118                                         igniter = Bad;
119                                 else
120                                         igniter = Unknown;
121                                 ok = true;
122                         } else if (get_string(line, "Error ", status_name)) {
123                                 armed = Unknown;
124                                 igniter = Unknown;
125                                 ok = false;
126                                 break;
127                         }
128                 }
129                 stop_serial();
130                 if (!ok) {
131                         armed = Unknown;
132                         igniter = Unknown;
133                 }
134                 return ok;
135         }
136
137         public static String status_string(int status) {
138                 switch (status) {
139                 case Good:
140                         return "good";
141                 case Bad:
142                         return "open";
143                 }
144                 return "unknown";
145         }
146
147         public void arm() {
148                 if (serial == null)
149                         return;
150                 try {
151                         start_serial();
152                         serial.printf("a %d %d\n", launcher_serial, launcher_channel);
153                         serial.flush_output();
154                 } catch (InterruptedException ie) {
155                 } finally {
156                         try {
157                                 stop_serial();
158                         } catch (InterruptedException ie) {
159                         }
160                 }
161         }
162
163         public void fire() {
164                 if (serial == null)
165                         return;
166                 try {
167                         start_serial();
168                         serial.printf("i %d %d\n", launcher_serial, launcher_channel);
169                         serial.flush_output();
170                 } catch (InterruptedException ie) {
171                 } finally {
172                         try {
173                                 stop_serial();
174                         } catch (InterruptedException ie) {
175                         }
176                 }
177         }
178
179         public void close() {
180                 try {
181                         stop_serial();
182                 } catch (InterruptedException ie) {
183                 }
184                 serial.close();
185                 serial = null;
186         }
187
188         public void set_frame(Frame frame) {
189                 serial.set_frame(frame);
190         }
191
192         public void set_remote(int in_serial, int in_channel) {
193                 launcher_serial = in_serial;
194                 launcher_channel = in_channel;
195         }
196
197         public AltosLaunch(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
198
199                 device = in_device;
200                 serial = new AltosSerial(device);
201         }
202 }