1da94a6769ca191a0b5e6eefa0149003e44a307d
[fw/altos] / altosui / AltosEepromDownload.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 altosui;
19
20 import java.awt.*;
21 import java.awt.event.*;
22 import javax.swing.*;
23 import javax.swing.filechooser.FileNameExtensionFilter;
24 import javax.swing.table.*;
25 import java.io.*;
26 import java.util.*;
27 import java.text.*;
28 import java.util.prefs.*;
29 import java.util.concurrent.*;
30
31 import libaltosJNI.*;
32
33 public class AltosEepromDownload implements Runnable {
34
35         JFrame                  frame;
36         AltosSerial             serial_line;
37         boolean                 remote;
38         Thread                  eeprom_thread;
39         AltosEepromMonitor      monitor;
40
41         int                     flight;
42         int                     year, month, day;
43         boolean                 want_file;
44         FileWriter              eeprom_file;
45         LinkedList<String>      eeprom_pending;
46
47         AltosEepromList         flights;
48         ActionListener          listener;
49         boolean                 success;
50         ParseException          parse_exception;
51
52         private void FlushPending() throws IOException {
53                 for (String s : flights.config_data) {
54                         eeprom_file.write(s);
55                         eeprom_file.write('\n');
56                 }
57
58                 for (String s : eeprom_pending)
59                         eeprom_file.write(s);
60         }
61
62         private void CheckFile(boolean force) throws IOException {
63                 if (eeprom_file != null)
64                         return;
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");
69                         else
70                                 eeprom_name = new AltosFile(flights.config_data.serial, flight, "eeprom");
71
72                         eeprom_file = new FileWriter(eeprom_name);
73                         if (eeprom_file != null) {
74                                 monitor.set_file(eeprom_name.getName());
75                                 FlushPending();
76                                 eeprom_pending = null;
77                         }
78                 }
79         }
80
81         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
82                 int                     block, state_block = 0;
83                 int                     state = 0;
84                 boolean                 done = false;
85                 int                     record;
86
87                 if (flights.config_data.serial == 0)
88                         throw new IOException("no serial number found");
89
90                 /* Reset per-capture variables */
91                 flight = 0;
92                 year = 0;
93                 month = 0;
94                 day = 0;
95                 want_file = false;
96                 eeprom_file = null;
97                 eeprom_pending = new LinkedList<String>();
98
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 */
102
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);
106
107                         AltosEepromBlock        eeblock = new AltosEepromBlock(serial_line, block);
108
109                         if (eeblock.has_flight) {
110                                 flight = eeblock.flight;
111                                 monitor.set_flight(flight);
112                         }
113                         if (eeblock.has_date) {
114                                 year = eeblock.year;
115                                 month = eeblock.month;
116                                 day = eeblock.day;
117                                 want_file = true;
118                         }
119
120                         if (eeblock.size() == 0 ||
121                             eeblock.has_state && eeblock.state == Altos.ao_flight_landed)
122                                         done = true;
123
124                         /* Monitor state transitions to update display */
125                         if (eeblock.has_state) {
126                                 if (eeblock.state > Altos.ao_flight_pad)
127                                         want_file = true;
128                                 if (eeblock.state > state)
129                                         state = eeblock.state;
130                         }
131
132                         if (parse_exception == null && eeblock.parse_exception != null)
133                                 parse_exception = eeblock.parse_exception;
134
135                         CheckFile(false);
136
137                         for (record = 0; record < eeblock.size(); record++) {
138                                 AltosEepromRecord r = eeblock.get(record);
139
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);
145                                         else
146                                                 eeprom_pending.add(log_line);
147                                 }
148                         }
149                 }
150                 CheckFile(true);
151                 if (eeprom_file != null) {
152                         eeprom_file.flush();
153                         eeprom_file.close();
154                 }
155         }
156
157         private void show_message_internal(String message, String title, int message_type) {
158                 JOptionPane.showMessageDialog(frame,
159                                               message,
160                                               title,
161                                               message_type);
162         }
163
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() {
169                                 public void run() {
170                                         try {
171                                                 show_message_internal(message, title, message_type);
172                                         } catch (Exception ex) {
173                                         }
174                                 }
175                         };
176                 SwingUtilities.invokeLater(r);
177         }
178
179         public void run () {
180                 if (remote)
181                         serial_line.start_remote();
182
183                 try {
184                         boolean failed = false;
185                         for (AltosEepromLog log : flights) {
186                                 parse_exception = null;
187                                 if (log.download) {
188                                         monitor.reset();
189                                         CaptureLog(log);
190                                 }
191                                 if (parse_exception != null) {
192                                         failed = true;
193                                         show_message(String.format("Flight %d download error\n%s\nValid log data saved",
194                                                                    log.flight,
195                                                                    parse_exception.getMessage()),
196                                                      serial_line.device.toShortString(),
197                                                      JOptionPane.WARNING_MESSAGE);
198                                 }
199                         }
200                         success = !failed;
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()),
209                                      "Connection Failed",
210                                      JOptionPane.ERROR_MESSAGE);
211                 }
212                 if (remote)
213                         serial_line.stop_remote();
214                 monitor.done();
215                 serial_line.flush_output();
216                 if (listener != null) {
217                         Runnable r = new Runnable() {
218                                         public void run() {
219                                                 try {
220                                                         listener.actionPerformed(new ActionEvent(this,
221                                                                                                  success ? 1 : 0,
222                                                                                                  "download"));
223                                                 } catch (Exception ex) {
224                                                 }
225                                         }
226                                 };
227                         SwingUtilities.invokeLater(r);
228                 }
229         }
230
231         public void start() {
232                 eeprom_thread = new Thread(this);
233                 eeprom_thread.start();
234         }
235
236         public void addActionListener(ActionListener l) {
237                 listener = l;
238         }
239
240         public AltosEepromDownload(JFrame given_frame,
241                                    AltosSerial given_serial_line,
242                                    boolean given_remote,
243                                    AltosEepromList given_flights) {
244
245                 frame = given_frame;
246                 serial_line = given_serial_line;
247                 remote = given_remote;
248                 flights = given_flights;
249                 success = false;
250
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();
256                                 }
257                         });
258         }
259 }