diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-12-31 11:34:22 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2016-12-31 11:34:22 +1300 |
commit | e8cd15c0fb192f635808adfde4e8614e6b4b3c3f (patch) | |
tree | 74a41d42c37d9a6b4756c6bddb90ec43c75ffd5b | |
parent | d13f618ee3b8463d063b7f67e039299560245597 (diff) | |
download | traccar-server-e8cd15c0fb192f635808adfde4e8614e6b4b3c3f.tar.gz traccar-server-e8cd15c0fb192f635808adfde4e8614e6b4b3c3f.tar.bz2 traccar-server-e8cd15c0fb192f635808adfde4e8614e6b4b3c3f.zip |
Rename Location to Geolocation
-rw-r--r-- | src/org/traccar/BasePipelineFactory.java | 4 | ||||
-rw-r--r-- | src/org/traccar/Context.java | 28 | ||||
-rw-r--r-- | src/org/traccar/LocationProviderHandler.java | 10 | ||||
-rw-r--r-- | src/org/traccar/geolocation/GeolocationProvider.java (renamed from src/org/traccar/location/LocationProvider.java) | 4 | ||||
-rw-r--r-- | src/org/traccar/geolocation/GoogleGeolocationProvider.java (renamed from src/org/traccar/location/GoogleLocationProvider.java) | 6 | ||||
-rw-r--r-- | src/org/traccar/geolocation/MozillaGeolocationProvider.java (renamed from src/org/traccar/location/MozillaLocationProvider.java) | 8 | ||||
-rw-r--r-- | src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java (renamed from src/org/traccar/location/OpenCellIdLocationProvider.java) | 8 | ||||
-rw-r--r-- | src/org/traccar/geolocation/UniversalGeolocationProvider.java (renamed from src/org/traccar/location/UniversalLocationProvider.java) | 20 | ||||
-rw-r--r-- | test/org/traccar/geolocation/GeolocationProviderTest.java (renamed from test/org/traccar/location/LocationProviderTest.java) | 8 |
9 files changed, 50 insertions, 46 deletions
diff --git a/src/org/traccar/BasePipelineFactory.java b/src/org/traccar/BasePipelineFactory.java index d73b022ae..94a6d85aa 100644 --- a/src/org/traccar/BasePipelineFactory.java +++ b/src/org/traccar/BasePipelineFactory.java @@ -130,9 +130,9 @@ public abstract class BasePipelineFactory implements ChannelPipelineFactory { Context.getReverseGeocoder(), Context.getConfig().getBoolean("geocoder.processInvalidPositions")); } - if (Context.getLocationProvider() != null) { + if (Context.getGeolocationProvider() != null) { locationProviderHandler = new LocationProviderHandler( - Context.getLocationProvider(), Context.getConfig().getBoolean("location.processInvalidPositions")); + Context.getGeolocationProvider(), Context.getConfig().getBoolean("location.processInvalidPositions")); } distanceHandler = new DistanceHandler(); diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 14ef85929..4825158c4 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -44,10 +44,10 @@ import org.traccar.geocode.NominatimReverseGeocoder; import org.traccar.geocode.OpenCageReverseGeocoder; import org.traccar.geocode.ReverseGeocoder; import org.traccar.helper.Log; -import org.traccar.location.GoogleLocationProvider; -import org.traccar.location.LocationProvider; -import org.traccar.location.MozillaLocationProvider; -import org.traccar.location.OpenCellIdLocationProvider; +import org.traccar.geolocation.GoogleGeolocationProvider; +import org.traccar.geolocation.GeolocationProvider; +import org.traccar.geolocation.MozillaGeolocationProvider; +import org.traccar.geolocation.OpenCellIdGeolocationProvider; import org.traccar.notification.EventForwarder; import org.traccar.web.WebServer; @@ -110,10 +110,10 @@ public final class Context { return reverseGeocoder; } - private static LocationProvider locationProvider; + private static GeolocationProvider geolocationProvider; - public static LocationProvider getLocationProvider() { - return locationProvider; + public static GeolocationProvider getGeolocationProvider() { + return geolocationProvider; } private static WebServer webServer; @@ -249,20 +249,20 @@ public final class Context { } } - if (config.getBoolean("location.enable")) { - String type = config.getString("location.type", "mozilla"); - String key = config.getString("location.key"); + if (config.getBoolean("geolocation.enable")) { + String type = config.getString("geolocation.type", "mozilla"); + String key = config.getString("geolocation.key"); switch (type) { case "google": - locationProvider = new GoogleLocationProvider(key); + geolocationProvider = new GoogleGeolocationProvider(key); case "opencellid": - locationProvider = new OpenCellIdLocationProvider(key); + geolocationProvider = new OpenCellIdGeolocationProvider(key); default: if (key != null) { - locationProvider = new MozillaLocationProvider(key); + geolocationProvider = new MozillaGeolocationProvider(key); } else { - locationProvider = new MozillaLocationProvider(); + geolocationProvider = new MozillaGeolocationProvider(); } break; } diff --git a/src/org/traccar/LocationProviderHandler.java b/src/org/traccar/LocationProviderHandler.java index ca3094ac2..bf37a8b88 100644 --- a/src/org/traccar/LocationProviderHandler.java +++ b/src/org/traccar/LocationProviderHandler.java @@ -21,16 +21,16 @@ import org.jboss.netty.channel.ChannelUpstreamHandler; import org.jboss.netty.channel.Channels; import org.jboss.netty.channel.MessageEvent; import org.traccar.helper.Log; -import org.traccar.location.LocationProvider; +import org.traccar.geolocation.GeolocationProvider; import org.traccar.model.Position; public class LocationProviderHandler implements ChannelUpstreamHandler { - private final LocationProvider locationProvider; + private final GeolocationProvider geolocationProvider; private final boolean processInvalidPositions; - public LocationProviderHandler(LocationProvider locationProvider, boolean processInvalidPositions) { - this.locationProvider = locationProvider; + public LocationProviderHandler(GeolocationProvider geolocationProvider, boolean processInvalidPositions) { + this.geolocationProvider = geolocationProvider; this.processInvalidPositions = processInvalidPositions; } @@ -47,7 +47,7 @@ public class LocationProviderHandler implements ChannelUpstreamHandler { final Position position = (Position) message; if ((position.getOutdated() || processInvalidPositions && !position.getValid()) && position.getNetwork() != null) { - locationProvider.getLocation(position.getNetwork(), new LocationProvider.LocationProviderCallback() { + geolocationProvider.getLocation(position.getNetwork(), new GeolocationProvider.LocationProviderCallback() { @Override public void onSuccess(double latitude, double longitude, double accuracy) { position.set(Position.KEY_APPROXIMATE, true); diff --git a/src/org/traccar/location/LocationProvider.java b/src/org/traccar/geolocation/GeolocationProvider.java index ed1494ec3..d9dec6bbb 100644 --- a/src/org/traccar/location/LocationProvider.java +++ b/src/org/traccar/geolocation/GeolocationProvider.java @@ -13,11 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.location; +package org.traccar.geolocation; import org.traccar.model.Network; -public interface LocationProvider { +public interface GeolocationProvider { interface LocationProviderCallback { diff --git a/src/org/traccar/location/GoogleLocationProvider.java b/src/org/traccar/geolocation/GoogleGeolocationProvider.java index d4c449170..5901b47cd 100644 --- a/src/org/traccar/location/GoogleLocationProvider.java +++ b/src/org/traccar/geolocation/GoogleGeolocationProvider.java @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.location; +package org.traccar.geolocation; -public class GoogleLocationProvider extends UniversalLocationProvider { +public class GoogleGeolocationProvider extends UniversalGeolocationProvider { private static final String URL = "https://www.googleapis.com/geolocation/v1/geolocate"; - public GoogleLocationProvider(String key) { + public GoogleGeolocationProvider(String key) { super(URL, key); } diff --git a/src/org/traccar/location/MozillaLocationProvider.java b/src/org/traccar/geolocation/MozillaGeolocationProvider.java index 90be7c456..af917d6fe 100644 --- a/src/org/traccar/location/MozillaLocationProvider.java +++ b/src/org/traccar/geolocation/MozillaGeolocationProvider.java @@ -13,17 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.location; +package org.traccar.geolocation; -public class MozillaLocationProvider extends UniversalLocationProvider { +public class MozillaGeolocationProvider extends UniversalGeolocationProvider { private static final String URL = "https://location.services.mozilla.com/v1/geolocate"; - public MozillaLocationProvider() { + public MozillaGeolocationProvider() { this("test"); } - public MozillaLocationProvider(String key) { + public MozillaGeolocationProvider(String key) { super(URL, key); } diff --git a/src/org/traccar/location/OpenCellIdLocationProvider.java b/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java index a9f815399..7d129e3e9 100644 --- a/src/org/traccar/location/OpenCellIdLocationProvider.java +++ b/src/org/traccar/geolocation/OpenCellIdGeolocationProvider.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.location; +package org.traccar.geolocation; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.Response; @@ -25,15 +25,15 @@ import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; -public class OpenCellIdLocationProvider implements LocationProvider { +public class OpenCellIdGeolocationProvider implements GeolocationProvider { private String url; - public OpenCellIdLocationProvider(String key) { + public OpenCellIdGeolocationProvider(String key) { this("http://opencellid.org/cell/get", key); } - public OpenCellIdLocationProvider(String url, String key) { + public OpenCellIdGeolocationProvider(String url, String key) { this.url = url + "?format=json&mcc=%d&mnc=%d&lac=%d&cellid=%d&key=" + key; } diff --git a/src/org/traccar/location/UniversalLocationProvider.java b/src/org/traccar/geolocation/UniversalGeolocationProvider.java index 63ab681e7..320d0774b 100644 --- a/src/org/traccar/location/UniversalLocationProvider.java +++ b/src/org/traccar/geolocation/UniversalGeolocationProvider.java @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package org.traccar.location; +package org.traccar.geolocation; import com.fasterxml.jackson.core.JsonProcessingException; import com.ning.http.client.AsyncCompletionHandler; @@ -25,11 +25,11 @@ import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; -public class UniversalLocationProvider implements LocationProvider { +public class UniversalGeolocationProvider implements GeolocationProvider { private String url; - public UniversalLocationProvider(String url, String key) { + public UniversalGeolocationProvider(String url, String key) { this.url = url + "?key=" + key; } @@ -42,11 +42,15 @@ public class UniversalLocationProvider implements LocationProvider { public Object onCompleted(Response response) throws Exception { try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { JsonObject json = reader.readObject(); - JsonObject location = json.getJsonObject("location"); - callback.onSuccess( - location.getJsonNumber("lat").doubleValue(), - location.getJsonNumber("lng").doubleValue(), - json.getJsonNumber("accuracy").doubleValue()); + if (json.containsKey("error")) { + callback.onFailure(new RuntimeException(json.getJsonObject("error").getString("message"))); + } else { + JsonObject location = json.getJsonObject("location"); + callback.onSuccess( + location.getJsonNumber("lat").doubleValue(), + location.getJsonNumber("lng").doubleValue(), + json.getJsonNumber("accuracy").doubleValue()); + } } return null; } diff --git a/test/org/traccar/location/LocationProviderTest.java b/test/org/traccar/geolocation/GeolocationProviderTest.java index c4357e1d6..adfd36b96 100644 --- a/test/org/traccar/location/LocationProviderTest.java +++ b/test/org/traccar/geolocation/GeolocationProviderTest.java @@ -1,4 +1,4 @@ -package org.traccar.location; +package org.traccar.geolocation; import org.junit.Assert; import org.junit.Test; @@ -6,7 +6,7 @@ import org.traccar.BaseTest; import org.traccar.model.CellTower; import org.traccar.model.Network; -public class LocationProviderTest extends BaseTest { +public class GeolocationProviderTest extends BaseTest { private boolean enable = false; @@ -18,11 +18,11 @@ public class LocationProviderTest extends BaseTest { } public void testGoogleLocationProvider() throws Exception { - GoogleLocationProvider locationProvider = new GoogleLocationProvider("KEY"); + GoogleGeolocationProvider locationProvider = new GoogleGeolocationProvider("KEY"); Network network = new Network(CellTower.from(260, 2, 10250, 26511)); - locationProvider.getLocation(network, new LocationProvider.LocationProviderCallback() { + locationProvider.getLocation(network, new GeolocationProvider.LocationProviderCallback() { @Override public void onSuccess(double latitude, double longitude, double accuracy) { Assert.assertEquals(60.07254, latitude, 0.00001); |