Restructure Traccar integration on device management (#81)

Co-authored-by: shamalka <shamalkanavod95@gmail.com>
Reviewed-on: community/device-mgt-core#81
Co-authored-by: Shamalka Navod <shamalka@entgra.io>
Co-committed-by: Shamalka Navod <shamalka@entgra.io>
add-tenant-mgt-module
Shamalka Navod 2 years ago committed by Pahansith Gunathilake
parent f5ef8bafd6
commit fbbd07d262

@ -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.report.mgt.ReportManagementService;
import org.wso2.carbon.device.mgt.common.spi.DeviceTypeGeneratorService; 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.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.app.mgt.ApplicationManagementProviderService;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion; import org.wso2.carbon.device.mgt.core.dto.DeviceTypeVersion;

@ -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);
}

@ -382,25 +382,11 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
deviceLocation.getBearing(), deviceLocation.getBearing(),
deviceLocation.getDistance() deviceLocation.getDistance()
}; };
// DeviceManagerUtil.getEventPublisherService().publishEvent(
// LOCATION_EVENT_STREAM_DEFINITION, "1.0.0", metaData, new Object[0], payload
// );
} }
//Tracker update GPS Location //Tracker update GPS Location
if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) {
try { DeviceManagementDataHolder.getInstance().getTraccarManagementService().updateLocation(device, deviceLocation);
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
}
} else { } else {
if(!HttpReportingUtil.isLocationPublishing()) { if(!HttpReportingUtil.isLocationPublishing()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
@ -413,8 +399,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} }
} }
} }
//Tracker update GPS Location
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location " + throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location " +
@ -453,18 +437,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
for (DeviceLocation deviceLocation: deviceLocations) { for (DeviceLocation deviceLocation: deviceLocations) {
//Tracker update GPS Location //Tracker update GPS Location
if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) {
try { DeviceManagementDataHolder.getInstance().getTraccarManagementService().updateLocation(device, deviceLocation);
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
}
} else { } else {
if(!HttpReportingUtil.isLocationPublishing()) { if(!HttpReportingUtil.isLocationPublishing()) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {

@ -19,6 +19,7 @@
package org.wso2.carbon.device.mgt.core.internal; package org.wso2.carbon.device.mgt.core.internal;
import io.entgra.server.bootup.heartbeat.beacon.service.HeartBeatManagementService; 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.DeviceStatusTaskPluginConfig;
import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig; import org.wso2.carbon.device.mgt.common.OperationMonitoringTaskConfig;
import org.wso2.carbon.device.mgt.common.app.mgt.ApplicationManager; 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.metadata.mgt.WhiteLabelManagementService;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManager; 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.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.app.mgt.config.AppManagementConfig;
import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig; import org.wso2.carbon.device.mgt.core.config.license.LicenseConfig;
import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager; import org.wso2.carbon.device.mgt.core.device.details.mgt.DeviceInformationManager;
@ -90,6 +92,7 @@ public class DeviceManagementDataHolder {
private DeviceAPIClientService deviceAPIClientService; private DeviceAPIClientService deviceAPIClientService;
private MetadataManagementService metadataManagementService; private MetadataManagementService metadataManagementService;
private WhiteLabelManagementService whiteLabelManagementService; private WhiteLabelManagementService whiteLabelManagementService;
private TraccarManagementService traccarManagementService;
private final Map<DeviceType, DeviceStatusTaskPluginConfig> deviceStatusTaskPluginConfigs = Collections.synchronizedMap( private final Map<DeviceType, DeviceStatusTaskPluginConfig> deviceStatusTaskPluginConfigs = Collections.synchronizedMap(
new HashMap<>()); new HashMap<>());
@ -381,4 +384,20 @@ public class DeviceManagementDataHolder {
public void setWhiteLabelManagementService(WhiteLabelManagementService whiteLabelManagementService) { public void setWhiteLabelManagementService(WhiteLabelManagementService whiteLabelManagementService) {
this.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;
}
} }

@ -48,6 +48,7 @@ import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HTTP; import org.apache.http.protocol.HTTP;
import org.opensaml.xmlsec.signature.P;
import org.wso2.carbon.CarbonConstants; import org.wso2.carbon.CarbonConstants;
import org.wso2.carbon.context.CarbonContext; import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.context.PrivilegedCarbonContext;
@ -433,23 +434,12 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
//enroll Traccar device //enroll Traccar device
if (HttpReportingUtil.isTrackerEnabled()) { if (HttpReportingUtil.isTrackerEnabled()) {
try { DeviceManagementDataHolder.getInstance().getTraccarManagementService().addDevice(device);
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
}
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Traccar is disabled"); log.debug("Traccar is disabled");
} }
} }
//enroll Traccar device
if (status) { if (status) {
addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership()); addDeviceToGroups(deviceIdentifier, device.getEnrolmentInfo().getOwnership());
@ -534,24 +524,13 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
extractDeviceLocationToUpdate(device); extractDeviceLocationToUpdate(device);
//enroll Traccar device //enroll Traccar device
if (HttpReportingUtil.isTrackerEnabled()) { if (HttpReportingUtil.isTrackerEnabled()) {
try { DeviceManagementDataHolder.getInstance().getTraccarManagementService().updateDevice(device);
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
}
} else { } else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Traccar is disabled"); log.debug("Traccar is disabled");
} }
} }
//enroll Traccar device
return status; return status;
} }
@ -635,8 +614,11 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
//procees to dis-enroll a device from traccar starts //procees to dis-enroll a device from traccar starts
if (HttpReportingUtil.isTrackerEnabled()) { if (HttpReportingUtil.isTrackerEnabled()) {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() DeviceManagementDataHolder.getInstance().getTraccarManagementService().unLinkTraccarDevice(device.getEnrolmentInfo().getId());
.disEnrollDevice(device.getId(), tenantId); } else {
if (log.isDebugEnabled()) {
log.debug("Traccar is disabled");
}
} }
//procees to dis-enroll a device from traccar ends //procees to dis-enroll a device from traccar ends
@ -756,6 +738,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Successfully permanently deleted the details of devices : " + validDeviceIdentifiers); 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; return true;
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Error occurred while initiating transaction"; String msg = "Error occurred while initiating transaction";

@ -787,3 +787,16 @@ CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES (
DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE
); );
-- END OF DYNAMIC TASK TABLE-- -- 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 --

Loading…
Cancel
Save