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.*;
29 public class TeleGPS extends AltosUIFrame implements AltosFlightDisplay, AltosFontListener, ActionListener {
31 static String[] telegps_icon_names = {
40 static { set_icon_names(telegps_icon_names); }
42 static AltosVoice voice;
44 static AltosVoice voice() {
46 voice = new AltosVoice();
50 AltosFlightReader reader;
51 AltosDisplayThread thread;
58 AltosFreqList frequencies;
62 TeleGPSStatus telegps_status;
63 TeleGPSStatusUpdate status_update;
69 AltosInfoTable info_table;
71 LinkedList<AltosFlightDisplay> displays;
74 final static String new_command = "new";
75 final static String preferences_command = "preferences";
76 final static String load_maps_command = "loadmaps";
77 final static String close_command = "close";
78 final static String exit_command = "exit";
80 static final String[][] file_menu_entries = new String[][] {
81 { "New Window", new_command },
82 { "Preferences", preferences_command },
83 { "Load Maps", load_maps_command },
84 { "Close", close_command },
85 { "Exit", exit_command },
89 final static String monitor_command = "monitor";
90 final static String disconnect_command = "disconnect";
91 final static String scan_command = "scan";
93 static final String[][] monitor_menu_entries = new String[][] {
94 { "Monitor Device", monitor_command },
95 { "Disconnect", disconnect_command },
96 { "Scan Channels", scan_command },
100 final static String download_command = "download";
101 final static String export_command = "export";
102 final static String graph_command = "graph";
103 final static String configure_command = "configure";
104 final static String flash_command = "flash";
106 static final String[][] device_menu_entries = new String[][] {
107 { "Download Data", download_command },
108 { "Configure Device", configure_command },
109 { "Export Data", export_command },
110 { "Graph Data", graph_command },
111 { "Flash Device", flash_command },
114 void stop_display() {
115 if (thread != null && thread.isAlive()) {
119 } catch (InterruptedException ie) {}
124 public void reset() {
125 for (AltosFlightDisplay display : displays)
129 public void set_font() {
130 for (AltosFlightDisplay display : displays)
134 public void font_size_changed(int font_size) {
138 public void show(AltosState state, AltosListenerState listener_state) {
140 status_update.saved_state = state;
143 state = new AltosState();
146 for (AltosFlightDisplay display : displays) {
147 display.show(state, listener_state);
150 } catch (Exception ex) {
151 System.out.printf("Exception %s\n", ex.toString());
152 for (StackTraceElement e : ex.getStackTrace())
153 System.out.printf("%s\n", e.toString());
162 new TeleGPSPreferences(this, voice());
166 new AltosSiteMapPreload(this);
172 remove_frequency_menu();
175 void connect(AltosDevice device) {
179 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
181 add_frequency_menu(device.getSerial(), reader);
182 } catch (FileNotFoundException ee) {
183 JOptionPane.showMessageDialog(this,
185 String.format ("Cannot open %s", device.toShortString()),
186 JOptionPane.ERROR_MESSAGE);
187 } catch (AltosSerialInUseException si) {
188 JOptionPane.showMessageDialog(this,
189 String.format("Device \"%s\" already in use",
190 device.toShortString()),
192 JOptionPane.ERROR_MESSAGE);
193 } catch (IOException ee) {
194 JOptionPane.showMessageDialog(this,
195 String.format ("Unknown I/O error on %s", device.toShortString()),
197 JOptionPane.ERROR_MESSAGE);
198 } catch (TimeoutException te) {
199 JOptionPane.showMessageDialog(this,
200 String.format ("Timeout on %s", device.toShortString()),
202 JOptionPane.ERROR_MESSAGE);
203 } catch (InterruptedException ie) {
204 JOptionPane.showMessageDialog(this,
205 String.format("Interrupted %s", device.toShortString()),
206 "Interrupted exception",
207 JOptionPane.ERROR_MESSAGE);
212 AltosDevice device = AltosDeviceUIDialog.show(this,
213 AltosLib.product_basestation);
219 public void scan_device_selected(AltosDevice device) {
224 new AltosScanUI(this, false);
228 new AltosEepromManage(this, AltosLib.product_telegps);
232 new TeleGPSConfig(this);
236 AltosDataChooser chooser;
237 chooser = new AltosDataChooser(this);
238 AltosStateIterable states = chooser.runDialog();
241 new AltosCSVUI(this, states, chooser.file());
248 AltosFlashUI.show(this);
251 public void actionPerformed(ActionEvent ev) {
254 if (new_command.equals(ev.getActionCommand())) {
258 if (preferences_command.equals(ev.getActionCommand())) {
262 if (load_maps_command.equals(ev.getActionCommand())) {
266 if (close_command.equals(ev.getActionCommand())) {
270 if (exit_command.equals(ev.getActionCommand()))
274 if (monitor_command.equals(ev.getActionCommand())) {
278 if (disconnect_command.equals(ev.getActionCommand())) {
282 if (scan_command.equals(ev.getActionCommand())) {
288 if (download_command.equals(ev.getActionCommand())) {
292 if (configure_command.equals(ev.getActionCommand())) {
296 if (export_command.equals(ev.getActionCommand())) {
300 if (graph_command.equals(ev.getActionCommand())) {
304 if (flash_command.equals(ev.getActionCommand())) {
310 void add_frequency_menu(int serial, final AltosFlightReader reader) {
312 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
313 frequencies.set_product("Monitor");
314 frequencies.set_serial(serial);
315 frequencies.addActionListener(new ActionListener() {
316 public void actionPerformed(ActionEvent e) {
317 double frequency = frequencies.frequency();
319 reader.set_frequency(frequency);
320 } catch (TimeoutException te) {
321 } catch (InterruptedException ie) {
323 reader.save_frequency();
326 menu_bar.add(frequencies);
329 void remove_frequency_menu() {
330 if (frequencies != null) {
331 menu_bar.remove(frequencies);
336 public void set_reader(AltosFlightReader reader) {
337 setTitle(String.format("TeleGPS %s", reader.name));
338 thread = new AltosDisplayThread(this, voice(), this, reader);
342 static int number_of_windows;
344 private void close() {
345 AltosUIPreferences.unregister_font_listener(this);
349 if (number_of_windows == 0)
353 private void add_menu(JMenu menu, String label, String action) {
354 JMenuItem item = new JMenuItem(label);
356 item.addActionListener(this);
357 item.setActionCommand(action);
361 private JMenu make_menu(String label, String[][] items) {
362 JMenu menu = new JMenu(label);
363 for (int i = 0; i < items.length; i++)
364 add_menu(menu, items[i][0], items[i][1]);
371 AltosUIPreferences.set_component(this);
375 bag = getContentPane();
376 bag.setLayout(new GridBagLayout());
378 GridBagConstraints c = new GridBagConstraints();
382 menu_bar = new JMenuBar();
383 setJMenuBar(menu_bar);
385 file_menu = make_menu("File", file_menu_entries);
386 monitor_menu = make_menu("Monitor", monitor_menu_entries);
387 device_menu = make_menu("Device", device_menu_entries);
388 displays = new LinkedList<AltosFlightDisplay>();
392 /* TeleGPS status is always visible */
393 telegps_status = new TeleGPSStatus();
396 c.fill = GridBagConstraints.HORIZONTAL;
399 bag.add(telegps_status, c);
401 displays.add(telegps_status);
404 /* The rest of the window uses a tabbed pane to
405 * show one of the alternate data views
407 pane = new JTabbedPane();
409 /* Make the tabbed pane use the rest of the window space */
412 c.fill = GridBagConstraints.BOTH;
418 sitemap = new AltosSiteMap();
419 pane.add("Site Map", sitemap);
420 displays.add(sitemap);
422 gps_info = new TeleGPSInfo();
423 pane.add("Info", gps_info);
424 displays.add(gps_info);
426 info_table = new AltosInfoTable();
427 pane.add("Table", info_table);
428 displays.add(info_table);
430 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
432 AltosUIPreferences.register_font_listener(this);
434 addWindowListener(new WindowAdapter() {
436 public void windowClosing(WindowEvent e) {
446 status_update = new TeleGPSStatusUpdate(telegps_status);
448 new javax.swing.Timer(100, status_update).start();
451 public TeleGPS(AltosFlightReader reader) {
456 public TeleGPS(AltosDevice device) {
461 static AltosStateIterable record_iterable(File file) {
464 in = new FileInputStream(file);
465 } catch (Exception e) {
466 System.out.printf("Failed to open file '%s'\n", file);
469 if (file.getName().endsWith("telem"))
470 return new AltosTelemetryFile(in);
472 return new AltosEepromFile(in);
475 static AltosReplayReader replay_file(File file) {
476 AltosStateIterable states = record_iterable(file);
479 return new AltosReplayReader(states.iterator(), file);
482 static boolean process_replay(File file) {
483 AltosReplayReader new_reader = replay_file(file);
484 if (new_reader == null)
487 new TeleGPS(new_reader);
491 static final int process_none = 0;
492 static final int process_csv = 1;
493 static final int process_kml = 2;
494 static final int process_graph = 3;
495 static final int process_replay = 4;
496 static final int process_summary = 5;
497 static final int process_cat = 6;
499 public static boolean load_library(Frame frame) {
500 if (!AltosUILib.load_library()) {
501 JOptionPane.showMessageDialog(frame,
502 String.format("No AltOS library in \"%s\"",
503 System.getProperty("java.library.path","<undefined>")),
504 "Cannot load device access library",
505 JOptionPane.ERROR_MESSAGE);
511 public static void help(int code) {
512 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
513 System.out.printf(" Options:\n");
514 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
515 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
516 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
517 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
518 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
522 public static void main(String[] args) {
527 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
528 } catch (Exception e) {
531 boolean any_created = false;
534 /* Handle batch-mode */
535 int process = process_none;
536 for (int i = 0; i < args.length; i++) {
537 if (args[i].equals("--help"))
539 else if (args[i].equals("--fetchmaps")) {
540 if (args.length < i + 3) {
543 double lat = Double.parseDouble(args[i+1]);
544 double lon = Double.parseDouble(args[i+2]);
545 AltosSiteMap.prefetchMaps(lat, lon);
548 } else if (args[i].equals("--replay"))
549 process = process_replay;
550 else if (args[i].equals("--kml"))
551 process = process_kml;
552 else if (args[i].equals("--csv"))
553 process = process_csv;
554 else if (args[i].equals("--graph"))
555 process = process_graph;
556 else if (args[i].equals("--summary"))
557 process = process_summary;
558 else if (args[i].equals("--cat"))
559 process = process_cat;
560 else if (args[i].startsWith("--"))
563 File file = new File(args[i]);
570 if (!process_replay(file))
580 case process_summary:
591 java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_basestation);
593 for (AltosDevice device : devices) {