Fix java version detection and downloading
[fw/altos] / altosui / AltosUI.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 java.io.*;
24 import java.util.concurrent.*;
25 import org.altusmetrum.altoslib_5.*;
26 import org.altusmetrum.altosuilib_3.*;
27
28 public class AltosUI extends AltosUIFrame {
29         public AltosVoice voice = new AltosVoice();
30
31         public static boolean load_library(Frame frame) {
32                 if (!Altos.load_library()) {
33                         JOptionPane.showMessageDialog(frame,
34                                                       String.format("No AltOS library in \"%s\"",
35                                                                     System.getProperty("java.library.path","<undefined>")),
36                                                       "Cannot load device access library",
37                                                       JOptionPane.ERROR_MESSAGE);
38                         return false;
39                 }
40                 return true;
41         }
42
43         void telemetry_window(AltosDevice device) {
44                 try {
45                         AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
46                         if (reader != null)
47                                 new AltosFlightUI(voice, reader, device.getSerial());
48                 } catch (FileNotFoundException ee) {
49                         JOptionPane.showMessageDialog(AltosUI.this,
50                                                       ee.getMessage(),
51                                                       String.format ("Cannot open %s", device.toShortString()),
52                                                       JOptionPane.ERROR_MESSAGE);
53                 } catch (AltosSerialInUseException si) {
54                         JOptionPane.showMessageDialog(AltosUI.this,
55                                                       String.format("Device \"%s\" already in use",
56                                                                     device.toShortString()),
57                                                       "Device in use",
58                                                       JOptionPane.ERROR_MESSAGE);
59                 } catch (IOException ee) {
60                         JOptionPane.showMessageDialog(AltosUI.this,
61                                                       String.format ("Unknown I/O error on %s", device.toShortString()),
62                                                       "Unknown I/O error",
63                                                       JOptionPane.ERROR_MESSAGE);
64                 } catch (TimeoutException te) {
65                         JOptionPane.showMessageDialog(this,
66                                                       String.format ("Timeout on %s", device.toShortString()),
67                                                       "Timeout error",
68                                                       JOptionPane.ERROR_MESSAGE);
69                 } catch (InterruptedException ie) {
70                         JOptionPane.showMessageDialog(this,
71                                                       String.format("Interrupted %s", device.toShortString()),
72                                                       "Interrupted exception",
73                                                       JOptionPane.ERROR_MESSAGE);
74                 }
75         }
76
77         public void scan_device_selected(AltosDevice device) {
78                 telemetry_window(device);
79         }
80
81         Container       pane;
82         GridBagLayout   gridbag;
83
84         JButton addButton(int x, int y, String label) {
85                 GridBagConstraints      c;
86                 JButton                 b;
87
88                 c = new GridBagConstraints();
89                 c.gridx = x; c.gridy = y;
90                 c.fill = GridBagConstraints.BOTH;
91                 c.weightx = 1;
92                 c.weighty = 1;
93                 b = new JButton(label);
94
95                 //Dimension ps = b.getPreferredSize();
96
97                 gridbag.setConstraints(b, c);
98                 add(b, c);
99                 return b;
100         }
101
102         public AltosUI() {
103
104                 load_library(null);
105
106                 AltosUIPreferences.set_component(this);
107
108                 pane = getContentPane();
109                 gridbag = new GridBagLayout();
110                 pane.setLayout(gridbag);
111
112                 JButton b;
113
114                 b = addButton(0, 0, "Monitor Flight");
115                 b.addActionListener(new ActionListener() {
116                                         public void actionPerformed(ActionEvent e) {
117                                                 ConnectToDevice();
118                                         }
119                                 });
120                 b.setToolTipText("Connect to TeleDongle and monitor telemetry");
121                 b = addButton(1, 0, "Save Flight Data");
122                 b.addActionListener(new ActionListener() {
123                                         public void actionPerformed(ActionEvent e) {
124                                                 SaveFlightData();
125                                         }
126                                 });
127                 b.setToolTipText("Download and/or delete flight data from an altimeter");
128                 b = addButton(2, 0, "Replay Flight");
129                 b.addActionListener(new ActionListener() {
130                                         public void actionPerformed(ActionEvent e) {
131                                                 Replay();
132                                         }
133                                 });
134                 b.setToolTipText("Watch an old flight in real-time");
135                 b = addButton(3, 0, "Graph Data");
136                 b.addActionListener(new ActionListener() {
137                                         public void actionPerformed(ActionEvent e) {
138                                                 GraphData();
139                                         }
140                                 });
141                 b.setToolTipText("Present flight data in a graph and table of statistics");
142                 b = addButton(4, 0, "Export Data");
143                 b.addActionListener(new ActionListener() {
144                                         public void actionPerformed(ActionEvent e) {
145                                                 ExportData();
146                                         }
147                                 });
148                 b.setToolTipText("Convert flight data for a spreadsheet or GoogleEarth");
149                 b = addButton(0, 1, "Configure Altimeter");
150                 b.addActionListener(new ActionListener() {
151                                         public void actionPerformed(ActionEvent e) {
152                                                 ConfigureTeleMetrum();
153                                         }
154                                 });
155                 b.setToolTipText("Set flight, storage and communication parameters");
156                 b = addButton(1, 1, "Configure AltosUI");
157                 b.addActionListener(new ActionListener() {
158                                 public void actionPerformed(ActionEvent e) {
159                                         ConfigureAltosUI();
160                                 }
161                         });
162                 b.setToolTipText("Global AltosUI settings");
163
164                 b = addButton(2, 1, "Configure Ground Station");
165                 b.addActionListener(new ActionListener() {
166                                 public void actionPerformed(ActionEvent e) {
167                                         ConfigureTeleDongle();
168                                 }
169                         });
170
171                 b = addButton(3, 1, "Flash Image");
172                 b.addActionListener(new ActionListener() {
173                                 public void actionPerformed(ActionEvent e) {
174                                         FlashImage();
175                                 }
176                         });
177                 b.setToolTipText("Replace the firmware in any AltusMetrum product");
178
179                 b = addButton(4, 1, "Fire Igniter");
180                 b.addActionListener(new ActionListener() {
181                                 public void actionPerformed(ActionEvent e) {
182                                         FireIgniter();
183                                 }
184                         });
185                 b.setToolTipText("Remote control of igniters for deployment testing");
186                 b = addButton(0, 2, "Scan Channels");
187                 b.addActionListener(new ActionListener() {
188                                 public void actionPerformed(ActionEvent e) {
189                                         ScanChannels();
190                                 }
191                         });
192                 b.setToolTipText("Find what channel an altimeter is sending telemetry on");
193                 b = addButton(1, 2, "Load Maps");
194                 b.addActionListener(new ActionListener() {
195                                 public void actionPerformed(ActionEvent e) {
196                                         LoadMaps();
197                                 }
198                         });
199                 b.setToolTipText("Download satellite images for off-line flight monitoring");
200                 b = addButton(2, 2, "Monitor Idle");
201                 b.addActionListener(new ActionListener() {
202                                 public void actionPerformed(ActionEvent e) {
203                                         IdleMonitor();
204                                 }
205                         });
206                 b.setToolTipText("Check flight readiness of altimeter in idle mode");
207
208 //              b = addButton(3, 2, "Launch Controller");
209 //              b.addActionListener(new ActionListener() {
210 //                              public void actionPerformed(ActionEvent e) {
211 //                                      LaunchController();
212 //                              }
213 //                      });
214
215                 b = addButton(4, 2, "Quit");
216                 b.addActionListener(new ActionListener() {
217                                 public void actionPerformed(ActionEvent e) {
218                                         System.exit(0);
219                                 }
220                         });
221                 b.setToolTipText("Close all active windows and terminate AltosUI");
222
223                 setTitle("AltOS");
224
225                 pane.doLayout();
226                 pane.validate();
227
228                 doLayout();
229                 validate();
230
231                 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
232                 addWindowListener(new WindowAdapter() {
233                         @Override
234                         public void windowClosing(WindowEvent e) {
235                                 System.exit(0);
236                         }
237                 });
238
239                 setLocationByPlatform(false);
240
241                 /* Insets aren't set before the window is visible */
242                 setVisible(true);
243         }
244
245         private void ConnectToDevice() {
246                 AltosDevice     device = AltosDeviceUIDialog.show(AltosUI.this,
247                                                                 Altos.product_basestation);
248
249                 if (device != null)
250                         telemetry_window(device);
251         }
252
253         void ConfigureCallsign() {
254                 String  result;
255                 result = JOptionPane.showInputDialog(AltosUI.this,
256                                                      "Configure Callsign",
257                                                      AltosUIPreferences.callsign());
258                 if (result != null)
259                         AltosUIPreferences.set_callsign(result);
260         }
261
262         void ConfigureTeleMetrum() {
263                 new AltosConfig(AltosUI.this);
264         }
265
266         void ConfigureTeleDongle() {
267                 new AltosConfigTD(AltosUI.this);
268         }
269
270         void FlashImage() {
271                 AltosFlashUI.show(AltosUI.this);
272         }
273
274         void FireIgniter() {
275                 new AltosIgniteUI(AltosUI.this);
276         }
277
278         void ScanChannels() {
279                 new AltosScanUI(AltosUI.this, true);
280         }
281
282         void LoadMaps() {
283                 new AltosUIMapPreload(AltosUI.this);
284         }
285
286         void LaunchController() {
287                 new AltosLaunchUI(AltosUI.this);
288         }
289
290         /*
291          * Replay a flight from telemetry data
292          */
293         private void Replay() {
294                 AltosDataChooser chooser = new AltosDataChooser(
295                         AltosUI.this);
296
297                 Iterable<AltosState> states = chooser.runDialog();
298                 if (states != null) {
299                         AltosFlightReader reader = new AltosReplayReader(states.iterator(),
300                                                                          chooser.file());
301                         new AltosFlightUI(voice, reader);
302                 }
303         }
304
305         /* Connect to TeleMetrum, either directly or through
306          * a TeleDongle over the packet link
307          */
308         private void SaveFlightData() {
309                 new AltosEepromManage(AltosUI.this, AltosLib.product_any);
310         }
311
312         /* Load a flight log file and write out a CSV file containing
313          * all of the data in standard units
314          */
315
316         private void ExportData() {
317                 AltosDataChooser chooser;
318                 chooser = new AltosDataChooser(this);
319                 AltosStateIterable states = chooser.runDialog();
320                 if (states == null)
321                         return;
322                 new AltosCSVUI(AltosUI.this, states, chooser.file());
323         }
324
325         /* Load a flight log CSV file and display a pretty graph.
326          */
327
328         private void GraphData() {
329                 AltosDataChooser chooser;
330                 chooser = new AltosDataChooser(this);
331                 AltosStateIterable states = chooser.runDialog();
332                 if (states == null)
333                         return;
334                 try {
335                         new AltosGraphUI(states, chooser.file());
336                 } catch (InterruptedException ie) {
337                 } catch (IOException ie) {
338                 }
339         }
340
341         private void ConfigureAltosUI() {
342                 new AltosConfigureUI(AltosUI.this, voice);
343         }
344
345         private void IdleMonitor() {
346                 try {
347                         new AltosIdleMonitorUI(this);
348                 } catch (Exception e) {
349                 }
350         }
351
352         static AltosStateIterable open_logfile(File file) {
353                 try {
354                         FileInputStream in;
355
356                         in = new FileInputStream(file);
357                         if (file.getName().endsWith("telem"))
358                                 return new AltosTelemetryFile(in);
359                         else
360                                 return new AltosEepromFile(in);
361                 } catch (FileNotFoundException fe) {
362                         System.out.printf("%s\n", fe.getMessage());
363                         return null;
364                 }
365         }
366
367         static AltosWriter open_csv(File file) {
368                 try {
369                         return new AltosCSV(file);
370                 } catch (FileNotFoundException fe) {
371                         System.out.printf("%s\n", fe.getMessage());
372                         return null;
373                 }
374         }
375
376         static AltosWriter open_kml(File file) {
377                 try {
378                         return new AltosKML(file);
379                 } catch (FileNotFoundException fe) {
380                         System.out.printf("%s\n", fe.getMessage());
381                         return null;
382                 }
383         }
384
385         static final int process_none = 0;
386         static final int process_csv = 1;
387         static final int process_kml = 2;
388         static final int process_graph = 3;
389         static final int process_replay = 4;
390         static final int process_summary = 5;
391         static final int process_cat = 6;
392
393         static boolean process_csv(File input) {
394                 AltosStateIterable states = open_logfile(input);
395                 if (states == null)
396                         return false;
397
398                 File output = Altos.replace_extension(input,".csv");
399                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
400                 if (input.equals(output)) {
401                         System.out.printf("Not processing '%s'\n", input);
402                         return false;
403                 } else {
404                         AltosWriter writer = open_csv(output);
405                         if (writer == null)
406                                 return false;
407                         writer.write(states);
408                         writer.close();
409                 }
410                 return true;
411         }
412
413         static boolean process_kml(File input) {
414                 AltosStateIterable states = open_logfile(input);
415                 if (states == null)
416                         return false;
417
418                 File output = Altos.replace_extension(input,".kml");
419                 System.out.printf("Processing \"%s\" to \"%s\"\n", input, output);
420                 if (input.equals(output)) {
421                         System.out.printf("Not processing '%s'\n", input);
422                         return false;
423                 } else {
424                         AltosWriter writer = open_kml(output);
425                         if (writer == null)
426                                 return false;
427                         writer.write(states);
428                         writer.close();
429                         return true;
430                 }
431         }
432
433         static AltosStateIterable record_iterable(File file) {
434                 FileInputStream in;
435                 try {
436                         in = new FileInputStream(file);
437                 } catch (Exception e) {
438                         System.out.printf("Failed to open file '%s'\n", file);
439                         return null;
440                 }
441                 if (file.getName().endsWith("telem"))
442                         return new AltosTelemetryFile(in);
443                 else
444                         return new AltosEepromFile(in);
445         }
446
447         static AltosReplayReader replay_file(File file) {
448                 AltosStateIterable states = record_iterable(file);
449                 if (states == null)
450                         return null;
451                 return new AltosReplayReader(states.iterator(), file);
452         }
453
454         static boolean process_replay(File file) {
455                 AltosReplayReader reader = replay_file(file);
456                 if (reader == null)
457                         return false;
458                 AltosFlightUI flight_ui = new AltosFlightUI(new AltosVoice(), reader);
459                 flight_ui.set_exit_on_close();
460                 return true;
461         }
462
463         static boolean process_graph(File file) {
464                 AltosStateIterable states = record_iterable(file);
465                 if (states == null)
466                         return false;
467                 try {
468                         new AltosGraphUI(states, file);
469                         return true;
470                 } catch (InterruptedException ie) {
471                 } catch (IOException ie) {
472                 }
473                 return false;
474         }
475
476         static boolean process_summary(File file) {
477                 AltosStateIterable states = record_iterable(file);
478                 if (states == null)
479                         return false;
480                 try {
481                         AltosFlightStats stats = new AltosFlightStats(states);
482                         if (stats.serial > 0)
483                                 System.out.printf("Serial:       %5d\n", stats.serial);
484                         if (stats.flight > 0)
485                                 System.out.printf("Flight:       %5d\n", stats.flight);
486                         if (stats.year > 0)
487                                 System.out.printf("Date:    %04d-%02d-%02d\n",
488                                                   stats.year, stats.month, stats.day);
489                         if (stats.hour > 0)
490                                 System.out.printf("Time:      %02d:%02d:%02d UTC\n",
491                                                   stats.hour, stats.minute, stats.second);
492                         System.out.printf("Max height:  %6.0f m    %6.0f ft\n",
493                                           stats.max_height,
494                                           AltosConvert.meters_to_feet(stats.max_height));
495                         System.out.printf("Max speed:   %6.0f m/s  %6.0f ft/s  %6.4f Mach\n",
496                                           stats.max_speed,
497                                           AltosConvert.meters_to_feet(stats.max_speed),
498                                           AltosConvert.meters_to_mach(stats.max_speed));
499                         if (stats.max_acceleration != AltosLib.MISSING) {
500                                 System.out.printf("Max accel:   %6.0f m/s² %6.0f ft/s² %6.2f g\n",
501                                                   stats.max_acceleration,
502                                                   AltosConvert.meters_to_feet(stats.max_acceleration),
503                                                   AltosConvert.meters_to_g(stats.max_acceleration));
504                         }
505                         System.out.printf("Drogue rate: %6.0f m/s  %6.0f ft/s\n",
506                                           stats.state_speed[Altos.ao_flight_drogue],
507                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_drogue]));
508                         System.out.printf("Main rate:   %6.0f m/s  %6.0f ft/s\n",
509                                           stats.state_speed[Altos.ao_flight_main],
510                                           AltosConvert.meters_to_feet(stats.state_speed[Altos.ao_flight_main]));
511                         System.out.printf("Flight time: %6.0f s\n",
512                                           stats.state_end[Altos.ao_flight_main] -
513                                           stats.state_start[Altos.ao_flight_boost]);
514                         return true;
515                 } catch (InterruptedException ie) {
516                 } catch (IOException ie) {
517                 }
518                 return false;
519         }
520
521         static boolean process_cat(File file) {
522                 try {
523                         AltosStateIterable eef = record_iterable(file);
524
525                         System.out.printf ("process cat\n");
526                         for (AltosState state : eef) {
527                                 System.out.printf ("tick %d state %d height %g\n",
528                                                    state.tick, state.state, state.height());
529                                 if ((state.set & AltosState.set_gps) != 0)
530                                         System.out.printf ("time %g lat %g lon %g alt %g\n",
531                                                            state.time_since_boost(),
532                                                            state.gps.lat,
533                                                            state.gps.lon,
534                                                            state.gps.alt);
535                         }
536
537                 } catch (Exception e) {
538                         System.out.printf("Failed to open file '%s'\n", file);
539                         return false;
540                 }
541                 return true;
542         }
543
544         public static void help(int code) {
545                 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
546                 System.out.printf("  Options:\n");
547                 System.out.printf("    --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
548                 System.out.printf("    --replay <filename>\t\trelive the glory of past flights \n");
549                 System.out.printf("    --graph <filename>\t\tgraph a flight\n");
550                 System.out.printf("    --csv\tgenerate comma separated output for spreadsheets, etc\n");
551                 System.out.printf("    --kml\tgenerate KML output for use with Google Earth\n");
552                 System.exit(code);
553         }
554
555         public static void main(final String[] args) {
556                 int     errors = 0;
557                 load_library(null);
558                 try {
559                         UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
560                 } catch (Exception e) {
561                 }
562                 /* Handle batch-mode */
563                 if (args.length == 0) {
564                         AltosUI altosui = new AltosUI();
565
566                         java.util.List<AltosDevice> devices = AltosUSBDevice.list(Altos.product_basestation);
567                         if (devices != null)
568                                 for (AltosDevice device : devices)
569                                         altosui.telemetry_window(device);
570                 } else {
571                         int process = process_none;
572                         for (int i = 0; i < args.length; i++) {
573                                 if (args[i].equals("--help"))
574                                         help(0);
575                                 else if (args[i].equals("--fetchmaps")) {
576                                         if (args.length < i + 3) {
577                                                 help(1);
578                                         } else {
579                                                 double lat = Double.parseDouble(args[i+1]);
580                                                 double lon = Double.parseDouble(args[i+2]);
581 //                                              AltosSiteMap.prefetchMaps(lat, lon);
582                                                 i += 2;
583                                         }
584                                 } else if (args[i].equals("--replay"))
585                                         process = process_replay;
586                                 else if (args[i].equals("--kml"))
587                                         process = process_kml;
588                                 else if (args[i].equals("--csv"))
589                                         process = process_csv;
590                                 else if (args[i].equals("--graph"))
591                                         process = process_graph;
592                                 else if (args[i].equals("--summary"))
593                                         process = process_summary;
594                                 else if (args[i].equals("--cat"))
595                                         process = process_cat;
596                                 else if (args[i].startsWith("--"))
597                                         help(1);
598                                 else {
599                                         File file = new File(args[i]);
600                                         switch (process) {
601                                         case process_none:
602                                         case process_graph:
603                                                 if (!process_graph(file))
604                                                         ++errors;
605                                                 break;
606                                         case process_replay:
607                                                 if (!process_replay(file))
608                                                         ++errors;
609                                                 break;
610                                         case process_kml:
611                                                 if (!process_kml(file))
612                                                         ++errors;
613                                                 break;
614                                         case process_csv:
615                                                 if (!process_csv(file))
616                                                         ++errors;
617                                                 break;
618                                         case process_summary:
619                                                 if (!process_summary(file))
620                                                         ++errors;
621                                                 break;
622                                         case process_cat:
623                                                 if (!process_cat(file))
624                                                         ++errors;
625                                         }
626                                 }
627                         }
628                 }
629                 if (errors != 0)
630                         System.exit(errors);
631         }
632 }