-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathBasicNMEAParser.java
375 lines (324 loc) · 13 KB
/
BasicNMEAParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package com.github.petr_s.nmea.basic;
import com.github.petr_s.nmea.basic.BasicNMEAHandler.FixQuality;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.github.petr_s.nmea.basic.BasicNMEAHandler.FixType;
public class BasicNMEAParser {
private static final float KNOTS2MPS = 0.514444f;
private static final SimpleDateFormat TIME_FORMAT = new SimpleDateFormat("HHmmss", Locale.US);
private static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("ddMMyy", Locale.US);
private static final String COMMA = ",";
private static final String CAP_FLOAT = "(\\d*[.]?\\d+)";
private static final String HEX_INT = "[0-9a-fA-F]";
private static final Pattern GENERAL_SENTENCE = Pattern.compile("^\\$(\\w{5}),(.*)[*](" + HEX_INT + "{2})$");
private static final Pattern GPRMC = Pattern.compile("(\\d{5})?" +
"(\\d[.]?\\d*)?" + COMMA +
regexify(Status.class) + COMMA +
"(\\d{2})(\\d{2}[.]\\d+)?" + COMMA +
regexify(VDir.class) + "?" + COMMA +
"(\\d{3})(\\d{2}[.]\\d+)?" + COMMA +
regexify(HDir.class) + "?" + COMMA +
CAP_FLOAT + "?" + COMMA +
CAP_FLOAT + "?" + COMMA +
"(\\d{6})?" + COMMA +
CAP_FLOAT + "?" + COMMA +
regexify(HDir.class) + "?" + COMMA + "?" +
regexify(FFA.class) + "?");
private static final Pattern GPGGA = Pattern.compile("(\\d{5})?" +
"(\\d[.]?\\d*)?" + COMMA +
"(\\d{2})(\\d{2}[.]\\d+)?" + COMMA +
regexify(VDir.class) + "?" + COMMA +
"(\\d{3})(\\d{2}[.]\\d+)?" + COMMA +
regexify(HDir.class) + "?" + COMMA +
"(\\d)?" + COMMA +
"(\\d{2})?" + COMMA +
CAP_FLOAT + "?" + COMMA +
CAP_FLOAT + "?,[M]" + COMMA +
CAP_FLOAT + "?,[M]" + COMMA +
CAP_FLOAT + "?" + COMMA +
"(\\d{4})?");
private static final Pattern GPGSV = Pattern.compile("(\\d+)" + COMMA +
"(\\d+)" + COMMA +
"(\\d{2})" + COMMA +
"(\\d{2})" + COMMA +
"(\\d{2})" + COMMA +
"(\\d{3})" + COMMA +
"(\\d{2})" + COMMA +
"(\\d{2})?" + COMMA + "?" +
"(\\d{2})?" + COMMA + "?" +
"(\\d{3})?" + COMMA + "?" +
"(\\d{2})?" + COMMA + "?" +
"(\\d{2})?" + COMMA + "?" +
"(\\d{2})?" + COMMA + "?" +
"(\\d{3})?" + COMMA + "?" +
"(\\d{2})?" + COMMA + "?" +
"(\\d{2})?" + COMMA + "?" +
"(\\d{2})?" + COMMA + "?" +
"(\\d{3})?" + COMMA + "?" +
"(\\d{2})?");
private static final Pattern GPGSA = Pattern.compile(regexify(Mode.class) + COMMA +
"(\\d)" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
"(\\d{2})?" + COMMA +
CAP_FLOAT + "?" + COMMA +
CAP_FLOAT + "?" + COMMA +
CAP_FLOAT + "?");
private static HashMap<String, ParsingFunction> functions = new HashMap<>();
static {
TIME_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
functions.put("GPRMC", new ParsingFunction() {
@Override
public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception {
return parseGPRMC(handler, sentence);
}
});
functions.put("GPGGA", new ParsingFunction() {
@Override
public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception {
return parseGPGGA(handler, sentence);
}
});
functions.put("GPGSV", new ParsingFunction() {
@Override
public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception {
return parseGPGSV(handler, sentence);
}
});
functions.put("GPGSA", new ParsingFunction() {
@Override
public boolean parse(BasicNMEAHandler handler, String sentence) throws Exception {
return parseGPGSA(handler, sentence);
}
});
}
private final BasicNMEAHandler handler;
public BasicNMEAParser(BasicNMEAHandler handler) {
this.handler = handler;
if (handler == null) {
throw new NullPointerException();
}
}
private static boolean parseGPRMC(BasicNMEAHandler handler, String sentence) throws Exception {
ExMatcher matcher = new ExMatcher(GPRMC.matcher(sentence));
if (matcher.matches()) {
long time = TIME_FORMAT.parse(matcher.nextString("time") + "0").getTime();
Float ms = matcher.nextFloat("time-ms");
if (ms != null) {
time += ms * 1000;
}
if (Status.valueOf(matcher.nextString("status")) == Status.A) {
double latitude = toDegrees(matcher.nextInt("degrees"),
matcher.nextFloat("minutes"));
VDir vDir = VDir.valueOf(matcher.nextString("vertical-direction"));
double longitude = toDegrees(matcher.nextInt("degrees"),
matcher.nextFloat("minutes"));
HDir hDir = HDir.valueOf(matcher.nextString("horizontal-direction"));
float speed = matcher.nextFloat("speed") * KNOTS2MPS;
float direction = matcher.nextFloat("direction", 0.0f);
long date = DATE_FORMAT.parse(matcher.nextString("date")).getTime();
Float magVar = matcher.nextFloat("magnetic-variation");
String magVarDir = matcher.nextString("direction");
String faa = matcher.nextString("faa");
handler.onRMC(date,
time,
vDir.equals(VDir.N) ? latitude : -latitude,
hDir.equals(HDir.E) ? longitude : -longitude,
speed,
direction);
return true;
}
}
return false;
}
private static boolean parseGPGGA(BasicNMEAHandler handler, String sentence) throws Exception {
ExMatcher matcher = new ExMatcher(GPGGA.matcher(sentence));
if (matcher.matches()) {
long time = TIME_FORMAT.parse(matcher.nextString("time") + "0").getTime();
Float ms = matcher.nextFloat("time-ms");
if (ms != null) {
time += ms * 1000;
}
double latitude = toDegrees(matcher.nextInt("degrees"),
matcher.nextFloat("minutes"));
VDir vDir = VDir.valueOf(matcher.nextString("vertical-direction"));
double longitude = toDegrees(matcher.nextInt("degrees"),
matcher.nextFloat("minutes"));
HDir hDir = HDir.valueOf(matcher.nextString("horizontal-direction"));
FixQuality quality = FixQuality.values()[matcher.nextInt("quality")];
int satellites = matcher.nextInt("n-satellites");
float hdop = matcher.nextFloat("hdop");
float altitude = matcher.nextFloat("altitude");
float separation = matcher.nextFloat("separation");
Float age = matcher.nextFloat("age");
Integer station = matcher.nextInt("station");
handler.onGGA(time,
vDir.equals(VDir.N) ? latitude : -latitude,
hDir.equals(HDir.E) ? longitude : -longitude,
altitude - separation,
quality,
satellites,
hdop);
return true;
}
return false;
}
private static boolean parseGPGSV(BasicNMEAHandler handler, String sentence) throws Exception {
ExMatcher matcher = new ExMatcher(GPGSV.matcher(sentence));
if (matcher.matches()) {
int sentences = matcher.nextInt("n-sentences");
int index = matcher.nextInt("sentence-index") - 1;
int satellites = matcher.nextInt("n-satellites");
for (int i = 0; i < 4; i++) {
Integer prn = matcher.nextInt("prn");
Integer elevation = matcher.nextInt("elevation");
Integer azimuth = matcher.nextInt("azimuth");
Integer snr = matcher.nextInt("snr");
if (prn != null) {
handler.onGSV(satellites, index * 4 + i, prn, elevation, azimuth, snr);
}
}
return true;
}
return false;
}
private static boolean parseGPGSA(BasicNMEAHandler handler, String sentence) {
ExMatcher matcher = new ExMatcher(GPGSA.matcher(sentence));
if (matcher.matches()) {
Mode mode = Mode.valueOf(matcher.nextString("mode"));
FixType type = FixType.values()[matcher.nextInt("fix-type")];
Set<Integer> prns = new HashSet<>();
for (int i = 0; i < 12; i++) {
Integer prn = matcher.nextInt("prn");
if (prn != null) {
prns.add(prn);
}
}
float pdop = matcher.nextFloat("pdop");
float hdop = matcher.nextFloat("hdop");
float vdop = matcher.nextFloat("vdop");
handler.onGSA(type, prns, pdop, hdop, vdop);
return true;
}
return false;
}
private static int calculateChecksum(String sentence) throws UnsupportedEncodingException {
byte[] bytes = sentence.substring(1, sentence.length() - 3).getBytes("US-ASCII");
int checksum = 0;
for (byte b : bytes) {
checksum ^= b;
}
return checksum;
}
private static double toDegrees(int degrees, float minutes) {
return degrees + minutes / 60.0;
}
private static <T extends Enum<T>> String regexify(Class<T> clazz) {
StringBuilder sb = new StringBuilder();
sb.append("([");
for (T c : clazz.getEnumConstants()) {
sb.append(c.toString());
}
sb.append("])");
return sb.toString();
}
public synchronized void parse(String sentence) {
if (sentence == null) {
throw new NullPointerException();
}
handler.onStart();
try {
ExMatcher matcher = new ExMatcher(GENERAL_SENTENCE.matcher(sentence));
if (matcher.matches()) {
String type = matcher.nextString("type");
String content = matcher.nextString("content");
int expected_checksum = matcher.nextHexInt("checksum");
int actual_checksum = calculateChecksum(sentence);
if (actual_checksum != expected_checksum) {
handler.onBadChecksum(expected_checksum, actual_checksum);
} else if (!functions.containsKey(type) || !functions.get(type).parse(handler, content)) {
handler.onUnrecognized(sentence);
}
} else {
handler.onUnrecognized(sentence);
}
} catch (Exception e) {
handler.onException(e);
} finally {
handler.onFinished();
}
}
private enum Status {
A,
V
}
private enum HDir {
E,
W
}
private enum VDir {
N,
S,
}
private enum Mode {
A,
M
}
private enum FFA {
A,
D,
E,
M,
S,
N
}
private static abstract class ParsingFunction {
public abstract boolean parse(BasicNMEAHandler handler, String sentence) throws Exception;
}
private static class ExMatcher {
Matcher original;
int index;
ExMatcher(Matcher original) {
this.original = original;
reset();
}
void reset() {
index = 1;
}
boolean matches() {
return original.matches();
}
String nextString(String name) {
return original.group(index++);
}
Float nextFloat(String name, Float defaultValue) {
Float next = nextFloat(name);
return next == null ? defaultValue : next;
}
Float nextFloat(String name) {
String next = nextString(name);
return next == null ? null : Float.parseFloat(next);
}
Integer nextInt(String name) {
String next = nextString(name);
return next == null ? null : Integer.parseInt(next);
}
Integer nextHexInt(String name) {
String next = nextString(name);
return next == null ? null : Integer.parseInt(next, 16);
}
}
}