]> git.gag.com Git - fw/altos/blob - altoslib/AltosGPSQuery.java
altosdroid: implement tabs interface
[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_1;
19
20 import java.util.concurrent.*;
21
22 class AltosGPSQuery extends AltosGPS {
23         public AltosGPSQuery (AltosLink link, AltosConfigData config_data)
24                 throws TimeoutException, InterruptedException {
25                 boolean says_done = config_data.compare_version("1.0") >= 0;
26                 link.printf("g\n");
27                 for (;;) {
28                         String line = link.get_reply_no_dialog(5000);
29                         if (line == null)
30                                 throw new TimeoutException();
31                         String[] bits = line.split("\\s+");
32                         if (bits.length == 0)
33                                 continue;
34                         if (line.startsWith("Date:")) {
35                                 if (bits.length < 2)
36                                         continue;
37                                 String[] d = bits[1].split(":");
38                                 if (d.length < 3)
39                                         continue;
40                                 year = Integer.parseInt(d[0]) + 2000;
41                                 month = Integer.parseInt(d[1]);
42                                 day = Integer.parseInt(d[2]);
43                                 continue;
44                         }
45                         if (line.startsWith("Time:")) {
46                                 if (bits.length < 2)
47                                         continue;
48                                 String[] d = bits[1].split("/");
49                                 if (d.length < 3)
50                                         continue;
51                                 hour = Integer.parseInt(d[0]);
52                                 minute = Integer.parseInt(d[1]);
53                                 second = Integer.parseInt(d[2]);
54                                 continue;
55                         }
56                         if (line.startsWith("Lat/Lon:")) {
57                                 if (bits.length < 3)
58                                         continue;
59                                 lat = Integer.parseInt(bits[1]) * 1.0e-7;
60                                 lon = Integer.parseInt(bits[2]) * 1.0e-7;
61                                 continue;
62                         }
63                         if (line.startsWith("Alt:")) {
64                                 if (bits.length < 2)
65                                         continue;
66                                 alt = Integer.parseInt(bits[1]);
67                                 continue;
68                         }
69                         if (line.startsWith("Flags:")) {
70                                 if (bits.length < 2)
71                                         continue;
72                                 int status = Integer.decode(bits[1]);
73                                 connected = (status & AltosLib.AO_GPS_RUNNING) != 0;
74                                 locked = (status & AltosLib.AO_GPS_VALID) != 0;
75                                 if (!says_done)
76                                         break;
77                                 continue;
78                         }
79                         if (line.startsWith("Sats:")) {
80                                 if (bits.length < 2)
81                                         continue;
82                                 nsat = Integer.parseInt(bits[1]);
83                                 cc_gps_sat = new AltosGPSSat[nsat];
84                                 for (int i = 0; i < nsat; i++) {
85                                         int     svid = Integer.parseInt(bits[2+i*2]);
86                                         int     cc_n0 = Integer.parseInt(bits[3+i*2]);
87                                         cc_gps_sat[i] = new AltosGPSSat(svid, cc_n0);
88                                 }
89                         }
90                         if (line.startsWith("done"))
91                                 break;
92                         if (line.startsWith("Syntax error"))
93                                 break;
94                 }
95         }
96 }
97