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