2 * Copyright © 2010 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 import java.awt.event.*;
25 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_2.*;
28 public class AltosEepromDownload implements Runnable {
31 AltosSerial serial_line;
34 AltosEepromMonitor monitor;
37 FileWriter eeprom_file;
38 LinkedList<String> eeprom_pending;
40 AltosEepromList flights;
41 ActionListener listener;
43 ParseException parse_exception;
46 private void FlushPending() throws IOException {
47 for (String s : flights.config_data) {
49 eeprom_file.write('\n');
52 for (String s : eeprom_pending)
56 private void CheckFile(boolean force) throws IOException {
57 if (eeprom_file != null)
59 if (force || (state.flight != 0 && want_file)) {
60 AltosFile eeprom_name;
61 AltosGPS gps = state.gps;
64 gps.year != AltosLib.MISSING &&
65 gps.month != AltosLib.MISSING &&
66 gps.day != AltosLib.MISSING)
68 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
69 state.serial, state.flight, "eeprom");
71 eeprom_name = new AltosFile(state.serial, state.flight, "eeprom");
73 eeprom_file = new FileWriter(eeprom_name);
74 if (eeprom_file != null) {
75 monitor.set_file(eeprom_name.getName());
77 eeprom_pending = null;
85 void LogEeprom(AltosEeprom r) throws IOException {
86 if (r.cmd != Altos.AO_LOG_INVALID) {
87 String line = r.string();
88 if (eeprom_file != null)
89 eeprom_file.write(line);
91 eeprom_pending.add(line);
95 void CaptureEeprom(AltosEepromChunk eechunk, int log_format) throws IOException {
96 boolean any_valid = false;
98 int record_length = 8;
100 state.set_serial(flights.config_data.serial);
102 for (int i = 0; i < AltosEepromChunk.chunk_size && !done; i += record_length) {
103 AltosEeprom r = eechunk.eeprom(i, log_format);
105 record_length = r.record_length();
107 r.update_state(state);
109 /* Monitor state transitions to update display */
110 if (state.state != AltosLib.ao_flight_invalid &&
111 state.state <= AltosLib.ao_flight_landed)
113 if (state.state > Altos.ao_flight_pad)
115 if (state.state == AltosLib.ao_flight_landed)
119 if (state.gps != null)
133 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
134 int block, state_block = 0;
135 int log_format = flights.config_data.log_format;
137 state = new AltosState();
142 if (flights.config_data.serial < 0)
143 throw new IOException("no serial number found");
145 /* Reset per-capture variables */
148 eeprom_pending = new LinkedList<String>();
150 /* Set serial number in the monitor dialog window */
151 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
153 state_block = log.start_block;
154 for (block = log.start_block; !done && block < log.end_block; block++) {
155 monitor.set_value(state.state_name(),
158 block - log.start_block);
160 AltosEepromChunk eechunk = new AltosEepromChunk(serial_line, block, block == log.start_block);
163 * Guess what kind of data is there if the device
167 if (log_format == Altos.AO_LOG_FORMAT_UNKNOWN) {
168 if (block == log.start_block) {
169 if (eechunk.data(0) == Altos.AO_LOG_FLIGHT)
170 log_format = Altos.AO_LOG_FORMAT_FULL;
172 log_format = Altos.AO_LOG_FORMAT_TINY;
176 CaptureEeprom (eechunk, log_format);
179 if (eeprom_file != null) {
185 private void show_message_internal(String message, String title, int message_type) {
186 JOptionPane.showMessageDialog(frame,
192 private void show_message(String in_message, String in_title, int in_message_type) {
193 final String message = in_message;
194 final String title = in_title;
195 final int message_type = in_message_type;
196 Runnable r = new Runnable() {
199 show_message_internal(message, title, message_type);
200 } catch (Exception ex) {
204 SwingUtilities.invokeLater(r);
209 boolean failed = false;
211 serial_line.start_remote();
213 for (AltosEepromLog log : flights) {
214 parse_exception = null;
219 if (parse_exception != null) {
221 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
223 parse_exception.getMessage()),
224 serial_line.device.toShortString(),
225 JOptionPane.WARNING_MESSAGE);
229 } catch (IOException ee) {
230 show_message(ee.getLocalizedMessage(),
231 serial_line.device.toShortString(),
232 JOptionPane.ERROR_MESSAGE);
233 } catch (InterruptedException ie) {
234 System.out.printf("download interrupted\n");
235 } catch (TimeoutException te) {
236 show_message(String.format("Connection to \"%s\" failed",
237 serial_line.device.toShortString()),
239 JOptionPane.ERROR_MESSAGE);
243 serial_line.stop_remote();
244 } catch (InterruptedException ie) {
247 serial_line.flush_output();
250 if (listener != null) {
251 Runnable r = new Runnable() {
254 listener.actionPerformed(new ActionEvent(this,
257 } catch (Exception ex) {
261 SwingUtilities.invokeLater(r);
265 public void start() {
266 eeprom_thread = new Thread(this);
267 eeprom_thread.start();
270 public void addActionListener(ActionListener l) {
274 public AltosEepromDownload(JFrame given_frame,
275 AltosSerial given_serial_line,
276 boolean given_remote,
277 AltosEepromList given_flights) {
280 serial_line = given_serial_line;
281 serial_line.set_frame(frame);
282 remote = given_remote;
283 flights = given_flights;
286 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
287 monitor.addActionListener(new ActionListener() {
288 public void actionPerformed(ActionEvent e) {
289 if (eeprom_thread != null)
290 eeprom_thread.interrupt();