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_12;
24 import java.util.concurrent.*;
26 class AltosEepromNameData extends AltosDataListener {
29 public void set_rssi(int rssi, int status) { }
30 public void set_received_time(long received_time) { }
32 public void set_acceleration(double accel) { }
33 public void set_pressure(double pa) { }
34 public void set_thrust(double N) { }
36 public void set_temperature(double deg_c) { }
37 public void set_battery_voltage(double volts) { }
39 public void set_apogee_voltage(double volts) { }
40 public void set_main_voltage(double volts) { }
42 public void set_gps(AltosGPS gps) {
44 gps.year != AltosLib.MISSING &&
45 gps.month != AltosLib.MISSING &&
46 gps.day != AltosLib.MISSING) {
51 public boolean done() {
57 public void set_gyro(double roll, double pitch, double yaw) { }
58 public void set_accel_ground(double along, double across, double through) { }
59 public void set_accel(double along, double across, double through) { }
60 public void set_mag(double along, double across, double through) { }
61 public void set_pyro_voltage(double volts) { }
62 public void set_igniter_voltage(double[] voltage) { }
63 public void set_pyro_fired(int pyro_mask) { }
64 public void set_companion(AltosCompanion companion) { }
65 public void set_kalman(double height, double speed, double acceleration) { }
66 public void set_orient(double new_orient) { }
68 public AltosEepromNameData(AltosCalData cal_data) {
73 public class AltosEepromDownload implements Runnable {
78 AltosEepromMonitor monitor;
80 AltosEepromList flights;
83 private boolean has_gps_date(AltosState state) {
87 AltosGPS gps = state.gps;
90 gps.year != AltosLib.MISSING &&
91 gps.month != AltosLib.MISSING &&
92 gps.day != AltosLib.MISSING;
95 private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
96 AltosFile eeprom_name;
98 if (name_data.gps != null) {
99 AltosGPS gps = name_data.gps;
100 eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
101 serial, flight, "eeprom");
103 eeprom_name = new AltosFile(serial, flight, "eeprom");
112 void LogError(String error) {
113 if (parse_errors != null)
114 parse_errors.concat(error.concat("\n"));
116 parse_errors = error;
119 class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
122 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
124 return super.get(start);
125 AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
131 public BlockCache(AltosEepromLog log) {
136 int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
137 int low = log.start_block;
138 int high = log.end_block - 1;
140 while (low <= high) {
141 int mid = (high + low) / 2;
143 if (!cache.get(mid, true).erased())
151 void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
152 int block, state_block = 0;
153 int log_format = flights.config_data.log_format;
154 BlockCache cache = new BlockCache(log);
158 if (flights.config_data.serial < 0)
159 throw new IOException("no serial number found");
161 /* Set serial number in the monitor dialog window */
162 monitor.set_serial(log.serial);
163 monitor.set_flight(log.flight);
165 int start_block = log.start_block;
166 int end_block = FindLastLog(log, cache);
168 monitor.set_max(end_block - start_block - 1);
170 ArrayList<Byte> data = new ArrayList<Byte>();
172 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
174 for (block = start_block; block < end_block; block++) {
175 monitor.set_block(block - start_block);
177 AltosEepromChunk eechunk = cache.get(block, false);
179 for (int i = 0; i < eechunk.data.length; i++)
180 data.add((byte) eechunk.data[i]);
183 /* Construct our internal representation of the eeprom data */
184 AltosEeprom eeprom = new AltosEeprom(flights.config_data, data);
186 /* Now see if we can't actually parse the resulting
187 * file to generate a better filename. Note that this
188 * doesn't need to work; we'll still save the data using
189 * a less accurate name.
191 AltosEepromRecordSet set = new AltosEepromRecordSet(eeprom);
192 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
194 for (AltosEepromRecord record : set.ordered) {
195 record.provide_data(name_data, set.cal_data());
196 if (name_data.done())
200 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
202 monitor.set_filename(f.toString());
204 FileWriter w = new FileWriter(f);
211 boolean success = false;
214 boolean failed = false;
218 for (AltosEepromLog log : flights) {
224 } catch (ParseException e) {
225 LogError(e.getMessage());
228 if (parse_errors != null) {
230 monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
234 AltosEepromMonitor.WARNING_MESSAGE);
238 } catch (IOException ee) {
239 monitor.show_message(ee.getLocalizedMessage(),
241 AltosEepromMonitor.ERROR_MESSAGE);
242 } catch (InterruptedException ie) {
243 monitor.show_message(String.format("Connection to \"%s\" interrupted",
245 "Connection Interrupted",
246 AltosEepromMonitor.ERROR_MESSAGE);
247 } catch (TimeoutException te) {
248 monitor.show_message(String.format("Connection to \"%s\" failed",
251 AltosEepromMonitor.ERROR_MESSAGE);
256 } catch (InterruptedException ie) {
261 monitor.done(success);
264 public void start() {
265 eeprom_thread = new Thread(this);
266 monitor.set_thread(eeprom_thread);
267 eeprom_thread.start();
270 public AltosEepromDownload(AltosEepromMonitor given_monitor,
271 AltosLink given_link,
272 boolean given_remote,
273 AltosEepromList given_flights) {
275 monitor = given_monitor;
277 remote = given_remote;
278 flights = given_flights;