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