+package org.altusmetrum.AltosDroid;\r
+\r
+import java.text.*;\r
+import java.io.*;\r
+import java.util.concurrent.*;\r
+import android.util.Log;\r
+import android.os.Handler;\r
+\r
+import org.altusmetrum.AltosLib.*;\r
+\r
+\r
+public class TelemetryReader extends Thread {\r
+\r
+ private static final String TAG = "TelemetryReader";\r
+\r
+ int crc_errors;\r
+\r
+ Handler handler;\r
+\r
+ AltosLink link;\r
+ AltosRecord previous;\r
+\r
+ LinkedBlockingQueue<AltosLine> telem;\r
+\r
+ public AltosRecord read() throws ParseException, AltosCRCException, InterruptedException, IOException {\r
+ AltosLine l = telem.take();\r
+ if (l.line == null)\r
+ throw new IOException("IO error");\r
+ AltosRecord next = AltosTelemetry.parse(l.line, previous);\r
+ previous = next;\r
+ return next;\r
+ }\r
+\r
+ public void close() {\r
+ previous = null;\r
+ link.remove_monitor(telem);\r
+ link = null;\r
+ telem.clear();\r
+ telem = null;\r
+ }\r
+\r
+ public void run() {\r
+ AltosState state = null;\r
+\r
+ try {\r
+ for (;;) {\r
+ try {\r
+ AltosRecord record = read();\r
+ if (record == null)\r
+ break;\r
+ state = new AltosState(record, state);\r
+\r
+ handler.obtainMessage(TelemetryService.MSG_TELEMETRY, state).sendToTarget();\r
+ } catch (ParseException pp) {\r
+ Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));\r
+ } catch (AltosCRCException ce) {\r
+ ++crc_errors;\r
+ }\r
+ }\r
+ } catch (InterruptedException ee) {\r
+ } catch (IOException ie) {\r
+ } finally {\r
+ close();\r
+ }\r
+ }\r
+\r
+ public TelemetryReader (AltosLink in_link, Handler in_handler) {\r
+ link = in_link;\r
+ handler = in_handler;\r
+\r
+ previous = null;\r
+ telem = new LinkedBlockingQueue<AltosLine>();\r
+ link.add_monitor(telem);\r
+ }\r
+}\r