Changeset 18941 in osm for applications
- Timestamp:
- 2009-12-04T18:44:56+01:00 (15 years ago)
- Location:
- applications/editors/josm/plugins/livegps/src/livegps
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/livegps/src/livegps/LiveGpsAcquirer.java
r16945 r18941 2 2 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.beans.PropertyChangeEvent; 5 6 import java.beans.PropertyChangeListener; 6 7 import java.io.BufferedReader; 7 import java.io.FileInputStream;8 import java.io.FileNotFoundException;9 8 import java.io.IOException; 10 9 import java.io.InputStreamReader; … … 13 12 import java.util.ArrayList; 14 13 import java.util.List; 15 import java.util.Properties; 16 14 15 import org.openstreetmap.josm.Main; 17 16 import org.openstreetmap.josm.data.coor.LatLon; 18 import org.openstreetmap.josm.Main;19 17 20 18 public class LiveGpsAcquirer implements Runnable { … … 22 20 BufferedReader gpsdReader; 23 21 boolean connected = false; 24 String gpsdHost = Main.pref.get("livegps.gpsd.host", "localhost");22 String gpsdHost = Main.pref.get("livegps.gpsd.host", "localhost"); 25 23 int gpsdPort = Main.pref.getInteger("livegps.gpsd.port", 2947); 26 24 boolean shutdownFlag = false; 27 private List<PropertyChangeListener> propertyChangeListener = new ArrayList<PropertyChangeListener>();25 private final List<PropertyChangeListener> propertyChangeListener = new ArrayList<PropertyChangeListener>(); 28 26 private PropertyChangeEvent lastStatusEvent; 29 27 private PropertyChangeEvent lastDataEvent; 30 28 31 29 /** 30 * The LiveGpsSuppressor is queried, if an event shall be suppressed. 31 */ 32 private LiveGpsSuppressor suppressor = null; 33 34 /** 35 * separate thread, where the LiveGpsSuppressor executes. 36 */ 37 private Thread suppressorThread = null; 38 39 /** 32 40 * Adds a property change listener to the acquirer. 33 41 * @param listener the new listener 34 42 */ 35 43 public void addPropertyChangeListener(PropertyChangeListener listener) { 36 if (!propertyChangeListener.contains(listener)) {44 if (!propertyChangeListener.contains(listener)) { 37 45 propertyChangeListener.add(listener); 38 46 } … … 44 52 */ 45 53 public void removePropertyChangeListener(PropertyChangeListener listener) { 46 if (propertyChangeListener.contains(listener)) {54 if (propertyChangeListener.contains(listener)) { 47 55 propertyChangeListener.remove(listener); 48 56 } … … 52 60 * Fire a gps status change event. Fires events with key "gpsstatus" and a {@link LiveGpsStatus} 53 61 * object as value. 62 * The status event may be sent any time. 54 63 * @param status the status. 55 64 * @param statusMessage the status message. 56 65 */ 57 public void fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus status, String statusMessage) { 58 PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsstatus", null, new LiveGpsStatus(status, statusMessage)); 59 if(!event.equals(lastStatusEvent)) { 66 public void fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus status, 67 String statusMessage) { 68 PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsstatus", 69 null, new LiveGpsStatus(status, statusMessage)); 70 71 if (!event.equals(lastStatusEvent)) { 60 72 firePropertyChangeEvent(event); 61 73 lastStatusEvent = event; … … 66 78 * Fire a gps data change event to all listeners. Fires events with key "gpsdata" and a 67 79 * {@link LiveGpsData} object as values. 80 * This event is only sent, when the suppressor permits it. This 81 * event will cause the UI to re-draw itself, which has some performance penalty, 68 82 * @param oldData the old gps data. 69 83 * @param newData the new gps data. 70 84 */ 71 85 public void fireGpsDataChangeEvent(LiveGpsData oldData, LiveGpsData newData) { 72 PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsdata", oldData, newData); 73 if(!event.equals(lastDataEvent)) { 86 PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsdata", 87 oldData, newData); 88 89 if (!event.equals(lastDataEvent) && checkSuppress()) { 74 90 firePropertyChangeEvent(event); 75 91 lastDataEvent = event; … … 90 106 LiveGpsData oldGpsData = null; 91 107 LiveGpsData gpsData = null; 108 109 initSuppressor(); 110 92 111 shutdownFlag = false; 93 while (!shutdownFlag) {112 while (!shutdownFlag) { 94 113 double lat = 0; 95 114 double lon = 0; … … 98 117 boolean haveFix = false; 99 118 100 try 101 { 102 if (!connected) 103 { 119 try { 120 if (!connected) { 104 121 System.out.println("LiveGps tries to connect to gpsd"); 105 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTING, tr("Connecting")); 122 fireGpsStatusChangeEvent( 123 LiveGpsStatus.GpsStatus.CONNECTING, 124 tr("Connecting")); 106 125 InetAddress[] addrs = InetAddress.getAllByName(gpsdHost); 107 for (int i =0; i < addrs.length && gpsdSocket == null; i++) {126 for (int i = 0; i < addrs.length && gpsdSocket == null; i++) { 108 127 try { 109 128 gpsdSocket = new Socket(addrs[i], gpsdPort); 110 129 break; 111 130 } catch (Exception e) { 112 System.out.println("LiveGps: Could not open connection to gpsd: " + e); 131 System.out 132 .println("LiveGps: Could not open connection to gpsd: " 133 + e); 113 134 gpsdSocket = null; 114 135 } 115 136 } 116 137 117 if (gpsdSocket != null) 118 { 119 gpsdReader = new BufferedReader(new InputStreamReader(gpsdSocket.getInputStream())); 120 gpsdSocket.getOutputStream().write(new byte[] { 'w', 13, 10 }); 121 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTING, tr("Connecting")); 138 if (gpsdSocket != null) { 139 gpsdReader = new BufferedReader(new InputStreamReader( 140 gpsdSocket.getInputStream())); 141 gpsdSocket.getOutputStream().write( 142 new byte[] { 'w', 13, 10 }); 143 fireGpsStatusChangeEvent( 144 LiveGpsStatus.GpsStatus.CONNECTING, 145 tr("Connecting")); 122 146 connected = true; 123 System.out.println("LiveGps: Connected to gpsd"); 124 } 125 } 126 127 128 if(connected) { 147 System.out.println("LiveGps: Connected to gpsd"); 148 } 149 } 150 151 if (connected) { 129 152 // <FIXXME date="23.06.2007" author="cdaller"> 130 // TODO this read is blocking if gps is connected but has no fix, so gpsd does not send positions 153 // TODO this read is blocking if gps is connected but has no 154 // fix, so gpsd does not send positions 131 155 String line = gpsdReader.readLine(); 132 156 // </FIXXME> 133 if (line == null) break; 157 if (line == null) 158 break; 134 159 String words[] = line.split(","); 135 160 … … 141 166 for (int i = 1; i < words.length; i++) { 142 167 143 if ((words[i].length() < 2) || (words[i].charAt(1) != '=')) { 168 if ((words[i].length() < 2) 169 || (words[i].charAt(1) != '=')) { 144 170 // unexpected response. 145 171 continue; … … 150 176 oldGpsData = gpsData; 151 177 gpsData = new LiveGpsData(); 152 switch (what) {178 switch (what) { 153 179 case 'O': 154 180 // full report, tab delimited. … … 160 186 speed = Float.parseFloat(status[9]); 161 187 course = Float.parseFloat(status[8]); 162 //view.setSpeed(speed); 163 //view.setCourse(course); 164 } catch (NumberFormatException nex) {} 188 // view.setSpeed(speed); 189 // view.setCourse(course); 190 } catch (NumberFormatException nex) { 191 } 165 192 haveFix = true; 166 193 } … … 179 206 // not interested 180 207 } 181 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTED, tr("Connected")); 208 fireGpsStatusChangeEvent( 209 LiveGpsStatus.GpsStatus.CONNECTED, 210 tr("Connected")); 182 211 gpsData.setFix(haveFix); 183 212 if (haveFix) { 184 // view.setCurrentPosition(lat, lon);213 // view.setCurrentPosition(lat, lon); 185 214 gpsData.setLatLon(new LatLon(lat, lon)); 186 215 gpsData.setSpeed(speed); … … 191 220 } else { 192 221 // not connected: 193 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED, tr("Not connected")); 194 try { Thread.sleep(1000); } catch (InterruptedException ignore) {}; 195 } 196 } catch(IOException iox) { 222 fireGpsStatusChangeEvent( 223 LiveGpsStatus.GpsStatus.DISCONNECTED, 224 tr("Not connected")); 225 try { 226 Thread.sleep(1000); 227 } catch (InterruptedException ignore) { 228 } 229 ; 230 } 231 } catch (IOException iox) { 197 232 connected = false; 198 if (gpsData != null) {233 if (gpsData != null) { 199 234 gpsData.setFix(false); 200 235 fireGpsDataChangeEvent(oldGpsData, gpsData); 201 236 } 202 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTION_FAILED, tr("Connection Failed")); 203 try { Thread.sleep(1000); } catch (InterruptedException ignore) {}; 237 fireGpsStatusChangeEvent( 238 LiveGpsStatus.GpsStatus.CONNECTION_FAILED, 239 tr("Connection Failed")); 240 try { 241 Thread.sleep(1000); 242 } catch (InterruptedException ignore) { 243 } 244 ; 204 245 // send warning to layer 205 246 … … 207 248 } 208 249 209 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED, tr("Not connected")); 250 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED, 251 tr("Not connected")); 210 252 if (gpsdSocket != null) { 211 253 try { 212 gpsdSocket.close(); 213 gpsdSocket = null; 214 System.out.println("LiveGps: Disconnected from gpsd"); 254 gpsdSocket.close(); 255 gpsdSocket = null; 256 System.out.println("LiveGps: Disconnected from gpsd"); 257 } catch (Exception e) { 258 System.out 259 .println("LiveGps: Unable to close socket; reconnection may not be possible"); 215 260 } 216 catch (Exception e) { 217 System.out.println("LiveGps: Unable to close socket; reconnection may not be possible"); 261 } 262 } 263 264 /** 265 * Initialize the suppressor and start its thread. 266 */ 267 private void initSuppressor() { 268 suppressor = new LiveGpsSuppressor(); 269 suppressorThread = new Thread(suppressor); 270 suppressorThread.start(); 271 } 272 273 public void shutdown() { 274 // shut down the suppressor thread, too. 275 suppressor.shutdown(); 276 this.suppressor = null; 277 shutdownFlag = true; 278 } 279 280 /** 281 * Check, if an event may be sent now. 282 * @return true, if an event may be sent. 283 */ 284 private boolean checkSuppress() { 285 if (suppressor != null) { 286 if (suppressor.isAllowUpdate()) { 287 return true; 288 } else { 289 return false; 218 290 } 219 } 220 } 221 222 public void shutdown() 223 { 224 shutdownFlag = true; 291 } else { 292 return true; 293 } 225 294 } 226 295 } -
applications/editors/josm/plugins/livegps/src/livegps/LiveGpsPlugin.java
r18408 r18941 174 174 } 175 175 176 177 176 /** 178 177 * Add a listener for gps events.
Note:
See TracChangeset
for help on using the changeset viewer.