From 7e914aeafde0ed9eeb046f5736bc975746622eb3 Mon Sep 17 00:00:00 2001 From: Deenath Geeganage Date: Fri, 25 Nov 2022 11:59:56 +0000 Subject: [PATCH] Device names in endpoint-mgt and traccar are not consistent - Fix (#21) Fix: [https://roadmap.entgra.net/issues/9656](https://roadmap.entgra.net/issues/9656) When renaming an agent, the update wasn't send to the traccar server. Added new methods and combined it with existing agent modification code Co-authored-by: Deenath Geegange Co-authored-by: Pahansith Gunathilake Reviewed-on: https://repository.entgra.net/community/device-mgt-core/pulls/21 Co-authored-by: Deenath Geeganage Co-committed-by: Deenath Geeganage --- .../DeviceManagementProviderServiceImpl.java | 18 +++++++ .../api/service/DeviceAPIClientService.java | 8 ++++ .../api/service/TraccarClientFactory.java | 48 +++++++++++++++++++ .../impl/DeviceAPIClientServiceImpl.java | 11 +++++ .../traccar/common/beans/TraccarDevice.java | 6 +++ .../core/traccar/common/util/TraccarUtil.java | 8 ++++ 6 files changed, 99 insertions(+) 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 2f0439f8ca7..d6cf2d7fc34 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 @@ -527,6 +527,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } 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 + } + } else { + log.info("Traccar is disabled"); + } + //enroll Traccar device return status; } 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 index 73aca76059b..5dacc735d1a 100644 --- 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 @@ -50,6 +50,14 @@ public interface DeviceAPIClientService { */ void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException; + /** + * Updates device Traccar configuration records (like device name) + * + * @params device to be modifies + * @throws TrackerManagementDAOException errors thrown while modifing a traccar device + */ + void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException; + /** * Delete a device Traccar configuration records * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java index e0124c53efd..bffa6f5e859 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java @@ -469,6 +469,54 @@ public class TraccarClientFactory { } } + /** + * Modify the Device + * + * @param traccarDevice with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId + * Model, Contact, Category, fenceIds + * @throws TrackerManagementDAOException Failed while add Traccar Device the operation + */ + public void modifyDevice(TraccarDevice traccarDevice, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { + TrackerDeviceInfo trackerDeviceInfo = null; + try { + TrackerManagementDAOFactory.openConnection(); + trackerDeviceInfo = trackerDAO.getTrackerDevice(traccarDevice.getId(), tenantId); + } catch (TrackerManagementDAOException e) { + String msg = "Error occurred while mapping with deviceId ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + if (trackerDeviceInfo != null) { + log.info("Preparing to rename device (" + traccarDevice.getId() + " to taccar server"); + String url = defaultPort + "/api/devices/" + trackerDeviceInfo.getTraccarDeviceId(); + JSONObject payload = TraccarUtil.TraccarDevicePayload(traccarDevice, trackerDeviceInfo.getTraccarDeviceId()); + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + String result = res.get(); + log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar."); + if (res.isDone() && result.charAt(0) == '{') { + log.info("Succesfully renamed Traccar Device - " + traccarDevice.getId() + "in server"); + } else { + log.info("Failed to rename Traccar Device" + traccarDevice.getId() + "in server"); + } + } else { +// forward to add device + try { + addDevice(traccarDevice, tenantId); + } catch (TrackerAlreadyExistException e) { + String msg = "The device already exist"; + log.error(msg, e); + } + } + } + /** * Add Device GPS Location operation. * 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 index 9095f3dd5b8..6b955d028d4 100644 --- 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 @@ -62,6 +62,17 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override + public void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException { + TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getDeviceIdentifier(), device.getName()); + try { + client.modifyDevice(traccarDevice, tenantId); + } catch (TrackerManagementDAOException e) { + String msg = "Error occurred while mapping with deviceId"; + log.error(msg, e); + } + } + @Override public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException { TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java index 163ae4f3a5d..74648766642 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java @@ -55,6 +55,12 @@ public class TraccarDevice { this.category =category; } + public TraccarDevice(int id, String uniqueId, String deviceName) { + this.id = id; + this.uniqueId = uniqueId; + this.deviceName = deviceName; + } + public TraccarDevice(){ } public int getId() { return id; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java index 986b10624d8..ed32984d639 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java @@ -66,4 +66,12 @@ public class TraccarUtil { payload.put("attributes", new JSONObject()); return payload; } + + public static JSONObject TraccarDevicePayload(TraccarDevice deviceInfo, int id) { + JSONObject payload = new JSONObject(); + payload.put("id", id); + payload.put("name", deviceInfo.getDeviceName()); + payload.put("uniqueId", deviceInfo.getUniqueId()); + return payload; + } }