diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-12-20 17:22:21 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2016-12-20 17:22:21 +1300 |
commit | 0a8d47bec2a232aad6353d34a101eb82c9d4f7ae (patch) | |
tree | 2bad0ed0875b4e2ef751ffc6a7d25591cd6ae3fd /src/org/traccar | |
parent | c5594e16882ac3ec9215c47047e857566f8166b8 (diff) | |
download | traccar-server-0a8d47bec2a232aad6353d34a101eb82c9d4f7ae.tar.gz traccar-server-0a8d47bec2a232aad6353d34a101eb82c9d4f7ae.tar.bz2 traccar-server-0a8d47bec2a232aad6353d34a101eb82c9d4f7ae.zip |
Improved network location providers
Diffstat (limited to 'src/org/traccar')
-rw-r--r-- | src/org/traccar/Context.java | 13 | ||||
-rw-r--r-- | src/org/traccar/LocationProviderHandler.java | 8 | ||||
-rw-r--r-- | src/org/traccar/location/BaseLocationProvider.java | 52 | ||||
-rw-r--r-- | src/org/traccar/location/CellInfo.java | 136 | ||||
-rw-r--r-- | src/org/traccar/location/GoogleLocationProvider.java | 26 | ||||
-rw-r--r-- | src/org/traccar/location/LocationProvider.java | 8 | ||||
-rw-r--r-- | src/org/traccar/location/MozillaLocationProvider.java | 53 | ||||
-rw-r--r-- | src/org/traccar/location/OpenCellIdLocationProvider.java | 56 | ||||
-rw-r--r-- | src/org/traccar/location/UniversalLocationProvider.java | 66 | ||||
-rw-r--r-- | src/org/traccar/model/Position.java | 4 |
10 files changed, 146 insertions, 276 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 5d6e14efc..14ef85929 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -44,6 +44,7 @@ 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; @@ -249,20 +250,21 @@ public final class Context { } if (config.getBoolean("location.enable")) { - String type = config.getString("location.type", "opencellid"); + String type = config.getString("location.type", "mozilla"); String key = config.getString("location.key"); switch (type) { - case "mozilla": + case "google": + locationProvider = new GoogleLocationProvider(key); + case "opencellid": + locationProvider = new OpenCellIdLocationProvider(key); + default: if (key != null) { locationProvider = new MozillaLocationProvider(key); } else { locationProvider = new MozillaLocationProvider(); } break; - default: - locationProvider = new OpenCellIdLocationProvider(key); - break; } } @@ -311,6 +313,7 @@ public final class Context { public static void init(IdentityManager testIdentityManager) { config = new Config(); + objectMapper = new ObjectMapper(); identityManager = testIdentityManager; } diff --git a/src/org/traccar/LocationProviderHandler.java b/src/org/traccar/LocationProviderHandler.java index d47f05bd1..5f07358ec 100644 --- a/src/org/traccar/LocationProviderHandler.java +++ b/src/org/traccar/LocationProviderHandler.java @@ -44,15 +44,17 @@ public class LocationProviderHandler implements ChannelUpstreamHandler { Object message = e.getMessage(); if (message instanceof Position) { final Position position = (Position) message; - if (position.getOutdated() || processInvalidPositions && !position.getValid()) { - locationProvider.getLocation(position.getAttributes(), new LocationProvider.LocationProviderCallback() { + if ((position.getOutdated() || processInvalidPositions && !position.getValid()) + && position.getNetwork() != null) { + locationProvider.getLocation(position.getNetwork(), new LocationProvider.LocationProviderCallback() { @Override - public void onSuccess(double latitude, double longitude) { + public void onSuccess(double latitude, double longitude, double accuracy) { position.set(Position.KEY_APPROXIMATE, true); position.setValid(true); position.setFixTime(position.getDeviceTime()); position.setLatitude(latitude); position.setLongitude(longitude); + position.setAccuracy(accuracy); Channels.fireMessageReceived(ctx, position, e.getRemoteAddress()); } diff --git a/src/org/traccar/location/BaseLocationProvider.java b/src/org/traccar/location/BaseLocationProvider.java deleted file mode 100644 index d228685e2..000000000 --- a/src/org/traccar/location/BaseLocationProvider.java +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Copyright 2015 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.location; - -import org.traccar.Config; -import org.traccar.Context; -import org.traccar.model.Position; - -import java.util.Map; - -public abstract class BaseLocationProvider implements LocationProvider { - - @Override - public void getLocation(Map<String, Object> attributes, LocationProviderCallback callback) { - - Config config = Context.getConfig(); - - Number mcc = (Number) attributes.get(Position.KEY_MCC); - if (mcc == null && config.hasKey("location.mcc")) { - mcc = config.getInteger("location.mcc"); - } - - Number mnc = (Number) attributes.get(Position.KEY_MNC); - if (mnc == null && config.hasKey("location.mnc")) { - mnc = config.getInteger("location.mnc"); - } - - Number lac = (Number) attributes.get(Position.KEY_LAC); - Number cid = (Number) attributes.get(Position.KEY_CID); - - if (mcc != null && mnc != null && lac != null && cid != null) { - getLocation(mcc.intValue(), mnc.intValue(), lac.longValue(), cid.longValue(), callback); - } - - } - - protected abstract void getLocation(int mcc, int mnc, long lac, long cid, LocationProviderCallback callback); - -} diff --git a/src/org/traccar/location/CellInfo.java b/src/org/traccar/location/CellInfo.java deleted file mode 100644 index 2257cb1e8..000000000 --- a/src/org/traccar/location/CellInfo.java +++ /dev/null @@ -1,136 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.location; - -import com.fasterxml.jackson.annotation.JsonInclude; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import org.traccar.Config; -import org.traccar.Context; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.List; - -public class CellInfo { - - @JsonInclude(JsonInclude.Include.NON_DEFAULT) - public static class Cell { - - private int mcc; - private int mnc; - private int lac; - private int cid; - private int signal; - - public Cell() { - } - - public Cell(int mcc, int mnc, int lac, int cid, int signal) { - this.mcc = mcc; - this.mnc = mnc; - this.lac = lac; - this.cid = cid; - this.signal = signal; - } - - public int getMcc() { - return mcc; - } - - public void setMcc(int mcc) { - this.mcc = mcc; - } - - public int getMnc() { - return mnc; - } - - public void setMnc(int mnc) { - this.mnc = mnc; - } - - public int getLac() { - return lac; - } - - public void setLac(int lac) { - this.lac = lac; - } - - public int getCid() { - return cid; - } - - public void setCid(int cid) { - this.cid = cid; - } - - public int getSignal() { - return signal; - } - - public void setSignal(int signal) { - this.signal = signal; - } - } - - public CellInfo() { - } - - public CellInfo(Collection<Cell> cells) { - this.cells.addAll(cells); - } - - private List<Cell> cells = new ArrayList<>(); - - public List<Cell> getCells() { - return cells; - } - - public void addCell(int lac, int cid) { - Config config = Context.getConfig(); - if (config.hasKey("location.mcc") && config.hasKey("location.mnc")) { - int mcc = config.getInteger("location.mcc"); - int mnc = config.getInteger("location.mnc"); - cells.add(new Cell(mcc, mnc, lac, cid, 0)); - } - } - - public void addCell(int mcc, int mnc, int lac, int cid) { - cells.add(new Cell(mcc, mnc, lac, cid, 0)); - } - - @Override - public String toString() { - try { - return new ObjectMapper().writeValueAsString(cells); - } catch (JsonProcessingException e) { - throw new RuntimeException(e); - } - } - - public static CellInfo fromString(String json) { - try { - return new CellInfo(Arrays.asList(new ObjectMapper().readValue(json, Cell[].class))); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - -} diff --git a/src/org/traccar/location/GoogleLocationProvider.java b/src/org/traccar/location/GoogleLocationProvider.java new file mode 100644 index 000000000..d4c449170 --- /dev/null +++ b/src/org/traccar/location/GoogleLocationProvider.java @@ -0,0 +1,26 @@ +/* + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.location; + +public class GoogleLocationProvider extends UniversalLocationProvider { + + private static final String URL = "https://www.googleapis.com/geolocation/v1/geolocate"; + + public GoogleLocationProvider(String key) { + super(URL, key); + } + +} diff --git a/src/org/traccar/location/LocationProvider.java b/src/org/traccar/location/LocationProvider.java index cc445c2b3..47b9f8909 100644 --- a/src/org/traccar/location/LocationProvider.java +++ b/src/org/traccar/location/LocationProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,18 +15,18 @@ */ package org.traccar.location; -import java.util.Map; +import org.traccar.model.Network; public interface LocationProvider { interface LocationProviderCallback { - void onSuccess(double latitude, double longitude); + void onSuccess(double latitude, double longitude, double accuracy); void onFailure(); } - void getLocation(Map<String, Object> attributes, LocationProviderCallback callback); + void getLocation(Network network, LocationProviderCallback callback); } diff --git a/src/org/traccar/location/MozillaLocationProvider.java b/src/org/traccar/location/MozillaLocationProvider.java index cbfc19550..90be7c456 100644 --- a/src/org/traccar/location/MozillaLocationProvider.java +++ b/src/org/traccar/location/MozillaLocationProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,63 +15,16 @@ */ package org.traccar.location; -import com.ning.http.client.AsyncCompletionHandler; -import com.ning.http.client.Response; -import org.traccar.Context; - -import javax.json.Json; -import javax.json.JsonObject; -import javax.json.JsonReader; - -public class MozillaLocationProvider extends BaseLocationProvider { +public class MozillaLocationProvider extends UniversalLocationProvider { private static final String URL = "https://location.services.mozilla.com/v1/geolocate"; - private String url; - private String template; - public MozillaLocationProvider() { this("test"); } public MozillaLocationProvider(String key) { - this.url = URL + "?key=" + key; - - template = new StringBuilder() - .append("{\"cellTowers\":[{") - .append("\"radioType\":\"gsm\",") - .append("\"mobileCountryCode\":%d,") - .append("\"mobileNetworkCode\":%d,") - .append("\"locationAreaCode\":%d,") - .append("\"cellId\":%d") - .append("}]}") - .toString(); - } - - protected void getLocation(int mcc, int mnc, long lac, long cid, final LocationProviderCallback callback) { - String body = String.format(template, mcc, mnc, lac, cid); - Context.getAsyncHttpClient().preparePost(url).setBody(body).execute(new AsyncCompletionHandler() { - @Override - public Object onCompleted(Response response) throws Exception { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - JsonObject json = reader.readObject().getJsonObject("location"); - if (json.containsKey("lat") && json.containsKey("lon")) { - callback.onSuccess( - json.getJsonNumber("lat").doubleValue(), - json.getJsonNumber("lon").doubleValue()); - } else { - callback.onFailure(); - } - } - return null; - } - - @Override - public void onThrowable(Throwable t) { - callback.onFailure(); - } - }); - + super(URL, key); } } diff --git a/src/org/traccar/location/OpenCellIdLocationProvider.java b/src/org/traccar/location/OpenCellIdLocationProvider.java index d5d1b0ace..b45797b80 100644 --- a/src/org/traccar/location/OpenCellIdLocationProvider.java +++ b/src/org/traccar/location/OpenCellIdLocationProvider.java @@ -18,12 +18,14 @@ package org.traccar.location; import com.ning.http.client.AsyncCompletionHandler; import com.ning.http.client.Response; import org.traccar.Context; +import org.traccar.model.CellTower; +import org.traccar.model.Network; import javax.json.Json; import javax.json.JsonObject; import javax.json.JsonReader; -public class OpenCellIdLocationProvider extends BaseLocationProvider { +public class OpenCellIdLocationProvider implements LocationProvider { private String url; @@ -35,29 +37,39 @@ public class OpenCellIdLocationProvider extends BaseLocationProvider { this.url = url + "?format=json&mcc=%d&mnc=%d&lac=%d&cellid=%d&key=" + key; } - protected void getLocation(int mcc, int mnc, long lac, long cid, final LocationProviderCallback callback) { - Context.getAsyncHttpClient().prepareGet(String.format(url, mcc, mnc, lac, cid)) - .execute(new AsyncCompletionHandler() { - @Override - public Object onCompleted(Response response) throws Exception { - try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { - JsonObject json = reader.readObject(); - if (json.containsKey("lat") && json.containsKey("lon")) { - callback.onSuccess( - json.getJsonNumber("lat").doubleValue(), - json.getJsonNumber("lon").doubleValue()); - } else { - callback.onFailure(); + @Override + public void getLocation(Network network, final LocationProviderCallback callback) { + if (network.getCellTowers() != null && !network.getCellTowers().isEmpty()) { + + CellTower cellTower = network.getCellTowers().iterator().next(); + String request = String.format(url, cellTower.getMobileCountryCode(), cellTower.getMobileNetworkCode(), + cellTower.getLocationAreaCode(), cellTower.getCellId()); + + Context.getAsyncHttpClient().prepareGet(request).execute(new AsyncCompletionHandler() { + @Override + public Object onCompleted(Response response) throws Exception { + try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { + JsonObject json = reader.readObject(); + if (json.containsKey("lat") && json.containsKey("lon")) { + callback.onSuccess( + json.getJsonNumber("lat").doubleValue(), + json.getJsonNumber("lon").doubleValue(), 0); + } else { + callback.onFailure(); + } } + return null; } - return null; - } - - @Override - public void onThrowable(Throwable t) { - callback.onFailure(); - } - }); + + @Override + public void onThrowable(Throwable t) { + callback.onFailure(); + } + }); + + } else { + callback.onFailure(); + } } } diff --git a/src/org/traccar/location/UniversalLocationProvider.java b/src/org/traccar/location/UniversalLocationProvider.java new file mode 100644 index 000000000..624d020a5 --- /dev/null +++ b/src/org/traccar/location/UniversalLocationProvider.java @@ -0,0 +1,66 @@ +/* + * Copyright 2016 Anton Tananaev (anton@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.location; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.ning.http.client.AsyncCompletionHandler; +import com.ning.http.client.Response; +import org.traccar.Context; +import org.traccar.helper.Log; +import org.traccar.model.Network; + +import javax.json.Json; +import javax.json.JsonObject; +import javax.json.JsonReader; + +public class UniversalLocationProvider implements LocationProvider { + + private String url; + + public UniversalLocationProvider(String url, String key) { + this.url = url + "?key=" + key; + } + + @Override + public void getLocation(Network network, final LocationProviderCallback callback) { + try { + String request = Context.getObjectMapper().writeValueAsString(network); + Context.getAsyncHttpClient().preparePost(url).setBody(request).execute(new AsyncCompletionHandler() { + @Override + 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("lon").doubleValue(), + json.getJsonNumber("accuracy").doubleValue()); + } + return null; + } + + @Override + public void onThrowable(Throwable t) { + callback.onFailure(); + } + }); + } catch (JsonProcessingException e) { + Log.warning(e); + callback.onFailure(); + } + } + +} diff --git a/src/org/traccar/model/Position.java b/src/org/traccar/model/Position.java index fd3e2196c..b43f2455e 100644 --- a/src/org/traccar/model/Position.java +++ b/src/org/traccar/model/Position.java @@ -35,10 +35,6 @@ public class Position extends Message { public static final String KEY_OUTPUT = "output"; public static final String KEY_POWER = "power"; public static final String KEY_BATTERY = "battery"; - public static final String KEY_MCC = "mcc"; - public static final String KEY_MNC = "mnc"; - public static final String KEY_LAC = "lac"; - public static final String KEY_CID = "cid"; public static final String KEY_FUEL = "fuel"; public static final String KEY_FUEL_CONSUMPTION = "fuelConsumption"; public static final String KEY_RFID = "rfid"; |