Keep tables together on a page
[fw/altos] / altoslib / 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 org.altusmetrum.altoslib_2;
19
20 import java.io.*;
21 import java.util.concurrent.*;
22
23 public class AltosIgnite {
24         AltosLink       link;
25         boolean         remote;
26         boolean         link_started;
27
28         public final static int None = 0;
29         public final static int Apogee = 1;
30         public final static int Main = 2;
31
32         public final static int Unknown = 0;
33         public final static int Ready = 1;
34         public final static int Active = 2;
35         public final static int Open = 3;
36
37         private void start_link() throws InterruptedException, TimeoutException {
38                 link_started = true;
39                 if (remote)
40                         link.start_remote();
41         }
42
43         private void stop_link() throws InterruptedException {
44                 if (!link_started)
45                         return;
46                 link_started = false;
47                 if (link == null)
48                         return;
49                 if (remote)
50                         link.stop_remote();
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         /*
68         private boolean get_string(String line, String label, string_ref s) {
69                 if (line.startsWith(label)) {
70                         String  quoted = line.substring(label.length()).trim();
71
72                         if (quoted.startsWith("\""))
73                                 quoted = quoted.substring(1);
74                         if (quoted.endsWith("\""))
75                                 quoted = quoted.substring(0,quoted.length()-1);
76                         s.set(quoted);
77                         return true;
78                 } else {
79                         return false;
80                 }
81         }
82         */
83
84         private int status(String status_name) {
85                 if (status_name.equals("unknown"))
86                         return Unknown;
87                 if (status_name.equals("ready"))
88                         return Ready;
89                 if (status_name.equals("active"))
90                         return Active;
91                 if (status_name.equals("open"))
92                         return Open;
93                 return Unknown;
94         }
95
96         public int status(int igniter) throws InterruptedException, TimeoutException {
97                 int status = Unknown;
98                 if (link == null)
99                         return status;
100                 //string_ref status_name = new string_ref();
101                 try {
102                         start_link();
103                         link.printf("t\n");
104                         for (;;) {
105                                 String line = link.get_reply(5000);
106                                 if (line == null)
107                                         throw new TimeoutException();
108                                 String[] items = line.split("\\s+");
109
110                                 if (items.length < 4)
111                                         continue;
112
113                                 if (!items[0].equals("Igniter:"))
114                                         continue;
115
116                                 if (!items[2].equals("Status:"))
117                                         continue;
118
119                                 if (items[1].equals("drogue")) {
120                                         if (igniter == Apogee)
121                                                 status = status(items[3]);
122                                 } else if (items[1].equals("main")) {
123                                         if (igniter == Main)
124                                                 status = status(items[3]);
125                                         break;
126                                 }
127                         }
128                 } finally {
129                         stop_link();
130                 }
131                 return status;
132         }
133
134         public static String status_string(int status) {
135                 switch (status) {
136                 case Unknown: return "Unknown";
137                 case Ready: return "Ready";
138                 case Active: return "Active";
139                 case Open: return "Open";
140                 default: return "Unknown";
141                 }
142         }
143
144         public void fire(int igniter) {
145                 if (link == null)
146                         return;
147                 try {
148                         start_link();
149                         switch (igniter) {
150                         case Main:
151                                 link.printf("i DoIt main\n");
152                                 break;
153                         case Apogee:
154                                 link.printf("i DoIt drogue\n");
155                                 break;
156                         }
157                 } catch (InterruptedException ie) {
158                 } catch (TimeoutException te) {
159                 } finally {
160                         try {
161                                 stop_link();
162                         } catch (InterruptedException ie) {
163                         }
164                 }
165         }
166
167         public void close() {
168                 try {
169                         stop_link();
170                 } catch (InterruptedException ie) {
171                 }
172                 link.close();
173                 link = null;
174         }
175
176         public AltosIgnite(AltosLink in_link, boolean in_remote)
177                 throws FileNotFoundException, TimeoutException, InterruptedException {
178
179                 link = in_link;
180                 remote = in_remote;
181         }
182 }