File: AltosIgnite.java

package info (click to toggle)
altos 1.6.8-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 63,748 kB
  • ctags: 36,577
  • sloc: ansic: 92,100; java: 36,273; makefile: 6,495; xml: 3,096; sh: 1,971; pascal: 1,581
file content (199 lines) | stat: -rw-r--r-- 4,604 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
 * Copyright © 2010 Keith Packard <keithp@keithp.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

package org.altusmetrum.altoslib_11;

import java.util.*;
import java.io.*;
import java.util.concurrent.*;

public class AltosIgnite {
	AltosLink	link;
	boolean		remote;
	boolean		close_on_exit;
	boolean		link_started;
	boolean		have_npyro = false;
	int		npyro;
	AltosConfigData	config_data;

	public final static String	None = null;
	public final static String	Apogee = "drogue";
	public final static String	Main = "main";

	public final static int	Unknown = 0;
	public final static int	Ready = 1;
	public final static int	Active = 2;
	public final static int	Open = 3;

	private void start_link() throws InterruptedException, TimeoutException {
		link_started = true;
		if (remote)
			link.start_remote();
	}

	private void stop_link() throws InterruptedException {
		if (!link_started)
			return;
		link_started = false;
		if (link == null)
			return;
		if (remote)
			link.stop_remote();
	}

	class string_ref {
		String	value;

		public String get() {
			return value;
		}
		public void set(String i) {
			value = i;
		}
		public string_ref() {
			value = null;
		}
	}

	/*
	private boolean get_string(String line, String label, string_ref s) {
		if (line.startsWith(label)) {
			String	quoted = line.substring(label.length()).trim();

			if (quoted.startsWith("\""))
				quoted = quoted.substring(1);
			if (quoted.endsWith("\""))
				quoted = quoted.substring(0,quoted.length()-1);
			s.set(quoted);
			return true;
		} else {
			return false;
		}
	}
	*/

	private int map_status(String status_name) {
		if (status_name.equals("unknown"))
			return Unknown;
		if (status_name.equals("ready"))
			return Ready;
		if (status_name.equals("active"))
			return Active;
		if (status_name.equals("open"))
			return Open;
		return Unknown;
	}

	private void get_npyro() throws InterruptedException, TimeoutException {
		if (config_data == null)
			config_data = new AltosConfigData(link);
		if (config_data != null)
			npyro = config_data.npyro;
		else
			npyro = 0;
		have_npyro = true;
	}

	public int npyro() throws InterruptedException, TimeoutException {
		if (!have_npyro) {
			start_link();
			get_npyro();
			stop_link();
		}
		return npyro;
	}

	public HashMap<String,Integer> status() throws InterruptedException, TimeoutException {
		HashMap<String,Integer> status = new HashMap<String,Integer>();

		if (link == null)
			return status;
		try {
			start_link();
			get_npyro();

			String last_igniter = Main;
			if (npyro > 0)
				last_igniter = String.format("%d", npyro - 1);

			link.printf("t\n");
			for (;;) {
				String line = link.get_reply(5000);
				if (line == null)
					throw new TimeoutException();
				String[] items = line.split("\\s+");

				if (items.length < 4)
					continue;

				if (!items[0].equals("Igniter:"))
					continue;

				if (!items[2].equals("Status:"))
					continue;

				status.put(items[1], map_status(items[3]));

				if (items[1].equals(last_igniter))
					break;
			}
		} finally {
			stop_link();
		}
		return status;
	}

	public static String status_string(int status) {
		switch (status) {
		case Unknown: return "Unknown";
		case Ready: return "Ready";
		case Active: return "Active";
		case Open: return "Open";
		default: return "Unknown";
		}
	}

	public void fire(String igniter) throws InterruptedException {
		if (link == null)
			return;
		try {
			start_link();
			link.printf("i DoIt %s\n", igniter);
		} catch (TimeoutException te) {
		} finally {
			stop_link();
		}
	}

	public void close() throws InterruptedException {
		stop_link();
		if (close_on_exit)
			link.close();
		link = null;
	}

	public AltosIgnite(AltosLink in_link, boolean in_remote, boolean in_close_on_exit) {
		link = in_link;
		remote = in_remote;
		close_on_exit = in_close_on_exit;
	}

	public AltosIgnite(AltosLink in_link, boolean in_remote) {
		this(in_link, in_remote, true);
	}
}