|
29 | 29 | import java.net.UnknownHostException; |
30 | 30 | import java.util.ArrayList; |
31 | 31 | import java.util.List; |
| 32 | +import java.util.concurrent.atomic.AtomicLong; |
32 | 33 |
|
33 | 34 | import org.json.JSONException; |
34 | 35 | import org.json.JSONObject; |
@@ -78,8 +79,9 @@ public class GPSdEndpoint { |
78 | 79 |
|
79 | 80 | private int port; |
80 | 81 |
|
81 | | - private JSONObject watch; |
| 82 | + private String lastWatch; |
82 | 83 |
|
| 84 | + private AtomicLong retryInterval = new AtomicLong(1000); |
83 | 85 |
|
84 | 86 | /** |
85 | 87 | * Instantiate this class to connect to a GPSd server |
@@ -178,7 +180,7 @@ public WatchObject watch(final boolean enable, final boolean dumpData) throws IO |
178 | 180 | * @throws JSONException |
179 | 181 | */ |
180 | 182 | public WatchObject watch(final boolean enable, final boolean dumpData, final String device) throws IOException, JSONException { |
181 | | - watch = new JSONObject(); |
| 183 | + JSONObject watch = new JSONObject(); |
182 | 184 | watch.put("class", "WATCH"); |
183 | 185 | watch.put("enable", enable); |
184 | 186 | watch.put("json", dumpData); |
@@ -237,7 +239,9 @@ private <T extends IGPSObject> T syncCommand(final String command, final Class<T |
237 | 239 | synchronized (this.asyncMutex) { |
238 | 240 | this.out.write(command + "\n"); |
239 | 241 | this.out.flush(); |
240 | | - |
| 242 | + if( responseClass == WatchObject.class ){ |
| 243 | + lastWatch=command; |
| 244 | + } |
241 | 245 | while (true) { |
242 | 246 | // wait for awaited message |
243 | 247 | final IGPSObject result = this.waitForResult(); |
@@ -342,10 +346,20 @@ void handleDisconnected() throws IOException{ |
342 | 346 |
|
343 | 347 | this.listenThread = new SocketThread(this.in, this, this.resultParser); |
344 | 348 | this.listenThread.start(); |
345 | | - if( watch!=null){ |
346 | | - this.syncCommand("?WATCH=" + watch.toString(), WatchObject.class); |
| 349 | + if( lastWatch!=null){ // restore watch if we had one. |
| 350 | + this.syncCommand("?WATCH=" + lastWatch, WatchObject.class); |
347 | 351 | } |
348 | 352 | } |
349 | 353 |
|
350 | 354 | } |
| 355 | + /** |
| 356 | + * Set a retry interval for reconnecting to GPSD if the socket closes. |
| 357 | + * Default value is 1000ms. |
| 358 | + * |
| 359 | + * @param millis how long to wait between each reconnection attempts. |
| 360 | + */ |
| 361 | + public void setRetryInterval(long millis){ |
| 362 | + retryInterval.set(millis); |
| 363 | + } |
| 364 | + |
351 | 365 | } |
0 commit comments