2 * Copyright © 2014 Keith Packard <keithp@keithp.com>
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.
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.
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.
18 package org.altusmetrum.telegps;
21 import java.awt.event.*;
24 import java.util.concurrent.*;
26 import org.altusmetrum.altoslib_4.*;
27 import org.altusmetrum.altosuilib_2.*;
31 implements AltosFlightDisplay, AltosFontListener, AltosUnitsListener, ActionListener
34 static String[] telegps_icon_names = {
43 static { set_icon_names(telegps_icon_names); }
45 static AltosVoice voice;
47 static AltosVoice voice() {
49 voice = new AltosVoice();
53 AltosFlightReader reader;
54 TeleGPSDisplayThread thread;
61 AltosFreqList frequencies;
65 TeleGPSStatus telegps_status;
66 TeleGPSStatusUpdate status_update;
72 AltosInfoTable info_table;
74 LinkedList<AltosFlightDisplay> displays;
77 final static String new_command = "new";
78 final static String preferences_command = "preferences";
79 final static String load_maps_command = "loadmaps";
80 final static String close_command = "close";
81 final static String exit_command = "exit";
83 static final String[][] file_menu_entries = new String[][] {
84 { "New Window", new_command },
85 { "Preferences", preferences_command },
86 { "Load Maps", load_maps_command },
87 { "Close", close_command },
88 { "Exit", exit_command },
92 final static String monitor_command = "monitor";
93 final static String disconnect_command = "disconnect";
94 final static String scan_command = "scan";
96 static final String[][] monitor_menu_entries = new String[][] {
97 { "Monitor Device", monitor_command },
98 { "Disconnect", disconnect_command },
99 { "Scan Channels", scan_command },
103 final static String download_command = "download";
104 final static String export_command = "export";
105 final static String graph_command = "graph";
106 final static String configure_command = "configure";
107 final static String flash_command = "flash";
109 static final String[][] device_menu_entries = new String[][] {
110 { "Download Data", download_command },
111 { "Configure Device", configure_command },
112 { "Export Data", export_command },
113 { "Graph Data", graph_command },
114 { "Flash Device", flash_command },
117 void stop_display() {
118 if (thread != null && thread.isAlive()) {
122 } catch (InterruptedException ie) {}
127 public void reset() {
128 for (AltosFlightDisplay display : displays)
132 public void font_size_changed(int font_size) {
133 for (AltosFlightDisplay display : displays)
134 display.font_size_changed(font_size);
137 public void units_changed(boolean imperial_units) {
138 for (AltosFlightDisplay display : displays)
139 display.units_changed(imperial_units);
142 public void show(AltosState state, AltosListenerState listener_state) {
144 status_update.saved_state = state;
147 state = new AltosState();
150 for (AltosFlightDisplay display : displays) {
151 display.show(state, listener_state);
154 } catch (Exception ex) {
155 System.out.printf("Exception %s\n", ex.toString());
156 for (StackTraceElement e : ex.getStackTrace())
157 System.out.printf("%s\n", e.toString());
166 new TeleGPSPreferences(this, voice());
170 new AltosUIMapPreload(this);
176 remove_frequency_menu();
179 void connect(AltosDevice device) {
183 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
185 add_frequency_menu(device.getSerial(), reader);
186 } catch (FileNotFoundException ee) {
187 JOptionPane.showMessageDialog(this,
189 String.format ("Cannot open %s", device.toShortString()),
190 JOptionPane.ERROR_MESSAGE);
191 } catch (AltosSerialInUseException si) {
192 JOptionPane.showMessageDialog(this,
193 String.format("Device \"%s\" already in use",
194 device.toShortString()),
196 JOptionPane.ERROR_MESSAGE);
197 } catch (IOException ee) {
198 JOptionPane.showMessageDialog(this,
199 String.format ("Unknown I/O error on %s", device.toShortString()),
201 JOptionPane.ERROR_MESSAGE);
202 } catch (TimeoutException te) {
203 JOptionPane.showMessageDialog(this,
204 String.format ("Timeout on %s", device.toShortString()),
206 JOptionPane.ERROR_MESSAGE);
207 } catch (InterruptedException ie) {
208 JOptionPane.showMessageDialog(this,
209 String.format("Interrupted %s", device.toShortString()),
210 "Interrupted exception",
211 JOptionPane.ERROR_MESSAGE);
216 AltosDevice device = AltosDeviceUIDialog.show(this,
217 AltosLib.product_basestation);
223 public void scan_device_selected(AltosDevice device) {
228 new AltosScanUI(this, false);
232 new AltosEepromManage(this, AltosLib.product_telegps);
236 new TeleGPSConfig(this);
240 AltosDataChooser chooser;
241 chooser = new AltosDataChooser(this);
242 AltosStateIterable states = chooser.runDialog();
245 new AltosCSVUI(this, states, chooser.file());
249 AltosDataChooser chooser;
250 chooser = new AltosDataChooser(this);
251 AltosStateIterable states = chooser.runDialog();
255 new TeleGPSGraphUI(states, chooser.file());
256 } catch (InterruptedException ie) {
257 } catch (IOException ie) {
262 AltosFlashUI.show(this);
265 public void actionPerformed(ActionEvent ev) {
268 if (new_command.equals(ev.getActionCommand())) {
272 if (preferences_command.equals(ev.getActionCommand())) {
276 if (load_maps_command.equals(ev.getActionCommand())) {
280 if (close_command.equals(ev.getActionCommand())) {
284 if (exit_command.equals(ev.getActionCommand()))
288 if (monitor_command.equals(ev.getActionCommand())) {
292 if (disconnect_command.equals(ev.getActionCommand())) {
296 if (scan_command.equals(ev.getActionCommand())) {
302 if (download_command.equals(ev.getActionCommand())) {
306 if (configure_command.equals(ev.getActionCommand())) {
310 if (export_command.equals(ev.getActionCommand())) {
314 if (graph_command.equals(ev.getActionCommand())) {
318 if (flash_command.equals(ev.getActionCommand())) {
324 void add_frequency_menu(int serial, final AltosFlightReader reader) {
326 if (frequencies != null)
329 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
330 frequencies.set_product("Monitor");
331 frequencies.set_serial(serial);
332 frequencies.addActionListener(new ActionListener() {
333 public void actionPerformed(ActionEvent e) {
334 double frequency = frequencies.frequency();
336 reader.set_frequency(frequency);
337 } catch (TimeoutException te) {
338 } catch (InterruptedException ie) {
340 reader.save_frequency();
343 menu_bar.add(frequencies);
346 void remove_frequency_menu() {
347 if (frequencies != null) {
348 menu_bar.remove(frequencies);
354 public void set_reader(AltosFlightReader reader) {
355 setTitle(String.format("TeleGPS %s", reader.name));
356 thread = new TeleGPSDisplayThread(this, voice(), this, reader);
360 static int number_of_windows;
362 static public void add_window() {
366 static public void subtract_window() {
368 if (number_of_windows == 0)
372 private void close() {
374 AltosUIPreferences.unregister_font_listener(this);
375 AltosPreferences.unregister_units_listener(this);
381 private void add_menu(JMenu menu, String label, String action) {
382 JMenuItem item = new JMenuItem(label);
384 item.addActionListener(this);
385 item.setActionCommand(action);
389 private JMenu make_menu(String label, String[][] items) {
390 JMenu menu = new JMenu(label);
391 for (int i = 0; i < items.length; i++)
392 add_menu(menu, items[i][0], items[i][1]);
399 AltosUIPreferences.set_component(this);
403 bag = getContentPane();
404 bag.setLayout(new GridBagLayout());
406 GridBagConstraints c = new GridBagConstraints();
410 menu_bar = new JMenuBar();
411 setJMenuBar(menu_bar);
413 file_menu = make_menu("File", file_menu_entries);
414 monitor_menu = make_menu("Monitor", monitor_menu_entries);
415 device_menu = make_menu("Device", device_menu_entries);
416 displays = new LinkedList<AltosFlightDisplay>();
420 /* TeleGPS status is always visible */
421 telegps_status = new TeleGPSStatus();
424 c.fill = GridBagConstraints.HORIZONTAL;
427 bag.add(telegps_status, c);
429 displays.add(telegps_status);
432 /* The rest of the window uses a tabbed pane to
433 * show one of the alternate data views
435 pane = new JTabbedPane();
437 /* Make the tabbed pane use the rest of the window space */
440 c.fill = GridBagConstraints.BOTH;
446 map = new AltosUIMap();
447 pane.add("Map", map);
450 gps_info = new TeleGPSInfo();
451 pane.add("Info", gps_info);
452 displays.add(gps_info);
454 info_table = new AltosInfoTable();
455 pane.add("Table", info_table);
456 displays.add(info_table);
458 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
460 AltosUIPreferences.register_font_listener(this);
461 AltosPreferences.register_units_listener(this);
463 addWindowListener(new WindowAdapter() {
465 public void windowClosing(WindowEvent e) {
475 status_update = new TeleGPSStatusUpdate(telegps_status);
477 new javax.swing.Timer(100, status_update).start();
480 public TeleGPS(AltosFlightReader reader) {
485 public TeleGPS(AltosDevice device) {
490 static AltosStateIterable record_iterable(File file) {
493 in = new FileInputStream(file);
494 } catch (Exception e) {
495 System.out.printf("Failed to open file '%s'\n", file);
498 if (file.getName().endsWith("telem"))
499 return new AltosTelemetryFile(in);
501 return new AltosEepromFile(in);
504 static AltosReplayReader replay_file(File file) {
505 AltosStateIterable states = record_iterable(file);
508 return new AltosReplayReader(states.iterator(), file);
511 static boolean process_graph(File file) {
512 AltosStateIterable states = record_iterable(file);
516 new TeleGPSGraphUI(states, file);
517 } catch (Exception e) {
523 static boolean process_replay(File file) {
524 AltosReplayReader new_reader = replay_file(file);
525 if (new_reader == null)
528 new TeleGPS(new_reader);
532 static final int process_none = 0;
533 static final int process_csv = 1;
534 static final int process_kml = 2;
535 static final int process_graph = 3;
536 static final int process_replay = 4;
537 static final int process_summary = 5;
538 static final int process_cat = 6;
540 public static boolean load_library(Frame frame) {
541 if (!AltosUILib.load_library()) {
542 JOptionPane.showMessageDialog(frame,
543 String.format("No AltOS library in \"%s\"",
544 System.getProperty("java.library.path","<undefined>")),
545 "Cannot load device access library",
546 JOptionPane.ERROR_MESSAGE);
552 public static void help(int code) {
553 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
554 System.out.printf(" Options:\n");
555 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
556 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
557 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
558 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
559 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
563 public static void main(String[] args) {
568 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
569 } catch (Exception e) {
572 boolean any_created = false;
575 /* Handle batch-mode */
576 int process = process_none;
577 for (int i = 0; i < args.length; i++) {
578 if (args[i].equals("--help"))
580 else if (args[i].equals("--fetchmaps")) {
581 if (args.length < i + 3) {
584 double lat = Double.parseDouble(args[i+1]);
585 double lon = Double.parseDouble(args[i+2]);
586 AltosUIMap.prefetch_maps(lat, lon);
589 } else if (args[i].equals("--replay"))
590 process = process_replay;
591 else if (args[i].equals("--kml"))
592 process = process_kml;
593 else if (args[i].equals("--csv"))
594 process = process_csv;
595 else if (args[i].equals("--graph"))
596 process = process_graph;
597 else if (args[i].equals("--summary"))
598 process = process_summary;
599 else if (args[i].equals("--cat"))
600 process = process_cat;
601 else if (args[i].startsWith("--"))
604 File file = new File(args[i]);
608 if (!process_graph(file))
612 if (!process_replay(file))
622 case process_summary:
632 if (number_of_windows == 0) {
633 java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_basestation);
635 for (AltosDevice device : devices) {
639 if (number_of_windows == 0)