diff options
-rw-r--r-- | src/org/traccar/ReverseGeocoderHandler.java | 19 | ||||
-rw-r--r-- | src/org/traccar/geocode/JsonReverseGeocoder.java | 8 | ||||
-rw-r--r-- | src/org/traccar/geocode/ReverseGeocoder.java | 4 | ||||
-rw-r--r-- | test/org/traccar/geocode/ReverseGeocoderTest.java | 30 |
4 files changed, 45 insertions, 16 deletions
diff --git a/src/org/traccar/ReverseGeocoderHandler.java b/src/org/traccar/ReverseGeocoderHandler.java index 88875fb3a..7304234cc 100644 --- a/src/org/traccar/ReverseGeocoderHandler.java +++ b/src/org/traccar/ReverseGeocoderHandler.java @@ -22,6 +22,7 @@ import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.MessageEvent; import org.traccar.geocode.AddressFormat; import org.traccar.geocode.ReverseGeocoder; +import org.traccar.helper.Log; import org.traccar.model.Position; public class ReverseGeocoderHandler implements ChannelUpstreamHandler { @@ -49,24 +50,30 @@ public class ReverseGeocoderHandler implements ChannelUpstreamHandler { return; } - final MessageEvent e = (MessageEvent) evt; - Object message = e.getMessage(); + final MessageEvent event = (MessageEvent) evt; + Object message = event.getMessage(); if (message instanceof Position) { final Position position = (Position) message; if (processInvalidPositions || position.getValid()) { geocoder.getAddress(addressFormat, position.getLatitude(), position.getLongitude(), new ReverseGeocoder.ReverseGeocoderCallback() { @Override - public void onResult(String address) { + public void onSuccess(String address) { position.setAddress(address); - Channels.fireMessageReceived(ctx, position, e.getRemoteAddress()); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); + } + + @Override + public void onFailure(Throwable e) { + Log.warning("Geocoding failed", e); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); } }); } else { - Channels.fireMessageReceived(ctx, position, e.getRemoteAddress()); + Channels.fireMessageReceived(ctx, position, event.getRemoteAddress()); } } else { - Channels.fireMessageReceived(ctx, message, e.getRemoteAddress()); + Channels.fireMessageReceived(ctx, message, event.getRemoteAddress()); } } diff --git a/src/org/traccar/geocode/JsonReverseGeocoder.java b/src/org/traccar/geocode/JsonReverseGeocoder.java index c107d24e8..d3661b53c 100644 --- a/src/org/traccar/geocode/JsonReverseGeocoder.java +++ b/src/org/traccar/geocode/JsonReverseGeocoder.java @@ -53,7 +53,7 @@ public abstract class JsonReverseGeocoder implements ReverseGeocoder { if (cache != null) { String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude)); if (cachedAddress != null) { - callback.onResult(cachedAddress); + callback.onSuccess(cachedAddress); return; } } @@ -69,9 +69,9 @@ public abstract class JsonReverseGeocoder implements ReverseGeocoder { if (cache != null) { cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); } - callback.onResult(formattedAddress); + callback.onSuccess(formattedAddress); } else { - callback.onResult(null); + callback.onFailure(new IllegalArgumentException("Empty address")); } } return null; @@ -79,7 +79,7 @@ public abstract class JsonReverseGeocoder implements ReverseGeocoder { @Override public void onThrowable(Throwable t) { - callback.onResult(null); + callback.onFailure(t); } }); } diff --git a/src/org/traccar/geocode/ReverseGeocoder.java b/src/org/traccar/geocode/ReverseGeocoder.java index e2641fa37..b67ac50bb 100644 --- a/src/org/traccar/geocode/ReverseGeocoder.java +++ b/src/org/traccar/geocode/ReverseGeocoder.java @@ -19,7 +19,9 @@ public interface ReverseGeocoder { interface ReverseGeocoderCallback { - void onResult(String address); + void onSuccess(String address); + + void onFailure(Throwable e); } diff --git a/test/org/traccar/geocode/ReverseGeocoderTest.java b/test/org/traccar/geocode/ReverseGeocoderTest.java index 2b0c30779..eab147cd5 100644 --- a/test/org/traccar/geocode/ReverseGeocoderTest.java +++ b/test/org/traccar/geocode/ReverseGeocoderTest.java @@ -35,9 +35,13 @@ public class ReverseGeocoderTest { reverseGeocoder.getAddress(new AddressFormat(), 37.4217550, -122.0846330, new ReverseGeocoder.ReverseGeocoderCallback() { @Override - public void onResult(String address) { + public void onSuccess(String address) { setAddress(address); } + + @Override + public void onFailure(Throwable e) { + } }); Assert.assertEquals("1600 Amphitheatre Pkwy, Mountain View, CA, US", waitAddress()); } @@ -47,9 +51,13 @@ public class ReverseGeocoderTest { reverseGeocoder.getAddress(new AddressFormat(), 40.7337807, -73.9974401, new ReverseGeocoder.ReverseGeocoderCallback() { @Override - public void onResult(String address) { + public void onSuccess(String address) { setAddress(address); } + + @Override + public void onFailure(Throwable e) { + } }); Assert.assertEquals("35 West 9th Street, NYC, New York, US", waitAddress()); } @@ -59,9 +67,13 @@ public class ReverseGeocoderTest { reverseGeocoder.getAddress(new AddressFormat(), 48.8530000, 2.3400000, new ReverseGeocoder.ReverseGeocoderCallback() { @Override - public void onResult(String address) { + public void onSuccess(String address) { setAddress(address); } + + @Override + public void onFailure(Throwable e) { + } }); Assert.assertEquals("Rue du Jardinet, Paris, FR", waitAddress()); } @@ -72,9 +84,13 @@ public class ReverseGeocoderTest { reverseGeocoder.getAddress(new AddressFormat(), 34.116302, -118.051519, new ReverseGeocoder.ReverseGeocoderCallback() { @Override - public void onResult(String address) { + public void onSuccess(String address) { setAddress(address); } + + @Override + public void onFailure(Throwable e) { + } }); Assert.assertEquals("Charleston Road, California, US", waitAddress()); } @@ -84,9 +100,13 @@ public class ReverseGeocoderTest { reverseGeocoder.getAddress(new AddressFormat(), 34.116302, -118.051519, new ReverseGeocoder.ReverseGeocoderCallback() { @Override - public void onResult(String address) { + public void onSuccess(String address) { setAddress(address); } + + @Override + public void onFailure(Throwable e) { + } }); Assert.assertEquals("Estrella Avenue, Arcadia, California, United States", waitAddress()); } |