altosui: Make deployment testing handle Connecting... dialog
[fw/altos] / altosui / AltosIgnite.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
29 public class AltosIgnite {
30         AltosDevice     device;
31         AltosSerial     serial;
32         boolean         remote;
33         boolean         serial_started;
34         final static int        None = 0;
35         final static int        Apogee = 1;
36         final static int        Main = 2;
37
38         final static int        Unknown = 0;
39         final static int        Ready = 1;
40         final static int        Active = 2;
41         final static int        Open = 3;
42
43         private void start_serial() throws InterruptedException {
44                 serial_started = true;
45                 if (remote)
46                         serial.start_remote();
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                 if (remote)
56                         serial.stop_remote();
57         }
58
59         class string_ref {
60                 String  value;
61
62                 public String get() {
63                         return value;
64                 }
65                 public void set(String i) {
66                         value = i;
67                 }
68                 public string_ref() {
69                         value = null;
70                 }
71         }
72
73         private boolean get_string(String line, String label, string_ref s) {
74                 if (line.startsWith(label)) {
75                         String  quoted = line.substring(label.length()).trim();
76
77                         if (quoted.startsWith("\""))
78                                 quoted = quoted.substring(1);
79                         if (quoted.endsWith("\""))
80                                 quoted = quoted.substring(0,quoted.length()-1);
81                         s.set(quoted);
82                         return true;
83                 } else {
84                         return false;
85                 }
86         }
87
88         private int status(String status_name) {
89                 if (status_name.equals("unknown"))
90                         return Unknown;
91                 if (status_name.equals("ready"))
92                         return Ready;
93                 if (status_name.equals("active"))
94                         return Active;
95                 if (status_name.equals("open"))
96                         return Open;
97                 return Unknown;
98         }
99
100         public int status(int igniter) throws InterruptedException, TimeoutException {
101                 int status = Unknown;
102                 if (serial == null)
103                         return status;
104                 string_ref status_name = new string_ref();
105                 start_serial();
106                 serial.printf("t\n");
107                 for (;;) {
108                         String line = serial.get_reply(5000);
109                         if (line == null)
110                                 throw new TimeoutException();
111                         if (get_string(line, "Igniter: drogue Status: ", status_name))
112                                 if (igniter == Apogee)
113                                         status = status(status_name.get());
114                         if (get_string(line, "Igniter:   main Status: ", status_name)) {
115                                 if (igniter == Main)
116                                         status = status(status_name.get());
117                                 break;
118                         }
119                 }
120                 stop_serial();
121                 return status;
122         }
123
124         public static String status_string(int status) {
125                 switch (status) {
126                 case Unknown: return "Unknown";
127                 case Ready: return "Ready";
128                 case Active: return "Active";
129                 case Open: return "Open";
130                 default: return "Unknown";
131                 }
132         }
133
134         public void fire(int igniter) {
135                 if (serial == null)
136                         return;
137                 try {
138                         start_serial();
139                         switch (igniter) {
140                         case Main:
141                                 serial.printf("i DoIt main\n");
142                                 break;
143                         case Apogee:
144                                 serial.printf("i DoIt drogue\n");
145                                 break;
146                         }
147                 } catch (InterruptedException ie) {
148                 } finally {
149                         try {
150                                 stop_serial();
151                         } catch (InterruptedException ie) {
152                         }
153                 }
154         }
155
156         public void close() {
157                 try {
158                         stop_serial();
159                 } catch (InterruptedException ie) {
160                 }
161                 serial.close();
162                 serial = null;
163         }
164
165         public void set_frame(Frame frame) {
166                 serial.set_frame(frame);
167         }
168
169         public AltosIgnite(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
170
171                 device = in_device;
172                 serial = new AltosSerial(device);
173                 remote = false;
174
175                 if (!device.matchProduct(AltosDevice.product_telemetrum))
176                         remote = true;
177         }
178 }