From c4d75234b088e631c2ab5f445cf07ec1880c082a Mon Sep 17 00:00:00 2001 From: Rushdi Date: Thu, 24 Feb 2022 23:45:13 +0530 Subject: [PATCH] Initiate traccar to locate the geo locations in the map --- .../org.wso2.carbon.device.mgt.core/pom.xml | 15 ++ .../impl/DeviceInformationManagerImpl.java | 19 ++ .../DeviceManagementProviderService.java | 4 +- .../DeviceManagementProviderServiceImpl.java | 18 +- .../api/service/DeviceAPIClientService.java | 33 +++ .../api/service/addons/TrackerClient.java | 139 ++++++++++++ .../impl/DeviceAPIClientServiceImpl.java | 44 ++++ .../core/traccar/common/TraccarClient.java | 33 +++ .../common/TraccarHandlerConstants.java | 29 +++ .../common/beans/TraccarDeviceInfo.java | 206 ++++++++++++++++++ .../core/traccar/common/config/Property.java | 49 +++++ .../common/config/TraccarConfiguration.java | 68 ++++++ .../traccar/common/config/TraccarGateway.java | 87 ++++++++ .../config/TraccarConfigurationManager.java | 102 +++++++++ 14 files changed, 841 insertions(+), 5 deletions(-) create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDeviceInfo.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/Property.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarConfiguration.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarGateway.java create mode 100644 components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/core/config/TraccarConfigurationManager.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml index 7ec5e847b63..d5618ef1d84 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/pom.xml @@ -342,6 +342,21 @@ com.google.guava guava + + com.fasterxml.jackson.core + jackson-databind + 2.11.2 + + + io.github.openfeign + feign-okhttp + + + org.wso2.orbit.com.squareup.okhttp + okhttp + 4.2.0.wso2v1 + compile + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 47f13ae9681..14a01479def 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; +import com.google.gson.Gson; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -43,10 +44,13 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.report.mgt.Constants; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; import org.wso2.carbon.user.api.UserStoreException; +import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; @@ -385,6 +389,18 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { // LOCATION_EVENT_STREAM_DEFINITION, "1.0.0", metaData, new Object[0], payload // ); } + + //Traccar update lat lon + TraccarDeviceInfo trackerinfo = new TraccarDeviceInfo(device.getDeviceIdentifier(), + deviceLocation.getUpdatedTime().getTime(), + deviceLocation.getLatitude(), deviceLocation.getLongitude(), + deviceLocation.getBearing(), deviceLocation.getSpeed()); + + DeviceAPIClientServiceImpl dac= new DeviceAPIClientServiceImpl(); + String deviceAPIClientResponse=dac.updateLocation(trackerinfo); + log.info("Location Update "+ new Gson().toJson(deviceAPIClientResponse)); + //Traccar update lat lon + DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location " + @@ -398,6 +414,9 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { // } catch (DataPublisherConfigurationException e) { // DeviceManagementDAOFactory.rollbackTransaction(); // throw new DeviceDetailsMgtException("Error occurred while publishing the device location information.", e); + } catch (IOException e) { + log.error("Error on Traccar" + e); + //e.printStackTrace(); } finally { DeviceManagementDAOFactory.closeConnection(); } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 4b1a424d27a..4e283160594 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -66,8 +66,8 @@ import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster; -import org.wso2.carbon.device.mgt.common.geo.service.GeoCoordinate; +import java.io.IOException; import java.sql.SQLException; import java.sql.Timestamp; import java.util.Collection; @@ -677,7 +677,7 @@ public interface DeviceManagementProviderService { boolean modifyEnrollment(Device device) throws DeviceManagementException; - boolean enrollDevice(Device device) throws DeviceManagementException; + boolean enrollDevice(Device device) throws DeviceManagementException, IOException; boolean saveConfiguration(PlatformConfiguration configuration) throws DeviceManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index d9bb601faeb..3e430021d4a 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -112,13 +112,14 @@ import org.wso2.carbon.device.mgt.core.dto.DeviceType; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeServiceIdentifier; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.common.geo.service.GeoCluster; -import org.wso2.carbon.device.mgt.common.geo.service.GeoCoordinate; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementServiceComponent; import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataDAO; import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory; import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation; +import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.email.sender.core.ContentProviderInfo; import org.wso2.carbon.email.sender.core.EmailContext; @@ -145,7 +146,6 @@ import java.util.*; import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; -//import org.wso2.carbon.device.mgt.analytics.data.publisher.exception.DataPublisherConfigurationException; public class DeviceManagementProviderServiceImpl implements DeviceManagementProviderService, PluginInitializationListener { @@ -213,7 +213,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } @Override - public boolean enrollDevice(Device device) throws DeviceManagementException { + public boolean enrollDevice(Device device) throws DeviceManagementException, IOException { if (device == null) { String msg = "Received empty device for device enrollment"; log.error(msg); @@ -394,6 +394,18 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv status = true; } + //Traccar update Latitude Longitude + String lastUpdatedTime = String.valueOf((new Date().getTime())); + TraccarDeviceInfo traccarDeviceInfo = new TraccarDeviceInfo(device.getName(), device.getDeviceIdentifier(), + "online", "false", lastUpdatedTime, "", "", "", "", + "", ""); + DeviceAPIClientServiceImpl dac= new DeviceAPIClientServiceImpl(); + String deviceAPIClientResponse=dac.addDevice(traccarDeviceInfo); + if (log.isDebugEnabled()) { + log.debug("Location Update "+ new Gson().toJson(deviceAPIClientResponse)); + } + //Traccar update Latitude Longitude + if (status) { addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership()); if (enrollmentConfiguration != null) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java new file mode 100644 index 00000000000..14bdcc9d244 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.api.service; + +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo; + +import java.io.IOException; + +public interface DeviceAPIClientService { + + String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException; + + String addDevice(TraccarDeviceInfo deviceInfo) throws IOException; + + String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java new file mode 100644 index 00000000000..d2cce2ddd2d --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java @@ -0,0 +1,139 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.api.service.addons; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONObject; +import org.wso2.carbon.device.mgt.core.traccar.common.TraccarClient; +import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo; +import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarGateway; +import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.ENDPOINT; +import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.AUTHORIZATION; +import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.AUTHORIZATION_KEY; +import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.MAIN_ENDPOINT; + +public class TrackerClient implements TraccarClient { + private static final Log log = LogFactory.getLog(TrackerClient.class); + + public String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException{ + //Retrieve the traccar Gateway by passing the Gateway name + TraccarGateway traccarGateway = getTraccarGateway(); + + //Retrieve the properties in the Traccar Gateway by passing the property name + String endpoint = traccarGateway.getPropertyByName(ENDPOINT).getValue(); + + OkHttpClient client = new OkHttpClient().newBuilder().build(); + Request request = new Request.Builder() + .url(endpoint+"id="+deviceInfo.getDeviceIdentifier()+ + "×tamp="+deviceInfo.getTimestamp()+ + "&lat="+deviceInfo.getLat()+"&lon="+deviceInfo.getLon()+ + "&bearing="+deviceInfo.getBearing()+"&speed="+deviceInfo.getSpeed()+"&ignition=true") + .method("GET", null) + .build(); + Response response = client.newCall(request).execute(); + log.info(String.valueOf(response)); + return String.valueOf(response); + } + + public String addDevice(TraccarDeviceInfo deviceInfo) throws IOException{ + + //Retrieve the traccar Gateway by passing the Gateway name + TraccarGateway traccarGateway = getTraccarGateway(); + + //Retrieve the properties in the Traccar Gateway by passing the property name + String endpoint = traccarGateway.getPropertyByName(MAIN_ENDPOINT).getValue(); + String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue(); + String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue(); + + OkHttpClient client = new OkHttpClient().newBuilder().build(); + MediaType mediaType = MediaType.parse("application/json"); + + JSONObject data = new JSONObject(); + data.put("name", deviceInfo.getDeviceName()); + data.put("uniqueId", deviceInfo.getUniqueId()); + data.put("status", deviceInfo.getStatus()); + data.put("disabled", deviceInfo.getDisabled()); + data.put("lastUpdate", deviceInfo.getLastUpdate()); + data.put("positionId", deviceInfo.getPositionId()); + data.put("groupId", deviceInfo.getGroupId()); + data.put("phone", deviceInfo.getPhone()); + data.put("model", deviceInfo.getModel()); + data.put("contact", deviceInfo.getContact()); + data.put("category", deviceInfo.getCategory()); + List geofenceIds = new ArrayList<>(); + data.put("geofenceIds", geofenceIds); + data.put("attributes", new JSONObject()); + + RequestBody body = RequestBody.create(mediaType, data.toString()); + + Request request = new Request.Builder() + .url(endpoint+"/devices") + .method("POST", body) + .addHeader("Content-Type", "application/json") + .addHeader(authorization, authorizationKey) + .build(); + Response response = client.newCall(request).execute(); + + return String.valueOf(response); + } + + // /TODO FIX THIS WITH GET REQUEST + public String deleteDevice(TraccarDeviceInfo deviceInfo){ + + //Retrieve the traccar Gateway by passing the Gateway name + TraccarGateway traccarGateway = getTraccarGateway(); + + //Retrieve the properties in the Traccar Gateway by passing the property name + String endpoint = traccarGateway.getPropertyByName(MAIN_ENDPOINT).getValue(); + String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue(); + String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue(); + + /* + OkHttpClient client = new OkHttpClient().newBuilder().build(); + MediaType mediaType = MediaType.parse("text/plain"); + RequestBody body = RequestBody.create(mediaType, ""); + Request request = new Request.Builder() + .url("endpoint+"/devices/"+deviceInfo) + .method("DELETE", body) + .addHeader(authorization, authorizationKey) + .build(); + Response response = client.newCall(request).execute(); + */ + + return ""; + } + + private TraccarGateway getTraccarGateway(){ + return TraccarConfigurationManager.getInstance().getTraccarConfig().getTraccarGateway(TraccarHandlerConstants.GATEWAY_NAME); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java new file mode 100644 index 00000000000..8fbe31caff4 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.api.service.impl; + +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.addons.TrackerClient; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo; + +import java.io.IOException; + +public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { + + public String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException { + TrackerClient client = new TrackerClient(); + return (client.updateLocation(deviceInfo)); + } + + public String addDevice(TraccarDeviceInfo deviceInfo) throws IOException { + TrackerClient client = new TrackerClient(); + return (client.addDevice(deviceInfo)); + } + + public String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException { + TrackerClient client = new TrackerClient(); + return (client.deleteDevice(deviceInfo)); + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java new file mode 100644 index 00000000000..7cd977c58f2 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.common; + +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo; + +import java.io.IOException; + +public interface TraccarClient { + + String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException; + + String addDevice(TraccarDeviceInfo deviceInfo) throws IOException; + + String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java new file mode 100644 index 00000000000..f1c4d6c5157 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarHandlerConstants.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.common; + +public class TraccarHandlerConstants { + public static final String TRACCAR_CONFIG_XML_NAME = "traccar-config.xml"; + public static final String GATEWAY_NAME = "sample"; + public static final String MAIN_ENDPOINT = "api-endpoint"; + public static final String ENDPOINT = "add-location-api-endpoint"; + public static final String AUTHORIZATION = "authorization"; + public static final String AUTHORIZATION_KEY = "authorization-key"; +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDeviceInfo.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDeviceInfo.java new file mode 100644 index 00000000000..f0e389c24a5 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDeviceInfo.java @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.common.beans; + +public class TraccarDeviceInfo { + private String deviceIdentifier; + private Long timestamp; + private Double lat; + private Double lon; + private Float bearing; + private Float speed; + + private String deviceName; + private String uniqueId; + private String status; + private String disabled; + private String lastUpdate; + private String positionId; + private String groupId; + private String phone; + private String model; + private String contact; + private String category; + + public TraccarDeviceInfo(String deviceIdentifier, long timestamp, Double lat, Double lon, + float bearing, float speed){ + this.deviceIdentifier =deviceIdentifier; + this.timestamp=timestamp; + this.lat=lat; + this.lon =lon; + this.bearing =bearing; + this.speed =speed; + } + + public TraccarDeviceInfo(String deviceName, String uniqueId, String status, String disabled, String lastUpdate, + String positionId, String groupId, String phone, String model, String contact, + String category){ + this.deviceName =deviceName; + this.uniqueId=uniqueId; + this.status=status; + this.disabled =disabled; + this.lastUpdate =lastUpdate; + this.positionId =positionId; + this.groupId =groupId; + this.phone =phone; + this.model =model; + this.contact =contact; + this.category =category; + } + + public TraccarDeviceInfo(){ } + + public Long getTimestamp() { + return timestamp; + } + + public void setTimestamp(Long timestamp) { + this.timestamp = timestamp; + } + + public Double getLat() { + return lat; + } + + public void setLat(Double lat) { + this.lat = lat; + } + + public Double getLon() { + return lon; + } + + public void setLon(Double lon) { + this.lon = lon; + } + + public Float getBearing() { + return bearing; + } + + public void setBearing(Float bearing) { + this.bearing = bearing; + } + + public Float getSpeed() { + return speed; + } + + public void setSpeed(Float speed) { + this.speed = speed; + } + + public String getDeviceName() { + return deviceName; + } + + public void setDeviceName(String deviceName) { + this.deviceName = deviceName; + } + + public String getUniqueId() { + return uniqueId; + } + + public void setUniqueId(String uniqueId) { + this.uniqueId = uniqueId; + } + + public String getStatus() { + return status; + } + + public void setStatus(String status) { + this.status = status; + } + + public String getDisabled() { + return disabled; + } + + public void setDisabled(String disabled) { + this.disabled = disabled; + } + + public String getLastUpdate() { + return lastUpdate; + } + + public void setLastUpdate(String lastUpdate) { + this.lastUpdate = lastUpdate; + } + + + public String getPhone() { + return phone; + } + + public void setPhone(String phone) { + this.phone = phone; + } + + public String getModel() { + return model; + } + + public void setModel(String model) { + this.model = model; + } + + public String getContact() { + return contact; + } + + public void setContact(String contact) { + this.contact = contact; + } + + public String getCategory() { + return category; + } + + public void setCategory(String category) { + this.category = category; + } + + public String getDeviceIdentifier() { + return deviceIdentifier; + } + + public void setDeviceIdentifier(String deviceIdentifier) { + this.deviceIdentifier = deviceIdentifier; + } + + public String getPositionId() { + return positionId; + } + + public void setPositionId(String positionId) { + this.positionId = positionId; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/Property.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/Property.java new file mode 100644 index 00000000000..8aca8595ae3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/Property.java @@ -0,0 +1,49 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.common.config; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlValue; + +@XmlRootElement(name = "Property") +public class Property { + + private String name; + private String value; + + @XmlAttribute(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlValue + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarConfiguration.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarConfiguration.java new file mode 100644 index 00000000000..ee355d31f02 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarConfiguration.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.common.config; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +@XmlRootElement(name = "TraccarConfiguration") +public class TraccarConfiguration { + + private List traccarGateways; + + @XmlElementWrapper(name = "Gateways") + @XmlElement(name = "Gateway") + public List getTraccarGateways() { + return traccarGateways; + } + + public void setTraccarGateways(List traccarGateways) { + this.traccarGateways = traccarGateways; + } + + /** + * Retrieve the default Traccar Gateway as defined in the Traccar configuration. + * @return default {@link TraccarGateway} + */ + public TraccarGateway getDefaultTraccarGateway() { + for (TraccarGateway traccarGateway : traccarGateways) { + if (traccarGateway.isDefault()) { + return traccarGateway; + } + } + return null; + } + + /** + * Retrieve Traccar Gateway by the provided Gateway Name + * @param gatewayName has the name of the Gateway to be retrieved + * @return retrieved {@link TraccarGateway} + */ + public TraccarGateway getTraccarGateway(String gatewayName) { + for (TraccarGateway traccarGateway : traccarGateways) { + if (gatewayName.equals(traccarGateway.getName())) { + return traccarGateway; + } + } + return null; + } +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarGateway.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarGateway.java new file mode 100644 index 00000000000..87f2a7fa025 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/config/TraccarGateway.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.common.config; + +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.XmlRootElement; +import java.util.List; + +@XmlRootElement(name = "Gateway") +public class TraccarGateway { + + private String name; + private String extensionClass; + private boolean isDefault; + private List properties; + + @XmlAttribute(name = "name") + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + @XmlAttribute(name = "extensionClass") + public String getExtensionClass() { + return extensionClass; + } + + public void setExtensionClass(String extensionClass) { + this.extensionClass = extensionClass; + } + + @XmlAttribute(name = "isDefault") + public boolean isDefault() { + return isDefault; + } + + public void setDefault(boolean aDefault) { + isDefault = aDefault; + } + + @XmlElementWrapper(name = "Properties") + @XmlElement(name = "Property") + public List getProperties() { + return properties; + } + + public void setProperties(List properties) { + this.properties = properties; + } + + /** + * Retrives the Property based on the provided property name + * @param propertyName has the name of the Property to be retrieved + * @return retrieved {@link Property} + */ + public Property getPropertyByName(String propertyName) { + for (Property property : properties) { + if (propertyName.equals(property.getName())) { + return property; + } + } + return null; + } +} + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/core/config/TraccarConfigurationManager.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/core/config/TraccarConfigurationManager.java new file mode 100644 index 00000000000..d1164bdb352 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/core/config/TraccarConfigurationManager.java @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved. + * + * Unauthorised copying/redistribution of this file, via any medium is strictly prohibited. + * + * Licensed under the Entgra Commercial License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://entgra.io/licenses/entgra-commercial/1.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.wso2.carbon.device.mgt.core.traccar.core.config; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.w3c.dom.Document; +import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; +import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; +import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfiguration; +import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; +import org.wso2.carbon.utils.CarbonUtils; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import java.io.File; + + +public class TraccarConfigurationManager { + private static final Log log = LogFactory.getLog(TraccarConfigurationManager.class); + private static TraccarConfigurationManager traccarConfigurationManager; + private TraccarConfiguration traccarConfiguration; + private static final String CarbonUtilsFile = CarbonUtils.getCarbonConfigDirPath() + File.separator; + private static final String TRACCAR_CONFIG_PATH = CarbonUtilsFile + TraccarHandlerConstants.TRACCAR_CONFIG_XML_NAME; + + /** + * Retrieve an instance of {@link TraccarConfigurationManager} + * @return an instance of {@link TraccarConfigurationManager} + */ + public static TraccarConfigurationManager getInstance() { + if (traccarConfigurationManager == null) { + synchronized (TraccarConfigurationManager.class) { + if (traccarConfigurationManager == null) { + traccarConfigurationManager = new TraccarConfigurationManager(); + } + } + } + return traccarConfigurationManager; + } + + /** + * Initialize the Traccar Configuration through the provided configuration location + * @param configLocation has the path of the Traccar configuration file + * @throws DeviceManagementException throws when there are any errors during the initialization of + * Traccar configuration + */ + public synchronized void initConfig(String configLocation) throws DeviceManagementException { + try { + File traccarConfig = new File(configLocation); + Document doc = DeviceManagerUtil.convertToDocument(traccarConfig); + + //Un-marshaling Traccar configuration + JAXBContext traccarContext = JAXBContext.newInstance(TraccarConfiguration.class); + Unmarshaller unmarshaller = traccarContext.createUnmarshaller(); + this.traccarConfiguration = (TraccarConfiguration) unmarshaller.unmarshal(doc); + } catch (JAXBException e) { + String msg = "Error occurred while initializing Traccar config '" + configLocation + "'"; + log.error(msg, e); + throw new DeviceManagementException(msg, e); + } + } + + /** + * Initialize the Traccar Configuration through the traccar-config.xml file in the TRACCAR_CONFIG_PATH + * @throws DeviceManagementException throws when there are any errors during the initialization of + * Traccar configuration + */ + public void initConfig() throws DeviceManagementException { + this.initConfig(TRACCAR_CONFIG_PATH); + } + + /** + * Retrieves the initialized {@link TraccarConfiguration} + * @return the initialized {@link TraccarConfiguration} + */ + public TraccarConfiguration getTraccarConfig() { + try{ + initConfig(); + }catch (Exception e){ + log.error("TraccarConfiguration:", e); + } + return traccarConfiguration; + } +}