]> git.gag.com Git - fw/altos/blob - altoslib/AltosEepromDownload.java
ao-tools: Create ao-sendfake to send flight data for simulation runs
[fw/altos] / altoslib / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
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.
13  *
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.
17  */
18
19 package org.altusmetrum.altoslib_12;
20
21 import java.io.*;
22 import java.util.*;
23 import java.text.*;
24 import java.util.concurrent.*;
25
26 class AltosEepromNameData extends AltosDataListener {
27         AltosGPS        gps = null;
28
29         public void set_rssi(int rssi, int status) { }
30         public void set_received_time(long received_time) { }
31
32         public void set_acceleration(double accel) { }
33         public void set_pressure(double pa) { }
34         public void set_thrust(double N) { }
35
36         public void set_temperature(double deg_c) { }
37         public void set_battery_voltage(double volts) { }
38
39         public void set_apogee_voltage(double volts) { }
40         public void set_main_voltage(double volts) { }
41
42         public void set_gps(AltosGPS gps) {
43                 if (gps != null &&
44                     gps.year != AltosLib.MISSING &&
45                     gps.month != AltosLib.MISSING &&
46                     gps.day != AltosLib.MISSING) {
47                         this.gps = gps;
48                 }
49         }
50
51         public boolean done() {
52                 if (gps == null)
53                         return false;
54                 return true;
55         }
56
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) { }
67
68         public AltosEepromNameData(AltosCalData cal_data) {
69                 super(cal_data);
70         }
71 }
72
73 public class AltosEepromDownload implements Runnable {
74
75         AltosLink               link;
76         boolean                 remote;
77         Thread                  eeprom_thread;
78         AltosEepromMonitor      monitor;
79
80         AltosEepromList         flights;
81         String                  parse_errors;
82
83         private boolean has_gps_date(AltosState state) {
84                 if (state == null)
85                         return false;
86
87                 AltosGPS gps = state.gps;
88
89                 return gps != null &&
90                         gps.year != AltosLib.MISSING &&
91                         gps.month != AltosLib.MISSING &&
92                         gps.day != AltosLib.MISSING;
93         }
94
95         private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
96                 AltosFile               eeprom_name;
97
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");
102                 } else
103                         eeprom_name = new AltosFile(serial, flight, "eeprom");
104
105                 return eeprom_name;
106         }
107
108         boolean                 done;
109         int                     prev_state;
110         int                     state_block;
111
112         void LogError(String error) {
113                 if (parse_errors != null)
114                         parse_errors.concat(error.concat("\n"));
115                 else
116                         parse_errors = error;
117         }
118
119         class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
120                 AltosEepromLog  log;
121
122                 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
123                         if (contains(start))
124                                 return super.get(start);
125                         AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
126                         if (add)
127                                 put(start, eechunk);
128                         return eechunk;
129                 }
130
131                 public BlockCache(AltosEepromLog log) {
132                         this.log = log;
133                 }
134         }
135
136         int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
137                 int     low = log.start_block;
138                 int     high = log.end_block - 1;
139
140                 while (low <= high) {
141                         int mid = (high + low) / 2;
142
143                         if (!cache.get(mid, true).erased())
144                                 low = mid + 1;
145                         else
146                                 high = mid - 1;
147                 }
148                 return low;
149         }
150
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);
155
156                 done = false;
157
158                 if (flights.config_data.serial < 0)
159                         throw new IOException("no serial number found");
160
161                 /* Set serial number in the monitor dialog window */
162                 monitor.set_serial(log.serial);
163                 monitor.set_flight(log.flight);
164
165                 int     start_block = log.start_block;
166                 int     end_block = FindLastLog(log, cache);
167
168                 monitor.set_max(end_block - start_block - 1);
169
170                 ArrayList<Byte> data = new ArrayList<Byte>();
171
172                 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
173
174                 for (block = start_block; block < end_block; block++) {
175                         monitor.set_block(block - start_block);
176
177                         AltosEepromChunk        eechunk = cache.get(block, false);
178
179                         for (int i = 0; i < eechunk.data.length; i++)
180                                 data.add((byte) eechunk.data[i]);
181                 }
182
183                 /* Construct our internal representation of the eeprom data */
184                 AltosEeprom     eeprom = new AltosEeprom(flights.config_data, data);
185
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.
190                  */
191                 AltosEepromRecordSet            set = new AltosEepromRecordSet(eeprom);
192                 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
193
194                 for (AltosEepromRecord record : set.ordered) {
195                         record.provide_data(name_data, set.cal_data());
196                         if (name_data.done())
197                                 break;
198                 }
199
200                 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
201
202                 monitor.set_filename(f.toString());
203
204                 FileWriter w = new FileWriter(f);
205
206                 eeprom.write(w);
207                 w.close();
208         }
209
210         public void run () {
211                 boolean success = false;
212
213                 try {
214                         boolean failed = false;
215                         if (remote)
216                                 link.start_remote();
217
218                         for (AltosEepromLog log : flights) {
219                                 parse_errors = null;
220                                 if (log.selected) {
221                                         monitor.reset();
222                                         try {
223                                                 CaptureLog(log);
224                                         } catch (ParseException e) {
225                                                 LogError(e.getMessage());
226                                         }
227                                 }
228                                 if (parse_errors != null) {
229                                         failed = true;
230                                         monitor.show_message(String.format("Flight %d download error. Valid log data saved\n%s",
231                                                                            log.flight,
232                                                                            parse_errors),
233                                                              link.name,
234                                                              AltosEepromMonitor.WARNING_MESSAGE);
235                                 }
236                         }
237                         success = !failed;
238                 } catch (IOException ee) {
239                         monitor.show_message(ee.getLocalizedMessage(),
240                                              link.name,
241                                              AltosEepromMonitor.ERROR_MESSAGE);
242                 } catch (InterruptedException ie) {
243                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
244                                                            link.name),
245                                              "Connection Interrupted",
246                                              AltosEepromMonitor.ERROR_MESSAGE);
247                 } catch (TimeoutException te) {
248                         monitor.show_message(String.format("Connection to \"%s\" failed",
249                                                            link.name),
250                                              "Connection Failed",
251                                              AltosEepromMonitor.ERROR_MESSAGE);
252                 } finally {
253                         if (remote) {
254                                 try {
255                                         link.stop_remote();
256                                 } catch (InterruptedException ie) {
257                                 }
258                         }
259                         link.flush_output();
260                 }
261                 monitor.done(success);
262         }
263
264         public void start() {
265                 eeprom_thread = new Thread(this);
266                 monitor.set_thread(eeprom_thread);
267                 eeprom_thread.start();
268         }
269
270         public AltosEepromDownload(AltosEepromMonitor given_monitor,
271                                    AltosLink given_link,
272                                    boolean given_remote,
273                                    AltosEepromList given_flights) {
274
275                 monitor = given_monitor;
276                 link = given_link;
277                 remote = given_remote;
278                 flights = given_flights;
279
280                 monitor.start();
281         }
282 }