submodule madness
[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_9;
19
20 import java.util.*;
21 import java.io.*;
22 import java.util.concurrent.*;
23
24 public class AltosIgnite {
25         AltosLink       link;
26         boolean         remote;
27         boolean         link_started;
28         boolean         have_npyro = false;
29         int             npyro;
30         AltosConfigData config_data;
31
32         public final static String      None = null;
33         public final static String      Apogee = "drogue";
34         public final static String      Main = "main";
35
36         public final static int Unknown = 0;
37         public final static int Ready = 1;
38         public final static int Active = 2;
39         public final static int Open = 3;
40
41         private void start_link() throws InterruptedException, TimeoutException {
42                 link_started = true;
43                 if (remote)
44                         link.start_remote();
45         }
46
47         private void stop_link() throws InterruptedException {
48                 if (!link_started)
49                         return;
50                 link_started = false;
51                 if (link == null)
52                         return;
53                 if (remote)
54                         link.stop_remote();
55         }
56
57         class string_ref {
58                 String  value;
59
60                 public String get() {
61                         return value;
62                 }
63                 public void set(String i) {
64                         value = i;
65                 }
66                 public string_ref() {
67                         value = null;
68                 }
69         }
70
71         /*
72         private boolean get_string(String line, String label, string_ref s) {
73                 if (line.startsWith(label)) {
74                         String  quoted = line.substring(label.length()).trim();
75
76                         if (quoted.startsWith("\""))
77                                 quoted = quoted.substring(1);
78                         if (quoted.endsWith("\""))
79                                 quoted = quoted.substring(0,quoted.length()-1);
80                         s.set(quoted);
81                         return true;
82                 } else {
83                         return false;
84                 }
85         }
86         */
87
88         private int map_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         private void get_npyro() throws InterruptedException, TimeoutException {
101                 if (config_data == null)
102                         config_data = new AltosConfigData(link);
103                 if (config_data != null)
104                         npyro = config_data.npyro;
105                 else
106                         npyro = 0;
107                 have_npyro = true;
108         }
109
110         public int npyro() throws InterruptedException, TimeoutException {
111                 if (!have_npyro) {
112                         start_link();
113                         get_npyro();
114                         stop_link();
115                 }
116                 return npyro;
117         }
118
119         public HashMap<String,Integer> status() throws InterruptedException, TimeoutException {
120                 HashMap<String,Integer> status = new HashMap<String,Integer>();
121
122                 if (link == null)
123                         return status;
124                 try {
125                         start_link();
126                         get_npyro();
127
128                         String last_igniter = Main;
129                         if (npyro > 0)
130                                 last_igniter = String.format("%d", npyro - 1);
131
132                         link.printf("t\n");
133                         for (;;) {
134                                 String line = link.get_reply(5000);
135                                 if (line == null)
136                                         throw new TimeoutException();
137                                 String[] items = line.split("\\s+");
138
139                                 if (items.length < 4)
140                                         continue;
141
142                                 if (!items[0].equals("Igniter:"))
143                                         continue;
144
145                                 if (!items[2].equals("Status:"))
146                                         continue;
147
148                                 status.put(items[1], map_status(items[3]));
149
150                                 if (items[1].equals(last_igniter))
151                                         break;
152                         }
153                 } finally {
154                         stop_link();
155                 }
156                 return status;
157         }
158
159         public static String status_string(int status) {
160                 switch (status) {
161                 case Unknown: return "Unknown";
162                 case Ready: return "Ready";
163                 case Active: return "Active";
164                 case Open: return "Open";
165                 default: return "Unknown";
166                 }
167         }
168
169         public void fire(String igniter) throws InterruptedException {
170                 if (link == null)
171                         return;
172                 try {
173                         start_link();
174                         link.printf("i DoIt %s\n", igniter);
175                 } catch (TimeoutException te) {
176                 } finally {
177                         stop_link();
178                 }
179         }
180
181         public void close() throws InterruptedException {
182                 stop_link();
183                 link.close();
184                 link = null;
185         }
186
187         public AltosIgnite(AltosLink in_link, boolean in_remote)
188                 throws FileNotFoundException, TimeoutException, InterruptedException {
189
190                 link = in_link;
191                 remote = in_remote;
192         }
193 }