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 export_command = "export";
93 final static String graph_command = "graph";
94 final static String configure_command = "configure";
95 final static String flash_command = "flash";
97 static final String[][] device_menu_entries = new String[][] {
98 { "Download Data", download_command },
99 { "Configure Device", configure_command },
100 { "Export Data", export_command },
101 { "Graph Data", graph_command },
102 { "Flash Device", flash_command },
105 void stop_display() {
106 if (thread != null && thread.isAlive()) {
110 } catch (InterruptedException ie) {}
115 public void reset() {
120 public void set_font() {
125 public void font_size_changed(int font_size) {
130 // AltosFlightStatusUpdate status_update;
132 public void show(AltosState state, AltosListenerState listener_state) {
133 // status_update.saved_state = state;
136 state = new AltosState();
138 sitemap.show(state, listener_state);
139 gps_info.show(state, listener_state);
140 telegps_status.show(state, listener_state);
144 AltosFreqList frequencies;
146 TeleGPSStatus telegps_status;
147 TeleGPSStatusUpdate status_update;
149 ActionListener show_timer;
156 new TeleGPSPreferences(this, voice());
160 new AltosSiteMapPreload(this);
166 remove_frequency_menu();
169 void connect(AltosDevice device) {
173 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
175 add_frequency_menu(device.getSerial(), reader);
176 } catch (FileNotFoundException ee) {
177 JOptionPane.showMessageDialog(this,
179 String.format ("Cannot open %s", device.toShortString()),
180 JOptionPane.ERROR_MESSAGE);
181 } catch (AltosSerialInUseException si) {
182 JOptionPane.showMessageDialog(this,
183 String.format("Device \"%s\" already in use",
184 device.toShortString()),
186 JOptionPane.ERROR_MESSAGE);
187 } catch (IOException ee) {
188 JOptionPane.showMessageDialog(this,
189 String.format ("Unknown I/O error on %s", device.toShortString()),
191 JOptionPane.ERROR_MESSAGE);
192 } catch (TimeoutException te) {
193 JOptionPane.showMessageDialog(this,
194 String.format ("Timeout on %s", device.toShortString()),
196 JOptionPane.ERROR_MESSAGE);
197 } catch (InterruptedException ie) {
198 JOptionPane.showMessageDialog(this,
199 String.format("Interrupted %s", device.toShortString()),
200 "Interrupted exception",
201 JOptionPane.ERROR_MESSAGE);
206 AltosDevice device = AltosDeviceUIDialog.show(this,
207 AltosLib.product_basestation);
213 public void scan_device_selected(AltosDevice device) {
218 new AltosScanUI(this, false);
222 new AltosEepromManage(this, AltosLib.product_telegps);
226 new TeleGPSConfig(this);
230 AltosDataChooser chooser;
231 chooser = new AltosDataChooser(this);
232 AltosStateIterable states = chooser.runDialog();
235 new AltosCSVUI(this, states, chooser.file());
242 AltosFlashUI.show(this);
245 public void actionPerformed(ActionEvent ev) {
248 if (new_command.equals(ev.getActionCommand())) {
252 if (preferences_command.equals(ev.getActionCommand())) {
256 if (load_maps_command.equals(ev.getActionCommand())) {
260 if (close_command.equals(ev.getActionCommand())) {
264 if (exit_command.equals(ev.getActionCommand()))
268 if (monitor_command.equals(ev.getActionCommand())) {
272 if (disconnect_command.equals(ev.getActionCommand())) {
276 if (scan_command.equals(ev.getActionCommand())) {
282 if (download_command.equals(ev.getActionCommand())) {
286 if (configure_command.equals(ev.getActionCommand())) {
290 if (export_command.equals(ev.getActionCommand())) {
294 if (graph_command.equals(ev.getActionCommand())) {
298 if (flash_command.equals(ev.getActionCommand())) {
304 void add_frequency_menu(int serial, final AltosFlightReader reader) {
306 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
307 frequencies.set_product("Monitor");
308 frequencies.set_serial(serial);
309 frequencies.addActionListener(new ActionListener() {
310 public void actionPerformed(ActionEvent e) {
311 double frequency = frequencies.frequency();
313 reader.set_frequency(frequency);
314 } catch (TimeoutException te) {
315 } catch (InterruptedException ie) {
317 reader.save_frequency();
320 menu_bar.add(frequencies);
323 void remove_frequency_menu() {
324 if (frequencies != null) {
325 menu_bar.remove(frequencies);
330 public void set_reader(AltosFlightReader reader) {
331 setTitle(String.format("TeleGPS %s", reader.name));
332 thread = new AltosDisplayThread(this, voice(), this, reader);
336 static int number_of_windows;
338 private void close() {
339 AltosUIPreferences.unregister_font_listener(this);
343 if (number_of_windows == 0)
347 private void add_menu(JMenu menu, String label, String action) {
348 JMenuItem item = new JMenuItem(label);
350 item.addActionListener(this);
351 item.setActionCommand(action);
355 private JMenu make_menu(String label, String[][] items) {
356 JMenu menu = new JMenu(label);
357 for (int i = 0; i < items.length; i++)
358 add_menu(menu, items[i][0], items[i][1]);
365 AltosUIPreferences.set_component(this);
369 bag = getContentPane();
370 bag.setLayout(new GridBagLayout());
372 GridBagConstraints c = new GridBagConstraints();
376 menu_bar = new JMenuBar();
377 setJMenuBar(menu_bar);
379 file_menu = make_menu("File", file_menu_entries);
380 monitor_menu = make_menu("Monitor", monitor_menu_entries);
381 device_menu = make_menu("Device", device_menu_entries);
385 /* TeleGPS status is always visible */
386 telegps_status = new TeleGPSStatus();
389 c.fill = GridBagConstraints.HORIZONTAL;
392 bag.add(telegps_status, c);
396 /* The rest of the window uses a tabbed pane to
397 * show one of the alternate data views
399 pane = new JTabbedPane();
401 /* Make the tabbed pane use the rest of the window space */
404 c.fill = GridBagConstraints.BOTH;
410 sitemap = new AltosSiteMap();
411 pane.add("Site Map", sitemap);
413 gps_info = new TeleGPSInfo();
414 pane.add("Info", gps_info);
416 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
418 AltosUIPreferences.register_font_listener(this);
420 addWindowListener(new WindowAdapter() {
422 public void windowClosing(WindowEvent e) {
432 status_update = new TeleGPSStatusUpdate(telegps_status);
434 new javax.swing.Timer(100, status_update).start();
437 public TeleGPS(AltosFlightReader reader) {
442 public TeleGPS(AltosDevice device) {
447 static AltosStateIterable record_iterable(File file) {
450 in = new FileInputStream(file);
451 } catch (Exception e) {
452 System.out.printf("Failed to open file '%s'\n", file);
455 if (file.getName().endsWith("telem"))
456 return new AltosTelemetryFile(in);
458 return new AltosEepromFile(in);
461 static AltosReplayReader replay_file(File file) {
462 AltosStateIterable states = record_iterable(file);
465 return new AltosReplayReader(states.iterator(), file);
468 static boolean process_replay(File file) {
469 AltosReplayReader new_reader = replay_file(file);
470 if (new_reader == null)
473 new TeleGPS(new_reader);
477 static final int process_none = 0;
478 static final int process_csv = 1;
479 static final int process_kml = 2;
480 static final int process_graph = 3;
481 static final int process_replay = 4;
482 static final int process_summary = 5;
483 static final int process_cat = 6;
485 public static boolean load_library(Frame frame) {
486 if (!AltosUILib.load_library()) {
487 JOptionPane.showMessageDialog(frame,
488 String.format("No AltOS library in \"%s\"",
489 System.getProperty("java.library.path","<undefined>")),
490 "Cannot load device access library",
491 JOptionPane.ERROR_MESSAGE);
497 public static void help(int code) {
498 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
499 System.out.printf(" Options:\n");
500 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
501 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
502 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
503 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
504 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
508 public static void main(String[] args) {
513 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
514 } catch (Exception e) {
517 boolean any_created = false;
520 /* Handle batch-mode */
521 int process = process_none;
522 for (int i = 0; i < args.length; i++) {
523 if (args[i].equals("--help"))
525 else if (args[i].equals("--fetchmaps")) {
526 if (args.length < i + 3) {
529 double lat = Double.parseDouble(args[i+1]);
530 double lon = Double.parseDouble(args[i+2]);
531 AltosSiteMap.prefetchMaps(lat, lon);
534 } else if (args[i].equals("--replay"))
535 process = process_replay;
536 else if (args[i].equals("--kml"))
537 process = process_kml;
538 else if (args[i].equals("--csv"))
539 process = process_csv;
540 else if (args[i].equals("--graph"))
541 process = process_graph;
542 else if (args[i].equals("--summary"))
543 process = process_summary;
544 else if (args[i].equals("--cat"))
545 process = process_cat;
546 else if (args[i].startsWith("--"))
549 File file = new File(args[i]);
556 if (!process_replay(file))
566 case process_summary:
577 java.util.List<AltosDevice> devices = AltosUSBDevice.list(AltosLib.product_basestation);
579 for (AltosDevice device : devices) {