Changeset 30234 in osm for applications/editors/josm/plugins/livegps
- Timestamp:
- 2014-01-27T00:58:04+01:00 (11 years ago)
- Location:
- applications/editors/josm/plugins/livegps
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/livegps/build.xml
r30131 r30234 2 2 <project name="livegps" default="dist" basedir="."> 3 3 <property name="commit.message" value="Changed the constructor signature of the plugin main class"/> 4 <property name="plugin.main.version" value="6 484"/>4 <property name="plugin.main.version" value="6756"/> 5 5 6 6 <!-- Configure these properties (replace "..." accordingly). -
applications/editors/josm/plugins/livegps/src/livegps/LiveGpsAcquirer.java
r29769 r30234 2 2 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 5 4 6 5 import java.beans.PropertyChangeEvent; … … 9 8 import java.io.IOException; 10 9 import java.io.InputStreamReader; 10 import java.io.StringReader; 11 11 import java.net.InetAddress; 12 12 import java.net.Socket; … … 14 14 import java.util.List; 15 15 16 import javax.json.Json; 17 import javax.json.JsonException; 18 import javax.json.JsonNumber; 19 import javax.json.JsonObject; 20 16 21 import org.openstreetmap.josm.Main; 17 18 import org.json.JSONObject;19 import org.json.JSONException;20 22 21 23 public class LiveGpsAcquirer implements Runnable { … … 41 43 */ 42 44 public LiveGpsAcquirer() { 43 super();44 45 45 46 gpsdHost = Main.pref.get(C_HOST, DEFAULT_HOST); … … 77 78 * @param statusMessage the status message. 78 79 */ 79 public void fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus status, 80 String statusMessage) { 80 public void fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus status, String statusMessage) { 81 81 PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsstatus", 82 82 null, new LiveGpsStatus(status, statusMessage)); … … 97 97 */ 98 98 public void fireGpsDataChangeEvent(LiveGpsData oldData, LiveGpsData newData) { 99 PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsdata", 100 oldData, newData); 99 PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsdata", oldData, newData); 101 100 102 101 if (!event.equals(lastDataEvent)) { … … 124 123 while (!shutdownFlag) { 125 124 126 127 125 while (!connected) { 126 try { 128 127 connect(); 129 } catch (IOException iox) { 130 fireGpsStatusChangeEvent( LiveGpsStatus.GpsStatus.CONNECTION_FAILED, tr("Connection Failed")); 131 try { 132 Thread.sleep(1000); 133 } catch (InterruptedException ignore) {} 134 } 135 } 136 137 assert (connected); 138 139 try { 140 String line; 141 142 // <FIXXME date="23.06.2007" author="cdaller"> 143 // TODO this read is blocking if gps is connected but has no 144 // fix, so gpsd does not send positions 145 line = gpsdReader.readLine(); 146 // </FIXXME> 147 if (line == null) 148 throw new IOException(); 149 150 if (JSONProtocol == true) 151 gpsData = ParseJSON(line); 152 else 153 gpsData = ParseOld(line); 154 155 if (gpsData == null) 156 continue; 157 158 fireGpsDataChangeEvent(oldGpsData, gpsData); 159 oldGpsData = gpsData; 128 } catch (IOException iox) { 129 fireGpsStatusChangeEvent( LiveGpsStatus.GpsStatus.CONNECTION_FAILED, tr("Connection Failed")); 130 try { 131 Thread.sleep(1000); 132 } catch (InterruptedException ignore) { 133 134 } 135 } 136 } 137 138 assert (connected); 139 140 try { 141 String line; 142 143 // <FIXXME date="23.06.2007" author="cdaller"> 144 // TODO this read is blocking if gps is connected but has no 145 // fix, so gpsd does not send positions 146 line = gpsdReader.readLine(); 147 // </FIXXME> 148 if (line == null) 149 throw new IOException(); 150 151 if (JSONProtocol == true) 152 gpsData = ParseJSON(line); 153 else 154 gpsData = ParseOld(line); 155 156 if (gpsData == null) 157 continue; 158 159 fireGpsDataChangeEvent(oldGpsData, gpsData); 160 oldGpsData = gpsData; 160 161 } catch (IOException iox) { 161 System.out.println("LiveGps: lost connection to gpsd");162 Main.warn("LiveGps: lost connection to gpsd"); 162 163 fireGpsStatusChangeEvent( 163 164 LiveGpsStatus.GpsStatus.CONNECTION_FAILED, 164 165 tr("Connection Failed")); 165 166 disconnect(); 166 167 try { 167 168 Thread.sleep(1000); … … 171 172 } 172 173 173 System.out.println("LiveGps: Disconnected from gpsd");174 Main.info("LiveGps: Disconnected from gpsd"); 174 175 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED, 175 176 tr("Not connected")); 176 177 disconnect(); 177 178 } 178 179 … … 182 183 183 184 private void connect() throws IOException { 184 J SONObject greeting;185 JsonObject greeting; 185 186 String line, type, release; 186 187 187 System.out.println("LiveGps: trying to connect to gpsd at " + gpsdHost + ":" + gpsdPort);188 Main.info("LiveGps: trying to connect to gpsd at " + gpsdHost + ":" + gpsdPort); 188 189 fireGpsStatusChangeEvent( LiveGpsStatus.GpsStatus.CONNECTING, tr("Connecting")); 189 190 … … 194 195 break; 195 196 } catch (IOException e) { 196 System.out.println("LiveGps: Could not open connection to gpsd: " + e);197 Main.warn("LiveGps: Could not open connection to gpsd: " + e); 197 198 gpsdSocket = null; 198 199 } … … 213 214 214 215 try { 215 greeting = new JSONObject(line);216 greeting = Json.createReader(new StringReader(line)).readObject(); 216 217 type = greeting.getString("class"); 217 218 if (type.equals("VERSION")) { 218 219 release = greeting.getString("release"); 219 System.out.println("LiveGps: Connected to gpsd " + release);220 Main.info("LiveGps: Connected to gpsd " + release); 220 221 } else 221 System.out.println("LiveGps: Unexpected JSON in gpsd greeting: " + line);222 } catch (J SONException jex) {222 Main.info("LiveGps: Unexpected JSON in gpsd greeting: " + line); 223 } catch (JsonException jex) { 223 224 if (line.startsWith("GPSD,")) { 224 225 connected = true; 225 226 JSONProtocol = false; 226 System.out.println("LiveGps: Connected to old gpsd protocol version.");227 Main.info("LiveGps: Connected to old gpsd protocol version."); 227 228 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTED, tr("Connected")); 228 229 } … … 230 231 231 232 if (JSONProtocol == true) { 232 JSONObject Watch = new JSONObject(); 233 try { 234 Watch.put("enable", true); 235 Watch.put("json", true); 236 } catch (JSONException je) {} 233 JsonObject Watch = Json.createObjectBuilder() 234 .add("enable", true) 235 .add("json", true) 236 .build(); 237 237 238 238 String Request = "?WATCH=" + Watch.toString() + ";\n"; … … 245 245 246 246 private void disconnect() { 247 248 249 247 assert(gpsdSocket != null); 248 249 connected = false; 250 250 251 251 try { 252 253 254 255 System.out.println("LiveGps: Unable to close socket; reconnection may not be possible");256 252 gpsdSocket.close(); 253 gpsdSocket = null; 254 } catch (Exception e) { 255 Main.warn("LiveGps: Unable to close socket; reconnection may not be possible"); 256 } 257 257 } 258 258 259 259 private LiveGpsData ParseJSON(String line) { 260 J SONObject report;260 JsonObject report; 261 261 String type; 262 262 double lat = 0; … … 268 268 269 269 try { 270 report = new JSONObject(line);270 report = Json.createReader(new StringReader(line)).readObject(); 271 271 type = report.getString("class"); 272 } catch (J SONException jex) {273 System.out.println("LiveGps: line read from gpsd is not a JSON object:" + line);272 } catch (JsonException jex) { 273 Main.warn("LiveGps: line read from gpsd is not a JSON object:" + line); 274 274 return null; 275 275 } … … 278 278 279 279 try { 280 lat = report.getDouble("lat"); 281 lon = report.getDouble("lon"); 282 speed = (new Float(report.getDouble("speed"))).floatValue(); 283 course = (new Float(report.getDouble("track"))).floatValue(); 284 if (report.has("epx")) 285 epx = (new Float(report.getDouble("epx"))).floatValue(); 286 if (report.has("epy")) 287 epy = (new Float(report.getDouble("epy"))).floatValue(); 280 lat = report.getJsonNumber("lat").doubleValue(); 281 lon = report.getJsonNumber("lon").doubleValue(); 282 speed = (new Float(report.getJsonNumber("speed").doubleValue())).floatValue(); 283 course = (new Float(report.getJsonNumber("track").doubleValue())).floatValue(); 284 JsonNumber epxJson = report.getJsonNumber("epx"); 285 if (epxJson != null) 286 epx = (new Float(epxJson.doubleValue())).floatValue(); 287 JsonNumber epyJson = report.getJsonNumber("epy"); 288 if (epyJson != null) 289 epy = (new Float(epyJson.doubleValue())).floatValue(); 288 290 289 291 return new LiveGpsData(lat, lon, course, speed, epx, epy); 290 } catch (JSONException je) {} 292 } catch (JsonException je) { 293 Main.debug(je.getMessage()); 294 } 291 295 292 296 return null;
Note:
See TracChangeset
for help on using the changeset viewer.