diff options
Diffstat (limited to 'src/org/traccar/location')
-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 |
7 files changed, 133 insertions, 264 deletions
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..7f7028545 --- /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("lng").doubleValue(), + json.getJsonNumber("accuracy").doubleValue()); + } + return null; + } + + @Override + public void onThrowable(Throwable t) { + callback.onFailure(); + } + }); + } catch (JsonProcessingException e) { + Log.warning(e); + callback.onFailure(); + } + } + +} |