altoslib: comment out un-used variables
[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;
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         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         private int status(String status_name) {
83                 if (status_name.equals("unknown"))
84                         return Unknown;
85                 if (status_name.equals("ready"))
86                         return Ready;
87                 if (status_name.equals("active"))
88                         return Active;
89                 if (status_name.equals("open"))
90                         return Open;
91                 return Unknown;
92         }
93
94         public int status(int igniter) throws InterruptedException, TimeoutException {
95                 int status = Unknown;
96                 if (link == null)
97                         return status;
98                 //string_ref status_name = new string_ref();
99                 try {
100                         start_link();
101                         link.printf("t\n");
102                         for (;;) {
103                                 String line = link.get_reply(5000);
104                                 if (line == null)
105                                         throw new TimeoutException();
106                                 String[] items = line.split("\\s+");
107
108                                 if (items.length < 4)
109                                         continue;
110
111                                 if (!items[0].equals("Igniter:"))
112                                         continue;
113
114                                 if (!items[2].equals("Status:"))
115                                         continue;
116
117                                 if (items[1].equals("drogue")) {
118                                         if (igniter == Apogee)
119                                                 status = status(items[3]);
120                                 } else if (items[1].equals("main")) {
121                                         if (igniter == Main)
122                                                 status = status(items[3]);
123                                         break;
124                                 }
125                         }
126                 } finally {
127                         stop_link();
128                 }
129                 return status;
130         }
131
132         public static String status_string(int status) {
133                 switch (status) {
134                 case Unknown: return "Unknown";
135                 case Ready: return "Ready";
136                 case Active: return "Active";
137                 case Open: return "Open";
138                 default: return "Unknown";
139                 }
140         }
141
142         public void fire(int igniter) {
143                 if (link == null)
144                         return;
145                 try {
146                         start_link();
147                         switch (igniter) {
148                         case Main:
149                                 link.printf("i DoIt main\n");
150                                 break;
151                         case Apogee:
152                                 link.printf("i DoIt drogue\n");
153                                 break;
154                         }
155                 } catch (InterruptedException ie) {
156                 } catch (TimeoutException te) {
157                 } finally {
158                         try {
159                                 stop_link();
160                         } catch (InterruptedException ie) {
161                         }
162                 }
163         }
164
165         public void close() {
166                 try {
167                         stop_link();
168                 } catch (InterruptedException ie) {
169                 }
170                 link.close();
171                 link = null;
172         }
173
174         public AltosIgnite(AltosLink in_link, boolean in_remote)
175                 throws FileNotFoundException, TimeoutException, InterruptedException {
176
177                 link = in_link;
178                 remote = in_remote;
179         }
180 }