Hello,

  gpsd gets new protocol and quickly forgets the old one. So I've made
a patch to support new protocol. It is WIP, yet, but I'm posting
it for your review. See attach.

  The patch works, however the movement of the cursor is quite bursty.
May be smth else apart from GPSd protocol is broken in this plugin, or
the problem is in gpsfake behavior.

  The patch requires the JSON Java classes from http://www.json.org/java/.
I took the source and put it into src in plugin directory. What is the right
way to make dependency on org.json class in a josm plugin?

-- 
Totus tuus, Glebius.
Index: LiveGpsAcquirer.java
===================================================================
--- LiveGpsAcquirer.java	(revision 21619)
+++ LiveGpsAcquirer.java	(working copy)
@@ -2,6 +2,8 @@
 
 import static org.openstreetmap.josm.tools.I18n.tr;
 
+import java.lang.Float;
+
 import java.beans.PropertyChangeEvent;
 import java.beans.PropertyChangeListener;
 import java.io.BufferedReader;
@@ -15,6 +17,9 @@
 import org.openstreetmap.josm.Main;
 import org.openstreetmap.josm.data.coor.LatLon;
 
+import org.json.JSONObject;
+import org.json.JSONException;
+
 public class LiveGpsAcquirer implements Runnable {
 	Socket gpsdSocket;
 	BufferedReader gpsdReader;
@@ -129,18 +134,18 @@
 							gpsdSocket = new Socket(addrs[i], gpsdPort);
 							break;
 						} catch (Exception e) {
-							System.out
-									.println("LiveGps: Could not open connection to gpsd: "
+							System.out.println("LiveGps: Could not open connection to gpsd: "
 											+ e);
 							gpsdSocket = null;
 						}
 					}
 
 					if (gpsdSocket != null) {
+						String Watch = new String("?WATCH={\"enable\":true,\"json\":true};\n");
+
 						gpsdReader = new BufferedReader(new InputStreamReader(
 								gpsdSocket.getInputStream()));
-						gpsdSocket.getOutputStream().write(
-								new byte[] { 'w', 13, 10 });
+						gpsdSocket.getOutputStream().write(Watch.getBytes());
 						fireGpsStatusChangeEvent(
 								LiveGpsStatus.GpsStatus.CONNECTING,
 								tr("Connecting"));
@@ -150,74 +155,48 @@
 				}
 
 				if (connected) {
+					String line, type;
+					JSONObject report;
+
 					// <FIXXME date="23.06.2007" author="cdaller">
 					// TODO this read is blocking if gps is connected but has no
 					// fix, so gpsd does not send positions
-					String line = gpsdReader.readLine();
+					line = gpsdReader.readLine();
 					// </FIXXME>
 					if (line == null)
 						break;
-					String words[] = line.split(",");
 
-					if ((words.length == 0) || (!words[0].equals("GPSD"))) {
-						// unexpected response.
-						continue;
+					try {
+						report = new JSONObject(line);
+						type = report.getString("class");
+					} catch (JSONException nex) {
+						System.out.println("not JSON class");
+						break;
 					}
+					if (!type.equals("TPV"))
+						continue;
 
-					for (int i = 1; i < words.length; i++) {
+					oldGpsData = gpsData;
+					gpsData = new LiveGpsData();
 
-						if ((words[i].length() < 2)
-								|| (words[i].charAt(1) != '=')) {
-							// unexpected response.
-							continue;
-						}
+					try {
+						lat = report.getDouble("lat");
+						lon = report.getDouble("lon");
+						speed = (new Float(report.getDouble("speed"))).floatValue();
+						course = (new Float(report.getDouble("track"))).floatValue();
+						haveFix = true;
+					} catch (JSONException nex) { haveFix = false; }
 
-						char what = words[i].charAt(0);
-						String value = words[i].substring(2);
-						oldGpsData = gpsData;
-						gpsData = new LiveGpsData();
-						switch (what) {
-						case 'O':
-							// full report, tab delimited.
-							String[] status = value.split("\\s+");
-							if (status.length >= 5) {
-								lat = Double.parseDouble(status[3]);
-								lon = Double.parseDouble(status[4]);
-								try {
-									speed = Float.parseFloat(status[9]);
-									course = Float.parseFloat(status[8]);
-									// view.setSpeed(speed);
-									// view.setCourse(course);
-								} catch (NumberFormatException nex) {
-								}
-								haveFix = true;
-							}
-							break;
-						case 'P':
-							// position report, tab delimited.
-							String[] pos = value.split("\\s+");
-							if (pos.length >= 2) {
-								lat = Double.parseDouble(pos[0]);
-								lon = Double.parseDouble(pos[1]);
-								speed = Float.NaN;
-								course = Float.NaN;
-								haveFix = true;
-							}
-							break;
-						default:
-							// not interested
-						}
-						fireGpsStatusChangeEvent(
-								LiveGpsStatus.GpsStatus.CONNECTED,
-								tr("Connected"));
-						gpsData.setFix(haveFix);
-						if (haveFix) {
-							// view.setCurrentPosition(lat, lon);
-							gpsData.setLatLon(new LatLon(lat, lon));
-							gpsData.setSpeed(speed);
-							gpsData.setCourse(course);
-							fireGpsDataChangeEvent(oldGpsData, gpsData);
-						}
+					fireGpsStatusChangeEvent(
+							LiveGpsStatus.GpsStatus.CONNECTED,
+							tr("Connected"));
+					gpsData.setFix(haveFix);
+					if (haveFix) {
+						// view.setCurrentPosition(lat, lon);
+						gpsData.setLatLon(new LatLon(lat, lon));
+						gpsData.setSpeed(speed);
+						gpsData.setCourse(course);
+						fireGpsDataChangeEvent(oldGpsData, gpsData);
 					}
 				} else {
 					// not connected:
@@ -226,9 +205,7 @@
 							tr("Not connected"));
 					try {
 						Thread.sleep(1000);
-					} catch (InterruptedException ignore) {
-					}
-					;
+					} catch (InterruptedException ignore) { }
 				}
 			} catch (IOException iox) {
 				connected = false;
_______________________________________________
josm-dev mailing list
[email protected]
http://lists.openstreetmap.org/listinfo/josm-dev

Reply via email to