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