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