]> git.gag.com Git - fw/altos/blob - altosui/AltosLaunch.java
altos: Support normalized log
[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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package altosui;
20
21 import java.io.*;
22 import java.util.concurrent.*;
23 import java.awt.*;
24 import org.altusmetrum.altosuilib_14.*;
25
26 public class AltosLaunch {
27         AltosDevice     device;
28         AltosSerial     serial;
29         boolean         serial_started;
30         int             launcher_serial;
31         int             launcher_channel;
32         int             rssi;
33
34         final static int        Unknown = -1;
35         final static int        Good = 0;
36         final static int        Bad = 1;
37
38         int             armed;
39         int             igniter;
40
41         private void start_serial() throws InterruptedException {
42                 serial_started = true;
43         }
44
45         private void stop_serial() throws InterruptedException {
46                 if (!serial_started)
47                         return;
48                 serial_started = false;
49                 if (serial == null)
50                         return;
51         }
52
53         class string_ref {
54                 String  value;
55
56                 public String get() {
57                         return value;
58                 }
59                 public void set(String i) {
60                         value = i;
61                 }
62                 public string_ref() {
63                         value = null;
64                 }
65         }
66
67         private boolean get_string(String line, String label, string_ref s) {
68                 if (line.startsWith(label)) {
69                         String  quoted = line.substring(label.length()).trim();
70
71                         if (quoted.startsWith("\""))
72                                 quoted = quoted.substring(1);
73                         if (quoted.endsWith("\""))
74                                 quoted = quoted.substring(0,quoted.length()-1);
75                         s.set(quoted);
76                         return true;
77                 } else {
78                         return false;
79                 }
80         }
81
82         public boolean status() throws InterruptedException, TimeoutException {
83                 boolean ok = false;
84                 if (serial == null)
85                         return false;
86                 string_ref status_name = new string_ref();
87                 start_serial();
88                 serial.printf("l %d %d\n", launcher_serial, launcher_channel);
89                 for (;;) {
90                         String line = serial.get_reply(20000);
91                         if (line == null)
92                                 throw new TimeoutException();
93                         if (get_string(line, "Rssi: ", status_name)) {
94                                 try {
95                                         rssi = (int) Altos.fromdec(status_name.get());
96                                 } catch (NumberFormatException ne) {
97                                 }
98                                 break;
99                         } else if (get_string(line, "Armed: ", status_name)) {
100                                 armed = Good;
101                                 String status = status_name.get();
102                                 if (status.startsWith("igniter good"))
103                                         igniter = Good;
104                                 else if (status.startsWith("igniter bad"))
105                                         igniter = Bad;
106                                 else
107                                         igniter = Unknown;
108                                 ok = true;
109                         } else if (get_string(line, "Disarmed: ", status_name)) {
110                                 armed = Bad;
111                                 if (status_name.get().startsWith("igniter good"))
112                                         igniter = Good;
113                                 else if (status_name.get().startsWith("igniter bad"))
114                                         igniter = Bad;
115                                 else
116                                         igniter = Unknown;
117                                 ok = true;
118                         } else if (get_string(line, "Error ", status_name)) {
119                                 armed = Unknown;
120                                 igniter = Unknown;
121                                 ok = false;
122                                 break;
123                         }
124                 }
125                 stop_serial();
126                 if (!ok) {
127                         armed = Unknown;
128                         igniter = Unknown;
129                 }
130                 return ok;
131         }
132
133         public static String status_string(int status) {
134                 switch (status) {
135                 case Good:
136                         return "good";
137                 case Bad:
138                         return "open";
139                 }
140                 return "unknown";
141         }
142
143         public void arm() {
144                 if (serial == null)
145                         return;
146                 try {
147                         start_serial();
148                         serial.printf("a %d %d\n", launcher_serial, launcher_channel);
149                         serial.flush_output();
150                 } catch (InterruptedException ie) {
151                 } finally {
152                         try {
153                                 stop_serial();
154                         } catch (InterruptedException ie) {
155                         }
156                 }
157         }
158
159         public void fire() {
160                 if (serial == null)
161                         return;
162                 try {
163                         start_serial();
164                         serial.printf("i %d %d\n", launcher_serial, launcher_channel);
165                         serial.flush_output();
166                 } catch (InterruptedException ie) {
167                 } finally {
168                         try {
169                                 stop_serial();
170                         } catch (InterruptedException ie) {
171                         }
172                 }
173         }
174
175         public void close() {
176                 try {
177                         stop_serial();
178                 } catch (InterruptedException ie) {
179                 }
180                 serial.close();
181                 serial = null;
182         }
183
184         public void set_frame(Frame frame) {
185                 serial.set_frame(frame);
186         }
187
188         public void set_remote(int in_serial, int in_channel) {
189                 launcher_serial = in_serial;
190                 launcher_channel = in_channel;
191         }
192
193         public AltosLaunch(AltosDevice in_device) throws FileNotFoundException, AltosSerialInUseException {
194
195                 device = in_device;
196                 serial = new AltosSerial(device);
197         }
198 }