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.
21 import java.awt.event.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
33 public class AltosEepromDownload implements Runnable {
36 AltosSerial serial_line;
39 AltosEepromMonitor monitor;
44 FileWriter eeprom_file;
45 LinkedList<String> eeprom_pending;
47 AltosEepromList flights;
48 ActionListener listener;
50 ParseException parse_exception;
52 private void FlushPending() throws IOException {
53 for (String s : flights.config_data) {
55 eeprom_file.write('\n');
58 for (String s : eeprom_pending)
62 private void CheckFile(boolean force) throws IOException {
63 if (eeprom_file != null)
65 if (force || (flight != 0 && want_file)) {
66 AltosFile eeprom_name;
67 if (year != 0 && month != 0 && day != 0)
68 eeprom_name = new AltosFile(year, month, day, flights.config_data.serial, flight, "eeprom");
70 eeprom_name = new AltosFile(flights.config_data.serial, flight, "eeprom");
72 eeprom_file = new FileWriter(eeprom_name);
73 if (eeprom_file != null) {
74 monitor.set_file(eeprom_name.getName());
76 eeprom_pending = null;
81 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
82 int block, state_block = 0;
87 if (flights.config_data.serial == 0)
88 throw new IOException("no serial number found");
90 /* Reset per-capture variables */
97 eeprom_pending = new LinkedList<String>();
99 /* Set serial number in the monitor dialog window */
100 monitor.set_serial(flights.config_data.serial);
101 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
103 state = 0; state_block = log.start_block;
104 for (block = log.start_block; !done && block < log.end_block; block++) {
105 monitor.set_value(Altos.state_to_string[state], state, block - state_block);
107 AltosEepromBlock eeblock = new AltosEepromBlock(serial_line, block);
109 if (eeblock.has_flight) {
110 flight = eeblock.flight;
111 monitor.set_flight(flight);
113 if (eeblock.has_date) {
115 month = eeblock.month;
120 if (eeblock.size() == 0 ||
121 eeblock.has_state && eeblock.state == Altos.ao_flight_landed)
124 /* Monitor state transitions to update display */
125 if (eeblock.has_state) {
126 if (eeblock.state > Altos.ao_flight_pad)
128 if (eeblock.state > state)
129 state = eeblock.state;
132 if (parse_exception == null && eeblock.parse_exception != null)
133 parse_exception = eeblock.parse_exception;
137 for (record = 0; record < eeblock.size(); record++) {
138 AltosEepromRecord r = eeblock.get(record);
140 if (r.cmd != Altos.AO_LOG_INVALID) {
141 String log_line = String.format("%c %4x %4x %4x\n",
142 r.cmd, r.tick, r.a, r.b);
143 if (eeprom_file != null)
144 eeprom_file.write(log_line);
146 eeprom_pending.add(log_line);
151 if (eeprom_file != null) {
157 private void show_message_internal(String message, String title, int message_type) {
158 JOptionPane.showMessageDialog(frame,
164 private void show_message(String in_message, String in_title, int in_message_type) {
165 final String message = in_message;
166 final String title = in_title;
167 final int message_type = in_message_type;
168 Runnable r = new Runnable() {
171 show_message_internal(message, title, message_type);
172 } catch (Exception ex) {
176 SwingUtilities.invokeLater(r);
181 serial_line.start_remote();
184 boolean failed = false;
185 for (AltosEepromLog log : flights) {
186 parse_exception = null;
191 if (parse_exception != null) {
193 show_message(String.format("Flight %d download error\n%s\nValid log data saved",
195 parse_exception.getMessage()),
196 serial_line.device.toShortString(),
197 JOptionPane.WARNING_MESSAGE);
201 } catch (IOException ee) {
202 show_message(ee.getLocalizedMessage(),
203 serial_line.device.toShortString(),
204 JOptionPane.ERROR_MESSAGE);
205 } catch (InterruptedException ie) {
206 } catch (TimeoutException te) {
207 show_message(String.format("Connection to \"%s\" failed",
208 serial_line.device.toShortString()),
210 JOptionPane.ERROR_MESSAGE);
213 serial_line.stop_remote();
215 serial_line.flush_output();
216 if (listener != null) {
217 Runnable r = new Runnable() {
220 listener.actionPerformed(new ActionEvent(this,
223 } catch (Exception ex) {
227 SwingUtilities.invokeLater(r);
231 public void start() {
232 eeprom_thread = new Thread(this);
233 eeprom_thread.start();
236 public void addActionListener(ActionListener l) {
240 public AltosEepromDownload(JFrame given_frame,
241 AltosSerial given_serial_line,
242 boolean given_remote,
243 AltosEepromList given_flights) {
246 serial_line = given_serial_line;
247 remote = given_remote;
248 flights = given_flights;
251 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
252 monitor.addActionListener(new ActionListener() {
253 public void actionPerformed(ActionEvent e) {
254 if (eeprom_thread != null)
255 eeprom_thread.interrupt();