Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 28a10db

Browse files
committed
adding LegacyParser
1 parent 7244f16 commit 28a10db

File tree

4 files changed

+356
-17
lines changed

4 files changed

+356
-17
lines changed

README.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@ About GPSd4Java
33

44
GPSd4Java is a library to use data from the GPSd daemon in your java applications. It provides a library to connect to GPSd and retrieve data.
55

6-
Use GPSd4Java with gpsd 3.6
7-
===========================
8-
9-
Version 1.2 is ONLY compatible with gpsd version 3.6. GPSd4Java 1.3 will be a compatibility release to support all active gpsd versions.
10-
11-
<dependencies>
12-
<dependency>
13-
<groupId>de.taimos</groupId>
14-
<artifactId>gpsd4java</artifactId>
15-
<version>1.2.0</version>
16-
</dependency>
17-
</dependencies>
18-
196
Use GPSd4Java
207
=============
218

@@ -25,7 +12,7 @@ You can use GPSd4Java with a Maven project. Just add the following lines to your
2512
<dependency>
2613
<groupId>de.taimos</groupId>
2714
<artifactId>gpsd4java</artifactId>
28-
<version>1.1.0</version>
15+
<version>1.2.0</version>
2916
</dependency>
3017
</dependencies>
3118

@@ -44,6 +31,6 @@ To get the latest development snapshot add the following lines to your pom.xml.
4431
<dependency>
4532
<groupId>de.taimos</groupId>
4633
<artifactId>gpsd4java</artifactId>
47-
<version>1.2.0-SNAPSHOT</version>
34+
<version>1.3.0-SNAPSHOT</version>
4835
</dependency>
4936
</dependencies>
Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
/**
2+
* Copyright 2011 Thorsten Höger, Taimos GmbH
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may
5+
* obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
10+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11+
* and limitations under the License.
12+
*/
13+
package de.taimos.gpsd4java.backend;
14+
15+
import java.text.DateFormat;
16+
import java.text.SimpleDateFormat;
17+
import java.util.Date;
18+
import java.util.TimeZone;
19+
import java.util.logging.Level;
20+
import java.util.logging.Logger;
21+
22+
import org.json.JSONObject;
23+
24+
import de.taimos.gpsd4java.types.ATTObject;
25+
import de.taimos.gpsd4java.types.DeviceObject;
26+
import de.taimos.gpsd4java.types.DevicesObject;
27+
import de.taimos.gpsd4java.types.ENMEAMode;
28+
import de.taimos.gpsd4java.types.EParity;
29+
import de.taimos.gpsd4java.types.GSTObject;
30+
import de.taimos.gpsd4java.types.IGPSObject;
31+
import de.taimos.gpsd4java.types.ParseException;
32+
import de.taimos.gpsd4java.types.PollObject;
33+
import de.taimos.gpsd4java.types.SATObject;
34+
import de.taimos.gpsd4java.types.SKYObject;
35+
import de.taimos.gpsd4java.types.TPVObject;
36+
import de.taimos.gpsd4java.types.VersionObject;
37+
import de.taimos.gpsd4java.types.WatchObject;
38+
import de.taimos.gpsd4java.types.subframes.ALMANACObject;
39+
import de.taimos.gpsd4java.types.subframes.EPHEM1Object;
40+
import de.taimos.gpsd4java.types.subframes.EPHEM2Object;
41+
import de.taimos.gpsd4java.types.subframes.EPHEM3Object;
42+
import de.taimos.gpsd4java.types.subframes.ERDObject;
43+
import de.taimos.gpsd4java.types.subframes.HEALTH2Object;
44+
import de.taimos.gpsd4java.types.subframes.HEALTHObject;
45+
import de.taimos.gpsd4java.types.subframes.IONOObject;
46+
import de.taimos.gpsd4java.types.subframes.SUBFRAMEObject;
47+
48+
/**
49+
* This class is used to parse responses from GPSd<br>
50+
*
51+
* @author thoeger
52+
*/
53+
public class LegacyResultParser extends AbstractResultParser {
54+
55+
private static final Logger log = Logger.getLogger(LegacyResultParser.class.getName());
56+
57+
private final DateFormat dateFormat; // Don't make this static!
58+
59+
/**
60+
* Create new ResultParser
61+
*/
62+
public LegacyResultParser() {
63+
this.dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
64+
this.dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
65+
}
66+
67+
/**
68+
* parse {@link JSONObject} into {@link IGPSObject}
69+
*
70+
* @param json
71+
* the {@link JSONObject} to parse
72+
* @return the parsed object
73+
* @throws ParseException
74+
* if parsing fails
75+
*/
76+
@Override
77+
public IGPSObject parse(final JSONObject json) throws ParseException {
78+
IGPSObject gps = null;
79+
final String clazz = json.optString("class");
80+
81+
if ("TPV".equals(clazz)) {
82+
final TPVObject tpv = new TPVObject();
83+
tpv.setTag(json.optString("tag", null));
84+
tpv.setDevice(json.optString("device", null));
85+
tpv.setTimestamp(this.parseTimestamp(json, "time"));
86+
tpv.setTimestampError(json.optDouble("ept", Double.NaN));
87+
tpv.setLatitude(json.optDouble("lat", Double.NaN));
88+
tpv.setLongitude(json.optDouble("lon", Double.NaN));
89+
tpv.setAltitude(json.optDouble("alt", Double.NaN));
90+
tpv.setLongitudeError(json.optDouble("epx", Double.NaN));
91+
tpv.setLatitudeError(json.optDouble("epy", Double.NaN));
92+
tpv.setAltitudeError(json.optDouble("epv", Double.NaN));
93+
tpv.setCourse(json.optDouble("track", Double.NaN));
94+
tpv.setSpeed(json.optDouble("speed", Double.NaN));
95+
tpv.setClimbRate(json.optDouble("climb", Double.NaN));
96+
tpv.setCourseError(json.optDouble("epd", Double.NaN));
97+
tpv.setSpeedError(json.optDouble("eps", Double.NaN));
98+
tpv.setClimbRateError(json.optDouble("epc", Double.NaN));
99+
tpv.setMode(ENMEAMode.fromInt(json.optInt("mode", 0)));
100+
gps = tpv;
101+
} else if ("SKY".equals(clazz)) {
102+
final SKYObject sky = new SKYObject();
103+
sky.setTag(json.optString("tag", null));
104+
sky.setDevice(json.optString("device", null));
105+
sky.setTimestamp(this.parseTimestamp(json, "time"));
106+
sky.setLongitudeDOP(json.optDouble("xdop", Double.NaN));
107+
sky.setLatitudeDOP(json.optDouble("ydop", Double.NaN));
108+
sky.setAltitudeDOP(json.optDouble("vdop", Double.NaN));
109+
sky.setTimestampDOP(json.optDouble("tdop", Double.NaN));
110+
sky.setHorizontalDOP(json.optDouble("hdop", Double.NaN));
111+
sky.setSphericalDOP(json.optDouble("pdop", Double.NaN));
112+
sky.setHypersphericalDOP(json.optDouble("gdop", Double.NaN));
113+
sky.setSatellites(this.parseObjectArray(json.optJSONArray("satellites"), SATObject.class));
114+
gps = sky;
115+
} else if ("GST".equals(clazz)) {
116+
final GSTObject gst = new GSTObject();
117+
gst.setTag(json.optString("tag", null));
118+
gst.setDevice(json.optString("device", null));
119+
gst.setTimestamp(this.parseTimestamp(json, "time"));
120+
gst.setRms(json.optDouble("rms", Double.NaN));
121+
gst.setMajor(json.optDouble("major", Double.NaN));
122+
gst.setMinor(json.optDouble("minor", Double.NaN));
123+
gst.setOrient(json.optDouble("orient", Double.NaN));
124+
gst.setLat(json.optDouble("lat", Double.NaN));
125+
gst.setLon(json.optDouble("lon", Double.NaN));
126+
gst.setAlt(json.optDouble("alt", Double.NaN));
127+
gps = gst;
128+
} else if ("ATT".equals(clazz)) {
129+
final ATTObject att = new ATTObject();
130+
att.setTag(json.optString("tag", null));
131+
att.setDevice(json.optString("device", null));
132+
att.setTimestamp(this.parseTimestamp(json, "time"));
133+
att.setHeading(json.optDouble("heading", Double.NaN));
134+
att.setPitch(json.optDouble("pitch", Double.NaN));
135+
att.setYaw(json.optDouble("yaw", Double.NaN));
136+
att.setRoll(json.optDouble("roll", Double.NaN));
137+
att.setDip(json.optDouble("dip", Double.NaN));
138+
att.setMag_len(json.optDouble("mag_len", Double.NaN));
139+
att.setMag_x(json.optDouble("mag_x", Double.NaN));
140+
att.setMag_y(json.optDouble("mag_y", Double.NaN));
141+
att.setMag_z(json.optDouble("mag_z", Double.NaN));
142+
att.setAcc_len(json.optDouble("acc_len", Double.NaN));
143+
att.setAcc_x(json.optDouble("acc_x", Double.NaN));
144+
att.setAcc_y(json.optDouble("acc_y", Double.NaN));
145+
att.setAcc_z(json.optDouble("acc_z", Double.NaN));
146+
att.setGyro_x(json.optDouble("gyro_x", Double.NaN));
147+
att.setGyro_y(json.optDouble("gyro_y", Double.NaN));
148+
att.setDepth(json.optDouble("depth", Double.NaN));
149+
att.setTemperature(json.optDouble("temperature", Double.NaN));
150+
att.setMagState(json.optString("mag_st", null));
151+
att.setRollState(json.optString("roll_st", null));
152+
att.setPitchState(json.optString("pitch_st", null));
153+
att.setYawState(json.optString("yaw_st", null));
154+
gps = att;
155+
} else if ("SUBFRAME".equals(clazz)) {
156+
final SUBFRAMEObject subframe = new SUBFRAMEObject();
157+
subframe.setDevice(json.optString("device", null));
158+
subframe.setMSBs(json.optInt("TOW17"));
159+
subframe.setSatelliteNumber(json.optInt("tSV"));
160+
subframe.setSubframeNumber(json.optInt("frame"));
161+
subframe.setScaled(json.optBoolean("scaled", false));
162+
subframe.setPageid(json.optInt("pageid"));
163+
if (json.has("system_message")) {
164+
subframe.setSystemMessage(json.optString("system_message"));
165+
} else if (json.has("ALMANAC")) {
166+
subframe.setAlmanac((ALMANACObject)this.parse(json.optJSONObject("ALMANAC")));
167+
} else if (json.has("EPHEM1")) {
168+
subframe.setEphem1((EPHEM1Object)this.parse(json.optJSONObject("EPHEM1")));
169+
} else if (json.has("EPHEM2")) {
170+
subframe.setEphem2((EPHEM2Object)this.parse(json.optJSONObject("EPHEM2")));
171+
} else if (json.has("EPHEM3")) {
172+
subframe.setEphem3((EPHEM3Object)this.parse(json.optJSONObject("EPHEM3")));
173+
} else if (json.has("ERD")) {
174+
subframe.setErd((ERDObject)this.parse(json.optJSONObject("ERD")));
175+
} else if (json.has("HEALTH")) {
176+
subframe.setHealth((HEALTHObject)this.parse(json.optJSONObject("HEALTH")));
177+
} else if (json.has("HEALTH2")) {
178+
subframe.setHealth2((HEALTH2Object)this.parse(json.optJSONObject("HEALTH2")));
179+
} else if (json.has("IONO")) {
180+
subframe.setIono((IONOObject)this.parse(json.optJSONObject("IONO")));
181+
} else {
182+
System.err.println("Unknown subframe: " + json.toString());
183+
}
184+
gps = subframe;
185+
} else if ("VERSION".equals(clazz)) {
186+
final VersionObject ver = new VersionObject();
187+
ver.setRelease(json.optString("release", null));
188+
ver.setRev(json.optString("rev", null));
189+
ver.setProtocolMajor(json.optDouble("proto_major", 0));
190+
ver.setProtocolMinor(json.optDouble("proto_minor", 0));
191+
gps = ver;
192+
} else if ("DEVICES".equals(clazz)) {
193+
final DevicesObject devs = new DevicesObject();
194+
devs.setDevices(this.parseObjectArray(json.optJSONArray("devices"), DeviceObject.class));
195+
gps = devs;
196+
} else if ("DEVICE".equals(clazz)) {
197+
final DeviceObject dev = new DeviceObject();
198+
dev.setPath(json.optString("path", null));
199+
dev.setActivated(this.parseTimestamp(json, "activated"));
200+
dev.setDriver(json.optString("driver", null));
201+
dev.setBps(json.optInt("bps", 0));
202+
dev.setParity(EParity.fromString(json.optString("parity")));
203+
dev.setStopbit(json.optInt("stopbit"));
204+
dev.setNativeMode(json.optInt("native", 0) == 1);
205+
dev.setCycle(json.optInt("cycle"));
206+
dev.setMincycle(json.optInt("mincycle"));
207+
gps = dev;
208+
} else if ("WATCH".equals(clazz)) {
209+
final WatchObject watch = new WatchObject();
210+
watch.setEnable(json.optBoolean("enable", true));
211+
watch.setDump(json.optBoolean("json", false));
212+
gps = watch;
213+
} else if ("POLL".equals(clazz)) {
214+
// check this for gpsd version <= 3.5
215+
final PollObject poll = new PollObject();
216+
poll.setTimestamp(this.parseTimestamp(json, "time"));
217+
poll.setActive(json.optInt("active", 0));
218+
poll.setFixes(this.parseObjectArray(json.optJSONArray("fixes"), TPVObject.class));
219+
poll.setSkyviews(this.parseObjectArray(json.optJSONArray("skyviews"), SKYObject.class));
220+
gps = poll;
221+
} else if (json.has("PRN")) { // SATObject
222+
final SATObject sat = new SATObject();
223+
sat.setPRN(json.optInt("PRN", -1));
224+
sat.setAzimuth(json.optInt("az", -1));
225+
sat.setElevation(json.optInt("el", -1));
226+
sat.setSignalStrength(json.optInt("ss", -1));
227+
sat.setUsed(json.optBoolean("used", false));
228+
gps = sat;
229+
} else if (json.has("deltai")) { // ALMANACObject
230+
final ALMANACObject almanac = new ALMANACObject();
231+
almanac.setID(json.optInt("ID"));
232+
almanac.setHealth(json.optInt("Health"));
233+
almanac.setE(json.optDouble("e", Double.NaN));
234+
almanac.setToa(json.optInt("toa"));
235+
almanac.setDeltai(json.optDouble("deltai", Double.NaN));
236+
almanac.setOmegad(json.optDouble("Omegad", Double.NaN));
237+
almanac.setSqrtA(json.optDouble("sqrtA", Double.NaN));
238+
almanac.setOmega0(json.optDouble("Omega0", Double.NaN));
239+
almanac.setOmega(json.optDouble("omega", Double.NaN));
240+
almanac.setM0(json.optDouble("M0", Double.NaN));
241+
almanac.setAf0(json.optDouble("af0", Double.NaN));
242+
almanac.setAf1(json.optDouble("af1", Double.NaN));
243+
gps = almanac;
244+
} else if (json.has("IODC")) { // EPHEM1Object
245+
final EPHEM1Object emphem1 = new EPHEM1Object();
246+
emphem1.setWN(json.optInt("WN"));
247+
emphem1.setIODC(json.optInt("IODC"));
248+
emphem1.setL2(json.optInt("L2"));
249+
emphem1.setUra(json.optDouble("ura", Double.NaN));
250+
emphem1.setHlth(json.optDouble("hlth", Double.NaN));
251+
emphem1.setL2P(json.optInt("L2P"));
252+
emphem1.setTgd(json.optDouble("Tgd", Double.NaN));
253+
emphem1.setToc(json.optInt("toc"));
254+
emphem1.setAf2(json.optDouble("af2", Double.NaN));
255+
emphem1.setAf1(json.optDouble("af1", Double.NaN));
256+
emphem1.setAf0(json.optDouble("af0", Double.NaN));
257+
gps = emphem1;
258+
} else if (json.has("Crs")) { // EPHEM2Object
259+
final EPHEM2Object emphem2 = new EPHEM2Object();
260+
emphem2.setIODE(json.optInt("IODE"));
261+
emphem2.setCrs(json.optDouble("Crs", Double.NaN));
262+
emphem2.setDeltan(json.optDouble("deltan", Double.NaN));
263+
emphem2.setM0(json.optDouble("M0", Double.NaN));
264+
emphem2.setCuc(json.optDouble("Cuc", Double.NaN));
265+
emphem2.setE(json.optDouble("e", Double.NaN));
266+
emphem2.setCus(json.optDouble("Cus", Double.NaN));
267+
emphem2.setSqrtA(json.optInt("sqrtA"));
268+
emphem2.setToe(json.optInt("toe"));
269+
emphem2.setFIT(json.optInt("FIT"));
270+
emphem2.setAODO(json.optInt("AODO"));
271+
gps = emphem2;
272+
} else if (json.has("IDOT")) { // EPHEM3Object
273+
final EPHEM3Object emphem3 = new EPHEM3Object();
274+
emphem3.setIODE(json.optInt("IODE"));
275+
emphem3.setIDOT(json.optDouble("IDOT", Double.NaN));
276+
emphem3.setCic(json.optDouble("Cic", Double.NaN));
277+
emphem3.setOmega0(json.optDouble("Omega0", Double.NaN));
278+
emphem3.setCis(json.optDouble("Cis", Double.NaN));
279+
emphem3.setI0(json.optDouble("i0", Double.NaN));
280+
emphem3.setCrc(json.optDouble("Crc", Double.NaN));
281+
emphem3.setOmega(json.optDouble("omega", Double.NaN));
282+
emphem3.setOmegad(json.optDouble("Omegad", Double.NaN));
283+
gps = emphem3;
284+
} else if (json.has("ERD30")) { // ERDObject
285+
final ERDObject erd = new ERDObject();
286+
erd.setAi(json.optInt("ai"));
287+
for (int index = 1; index <= 30; index++) {
288+
erd.setERDbyIndex(index - 1, json.optInt("ERD" + index));
289+
}
290+
gps = erd;
291+
} else if (json.has("SVH32")) { // HEALTHObject
292+
final HEALTHObject health = new HEALTHObject();
293+
health.setData_id(json.optInt("data_id"));
294+
for (int index = 1; index <= 32; index++) {
295+
health.setSVbyIndex(index - 1, json.optInt("SV" + index));
296+
}
297+
for (int index = 0; index <= 7; index++) {
298+
health.setSVHbyIndex(index, json.optInt("SVH" + (index + 25)));
299+
}
300+
gps = health;
301+
} else if (json.has("WNa")) { // HEALTH2Object
302+
final HEALTH2Object health2 = new HEALTH2Object();
303+
health2.setToa(json.optInt("toa"));
304+
health2.setWNa(json.optInt("WNa"));
305+
for (int index = 1; index <= 24; index++) {
306+
health2.setSVbyIndex(index - 1, json.optInt("SV" + index));
307+
}
308+
gps = health2;
309+
} else if (json.has("WNlsf")) { // IONOObject
310+
final IONOObject iono = new IONOObject();
311+
iono.setAlpha0(json.optDouble("a0", Double.NaN));
312+
iono.setAlpha1(json.optDouble("a1", Double.NaN));
313+
iono.setAlpha2(json.optDouble("a2", Double.NaN));
314+
iono.setAlpha3(json.optDouble("a3", Double.NaN));
315+
iono.setBeta0(json.optDouble("b0", Double.NaN));
316+
iono.setBeta1(json.optDouble("b1", Double.NaN));
317+
iono.setBeta2(json.optDouble("b2", Double.NaN));
318+
iono.setBeta3(json.optDouble("b3", Double.NaN));
319+
iono.setA0(json.optDouble("A0", Double.NaN));
320+
iono.setA1(json.optDouble("A1", Double.NaN));
321+
iono.setTot(json.optDouble("tot", Double.NaN));
322+
iono.setWNt(json.optInt("WNt"));
323+
iono.setLeap(json.optInt("ls"));
324+
iono.setWNlsf(json.optInt("WNlsf"));
325+
iono.setDN(json.optInt("DN"));
326+
iono.setLsf(json.optInt("lsf"));
327+
gps = iono;
328+
} else {
329+
throw new ParseException("Invalid object class: " + clazz);
330+
}
331+
return gps;
332+
}
333+
334+
private double parseTimestamp(final JSONObject json, final String fieldName) {
335+
try {
336+
final String text = json.optString(fieldName, null);
337+
LegacyResultParser.log.log(Level.FINE, fieldName + ": {0}", text);
338+
339+
if (text != null) {
340+
final Date date = this.dateFormat.parse(text);
341+
if (LegacyResultParser.log.isLoggable(Level.FINE)) {
342+
LegacyResultParser.log.log(Level.FINE, "Date: {0}", DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL).format(date));
343+
}
344+
return (date.getTime() / 1000.0);
345+
}
346+
} catch (final Exception ex) {
347+
LegacyResultParser.log.log(Level.INFO, "Failed to parse time", ex);
348+
}
349+
return Double.NaN;
350+
}
351+
}

src/main/java/de/taimos/gpsd4java/backend/ResultParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public IGPSObject parse(final JSONObject json) throws ParseException {
211211
watch.setDump(json.optBoolean("json", false));
212212
gps = watch;
213213
} else if ("POLL".equals(clazz)) {
214+
System.out.println("POLL: " + json.toString());
214215
// check this for gpsd version <= 3.5
215216
// final PollObject poll = new PollObject();
216217
// poll.setTimestamp(this.parseTimestamp(json, "time"));

0 commit comments

Comments
 (0)