altoslib: Add AltosIdleReader
authorKeith Packard <keithp@keithp.com>
Sun, 15 May 2016 20:57:28 +0000 (13:57 -0700)
committerKeith Packard <keithp@keithp.com>
Sun, 15 May 2016 20:57:28 +0000 (13:57 -0700)
This provides an AltosFlightReader interface for monitor idle mode,
making that easier to provide in TeleGPS

Signed-off-by: Keith Packard <keithp@keithp.com>
altoslib/AltosIdleReader.java [new file with mode: 0644]
altoslib/Makefile.am

diff --git a/altoslib/AltosIdleReader.java b/altoslib/AltosIdleReader.java
new file mode 100644 (file)
index 0000000..795593f
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+ * Copyright © 2016 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+package org.altusmetrum.altoslib_11;
+
+import java.text.*;
+import java.io.*;
+import java.util.concurrent.*;
+
+public class AltosIdleReader extends AltosFlightReader {
+       AltosLink       link;
+       boolean         remote;
+       AltosState      state = null;
+       AltosIdleFetch  fetch;
+       long            next_millis;
+       static final long       report_interval = 5 * 1000;
+       static final long       minimum_delay = 1 * 1000;
+
+       private void start_link() throws InterruptedException, TimeoutException {
+               if (remote) {
+                       link.start_remote();
+               } else
+                       link.flush_input();
+       }
+
+       private boolean stop_link() throws InterruptedException, TimeoutException {
+               if (remote)
+                       link.stop_remote();
+               return link.reply_abort;
+       }
+
+       public AltosState read() throws InterruptedException, ParseException, AltosCRCException, IOException {
+               boolean worked = false;
+               boolean aborted = false;
+
+               if (state == null)
+                       state = new AltosState();
+               else
+                       state = state.clone();
+
+               long    delay = next_millis - System.currentTimeMillis();
+
+               if (delay > 0)
+                       Thread.sleep(delay);
+               next_millis = System.currentTimeMillis() + report_interval;
+               try {
+                       try {
+                               start_link();
+                               fetch.update_state(state);
+                               if (!link.has_error && !link.reply_abort)
+                                       worked = true;
+                       } catch (TimeoutException te) {
+                       } catch (AltosUnknownProduct ue) {
+                               worked = true;
+                       }
+               } finally {
+                       try {
+                               aborted = stop_link();
+                       } catch (TimeoutException te) {
+                               aborted = true;
+                       }
+                       if (worked) {
+                               if (remote) {
+                                       try {
+                                               state.set_rssi(link.rssi(), 0);
+                                       } catch (TimeoutException te) {
+                                               state.set_rssi(0, 0);
+                                       }
+                               }
+                       }
+               }
+
+               long    finish = System.currentTimeMillis();
+
+               if (next_millis - finish < minimum_delay)
+                       next_millis = finish + minimum_delay;
+
+               return state;
+       }
+
+       public void close(boolean interrupted) {
+               try {
+                       link.close();
+               } catch (InterruptedException ie) {
+               }
+       }
+
+       public void set_frequency(double frequency) throws InterruptedException, TimeoutException {
+               link.set_radio_frequency(frequency);
+       }
+
+       public void save_frequency() {
+               AltosPreferences.set_frequency(link.serial, link.frequency);
+       }
+
+       public void set_callsign(String callsign) throws InterruptedException, TimeoutException {
+               link.set_callsign(callsign);
+       }
+
+       public AltosIdleReader (AltosLink link, boolean remote)
+               throws IOException, InterruptedException, TimeoutException {
+               this.link = link;
+               this.remote = remote;
+               this.next_millis = System.currentTimeMillis();
+               fetch = new AltosIdleFetch(link);
+       }
+}
index edc443b0a0daf2c2c7e1aeb5b456f94d6541b93a..d4554df3bc0d8f62b85a6522d81131da54ca2a06 100644 (file)
@@ -64,6 +64,7 @@ altoslib_JAVA = \
        AltosIdleFetch.java \
        AltosIdleMonitor.java \
        AltosIdleMonitorListener.java \
+       AltosIdleReader.java \
        AltosIgnite.java \
        AltosIMU.java \
        AltosKML.java \