diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java index 37a08c7e00..e2100f03eb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/util/DeviceMgtAPIUtils.java @@ -77,6 +77,7 @@ import org.wso2.carbon.device.mgt.common.operation.mgt.Operation; import org.wso2.carbon.device.mgt.common.report.mgt.ReportManagementService; import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; import org.wso2.carbon.device.mgt.common.spi.OTPManagementService; +import org.wso2.carbon.device.mgt.common.spi.TraccarManagementService; import org.wso2.carbon.device.mgt.core.app.mgt.ApplicationManagementProviderService; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/TraccarManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/TraccarManagementService.java new file mode 100644 index 0000000000..0c3379eedf --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/spi/TraccarManagementService.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * WSO2 Inc. licenses this file to you 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.wso2.carbon.device.mgt.common.spi; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; + +public interface TraccarManagementService { + + /** + * Add the provided device to Traccar. + * @param device The device to be added to Traccar. + */ + void addDevice(Device device); + + /** + * Removes the Traccar device with the specified device ID from the logged in user. + * @param deviceEnrollmentId The enrollment ID of the device to be removed from Traccar. + */ + void unLinkTraccarDevice(int deviceEnrollmentId); + + /** + * Update the provided device to Traccar. + * @param device The device to be updated on Traccar. + */ + void updateDevice(Device device); + + /** + * Removes the device with the specified enrollment ID from Traccar. + * @param deviceEnrollmentId The enrollment ID of the device to be removed from Traccar. + */ + void removeDevice(int deviceEnrollmentId); + + /** + * Updates the location of the provided device with the specified device location. + * @param device The device whose location is to be updated. + * @param deviceLocation The new location of the device. + */ + void updateLocation(Device device, DeviceLocation deviceLocation); +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java index cc82921cbc..7dde5efb16 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/app/mgt/ApplicationManagerProviderServiceImpl.java @@ -354,4 +354,4 @@ public class ApplicationManagerProviderServiceImpl implements ApplicationManagem DeviceManagementDAOFactory.closeConnection(); } } -} \ No newline at end of file +} 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 b293798ba2..f17719d228 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 @@ -382,25 +382,11 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { deviceLocation.getBearing(), deviceLocation.getDistance() }; -// DeviceManagerUtil.getEventPublisherService().publishEvent( -// LOCATION_EVENT_STREAM_DEFINITION, "1.0.0", metaData, new Object[0], payload -// ); } //Tracker update GPS Location if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { - try { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .updateLocation(device, deviceLocation, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - } catch (ExecutionException e) { - log.error("ExecutionException : " + e); - //throw new RuntimeException(e); - //Exception was not thrown due to being conflicted with non-traccar features - } catch (InterruptedException e) { - log.error("InterruptedException : " + e); - //throw new RuntimeException(e); - //Exception was not thrown due to being conflicted with non-traccar features - } + DeviceManagementDataHolder.getInstance().getTraccarManagementService().updateLocation(device, deviceLocation); } else { if(!HttpReportingUtil.isLocationPublishing()) { if (log.isDebugEnabled()) { @@ -413,8 +399,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } } - //Tracker update GPS Location - DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location " + @@ -453,18 +437,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { for (DeviceLocation deviceLocation: deviceLocations) { //Tracker update GPS Location if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { - try { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .updateLocation(device, deviceLocation, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - } catch (ExecutionException e) { - log.error("ExecutionException : " + e); - //throw new RuntimeException(e); - // NOTE: Exception was not thrown due to being conflicted with non-traccar features - } catch (InterruptedException e) { - log.error("InterruptedException : " + e); - //throw new RuntimeException(e); - // NOTE: Exception was not thrown due to being conflicted with non-traccar features - } + DeviceManagementDataHolder.getInstance().getTraccarManagementService().updateLocation(device, deviceLocation); } else { if(!HttpReportingUtil.isLocationPublishing()) { if (log.isDebugEnabled()) { diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 13999efbc7..08cfdff591 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.internal; import io.entgra.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.DeviceStatusTaskPluginConfig; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; @@ -30,6 +31,7 @@ import org.wso2.carbon.device.mgt.common.metadata.mgt.MetadataManagementService; import org.wso2.carbon.device.mgt.common.metadata.mgt.WhiteLabelManagementService; import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; +import org.wso2.carbon.device.mgt.common.spi.TraccarManagementService; import org.wso2.carbon.device.mgt.core.app.mgt.config.AppManagementConfig; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; @@ -90,6 +92,7 @@ public class DeviceManagementDataHolder { private DeviceAPIClientService deviceAPIClientService; private MetadataManagementService metadataManagementService; private WhiteLabelManagementService whiteLabelManagementService; + private TraccarManagementService traccarManagementService; private final Map deviceStatusTaskPluginConfigs = Collections.synchronizedMap( new HashMap<>()); @@ -357,7 +360,7 @@ public class DeviceManagementDataHolder { OperationTimeoutTaskManagerService operationTimeoutTaskManagerService) { this.operationTimeoutTaskManagerService = operationTimeoutTaskManagerService; } - + public DeviceAPIClientService getDeviceAPIClientService() { return deviceAPIClientService; } @@ -381,4 +384,20 @@ public class DeviceManagementDataHolder { public void setWhiteLabelManagementService(WhiteLabelManagementService whiteLabelManagementService) { this.whiteLabelManagementService = whiteLabelManagementService; } + + public TraccarManagementService getTraccarManagementService() { + TraccarManagementService traccarManagementService; + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + traccarManagementService = (TraccarManagementService) ctx.getOSGiService( + TraccarManagementService.class, null); + if (traccarManagementService == null) { + String msg = "Traccar management service not initialized."; + throw new IllegalStateException(msg); + } + return traccarManagementService; + } + + public void setTraccarManagementService(TraccarManagementService traccarManagementService) { + this.traccarManagementService = traccarManagementService; + } } 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 6257e0b34d..d3363717ad 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 @@ -48,6 +48,7 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.protocol.HTTP; +import org.opensaml.xmlsec.signature.P; import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext; @@ -433,23 +434,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv //enroll Traccar device if (HttpReportingUtil.isTrackerEnabled()) { - try { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService().addDevice(device, tenantId); - } catch (ExecutionException e) { - log.error("ExecutionException : " + e); - //throw new RuntimeException(e); - //Exception was not thrown due to being conflicted with non-traccar features - } catch (InterruptedException e) { - log.error("InterruptedException : " + e); - //throw new RuntimeException(e); - //Exception was not thrown due to being conflicted with non-traccar features - } + DeviceManagementDataHolder.getInstance().getTraccarManagementService().addDevice(device); } else { if (log.isDebugEnabled()) { log.debug("Traccar is disabled"); } } - //enroll Traccar device if (status) { addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership()); @@ -534,24 +524,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv extractDeviceLocationToUpdate(device); //enroll Traccar device if (HttpReportingUtil.isTrackerEnabled()) { - try { - int tenantId = this.getTenantId(); - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService().modifyDevice(device, tenantId); - } catch (ExecutionException e) { - log.error("ExecutionException : " + e); - //throw new RuntimeException(e); - //Exception was not thrown due to being conflicted with non-traccar features - } catch (InterruptedException e) { - log.error("InterruptedException : " + e); - //throw new RuntimeException(e); - //Exception was not thrown due to being conflicted with non-traccar features - } + DeviceManagementDataHolder.getInstance().getTraccarManagementService().updateDevice(device); } else { if (log.isDebugEnabled()) { log.debug("Traccar is disabled"); } } - //enroll Traccar device + return status; } @@ -635,8 +614,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv //procees to dis-enroll a device from traccar starts if (HttpReportingUtil.isTrackerEnabled()) { - DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() - .disEnrollDevice(device.getId(), tenantId); + DeviceManagementDataHolder.getInstance().getTraccarManagementService().unLinkTraccarDevice(device.getEnrolmentInfo().getId()); + } else { + if (log.isDebugEnabled()) { + log.debug("Traccar is disabled"); + } } //procees to dis-enroll a device from traccar ends @@ -756,6 +738,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv if (log.isDebugEnabled()) { log.debug("Successfully permanently deleted the details of devices : " + validDeviceIdentifiers); } + if (HttpReportingUtil.isTrackerEnabled()) { + for (int enrollmentId : enrollmentIds) { + DeviceManagementDataHolder.getInstance().getTraccarManagementService().removeDevice(enrollmentId); + } + } else { + if (log.isDebugEnabled()) { + log.debug("Traccar is disabled"); + } + } return true; } catch (TransactionManagementException e) { String msg = "Error occurred while initiating transaction"; diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index f383c5d5b9..960fedcc20 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -786,4 +786,17 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( CONSTRAINT FK_DYNAMIC_TASK_TASK_PROPERTIES FOREIGN KEY (DYNAMIC_TASK_ID) REFERENCES DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE ); --- END OF DYNAMIC TASK TABLE-- \ No newline at end of file +-- END OF DYNAMIC TASK TABLE-- + +-- DM_TRACCAR_UNSYNCED_DEVICES TABLE -- +CREATE TABLE IF NOT EXISTS DM_TRACCAR_UNSYNCED_DEVICES ( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_NAME VARCHAR(100) NOT NULL, + IOTS_DEVICE_IDENTIFIER VARCHAR(300) DEFAULT NULL UNIQUE, + TRACCAR_DEVICE_UNIQUE_ID INT NOT NULL, + TRACCAR_USENAME VARCHAR(100) NULL, + STATUS VARCHAR(100) NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); +-- END OF DM_TRACCAR_UNSYNCED_DEVICES TABLE --