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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 package org.altusmetrum.altoslib_11;
24 import java.util.concurrent.*;
26 public class AltosEepromDownload implements Runnable {
31 AltosEepromMonitor monitor;
33 AltosEepromList flights;
36 private boolean has_gps_date(AltosState state) {
40 AltosGPS gps = state.gps;
43 gps.year != AltosLib.MISSING &&
44 gps.month != AltosLib.MISSING &&
45 gps.day != AltosLib.MISSING;
48 private AltosFile MakeFile(int serial, int flight, AltosState state) throws IOException {
49 AltosFile eeprom_name;
51 if (has_gps_date(state)) {
52 AltosGPS gps = state.gps;
53 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
54 serial, flight, "eeprom");
56 eeprom_name = new AltosFile(serial, flight, "eeprom");
65 void LogError(String error) {
66 if (parse_errors != null)
67 parse_errors.concat(error.concat("\n"));
72 class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
75 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
77 return super.get(start);
78 AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
84 public BlockCache(AltosEepromLog log) {
89 int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
90 int low = log.start_block;
91 int high = log.end_block - 1;
94 int mid = (high + low) / 2;
96 if (!cache.get(mid, true).erased())
104 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
105 int block, state_block = 0;
106 int log_format = flights.config_data.log_format;
107 BlockCache cache = new BlockCache(log);
111 if (flights.config_data.serial < 0)
112 throw new IOException("no serial number found");
114 /* Set serial number in the monitor dialog window */
115 monitor.set_serial(log.serial);
116 monitor.set_flight(log.flight);
118 int start_block = log.start_block;
119 int end_block = FindLastLog(log, cache);
121 monitor.set_max(end_block - start_block - 1);
123 ArrayList<Byte> data = new ArrayList<Byte>();
125 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
127 for (block = start_block; block < end_block; block++) {
128 monitor.set_block(block - start_block);
130 AltosEepromChunk eechunk = cache.get(block, false);
132 for (int i = 0; i < eechunk.data.length; i++)
133 data.add((byte) eechunk.data[i]);
136 /* Construct our internal representation of the eeprom data */
137 AltosEepromNew eeprom = new AltosEepromNew(flights.config_data, data);
139 /* Now see if we can't actually parse the resulting
140 * file to generate a better filename. Note that this
141 * doesn't need to work; we'll still save the data using
142 * a less accurate name.
144 AltosEepromRecordSet set = new AltosEepromRecordSet(eeprom);
146 AltosState state = new AltosState();
148 for (AltosState s : set) {
150 if (state.gps != null)
154 AltosFile f = MakeFile(flights.config_data.serial, log.flight, state);
156 monitor.set_filename(f.toString());
158 FileWriter w = new FileWriter(f);
165 boolean success = false;
168 boolean failed = false;
172 for (AltosEepromLog log : flights) {
178 } catch (ParseException e) {
179 LogError(e.getMessage());
182 if (parse_errors != null) {
184 monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
188 AltosEepromMonitor.WARNING_MESSAGE);
192 } catch (IOException ee) {
193 monitor.show_message(ee.getLocalizedMessage(),
195 AltosEepromMonitor.ERROR_MESSAGE);
196 } catch (InterruptedException ie) {
197 monitor.show_message(String.format("Connection to \"%s\" interrupted",
199 "Connection Interrupted",
200 AltosEepromMonitor.ERROR_MESSAGE);
201 } catch (TimeoutException te) {
202 monitor.show_message(String.format("Connection to \"%s\" failed",
205 AltosEepromMonitor.ERROR_MESSAGE);
210 } catch (InterruptedException ie) {
215 monitor.done(success);
218 public void start() {
219 eeprom_thread = new Thread(this);
220 monitor.set_thread(eeprom_thread);
221 eeprom_thread.start();
224 public AltosEepromDownload(AltosEepromMonitor given_monitor,
225 AltosLink given_link,
226 boolean given_remote,
227 AltosEepromList given_flights) {
229 monitor = given_monitor;
231 remote = given_remote;
232 flights = given_flights;