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;
154 new TeleGPSPreferences(this, voice());
158 new AltosSiteMapPreload(this);
162 AltosDevice device = AltosDeviceUIDialog.show(this,
163 AltosLib.product_basestation);
169 AltosFlightReader reader = new AltosTelemetryReader(new AltosSerial(device));
171 add_frequency_menu(device.getSerial(), reader);
172 } catch (FileNotFoundException ee) {
173 JOptionPane.showMessageDialog(this,
175 String.format ("Cannot open %s", device.toShortString()),
176 JOptionPane.ERROR_MESSAGE);
177 } catch (AltosSerialInUseException si) {
178 JOptionPane.showMessageDialog(this,
179 String.format("Device \"%s\" already in use",
180 device.toShortString()),
182 JOptionPane.ERROR_MESSAGE);
183 } catch (IOException ee) {
184 JOptionPane.showMessageDialog(this,
185 String.format ("Unknown I/O error on %s", device.toShortString()),
187 JOptionPane.ERROR_MESSAGE);
188 } catch (TimeoutException te) {
189 JOptionPane.showMessageDialog(this,
190 String.format ("Timeout on %s", device.toShortString()),
192 JOptionPane.ERROR_MESSAGE);
193 } catch (InterruptedException ie) {
194 JOptionPane.showMessageDialog(this,
195 String.format("Interrupted %s", device.toShortString()),
196 "Interrupted exception",
197 JOptionPane.ERROR_MESSAGE);
204 remove_frequency_menu();
214 new TeleGPSConfig(this);
223 public void actionPerformed(ActionEvent ev) {
226 if (new_command.equals(ev.getActionCommand())) {
230 if (preferences_command.equals(ev.getActionCommand())) {
234 if (load_maps_command.equals(ev.getActionCommand())) {
238 if (close_command.equals(ev.getActionCommand())) {
242 if (exit_command.equals(ev.getActionCommand()))
246 if (monitor_command.equals(ev.getActionCommand())) {
250 if (disconnect_command.equals(ev.getActionCommand())) {
254 if (scan_command.equals(ev.getActionCommand())) {
260 if (download_command.equals(ev.getActionCommand())) {
264 if (configure_command.equals(ev.getActionCommand())) {
268 if (export_command.equals(ev.getActionCommand())) {
272 if (graph_command.equals(ev.getActionCommand())) {
278 void add_frequency_menu(int serial, final AltosFlightReader reader) {
280 frequencies = new AltosFreqList(AltosUIPreferences.frequency(serial));
281 frequencies.set_product("Monitor");
282 frequencies.set_serial(serial);
283 frequencies.addActionListener(new ActionListener() {
284 public void actionPerformed(ActionEvent e) {
285 double frequency = frequencies.frequency();
287 reader.set_frequency(frequency);
288 } catch (TimeoutException te) {
289 } catch (InterruptedException ie) {
291 reader.save_frequency();
294 menu_bar.add(frequencies);
297 void remove_frequency_menu() {
298 if (frequencies != null) {
299 menu_bar.remove(frequencies);
304 public void set_reader(AltosFlightReader reader) {
305 setTitle(String.format("TeleGPS %s", reader.name));
306 thread = new AltosDisplayThread(this, voice(), this, reader);
310 static int number_of_windows;
312 private void close() {
313 AltosUIPreferences.unregister_font_listener(this);
317 if (number_of_windows == 0)
321 private void add_menu(JMenu menu, String label, String action) {
322 JMenuItem item = new JMenuItem(label);
324 item.addActionListener(this);
325 item.setActionCommand(action);
329 private JMenu make_menu(String label, String[][] items) {
330 JMenu menu = new JMenu(label);
331 for (int i = 0; i < items.length; i++)
332 add_menu(menu, items[i][0], items[i][1]);
339 AltosUIPreferences.set_component(this);
343 bag = getContentPane();
344 bag.setLayout(new GridBagLayout());
346 GridBagConstraints c = new GridBagConstraints();
350 menu_bar = new JMenuBar();
351 setJMenuBar(menu_bar);
353 file_menu = make_menu("File", file_menu_entries);
354 monitor_menu = make_menu("Monitor", monitor_menu_entries);
355 device_menu = make_menu("Device", device_menu_entries);
359 /* TeleGPS status is always visible */
360 telegps_status = new TeleGPSStatus();
363 c.fill = GridBagConstraints.HORIZONTAL;
366 bag.add(telegps_status, c);
370 /* The rest of the window uses a tabbed pane to
371 * show one of the alternate data views
373 pane = new JTabbedPane();
375 /* Make the tabbed pane use the rest of the window space */
378 c.fill = GridBagConstraints.BOTH;
384 sitemap = new AltosSiteMap();
385 pane.add("Site Map", sitemap);
387 setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
389 AltosUIPreferences.register_font_listener(this);
391 addWindowListener(new WindowAdapter() {
393 public void windowClosing(WindowEvent e) {
403 status_update = new TeleGPSStatusUpdate(telegps_status);
405 new javax.swing.Timer(100, status_update).start();
408 public TeleGPS(AltosFlightReader reader) {
413 static AltosStateIterable record_iterable(File file) {
416 in = new FileInputStream(file);
417 } catch (Exception e) {
418 System.out.printf("Failed to open file '%s'\n", file);
421 if (file.getName().endsWith("telem"))
422 return new AltosTelemetryFile(in);
424 return new AltosEepromFile(in);
427 static AltosReplayReader replay_file(File file) {
428 AltosStateIterable states = record_iterable(file);
431 return new AltosReplayReader(states.iterator(), file);
434 static boolean process_replay(File file) {
435 AltosReplayReader new_reader = replay_file(file);
436 if (new_reader == null)
439 new TeleGPS(new_reader);
443 static final int process_none = 0;
444 static final int process_csv = 1;
445 static final int process_kml = 2;
446 static final int process_graph = 3;
447 static final int process_replay = 4;
448 static final int process_summary = 5;
449 static final int process_cat = 6;
451 public static boolean load_library(Frame frame) {
452 if (!AltosUILib.load_library()) {
453 JOptionPane.showMessageDialog(frame,
454 String.format("No AltOS library in \"%s\"",
455 System.getProperty("java.library.path","<undefined>")),
456 "Cannot load device access library",
457 JOptionPane.ERROR_MESSAGE);
463 public static void help(int code) {
464 System.out.printf("Usage: altosui [OPTION]... [FILE]...\n");
465 System.out.printf(" Options:\n");
466 System.out.printf(" --fetchmaps <lat> <lon>\tpre-fetch maps for site map view\n");
467 System.out.printf(" --replay <filename>\t\trelive the glory of past flights \n");
468 System.out.printf(" --graph <filename>\t\tgraph a flight\n");
469 System.out.printf(" --csv\tgenerate comma separated output for spreadsheets, etc\n");
470 System.out.printf(" --kml\tgenerate KML output for use with Google Earth\n");
474 public static void main(String[] args) {
479 UIManager.setLookAndFeel(AltosUIPreferences.look_and_feel());
480 } catch (Exception e) {
483 boolean any_created = false;
486 /* Handle batch-mode */
487 int process = process_none;
488 for (int i = 0; i < args.length; i++) {
489 if (args[i].equals("--help"))
491 else if (args[i].equals("--fetchmaps")) {
492 if (args.length < i + 3) {
495 double lat = Double.parseDouble(args[i+1]);
496 double lon = Double.parseDouble(args[i+2]);
497 AltosSiteMap.prefetchMaps(lat, lon);
500 } else if (args[i].equals("--replay"))
501 process = process_replay;
502 else if (args[i].equals("--kml"))
503 process = process_kml;
504 else if (args[i].equals("--csv"))
505 process = process_csv;
506 else if (args[i].equals("--graph"))
507 process = process_graph;
508 else if (args[i].equals("--summary"))
509 process = process_summary;
510 else if (args[i].equals("--cat"))
511 process = process_cat;
512 else if (args[i].startsWith("--"))
515 File file = new File(args[i]);
522 if (!process_replay(file))
532 case process_summary: