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;
65 final static String new_command = "new";
66 final static String preferences_command = "preferences";
67 final static String load_maps_command = "loadmaps";
68 final static String close_command = "close";
69 final static String exit_command = "exit";
71 static final String[][] file_menu_entries = new String[][] {
72 { "New Window", new_command },
73 { "Preferences", preferences_command },
74 { "Load Maps", load_maps_command },
75 { "Close", close_command },
76 { "Exit", exit_command },
80 final static String monitor_command = "monitor";
81 final static String disconnect_command = "disconnect";
82 final static String scan_command = "scan";
84 static final String[][] monitor_menu_entries = new String[][] {
85 { "Monitor Device", monitor_command },
86 { "Disconnect", disconnect_command },
87 { "Scan Channels", scan_command },
91 final static String download_command = "download";
92 final static String configure_command = "configure";
93 final static String export_command = "export";
94 final static String graph_command = "graph";
96 static final String[][] device_menu_entries = new String[][] {
97 { "Download Data", download_command },
98 { "Configure Device", configure_command },
99 { "Export Data", export_command },
100 { "Graph Data", graph_command },
103 // private AltosInfoTable flightInfo;
105 boolean exit_on_close = false;
107 void stop_display() {
108 if (thread != null && thread.isAlive()) {
112 } catch (InterruptedException ie) {}
117 public void reset() {
122 public void set_font() {
127 public void font_size_changed(int font_size) {
132 // AltosFlightStatusUpdate status_update;
134 public void show(AltosState state, AltosListenerState listener_state) {
135 // status_update.saved_state = state;
138 state = new AltosState();
140 sitemap.show(state, listener_state);
141 gps_info.show(state, listener_state);
142 telegps_status.show(state, listener_state);
146 AltosFreqList frequencies;
148 TeleGPSStatus telegps_status;
149 TeleGPSStatusUpdate status_update;
151 ActionListener show_timer;
158 new TeleGPSPreferences(this, voice());
162 new AltosSiteMapPreload(this);
168 remove_frequency_menu();
171 void connect(AltosDevice device) {
175 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
177 add_frequency_menu(device.getSerial(), reader);
178 } catch (FileNotFoundException ee) {
179 JOptionPane.showMessageDialog(this,
181 String.format ("Cannot open %s", device.toShortString()),
182 JOptionPane.ERROR_MESSAGE);
183 } catch (AltosSerialInUseException si) {
184 JOptionPane.showMessageDialog(this,
185 String.format("Device \"%s\" already in use",
186 device.toShortString()),
188 JOptionPane.ERROR_MESSAGE);
189 } catch (IOException ee) {
190 JOptionPane.showMessageDialog(this,
191 String.format ("Unknown I/O error on %s", device.toShortString()),
193 JOptionPane.ERROR_MESSAGE);
194 } catch (TimeoutException te) {
195 JOptionPane.showMessageDialog(this,
196 String.format ("Timeout on %s", device.toShortString()),
198 JOptionPane.ERROR_MESSAGE);
199 } catch (InterruptedException ie) {
200 JOptionPane.showMessageDialog(this,
201 String.format("Interrupted %s", device.toShortString()),
202 "Interrupted exception",
203 JOptionPane.ERROR_MESSAGE);
208 AltosDevice device = AltosDeviceUIDialog.show(this,
209 AltosLib.product_basestation);
215 public void scan_device_selected(AltosDevice device) {
220 new AltosScanUI(this, false);
224 new AltosEepromManage(this, AltosLib.product_telegps);
228 new TeleGPSConfig(this);
232 AltosDataChooser chooser;
233 chooser = new AltosDataChooser(this);
234 AltosStateIterable states = chooser.runDialog();
237 new AltosCSVUI(this, states, chooser.file());
243 public void actionPerformed(ActionEvent ev) {
246 if (new_command.equals(ev.getActionCommand())) {
250 if (preferences_command.equals(ev.getActionCommand())) {
254 if (load_maps_command.equals(ev.getActionCommand())) {
258 if (close_command.equals(ev.getActionCommand())) {
262 if (exit_command.equals(ev.getActionCommand()))
266 if (monitor_command.equals(ev.getActionCommand())) {
270 if (disconnect_command.equals(ev.getActionCommand())) {
274 if (scan_command.equals(ev.getActionCommand())) {
280 if (download_command.equals(ev.getActionCommand())) {
284 if (configure_command.equals(ev.getActionCommand())) {
288 if (export_command.equals(ev.getActionCommand())) {
292 if (graph_command.equals(ev.getActionCommand())) {
298 void add_frequency_menu(int serial, final AltosFlightReader reader) {
300 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
301 frequencies.set_product("Monitor");
302 frequencies.set_serial(serial);
303 frequencies.addActionListener(new ActionListener() {
304 public void actionPerformed(ActionEvent e) {
305 double frequency = frequencies.frequency();
307 reader.set_frequency(frequency);
308 } catch (TimeoutException te) {
309 } catch (InterruptedException ie) {
311 reader.save_frequency();
314 menu_bar.add(frequencies);
317 void remove_frequency_menu() {
318 if (frequencies != null) {
319 menu_bar.remove(frequencies);
324 public void set_reader(AltosFlightReader reader) {
325 setTitle(String.format("TeleGPS %s", reader.name));
326 thread = new AltosDisplayThread(this, voice(), this, reader);
330 static int number_of_windows;
332 private void close() {
333 AltosUIPreferences.unregister_font_listener(this);
337 if (number_of_windows == 0)
341 private void add_menu(JMenu menu, String label, String action) {
342 JMenuItem item = new JMenuItem(label);
344 item.addActionListener(this);
345 item.setActionCommand(action);
349 private JMenu make_menu(String label, String[][] items) {
350 JMenu menu = new JMenu(label);
351 for (int i = 0; i < items.length; i++)
352 add_menu(menu, items[i][0], items[i][1]);
359 AltosUIPreferences.set_component(this);
363 bag = getContentPane();
364 bag.setLayout(new GridBagLayout());
366 GridBagConstraints c = new GridBagConstraints();
370 menu_bar = new JMenuBar();
371 setJMenuBar(menu_bar);
373 file_menu = make_menu("File", file_menu_entries);
374 monitor_menu = make_menu("Monitor", monitor_menu_entries);
375 device_menu = make_menu("Device", device_menu_entries);
379 /* TeleGPS status is always visible */
380 telegps_status = new TeleGPSStatus();
383 c.fill = GridBagConstraints.HORIZONTAL;
386 bag.add(telegps_status, c);
390 /* The rest of the window uses a tabbed pane to
391 * show one of the alternate data views
393 pane = new JTabbedPane();
395 /* Make the tabbed pane use the rest of the window space */
398 c.fill = GridBagConstraints.BOTH;
404 sitemap = new AltosSiteMap();
405 pane.add("Site Map", sitemap);
407 gps_info = new TeleGPSInfo();
408 pane.add("Info", gps_info);
410 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
412 AltosUIPreferences.register_font_listener(this);
414 addWindowListener(new WindowAdapter() {
416 public void windowClosing(WindowEvent e) {
426 status_update = new TeleGPSStatusUpdate(telegps_status);
428 new javax.swing.Timer(100, status_update).start();
431 public TeleGPS(AltosFlightReader reader) {
436 public TeleGPS(AltosDevice device) {
441 static AltosStateIterable record_iterable(File file) {
444 in = new FileInputStream(file);
445 } catch (Exception e) {
446 System.out.printf("Failed to open file '%s'\n", file);
449 if (file.getName().endsWith("telem"))
450 return new AltosTelemetryFile(in);
452 return new AltosEepromFile(in);
455 static AltosReplayReader replay_file(File file) {
456 AltosStateIterable states = record_iterable(file);
459 return new AltosReplayReader(states.iterator(), file);
462 static boolean process_replay(File file) {
463 AltosReplayReader new_reader = replay_file(file);
464 if (new_reader == null)
467 new TeleGPS(new_reader);
471 static final int process_none = 0;
472 static final int process_csv = 1;
473 static final int process_kml = 2;
474 static final int process_graph = 3;
475 static final int process_replay = 4;
476 static final int process_summary = 5;
477 static final int process_cat = 6;
479 public static boolean load_library(Frame frame) {
480 if (!AltosUILib.load_library()) {
481 JOptionPane.showMessageDialog(frame,
482 String.format("No AltOS library in \"%s\"",
483 System.getProperty("java.library.path","<undefined>")),
484 "Cannot load device access library",
485 JOptionPane.ERROR_MESSAGE);
491 public static void help(int code) {
492 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
493 System.out.printf(" Options:\n");
494 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
495 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
496 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
497 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
498 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
502 public static void main(String[] args) {
507 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
508 } catch (Exception e) {
511 boolean any_created = false;
514 /* Handle batch-mode */
515 int process = process_none;
516 for (int i = 0; i < args.length; i++) {
517 if (args[i].equals("--help"))
519 else if (args[i].equals("--fetchmaps")) {
520 if (args.length < i + 3) {
523 double lat = Double.parseDouble(args[i+1]);
524 double lon = Double.parseDouble(args[i+2]);
525 AltosSiteMap.prefetchMaps(lat, lon);
528 } else if (args[i].equals("--replay"))
529 process = process_replay;
530 else if (args[i].equals("--kml"))
531 process = process_kml;
532 else if (args[i].equals("--csv"))
533 process = process_csv;
534 else if (args[i].equals("--graph"))
535 process = process_graph;
536 else if (args[i].equals("--summary"))
537 process = process_summary;
538 else if (args[i].equals("--cat"))
539 process = process_cat;
540 else if (args[i].startsWith("--"))
543 File file = new File(args[i]);
550 if (!process_replay(file))
560 case process_summary:
571 java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_basestation);
573 for (AltosDevice device : devices) {