af70b15c5603139c318559c189e941535fae3872
[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
51         private void FlushPending() throws IOException {
52                 for (String s : flights.config_data) {
53                         eeprom_file.write(s);
54                         eeprom_file.write('\n');
55                 }
56
57                 for (String s : eeprom_pending)
58                         eeprom_file.write(s);
59         }
60
61         private void CheckFile(boolean force) throws IOException {
62                 if (eeprom_file != null)
63                         return;
64                 if (force || (flight != 0 && want_file)) {
65                         AltosFile               eeprom_name;
66                         if (year != 0 && month != 0 && day != 0)
67                                 eeprom_name = new AltosFile(year, month, day, flights.config_data.serial, flight, "eeprom");
68                         else
69                                 eeprom_name = new AltosFile(flights.config_data.serial, flight, "eeprom");
70
71                         eeprom_file = new FileWriter(eeprom_name);
72                         if (eeprom_file != null) {
73                                 monitor.set_file(eeprom_name.getName());
74                                 FlushPending();
75                                 eeprom_pending = null;
76                         }
77                 }
78         }
79
80         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException {
81                 int                     block, state_block = 0;
82                 int                     state = 0;
83                 boolean                 done = false;
84                 int                     record;
85
86                 if (flights.config_data.serial == 0)
87                         throw new IOException("no serial number found");
88
89                 /* Reset per-capture variables */
90                 flight = 0;
91                 year = 0;
92                 month = 0;
93                 day = 0;
94                 want_file = false;
95                 eeprom_file = null;
96                 eeprom_pending = new LinkedList<String>();
97
98                 /* Set serial number in the monitor dialog window */
99                 monitor.set_serial(flights.config_data.serial);
100                 /* Now scan the eeprom, reading blocks of data and converting to .eeprom file form */
101
102                 state = 0; state_block = log.start_block;
103                 for (block = log.start_block; !done && block < log.end_block; block++) {
104                         monitor.set_value(Altos.state_to_string[state], state, block - state_block);
105
106                         AltosEepromBlock        eeblock = new AltosEepromBlock(serial_line, block);
107
108                         if (eeblock.has_flight) {
109                                 flight = eeblock.flight;
110                                 monitor.set_flight(flight);
111                         }
112                         if (eeblock.has_date) {
113                                 year = eeblock.year;
114                                 month = eeblock.month;
115                                 day = eeblock.day;
116                                 want_file = true;
117                         }
118
119                         if (eeblock.size() == 0 ||
120                             eeblock.has_state && eeblock.state == Altos.ao_flight_landed)
121                                         done = true;
122
123                         /* Monitor state transitions to update display */
124                         if (eeblock.has_state) {
125                                 if (eeblock.state > Altos.ao_flight_pad)
126                                         want_file = true;
127                                 if (eeblock.state > state)
128                                         state = eeblock.state;
129                         }
130
131                         CheckFile(false);
132
133                         for (record = 0; record < eeblock.size(); record++) {
134                                 AltosEepromRecord r = eeblock.get(record);
135
136                                 String log_line = String.format("%c %4x %4x %4x\n",
137                                                                 r.cmd, r.tick, r.a, r.b);
138                                 if (eeprom_file != null)
139                                         eeprom_file.write(log_line);
140                                 else
141                                         eeprom_pending.add(log_line);
142                         }
143                 }
144                 CheckFile(true);
145                 if (eeprom_file != null) {
146                         eeprom_file.flush();
147                         eeprom_file.close();
148                 }
149         }
150
151         private void show_error_internal(String message, String title) {
152                 JOptionPane.showMessageDialog(frame,
153                                               message,
154                                               title,
155                                               JOptionPane.ERROR_MESSAGE);
156         }
157
158         private void show_error(String in_message, String in_title) {
159                 final String message = in_message;
160                 final String title = in_title;
161                 Runnable r = new Runnable() {
162                                 public void run() {
163                                         try {
164                                                 show_error_internal(message, title);
165                                         } catch (Exception ex) {
166                                         }
167                                 }
168                         };
169                 SwingUtilities.invokeLater(r);
170         }
171
172         public void run () {
173                 if (remote)
174                         serial_line.start_remote();
175
176                 try {
177                         for (AltosEepromLog log : flights) {
178                                 if (log.download) {
179                                         monitor.reset();
180                                         CaptureLog(log);
181                                 }
182                         }
183                         System.out.printf("All flights successfully downloaded\n");
184                         success = true;
185                 } catch (IOException ee) {
186                         show_error (serial_line.device.toShortString(),
187                                     ee.getLocalizedMessage());
188                 } catch (InterruptedException ie) {
189                 } catch (TimeoutException te) {
190                         show_error (String.format("Connection to \"%s\" failed",
191                                                   serial_line.device.toShortString()),
192                                     "Connection Failed");
193                 }
194                 if (remote)
195                         serial_line.stop_remote();
196                 monitor.done();
197                 serial_line.flush_output();
198                 if (listener != null) {
199                         Runnable r = new Runnable() {
200                                         public void run() {
201                                                 try {
202                                                         listener.actionPerformed(new ActionEvent(this,
203                                                                                                  success ? 1 : 0,
204                                                                                                  "download"));
205                                                 } catch (Exception ex) {
206                                                 }
207                                         }
208                                 };
209                         SwingUtilities.invokeLater(r);
210                 }
211         }
212
213         public void start() {
214                 eeprom_thread = new Thread(this);
215                 eeprom_thread.start();
216         }
217
218         public void addActionListener(ActionListener l) {
219                 listener = l;
220         }
221
222         public AltosEepromDownload(JFrame given_frame,
223                                    AltosSerial given_serial_line,
224                                    boolean given_remote,
225                                    AltosEepromList given_flights) {
226
227                 frame = given_frame;
228                 serial_line = given_serial_line;
229                 remote = given_remote;
230                 flights = given_flights;
231                 success = false;
232
233                 monitor = new AltosEepromMonitor(frame, Altos.ao_flight_boost, Altos.ao_flight_landed);
234                 monitor.addActionListener(new ActionListener() {
235                                 public void actionPerformed(ActionEvent e) {
236                                         if (eeprom_thread != null)
237                                                 eeprom_thread.interrupt();
238                                 }
239                         });
240         }
241 }