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;
64 final static String new_command = "new";
65 final static String preferences_command = "preferences";
66 final static String load_maps_command = "loadmaps";
67 final static String close_command = "close";
68 final static String exit_command = "exit";
70 static final String[][] file_menu_entries = new String[][] {
71 { "New Window", new_command },
72 { "Preferences", preferences_command },
73 { "Load Maps", load_maps_command },
74 { "Close", close_command },
75 { "Exit", exit_command },
79 final static String monitor_command = "monitor";
80 final static String disconnect_command = "disconnect";
81 final static String scan_command = "scan";
83 static final String[][] monitor_menu_entries = new String[][] {
84 { "Monitor Device", monitor_command },
85 { "Disconnect", disconnect_command },
86 { "Scan Channels", scan_command },
90 final static String download_command = "download";
91 final static String configure_command = "configure";
92 final static String export_command = "export";
93 final static String graph_command = "graph";
95 static final String[][] device_menu_entries = new String[][] {
96 { "Download Data", download_command },
97 { "Configure Device", configure_command },
98 { "Export Data", export_command },
99 { "Graph Data", graph_command },
102 // private AltosInfoTable flightInfo;
104 boolean exit_on_close = false;
106 void stop_display() {
107 if (thread != null && thread.isAlive()) {
111 } catch (InterruptedException ie) {}
116 public void reset() {
120 public void set_font() {
124 public void font_size_changed(int font_size) {
129 // AltosFlightStatusUpdate status_update;
131 public void show(AltosState state, AltosListenerState listener_state) {
132 // status_update.saved_state = state;
135 state = new AltosState();
137 sitemap.show(state, listener_state);
138 telegps_status.show(state, listener_state);
142 AltosFreqList frequencies;
144 TeleGPSStatus telegps_status;
145 TeleGPSStatusUpdate status_update;
147 ActionListener show_timer;
157 new AltosSiteMapPreload(this);
161 AltosDevice device = AltosDeviceUIDialog.show(this,
162 AltosLib.product_basestation);
168 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
170 add_frequency_menu(device.getSerial(), reader);
171 } catch (FileNotFoundException ee) {
172 JOptionPane.showMessageDialog(this,
174 String.format ("Cannot open %s", device.toShortString()),
175 JOptionPane.ERROR_MESSAGE);
176 } catch (AltosSerialInUseException si) {
177 JOptionPane.showMessageDialog(this,
178 String.format("Device \"%s\" already in use",
179 device.toShortString()),
181 JOptionPane.ERROR_MESSAGE);
182 } catch (IOException ee) {
183 JOptionPane.showMessageDialog(this,
184 String.format ("Unknown I/O error on %s", device.toShortString()),
186 JOptionPane.ERROR_MESSAGE);
187 } catch (TimeoutException te) {
188 JOptionPane.showMessageDialog(this,
189 String.format ("Timeout on %s", device.toShortString()),
191 JOptionPane.ERROR_MESSAGE);
192 } catch (InterruptedException ie) {
193 JOptionPane.showMessageDialog(this,
194 String.format("Interrupted %s", device.toShortString()),
195 "Interrupted exception",
196 JOptionPane.ERROR_MESSAGE);
203 remove_frequency_menu();
213 new TeleGPSConfig(this);
222 public void actionPerformed(ActionEvent ev) {
225 if (new_command.equals(ev.getActionCommand())) {
229 if (preferences_command.equals(ev.getActionCommand())) {
233 if (load_maps_command.equals(ev.getActionCommand())) {
237 if (close_command.equals(ev.getActionCommand())) {
241 if (exit_command.equals(ev.getActionCommand()))
245 if (monitor_command.equals(ev.getActionCommand())) {
249 if (disconnect_command.equals(ev.getActionCommand())) {
253 if (scan_command.equals(ev.getActionCommand())) {
259 if (download_command.equals(ev.getActionCommand())) {
263 if (configure_command.equals(ev.getActionCommand())) {
267 if (export_command.equals(ev.getActionCommand())) {
271 if (graph_command.equals(ev.getActionCommand())) {
277 void add_frequency_menu(int serial, final AltosFlightReader reader) {
279 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
280 frequencies.set_product("Monitor");
281 frequencies.set_serial(serial);
282 frequencies.addActionListener(new ActionListener() {
283 public void actionPerformed(ActionEvent e) {
284 double frequency = frequencies.frequency();
286 reader.set_frequency(frequency);
287 } catch (TimeoutException te) {
288 } catch (InterruptedException ie) {
290 reader.save_frequency();
293 menu_bar.add(frequencies);
296 void remove_frequency_menu() {
297 if (frequencies != null) {
298 menu_bar.remove(frequencies);
303 public void set_reader(AltosFlightReader reader) {
304 setTitle(String.format("TeleGPS %s", reader.name));
305 thread = new AltosDisplayThread(this, voice(), this, reader);
309 static int number_of_windows;
311 private void close() {
312 AltosUIPreferences.unregister_font_listener(this);
316 if (number_of_windows == 0)
320 private void add_menu(JMenu menu, String label, String action) {
321 JMenuItem item = new JMenuItem(label);
323 item.addActionListener(this);
324 item.setActionCommand(action);
328 private JMenu make_menu(String label, String[][] items) {
329 JMenu menu = new JMenu(label);
330 for (int i = 0; i < items.length; i++)
331 add_menu(menu, items[i][0], items[i][1]);
338 AltosUIPreferences.set_component(this);
342 bag = getContentPane();
343 bag.setLayout(new GridBagLayout());
345 GridBagConstraints c = new GridBagConstraints();
349 menu_bar = new JMenuBar();
350 setJMenuBar(menu_bar);
352 file_menu = make_menu("File", file_menu_entries);
353 monitor_menu = make_menu("Monitor", monitor_menu_entries);
354 device_menu = make_menu("Device", device_menu_entries);
358 /* TeleGPS status is always visible */
359 telegps_status = new TeleGPSStatus();
362 c.fill = GridBagConstraints.HORIZONTAL;
365 bag.add(telegps_status, c);
369 /* The rest of the window uses a tabbed pane to
370 * show one of the alternate data views
372 pane = new JTabbedPane();
374 /* Make the tabbed pane use the rest of the window space */
377 c.fill = GridBagConstraints.BOTH;
383 sitemap = new AltosSiteMap();
384 pane.add("Site Map", sitemap);
386 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
388 AltosUIPreferences.register_font_listener(this);
390 addWindowListener(new WindowAdapter() {
392 public void windowClosing(WindowEvent e) {
402 status_update = new TeleGPSStatusUpdate(telegps_status);
404 new javax.swing.Timer(100, status_update).start();
407 public TeleGPS(AltosFlightReader reader) {
412 static AltosStateIterable record_iterable(File file) {
415 in = new FileInputStream(file);
416 } catch (Exception e) {
417 System.out.printf("Failed to open file '%s'\n", file);
420 if (file.getName().endsWith("telem"))
421 return new AltosTelemetryFile(in);
423 return new AltosEepromFile(in);
426 static AltosReplayReader replay_file(File file) {
427 AltosStateIterable states = record_iterable(file);
430 return new AltosReplayReader(states.iterator(), file);
433 static boolean process_replay(File file) {
434 AltosReplayReader new_reader = replay_file(file);
435 if (new_reader == null)
438 new TeleGPS(new_reader);
442 static final int process_none = 0;
443 static final int process_csv = 1;
444 static final int process_kml = 2;
445 static final int process_graph = 3;
446 static final int process_replay = 4;
447 static final int process_summary = 5;
448 static final int process_cat = 6;
450 public static boolean load_library(Frame frame) {
451 if (!AltosUILib.load_library()) {
452 JOptionPane.showMessageDialog(frame,
453 String.format("No AltOS library in \"%s\"",
454 System.getProperty("java.library.path","<undefined>")),
455 "Cannot load device access library",
456 JOptionPane.ERROR_MESSAGE);
462 public static void help(int code) {
463 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
464 System.out.printf(" Options:\n");
465 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
466 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
467 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
468 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
469 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
473 public static void main(String[] args) {
478 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
479 } catch (Exception e) {
482 boolean any_created = false;
485 /* Handle batch-mode */
486 int process = process_none;
487 for (int i = 0; i < args.length; i++) {
488 if (args[i].equals("--help"))
490 else if (args[i].equals("--fetchmaps")) {
491 if (args.length < i + 3) {
494 double lat = Double.parseDouble(args[i+1]);
495 double lon = Double.parseDouble(args[i+2]);
496 AltosSiteMap.prefetchMaps(lat, lon);
499 } else if (args[i].equals("--replay"))
500 process = process_replay;
501 else if (args[i].equals("--kml"))
502 process = process_kml;
503 else if (args[i].equals("--csv"))
504 process = process_csv;
505 else if (args[i].equals("--graph"))
506 process = process_graph;
507 else if (args[i].equals("--summary"))
508 process = process_summary;
509 else if (args[i].equals("--cat"))
510 process = process_cat;
511 else if (args[i].startsWith("--"))
514 File file = new File(args[i]);
521 if (!process_replay(file))
531 case process_summary: