ca04795d5ac4650180671304c5fa554d7ef2918c
[fw/altos] / altoslib / AltosGPSQuery.java
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
7  *
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.
12  *
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.
16  */
17
18 package org.altusmetrum.AltosLib;
19
20 import java.util.concurrent.*;
21
22 class AltosADCTM {
23         int     tick;
24         int     accel;
25         int     pres;
26         int     temp;
27         int     batt;
28         int     drogue;
29         int     main;
30
31         public AltosADCTM(AltosLink link) throws InterruptedException, TimeoutException {
32                 link.printf("a\n");
33                 for (;;) {
34                         String line = link.get_reply_no_dialog(5000);
35                         if (line == null) {
36                                 throw new TimeoutException();
37                         }
38                         if (!line.startsWith("tick:"))
39                                 continue;
40                         String[] items = line.split("\\s+");
41                         for (int i = 0; i < items.length;) {
42                                 if (items[i].equals("tick:")) {
43                                         tick = Integer.parseInt(items[i+1]);
44                                         i += 2;
45                                         continue;
46                                 }
47                                 if (items[i].equals("accel:")) {
48                                         accel = Integer.parseInt(items[i+1]);
49                                         i += 2;
50                                         continue;
51                                 }
52                                 if (items[i].equals("pres:")) {
53                                         pres = Integer.parseInt(items[i+1]);
54                                         i += 2;
55                                         continue;
56                                 }
57                                 if (items[i].equals("temp:")) {
58                                         temp = Integer.parseInt(items[i+1]);
59                                         i += 2;
60                                         continue;
61                                 }
62                                 if (items[i].equals("batt:")) {
63                                         batt = Integer.parseInt(items[i+1]);
64                                         i += 2;
65                                         continue;
66                                 }
67                                 if (items[i].equals("drogue:")) {
68                                         drogue = Integer.parseInt(items[i+1]);
69                                         i += 2;
70                                         continue;
71                                 }
72                                 if (items[i].equals("main:")) {
73                                         main = Integer.parseInt(items[i+1]);
74                                         i += 2;
75                                         continue;
76                                 }
77                                 i++;
78                         }
79                         break;
80                 }
81         }
82 }
83
84 class AltosGPSQuery extends AltosGPS {
85         public AltosGPSQuery (AltosLink link, AltosConfigData config_data)
86                 throws TimeoutException, InterruptedException {
87                 boolean says_done = config_data.compare_version("1.0") >= 0;
88                 link.printf("g\n");
89                 for (;;) {
90                         String line = link.get_reply_no_dialog(5000);
91                         if (line == null)
92                                 throw new TimeoutException();
93                         String[] bits = line.split("\\s+");
94                         if (bits.length == 0)
95                                 continue;
96                         if (line.startsWith("Date:")) {
97                                 if (bits.length < 2)
98                                         continue;
99                                 String[] d = bits[1].split(":");
100                                 if (d.length < 3)
101                                         continue;
102                                 year = Integer.parseInt(d[0]) + 2000;
103                                 month = Integer.parseInt(d[1]);
104                                 day = Integer.parseInt(d[2]);
105                                 continue;
106                         }
107                         if (line.startsWith("Time:")) {
108                                 if (bits.length < 2)
109                                         continue;
110                                 String[] d = bits[1].split("/");
111                                 if (d.length < 3)
112                                         continue;
113                                 hour = Integer.parseInt(d[0]);
114                                 minute = Integer.parseInt(d[1]);
115                                 second = Integer.parseInt(d[2]);
116                                 continue;
117                         }
118                         if (line.startsWith("Lat/Lon:")) {
119                                 if (bits.length < 3)
120                                         continue;
121                                 lat = Integer.parseInt(bits[1]) * 1.0e-7;
122                                 lon = Integer.parseInt(bits[2]) * 1.0e-7;
123                                 continue;
124                         }
125                         if (line.startsWith("Alt:")) {
126                                 if (bits.length < 2)
127                                         continue;
128                                 alt = Integer.parseInt(bits[1]);
129                                 continue;
130                         }
131                         if (line.startsWith("Flags:")) {
132                                 if (bits.length < 2)
133                                         continue;
134                                 int status = Integer.decode(bits[1]);
135                                 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
136                                 locked = (status & AltosLib.AO_GPS_VALID) != 0;
137                                 if (!says_done)
138                                         break;
139                                 continue;
140                         }
141                         if (line.startsWith("Sats:")) {
142                                 if (bits.length < 2)
143                                         continue;
144                                 nsat = Integer.parseInt(bits[1]);
145                                 cc_gps_sat = new AltosGPSSat[nsat];
146                                 for (int i = 0; i < nsat; i++) {
147                                         int     svid = Integer.parseInt(bits[2+i*2]);
148                                         int     cc_n0 = Integer.parseInt(bits[3+i*2]);
149                                         cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
150                                 }
151                         }
152                         if (line.startsWith("done"))
153                                 break;
154                         if (line.startsWith("Syntax error"))
155                                 break;
156                 }
157         }
158 }
159