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