Changeset 12778 in osm for applications/editors/josm/plugins/livegps
- Timestamp:
- 2009-01-01T18:28:53+01:00 (16 years ago)
- Location:
- applications/editors/josm/plugins/livegps/livegps
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/livegps/livegps/LiveGpsAcquirer.java
r6709 r12778 18 18 19 19 public class LiveGpsAcquirer implements Runnable { 20 21 22 23 24 25 26 20 Socket gpsdSocket; 21 BufferedReader gpsdReader; 22 boolean connected = false; 23 String gpsdHost = "localhost"; 24 int gpsdPort = 2947; 25 String configFile = "liveGPS.conf"; 26 boolean shutdownFlag = false; 27 27 private List<PropertyChangeListener> propertyChangeListener = new ArrayList<PropertyChangeListener>(); 28 28 private PropertyChangeEvent lastStatusEvent; 29 29 private PropertyChangeEvent lastDataEvent; 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 * Adds a property change listener to the acquirer. 30 31 public LiveGpsAcquirer(String pluginDir) { 32 33 Properties liveGPSconfig = new Properties(); 34 35 FileInputStream fis = null; 36 37 try { 38 fis = new FileInputStream(pluginDir + configFile); 39 } catch (FileNotFoundException e) { 40 System.err.println("No liveGPS.conf found, using defaults"); 41 } 42 43 if(fis != null) 44 { 45 try { 46 liveGPSconfig.load(fis); 47 this.gpsdHost = liveGPSconfig.getProperty("host"); 48 this.gpsdPort = Integer.parseInt(liveGPSconfig.getProperty("port")); 49 50 } catch (IOException e) { 51 System.err.println("Error while loading liveGPS.conf, using defaults"); 52 } 53 54 if(this.gpsdHost == null || this.gpsdPort == 0) 55 { 56 System.err.println("Error in liveGPS.conf, using defaults"); 57 this.gpsdHost = "localhost"; 58 this.gpsdPort = 2947; 59 } 60 } 61 62 } 63 64 /** 65 * Adds a property change listener to the acquirer. 66 66 * @param listener the new listener 67 67 */ … … 71 71 } 72 72 } 73 73 74 74 /** 75 75 * Fire a gps status change event. Fires events with key "gpsstatus" and a {@link LiveGpsStatus} … … 87 87 88 88 /** 89 * Fire a gps data change event to all listeners. Fires events with key "gpsdata" and a 89 * Fire a gps data change event to all listeners. Fires events with key "gpsdata" and a 90 90 * {@link LiveGpsData} object as values. 91 91 * @param oldData the old gps data. … … 99 99 } 100 100 } 101 101 102 102 /** 103 103 * Fires the given event to all listeners. … … 107 107 for (PropertyChangeListener listener : propertyChangeListener) { 108 108 listener.propertyChange(event); 109 } 110 } 111 112 113 114 109 } 110 } 111 112 public void run() { 113 LiveGpsData oldGpsData = null; 114 LiveGpsData gpsData = null; 115 115 shutdownFlag = false; 116 117 118 116 while(!shutdownFlag) { 117 double lat = 0; 118 double lon = 0; 119 119 float speed = 0; 120 120 float course = 0; 121 122 123 124 125 126 127 121 boolean haveFix = false; 122 123 try 124 { 125 if (!connected) 126 { 127 System.out.println("LiveGps tries to connect to gpsd"); 128 128 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTING, tr("Connecting")); 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 129 InetAddress[] addrs = InetAddress.getAllByName(gpsdHost); 130 for (int i=0; i < addrs.length && gpsdSocket == null; i++) { 131 try { 132 gpsdSocket = new Socket(addrs[i], gpsdPort); 133 break; 134 } catch (Exception e) { 135 System.out.println("LiveGps: Could not open connection to gpsd: " + e); 136 gpsdSocket = null; 137 } 138 } 139 140 if (gpsdSocket != null) 141 { 142 gpsdReader = new BufferedReader(new InputStreamReader(gpsdSocket.getInputStream())); 143 gpsdSocket.getOutputStream().write(new byte[] { 'w', 13, 10 }); 144 144 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTING, tr("Connecting")); 145 146 147 148 145 connected = true; 146 System.out.println("LiveGps: Connected to gpsd"); 147 } 148 } 149 149 150 150 … … 153 153 // TODO this read is blocking if gps is connected but has no fix, so gpsd does not send positions 154 154 String line = gpsdReader.readLine(); 155 // </FIXXME> 155 // </FIXXME> 156 156 if (line == null) break; 157 157 String words[] = line.split(","); … … 189 189 } 190 190 break; 191 case 'P': 191 case 'P': 192 192 // position report, tab delimited. 193 193 String[] pos = value.split("\\s+"); … … 217 217 try { Thread.sleep(1000); } catch (InterruptedException ignore) {}; 218 218 } 219 220 221 222 223 224 219 } catch(IOException iox) { 220 connected = false; 221 if(gpsData != null) { 222 gpsData.setFix(false); 223 fireGpsDataChangeEvent(oldGpsData, gpsData); 224 } 225 225 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTION_FAILED, tr("Connection Failed")); 226 227 228 229 230 231 226 try { Thread.sleep(1000); } catch (InterruptedException ignore) {}; 227 // send warning to layer 228 229 } 230 } 231 232 232 fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED, tr("Not connected")); 233 234 try { 235 gpsdSocket.close(); 233 if (gpsdSocket != null) { 234 try { 235 gpsdSocket.close(); 236 236 gpsdSocket = null; 237 238 } 237 System.out.println("LiveGps: Disconnected from gpsd"); 238 } 239 239 catch (Exception e) { 240 240 System.out.println("LiveGps: Unable to close socket; reconnection may not be possible"); 241 241 }; 242 242 } 243 243 } 244 245 246 247 248 244 245 public void shutdown() 246 { 247 shutdownFlag = true; 248 } 249 249 } -
applications/editors/josm/plugins/livegps/livegps/LiveGpsData.java
r3336 r12778 1 1 /** 2 * 2 * 3 3 */ 4 4 package livegps; … … 22 22 private String wayString; 23 23 private Way way; 24 24 25 25 /** 26 26 * @param latitude … … 38 38 } 39 39 /** 40 * 40 * 41 41 */ 42 42 public LiveGpsData() { … … 91 91 this.speed = speed; 92 92 } 93 93 94 94 /** 95 95 * @return the latlon … … 98 98 return this.latLon; 99 99 } 100 100 101 101 /** 102 102 * @param latLon … … 105 105 this.latLon = latLon; 106 106 } 107 107 108 108 public String toString() { 109 return getClass().getSimpleName() + "[fix=" + fix + ", lat=" + latLon.lat() 109 return getClass().getSimpleName() + "[fix=" + fix + ", lat=" + latLon.lat() 110 110 + ", long=" + latLon.lon() + ", speed=" + speed + ", course=" + course + "]"; 111 112 } 113 111 112 } 113 114 114 /** 115 115 * Returns the name of the way that is closest to the current coordinates or an 116 116 * empty string if no way is around. 117 * 117 * 118 118 * @return the name of the way that is closest to the current coordinates. 119 119 */ … … 156 156 return wayString; 157 157 } 158 158 159 159 /** 160 160 * Returns the closest way to this position. … … 163 163 public Way getWay() { 164 164 if(way == null) { 165 EastNorth eastnorth = Main.proj.latlon2eastNorth(getLatLon()); 166 Point xy = Main.map.mapView.getPoint(eastnorth); 165 EastNorth eastnorth = Main.proj.latlon2eastNorth(getLatLon()); 166 Point xy = Main.map.mapView.getPoint(eastnorth); 167 167 way = Main.map.mapView.getNearestWay(xy); 168 168 } 169 169 return way; 170 171 } 172 170 171 } 172 173 173 /* (non-Javadoc) 174 174 * @see java.lang.Object#hashCode() … … 207 207 } 208 208 209 210 209 210 211 211 } -
applications/editors/josm/plugins/livegps/livegps/LiveGpsDialog.java
r11974 r12778 1 1 /** 2 * 2 * 3 3 */ 4 4 package livegps; … … 62 62 add(new JScrollPane(panel), BorderLayout.CENTER); 63 63 } 64 64 65 65 /* (non-Javadoc) 66 66 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) … … 79 79 speedLabel.setText((Math.round(mySpeed*100)/100) + "km/h"); // m(s to km/h 80 80 courseLabel.setText(data.getCourse() + "deg"); 81 81 82 82 String wayString = data.getWayInfo(); 83 83 if(wayString.length() > 0) { … … 86 86 wayLabel.setText("unknown"); 87 87 } 88 88 89 89 } else { 90 90 // fixLabel.setText("no fix"); … … 101 101 panel.setBackground(Color.RED); 102 102 } else { 103 panel.setBackground(Color.WHITE); 103 panel.setBackground(Color.WHITE); 104 104 } 105 105 } 106 106 107 107 } 108 109 110 111 108 } -
applications/editors/josm/plugins/livegps/livegps/LiveGpsLayer.java
r11934 r12778 25 25 public static final String LAYER_NAME = tr("LiveGPS layer"); 26 26 public static final String KEY_LIVEGPS_COLOR ="color.livegps.position"; 27 LatLon lastPos; 28 WayPoint lastPoint; 29 GpxTrack trackBeingWritten; 30 Collection<WayPoint> trackSegment; 31 float speed; 32 float course; 33 String status; 34 //JLabel lbl; 35 boolean autocenter; 36 private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); 37 38 public LiveGpsLayer(GpxData data) 39 { 40 super (data, LAYER_NAME); 41 trackBeingWritten = new GpxTrack(); 42 trackBeingWritten.attr.put("desc", "josm live gps"); 43 trackSegment = new ArrayList<WayPoint>(); 44 trackBeingWritten.trackSegs.add(trackSegment); 45 data.tracks.add(trackBeingWritten); 46 } 47 48 void setCurrentPosition(double lat, double lon) 49 { 50 //System.out.println("adding pos " + lat + "," + lon); 51 LatLon thisPos = new LatLon(lat, lon); 52 if ((lastPos != null) && (thisPos.equalsEpsilon(lastPos))) { 53 // no change in position 54 // maybe show a "paused" cursor or some such 55 return; 56 } 57 58 lastPos = thisPos; 59 lastPoint = new WayPoint(thisPos); 60 lastPoint.attr.put("time", dateFormat.format(new Date())); 61 // synchronize when adding data, as otherwise the autosave action 62 // needs concurrent access and this results in an exception! 63 synchronized (LiveGpsLock.class) { 64 trackSegment.add(lastPoint); 27 LatLon lastPos; 28 WayPoint lastPoint; 29 GpxTrack trackBeingWritten; 30 Collection<WayPoint> trackSegment; 31 float speed; 32 float course; 33 String status; 34 //JLabel lbl; 35 boolean autocenter; 36 private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); 37 38 public LiveGpsLayer(GpxData data) 39 { 40 super (data, LAYER_NAME); 41 trackBeingWritten = new GpxTrack(); 42 trackBeingWritten.attr.put("desc", "josm live gps"); 43 trackSegment = new ArrayList<WayPoint>(); 44 trackBeingWritten.trackSegs.add(trackSegment); 45 data.tracks.add(trackBeingWritten); 46 } 47 48 void setCurrentPosition(double lat, double lon) 49 { 50 //System.out.println("adding pos " + lat + "," + lon); 51 LatLon thisPos = new LatLon(lat, lon); 52 if ((lastPos != null) && (thisPos.equalsEpsilon(lastPos))) { 53 // no change in position 54 // maybe show a "paused" cursor or some such 55 return; 65 56 } 66 if (autocenter) {67 center();68 }69 70 //Main.map.repaint();71 }72 57 73 public void center() 74 { 75 if (lastPoint != null) 76 Main.map.mapView.zoomTo(lastPoint.eastNorth, Main.map.mapView.getScale()); 77 } 78 79 // void setStatus(String status) 80 // { 81 // this.status = status; 82 // Main.map.repaint(); 58 lastPos = thisPos; 59 lastPoint = new WayPoint(thisPos); 60 lastPoint.attr.put("time", dateFormat.format(new Date())); 61 // synchronize when adding data, as otherwise the autosave action 62 // needs concurrent access and this results in an exception! 63 synchronized (LiveGpsLock.class) { 64 trackSegment.add(lastPoint); 65 } 66 if (autocenter) { 67 center(); 68 } 69 70 //Main.map.repaint(); 71 } 72 73 public void center() 74 { 75 if (lastPoint != null) 76 Main.map.mapView.zoomTo(lastPoint.eastNorth, Main.map.mapView.getScale()); 77 } 78 79 // void setStatus(String status) 80 // { 81 // this.status = status; 82 // Main.map.repaint(); 83 83 // System.out.println("LiveGps status: " + status); 84 // } 85 86 void setSpeed(float metresPerSecond) 87 { 88 speed = metresPerSecond; 89 //Main.map.repaint(); 90 } 84 // } 91 85 92 void setCourse(float degrees) 93 { 94 course = degrees; 95 //Main.map.repaint(); 96 } 97 98 public void setAutoCenter(boolean ac) 99 { 100 autocenter = ac; 101 } 86 void setSpeed(float metresPerSecond) 87 { 88 speed = metresPerSecond; 89 //Main.map.repaint(); 90 } 102 91 103 @Override public void paint(Graphics g, MapView mv) 104 { 105 //System.out.println("in paint"); 106 synchronized (LiveGpsLock.class) { 107 //System.out.println("in synced paint"); 108 super.paint(g, mv); 109 // int statusHeight = 50; 110 // Rectangle mvs = mv.getBounds(); 111 // mvs.y = mvs.y + mvs.height - statusHeight; 112 // mvs.height = statusHeight; 113 // g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.8f)); 114 // g.fillRect(mvs.x, mvs.y, mvs.width, mvs.height); 92 void setCourse(float degrees) 93 { 94 course = degrees; 95 //Main.map.repaint(); 96 } 115 97 116 if (lastPoint != null) 117 { 118 Point screen = mv.getPoint(lastPoint.eastNorth); 119 g.setColor(Main.pref.getColor(KEY_LIVEGPS_COLOR, Color.RED)); 120 g.drawOval(screen.x-10, screen.y-10,20,20); 121 g.drawOval(screen.x-9, screen.y-9,18,18); 122 } 98 public void setAutoCenter(boolean ac) 99 { 100 autocenter = ac; 101 } 123 102 124 // lbl.setText("gpsd: "+status+" Speed: " + speed + " Course: "+course); 125 // lbl.setBounds(0, 0, mvs.width-10, mvs.height-10); 126 // Graphics sub = g.create(mvs.x+5, mvs.y+5, mvs.width-10, mvs.height-10); 127 // lbl.paint(sub); 103 @Override public void paint(Graphics g, MapView mv) 104 { 105 //System.out.println("in paint"); 106 synchronized (LiveGpsLock.class) { 107 //System.out.println("in synced paint"); 108 super.paint(g, mv); 109 // int statusHeight = 50; 110 // Rectangle mvs = mv.getBounds(); 111 // mvs.y = mvs.y + mvs.height - statusHeight; 112 // mvs.height = statusHeight; 113 // g.setColor(new Color(1.0f, 1.0f, 1.0f, 0.8f)); 114 // g.fillRect(mvs.x, mvs.y, mvs.width, mvs.height); 128 115 129 // if(status != null) { 130 // g.setColor(Color.WHITE); 131 // g.drawString("gpsd: " + status, 5, mv.getBounds().height - 15); // lower left corner 132 // } 133 } 134 } 135 116 if (lastPoint != null) 117 { 118 Point screen = mv.getPoint(lastPoint.eastNorth); 119 g.setColor(Main.pref.getColor(KEY_LIVEGPS_COLOR, Color.RED)); 120 g.drawOval(screen.x-10, screen.y-10,20,20); 121 g.drawOval(screen.x-9, screen.y-9,18,18); 122 } 123 124 // lbl.setText("gpsd: "+status+" Speed: " + speed + " Course: "+course); 125 // lbl.setBounds(0, 0, mvs.width-10, mvs.height-10); 126 // Graphics sub = g.create(mvs.x+5, mvs.y+5, mvs.width-10, mvs.height-10); 127 // lbl.paint(sub); 128 129 // if(status != null) { 130 // g.setColor(Color.WHITE); 131 // g.drawString("gpsd: " + status, 5, mv.getBounds().height - 15); // lower left corner 132 // } 133 } 134 } 135 136 136 /* (non-Javadoc) 137 137 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) … … 154 154 } 155 155 } 156 156 157 157 } 158 158 -
applications/editors/josm/plugins/livegps/livegps/LiveGpsLock.java
r3073 r12778 1 1 /** 2 * 2 * 3 3 */ 4 4 package livegps; … … 8 8 * read or write live gps data must synchronize to this class. Especially the save action 9 9 * takes quite long, so concurrency problems occur. 10 * 10 * 11 11 * @author cdaller 12 12 * -
applications/editors/josm/plugins/livegps/livegps/LiveGpsPlugin.java
r11974 r12778 24 24 public class LiveGpsPlugin extends Plugin 25 25 { 26 27 26 private LiveGpsAcquirer acquirer = null; 27 private Thread acquirerThread = null; 28 28 private JMenu lgpsmenu; 29 29 private JCheckBoxMenuItem lgpscapture; … … 32 32 private LiveGpsDialog lgpsdialog; 33 33 List<PropertyChangeListener>listenerQueue; 34 35 34 35 private GpxData data = new GpxData(); 36 36 private LiveGpsLayer lgpslayer; 37 38 37 38 39 39 public class CaptureAction extends JosmAction { 40 40 public CaptureAction() { … … 77 77 } 78 78 79 public LiveGpsPlugin() 79 public LiveGpsPlugin() 80 80 { 81 81 MainMenu menu = Main.main.menu; … … 111 111 } 112 112 } 113 113 114 114 /** 115 115 * Returns <code>true</code> if autocenter is selected. … … 119 119 return lgpsautocenter.isSelected(); 120 120 } 121 121 122 122 /** 123 123 * Enable or disable gps tracking … … 155 155 } 156 156 } 157 158 157 158 159 159 /** 160 160 * Add a listener for gps events. -
applications/editors/josm/plugins/livegps/livegps/LiveGpsStatus.java
r3073 r12778 1 1 /** 2 * 2 * 3 3 */ 4 4 package livegps;
Note:
See TracChangeset
for help on using the changeset viewer.