altoslib: Don't close telemetry reader at startup unless something fails
[fw/altos] / altoslib / AltosTelemetryReader.java
index dfbad5fb21c7b431b85cdc9e91990afcf2ffb60e..eeb35cb51c6579e141678d4c2cce352b9d3eb6d2 100644 (file)
@@ -34,7 +34,7 @@ public class AltosTelemetryReader extends AltosFlightReader {
                AltosLine l = telem.take();
                if (l.line == null)
                        throw new IOException("IO error");
-               AltosTelemetry  telem = AltosTelemetryLegacy.parse(l.line);
+               AltosTelemetry  telem = AltosTelemetry.parse(l.line);
                if (state == null)
                        state = new AltosState();
                else
@@ -54,7 +54,10 @@ public class AltosTelemetryReader extends AltosFlightReader {
        public void close(boolean interrupted) {
                link.remove_monitor(telem);
                log.close();
-               link.close();
+               try {
+                       link.close();
+               } catch (InterruptedException ie) {
+               }
        }
 
        public void set_frequency(double in_frequency) throws InterruptedException, TimeoutException {
@@ -83,7 +86,7 @@ public class AltosTelemetryReader extends AltosFlightReader {
                        else
                                return false;
                } catch (InterruptedException ie) {
-                       return true;
+                       return false;
                } catch (TimeoutException te) {
                        return true;
                }
@@ -114,13 +117,14 @@ public class AltosTelemetryReader extends AltosFlightReader {
                return link.has_monitor_battery();
        }
 
-       public double monitor_battery() {
+       public double monitor_battery() throws InterruptedException {
                return link.monitor_battery();
        }
 
        public AltosTelemetryReader (AltosLink in_link)
                throws IOException, InterruptedException, TimeoutException {
                link = in_link;
+               boolean success = false;
                try {
                        log = new AltosLog(link);
                        name = link.name;
@@ -130,12 +134,10 @@ public class AltosTelemetryReader extends AltosFlightReader {
                        telemetry = AltosPreferences.telemetry(link.serial);
                        set_telemetry(telemetry);
                        link.add_monitor(telem);
-               } catch (TimeoutException e) {
-                       close(true);
-                       throw(e);
-               } catch (InterruptedException e) {
-                       close(true);
-                       throw(e);
+                       success = true;
+               } finally {
+                       if (!success)
+                               close(true);
                }
        }
 }