From 04fa149433e88132f74eba7f58a51ee61eca250f Mon Sep 17 00:00:00 2001 From: Deenath Geegange Date: Fri, 25 Nov 2022 07:14:51 +0530 Subject: [PATCH] formatting issues fix + added new Traccar Device constructor --- .../api/service/TraccarClientFactory.java | 23 +++++++++---------- .../impl/DeviceAPIClientServiceImpl.java | 8 +++---- .../traccar/common/beans/TraccarDevice.java | 6 +++++ .../core/traccar/common/util/TraccarUtil.java | 10 ++++---- 4 files changed, 25 insertions(+), 22 deletions(-) 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 2bdc05ad248..6753359f896 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 @@ -476,25 +476,24 @@ public class TraccarClientFactory { * Model, Contact, Category, fenceIds * @throws TrackerManagementDAOException Failed while add Traccar Device the operation */ - public void modifyDevice(TraccarDevice traccarDevice,int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { + 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){ + 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) { + } 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"); + 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, @@ -503,14 +502,14 @@ public class TraccarClientFactory { 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" ); + log.info("Succesfully renamed Traccar Device - " + traccarDevice.getId() + "in server"); + } else { + log.info("Failed to rename Traccar Device" + traccarDevice.getId() + "in server"); } - }else { + } else { // forward to add device try { - addDevice(traccarDevice,tenantId); + addDevice(traccarDevice, tenantId); } catch (TrackerAlreadyExistException e) { String msg = "The device already exist"; log.error(msg, e); 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 d791f402193..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 @@ -63,10 +63,10 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } @Override - public void modifyDevice(Device device,int tenantId) throws ExecutionException,InterruptedException{ - TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getName(), device.getDeviceIdentifier(), null,null,null,null,null,null,null,null,null); - try{ - client.modifyDevice(traccarDevice,tenantId); + 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); 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..f045701b5ba 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 deviceIdentifier, String deviceName) { + this.id = id; + this.deviceIdentifier = deviceIdentifier; + 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 72cb86e9d5b..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 @@ -67,13 +67,11 @@ public class TraccarUtil { return payload; } - public static JSONObject TraccarDevicePayload(TraccarDevice deviceInfo,int id){ + 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()); -// add other params if needed -// as long as its null it wintt be overitten + payload.put("id", id); + payload.put("name", deviceInfo.getDeviceName()); + payload.put("uniqueId", deviceInfo.getUniqueId()); return payload; } } -- 2.36.3