map-server: Add maps proxy server
[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_13;
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                 super.set_gps(gps);
44                 if (gps != null &&
45                     gps.year != AltosLib.MISSING &&
46                     gps.month != AltosLib.MISSING &&
47                     gps.day != AltosLib.MISSING) {
48                         this.gps = gps;
49                 }
50         }
51
52         public boolean done() {
53                 if (gps == null)
54                         return false;
55                 return true;
56         }
57
58         public void set_gyro(double roll, double pitch, double yaw) { }
59         public void set_accel_ground(double along, double across, double through) { }
60         public void set_accel(double along, double across, double through) { }
61         public void set_mag(double along, double across, double through) { }
62         public void set_pyro_voltage(double volts) { }
63         public void set_igniter_voltage(double[] voltage) { }
64         public void set_pyro_fired(int pyro_mask) { }
65         public void set_companion(AltosCompanion companion) { }
66         public void set_kalman(double height, double speed, double acceleration) { }
67         public void set_orient(double new_orient) { }
68
69         public AltosEepromNameData(AltosCalData cal_data) {
70                 super(cal_data);
71         }
72 }
73
74 public class AltosEepromDownload implements Runnable {
75
76         AltosLink               link;
77         boolean                 remote;
78         Thread                  eeprom_thread;
79         AltosEepromMonitor      monitor;
80
81         AltosEepromList         flights;
82         String                  parse_errors;
83
84         private boolean has_gps_date(AltosState state) {
85                 if (state == null)
86                         return false;
87
88                 AltosGPS gps = state.gps;
89
90                 return gps != null &&
91                         gps.year != AltosLib.MISSING &&
92                         gps.month != AltosLib.MISSING &&
93                         gps.day != AltosLib.MISSING;
94         }
95
96         private AltosFile MakeFile(int serial, int flight, AltosEepromNameData name_data) throws IOException {
97                 AltosFile               eeprom_name;
98
99                 if (name_data.gps != null) {
100                         AltosGPS                gps = name_data.gps;
101                         eeprom_name = new AltosFile(gps.year, gps.month, gps.day,
102                                                     serial, flight, "eeprom");
103                 } else
104                         eeprom_name = new AltosFile(serial, flight, "eeprom");
105
106                 return eeprom_name;
107         }
108
109         boolean                 done;
110         int                     prev_state;
111         int                     state_block;
112
113         void LogError(String error) {
114                 if (parse_errors != null)
115                         parse_errors.concat(error.concat("\n"));
116                 else
117                         parse_errors = error;
118         }
119
120         class BlockCache extends Hashtable<Integer,AltosEepromChunk> {
121                 AltosEepromLog  log;
122
123                 AltosEepromChunk get(int start, boolean add) throws TimeoutException, InterruptedException {
124                         if (contains(start))
125                                 return super.get(start);
126                         AltosEepromChunk eechunk = new AltosEepromChunk(link, start, start == log.start_block);
127                         if (add)
128                                 put(start, eechunk);
129                         return eechunk;
130                 }
131
132                 public BlockCache(AltosEepromLog log) {
133                         this.log = log;
134                 }
135         }
136
137         int FindLastLog(AltosEepromLog log, BlockCache cache) throws TimeoutException, InterruptedException {
138                 int     low = log.start_block;
139                 int     high = log.end_block - 1;
140
141                 while (low <= high) {
142                         int mid = (high + low) / 2;
143
144                         if (!cache.get(mid, true).erased())
145                                 low = mid + 1;
146                         else
147                                 high = mid - 1;
148                 }
149                 return low;
150         }
151
152         void CaptureLog(AltosEepromLog log) throws IOException, InterruptedException, TimeoutException, ParseException {
153                 int                     block, state_block = 0;
154                 int                     log_format = flights.config_data.log_format;
155                 BlockCache              cache = new BlockCache(log);
156
157                 done = false;
158
159                 if (flights.config_data.serial < 0)
160                         throw new IOException("no serial number found");
161
162                 /* Set serial number in the monitor dialog window */
163                 monitor.set_serial(log.serial);
164                 monitor.set_flight(log.flight);
165
166                 int     start_block = log.start_block;
167                 int     end_block = FindLastLog(log, cache);
168
169                 monitor.set_max(end_block - start_block - 1);
170
171                 ArrayList<Byte> data = new ArrayList<Byte>();
172
173                 /* Now scan the eeprom, reading blocks of data to create a byte array of data */
174
175                 for (block = start_block; block < end_block; block++) {
176                         monitor.set_block(block - start_block);
177
178                         AltosEepromChunk        eechunk = cache.get(block, false);
179
180                         for (int i = 0; i < eechunk.data.length; i++)
181                                 data.add((byte) eechunk.data[i]);
182                 }
183
184                 /* Construct our internal representation of the eeprom data */
185                 AltosEeprom     eeprom = new AltosEeprom(flights.config_data, data);
186
187                 /* Now see if we can't actually parse the resulting
188                  * file to generate a better filename. Note that this
189                  * doesn't need to work; we'll still save the data using
190                  * a less accurate name.
191                  */
192                 AltosEepromRecordSet            set = new AltosEepromRecordSet(eeprom);
193                 AltosEepromNameData name_data = new AltosEepromNameData(set.cal_data());
194
195                 for (AltosEepromRecord record : set.ordered) {
196                         record.provide_data(name_data, set.cal_data());
197                         if (name_data.done())
198                                 break;
199                 }
200
201                 AltosFile f = MakeFile(flights.config_data.serial, log.flight, name_data);
202
203                 log.set_file(f);
204
205                 boolean do_write = true;
206
207                 if (f.exists())
208                         do_write = monitor.check_overwrite(f);
209
210                 if (do_write) {
211                         FileWriter w = new FileWriter(f);
212
213                         eeprom.write(w);
214                         w.close();
215                 }
216
217                 if (eeprom.errors != 0)
218                         throw new ParseException(String.format("%d CRC Errors", eeprom.errors), 0);
219         }
220
221         static String label(int flight) {
222                 if (flight < 0)
223                         return "Corrupt";
224                 else
225                         return "Flight";
226         }
227
228         static int flight(int flight) {
229                 if (flight < 0)
230                         return -flight;
231                 return flight;
232         }
233
234         public void run () {
235                 boolean success = false;
236
237                 try {
238                         if (remote)
239                                 link.start_remote();
240
241                         for (AltosEepromLog log : flights) {
242                                 parse_errors = null;
243                                 if (log.download_selected) {
244                                         monitor.reset();
245                                         try {
246                                                 CaptureLog(log);
247                                         } catch (ParseException e) {
248                                                 LogError(e.getMessage());
249                                         }
250                                 }
251                                 success = true;
252                                 if (parse_errors != null) {
253                                         monitor.show_message(String.format("%s %d download error. Valid log data saved\n%s",
254                                                                            label(log.flight),
255                                                                            flight(log.flight),
256                                                                            parse_errors),
257                                                              link.name,
258                                                              AltosEepromMonitor.WARNING_MESSAGE);
259                                 }
260                         }
261                 } catch (IOException ee) {
262                         monitor.show_message(ee.getLocalizedMessage(),
263                                              link.name,
264                                              AltosEepromMonitor.ERROR_MESSAGE);
265                 } catch (InterruptedException ie) {
266                         monitor.show_message(String.format("Connection to \"%s\" interrupted",
267                                                            link.name),
268                                              "Connection Interrupted",
269                                              AltosEepromMonitor.ERROR_MESSAGE);
270                 } catch (TimeoutException te) {
271                         monitor.show_message(String.format("Connection to \"%s\" failed",
272                                                            link.name),
273                                              "Connection Failed",
274                                              AltosEepromMonitor.ERROR_MESSAGE);
275                 } finally {
276                         if (remote) {
277                                 try {
278                                         link.stop_remote();
279                                 } catch (InterruptedException ie) {
280                                 }
281                         }
282                         link.flush_output();
283                 }
284                 monitor.done(success);
285         }
286
287         public void start() {
288                 eeprom_thread = new Thread(this);
289                 monitor.set_thread(eeprom_thread);
290                 eeprom_thread.start();
291         }
292
293         public AltosEepromDownload(AltosEepromMonitor given_monitor,
294                                    AltosLink given_link,
295                                    boolean given_remote,
296                                    AltosEepromList given_flights) {
297
298                 monitor = given_monitor;
299                 link = given_link;
300                 remote = given_remote;
301                 flights = given_flights;
302
303                 monitor.start();
304         }
305 }