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