altosdroid: Send LOCATION and CRC_ERROR messages to UI.
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryReader.java
1 /*\r
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>\r
3  * Copyright © 2012 Mike Beattie <mike@ethernal.org>\r
4  *\r
5  * This program is free software; you can redistribute it and/or modify\r
6  * it under the terms of the GNU General Public License as published by\r
7  * the Free Software Foundation; version 2 of the License.\r
8  *\r
9  * This program is distributed in the hope that it will be useful, but\r
10  * WITHOUT ANY WARRANTY; without even the implied warranty of\r
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
12  * General Public License for more details.\r
13  *\r
14  * You should have received a copy of the GNU General Public License along\r
15  * with this program; if not, write to the Free Software Foundation, Inc.,\r
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.\r
17  */\r
18 \r
19 \r
20 package org.altusmetrum.AltosDroid;\r
21 \r
22 import java.text.*;\r
23 import java.io.*;\r
24 import java.util.concurrent.*;\r
25 import android.util.Log;\r
26 import android.os.Handler;\r
27 \r
28 import org.altusmetrum.altoslib_1.*;\r
29 \r
30 \r
31 public class TelemetryReader extends Thread {\r
32 \r
33         private static final String TAG = "TelemetryReader";\r
34 \r
35         int         crc_errors;\r
36 \r
37         Handler     handler;\r
38 \r
39         TelemetryService service;\r
40         AltosLink   link;\r
41         AltosRecord previous;\r
42 \r
43         LinkedBlockingQueue<AltosLine> telem;\r
44 \r
45         public AltosRecord read() throws ParseException, AltosCRCException, InterruptedException, IOException {\r
46                 AltosLine l = telem.take();\r
47                 if (l.line == null)\r
48                         throw new IOException("IO error");\r
49                 AltosRecord     next = AltosTelemetry.parse(l.line, previous);\r
50                 previous = next;\r
51                 return next;\r
52         }\r
53 \r
54         public void close() {\r
55                 previous = null;\r
56                 link.remove_monitor(telem);\r
57                 link = null;\r
58                 telem.clear();\r
59                 telem = null;\r
60         }\r
61 \r
62         public void run() {\r
63                 AltosState  state = null;\r
64 \r
65                 try {\r
66                         for (;;) {\r
67                                 try {\r
68                                         AltosRecord record = read();\r
69                                         if (record == null)\r
70                                                 break;\r
71                                         state = new AltosState(record, state);\r
72                                         service.sendTelemetry(state);\r
73                                 } catch (ParseException pp) {\r
74                                         Log.e(TAG, String.format("Parse error: %d \"%s\"", pp.getErrorOffset(), pp.getMessage()));\r
75                                 } catch (AltosCRCException ce) {\r
76                                         ++crc_errors;\r
77                                         service.sendCrcErrors(crc_errors);\r
78                                 }\r
79                         }\r
80                 } catch (InterruptedException ee) {\r
81                 } catch (IOException ie) {\r
82                 } finally {\r
83                         close();\r
84                 }\r
85         }\r
86 \r
87         public TelemetryReader (TelemetryService in_service, AltosLink in_link, Handler in_handler) {\r
88                 service = in_service;\r
89                 link    = in_link;\r
90                 handler = in_handler;\r
91 \r
92                 previous = null;\r
93                 telem = new LinkedBlockingQueue<AltosLine>();\r
94                 link.add_monitor(telem);\r
95         }\r
96 }\r