From 1bd87e15f15ccfcc427002526298e356852724e7 Mon Sep 17 00:00:00 2001 From: shamalka Date: Fri, 9 Sep 2022 10:26:45 +0530 Subject: [PATCH 01/12] Fix traccar issues --- .../service/impl/DeviceManagementServiceImpl.java | 14 +++++++++----- .../api/service/addons/TraccarClientImpl.java | 6 ++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 71646f98cd..d4939b313f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -532,12 +532,15 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { for (Device device : devices.getList()) { TrackerDeviceInfo trackerDevice = DeviceAPIClientServiceImpl .getTrackerDevice(device.getId(), tenantId); - int traccarDeviceId = trackerDevice.getTraccarDeviceId(); - boolean getPermission = DeviceAPIClientServiceImpl.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); - traccarValidIdList.add(traccarDeviceId); - if (!getPermission) { - DeviceAPIClientServiceImpl.addTrackerUserDevicePermission(userId, traccarDeviceId); + if (trackerDevice != null) { + int traccarDeviceId = trackerDevice.getTraccarDeviceId(); + boolean getPermission = DeviceAPIClientServiceImpl.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); + traccarValidIdList.add(traccarDeviceId); + if (!getPermission) { + DeviceAPIClientServiceImpl.addTrackerUserDevicePermission(userId, traccarDeviceId); + } } + } //Remove necessary List getAllUserDevices = @@ -569,6 +572,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { log.error(msg, e); return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build(); } + /*Get Device Id List*/ return Response.status(Response.Status.OK).entity(obj.getString("token")).build(); } 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/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index 526de67f61..3efaefb191 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -465,6 +465,7 @@ public class TraccarClientImpl implements TraccarClient { TrackerManagementDAOFactory.closeConnection(); } + //check if the device is already exist before updating the location if (trackerDeviceInfo == null) { //add device if not exist @@ -481,7 +482,8 @@ public class TraccarClientImpl implements TraccarClient { executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.GET, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), - "http://localhost:")); + Objects.requireNonNull(HttpReportingUtil.trackerServer()).split(":")[0] + ":" + + Objects.requireNonNull(HttpReportingUtil.trackerServer()).split(":")[1] + ":")); } } @@ -579,7 +581,7 @@ public class TraccarClientImpl implements TraccarClient { authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); - + log.info("---------result--------"); if (result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { -- 2.36.3 From dac4e80df5d1e038a29f4ff447af3e7677562f85 Mon Sep 17 00:00:00 2001 From: shamalka Date: Fri, 9 Sep 2022 23:32:06 +0530 Subject: [PATCH 02/12] Change traccar port --- .../traccar/api/service/addons/TraccarClientImpl.java | 8 ++++++-- .../test/resources/config/operation/traccar-config.xml | 2 +- 2 files changed, 7 insertions(+), 3 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/addons/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index 3efaefb191..a1da55c659 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -403,6 +403,9 @@ public class TraccarClientImpl implements TraccarClient { authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); + log.info("---------result--------"); + log.info("---------result--------" + res.isDone()); + log.info("---------result-------- " + result); if (result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { @@ -581,8 +584,9 @@ public class TraccarClientImpl implements TraccarClient { authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); - log.info("---------result--------"); - if (result.charAt(0) == '{') { +// log.info(result); +// log.info("---------result--------"); + if (res.isDone() && result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { int traccarGroupId = obj.getInt("id"); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml index 3521695b04..54a5b01ef0 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml @@ -28,7 +28,7 @@ sample http://localhost: - 5055 + 8085 Authorization Basic YWRtaW46YWRtaW4= -- 2.36.3 From dcdfa17db5f4ced8f4616d4e683bc0050c6144f9 Mon Sep 17 00:00:00 2001 From: shamalka Date: Fri, 9 Sep 2022 23:34:30 +0530 Subject: [PATCH 03/12] Remove logs --- .../core/traccar/api/service/addons/TraccarClientImpl.java | 5 +---- 1 file changed, 1 insertion(+), 4 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/addons/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index a1da55c659..90de53d6c8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -404,8 +404,6 @@ public class TraccarClientImpl implements TraccarClient { serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); log.info("---------result--------"); - log.info("---------result--------" + res.isDone()); - log.info("---------result-------- " + result); if (result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { @@ -584,8 +582,7 @@ public class TraccarClientImpl implements TraccarClient { authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); -// log.info(result); -// log.info("---------result--------"); + log.info("---------result--------"); if (res.isDone() && result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { -- 2.36.3 From 47dfe0b7b82804c7cd3f4700cf550e37d01e284f Mon Sep 17 00:00:00 2001 From: shamalka Date: Mon, 12 Sep 2022 13:43:34 +0530 Subject: [PATCH 04/12] Add test logs --- .../core/traccar/api/service/addons/TraccarClientImpl.java | 3 +++ .../src/test/resources/config/operation/traccar-config.xml | 2 +- .../src/main/resources/conf/traccar-config.xml | 4 ++-- 3 files changed, 6 insertions(+), 3 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/addons/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index 90de53d6c8..bc51b1dfd5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -130,6 +130,7 @@ public class TraccarClientImpl implements TraccarClient { request = builder.url(serverUri + publisherUrlWithContext).addHeader(authorization, authorizeKey).build(); response = client.newCall(request).execute(); + log.info("Live Location: " + response.body().string()); return response.body().string(); } } @@ -481,6 +482,8 @@ public class TraccarClientImpl implements TraccarClient { "&lon=" + deviceInfo.getLon() + "&bearing=" + deviceInfo.getBearing() + "&speed=" + deviceInfo.getSpeed() + "&ignition=true"; + log.info("Live Location: " + url); + executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.GET, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), Objects.requireNonNull(HttpReportingUtil.trackerServer()).split(":")[0] + ":" + diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml index 54a5b01ef0..7a6de290f2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/test/resources/config/operation/traccar-config.xml @@ -27,7 +27,7 @@ sample http://localhost: - + 4385 8085 Authorization Basic YWRtaW46YWRtaW4= diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/traccar-config.xml b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/traccar-config.xml index 3521695b04..7a6de290f2 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/traccar-config.xml +++ b/features/device-mgt/org.wso2.carbon.device.mgt.basics.feature/src/main/resources/conf/traccar-config.xml @@ -27,8 +27,8 @@ sample http://localhost: - - 5055 + 4385 + 8085 Authorization Basic YWRtaW46YWRtaW4= -- 2.36.3 From 739090b42d472f5c20a69bd44d327cd0305d3204 Mon Sep 17 00:00:00 2001 From: shamalka Date: Wed, 28 Sep 2022 11:46:09 +0530 Subject: [PATCH 05/12] Traccar testing --- .../details/mgt/dao/DeviceDetailsDAO.java | 11 ++++++ .../mgt/dao/impl/DeviceDetailsDAOImpl.java | 29 ++++++++++++++ .../impl/DeviceInformationManagerImpl.java | 38 +++++++++++++++++-- .../DeviceManagementProviderServiceImpl.java | 3 ++ .../api/service/addons/TraccarClientImpl.java | 3 -- 5 files changed, 77 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java index d3a30b0a50..69bf4827cb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java @@ -99,6 +99,17 @@ public interface DeviceDetailsDAO { */ DeviceLocation getDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; + + /** + * This method will return the device location exist or not + * @param deviceId - id of the device. + * @param enrollmentId - enrolment id of the device. + * @return - if device location exist + * @throws DeviceDetailsMgtDAOException + */ + boolean hasLocations(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; + + /** * This method will delete the device location from the database. * @param deviceId diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index ff17b026f4..d44df5f7d3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -378,6 +378,35 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { } } + @Override + public boolean hasLocations(int deviceId, int enrollmentId) throws + DeviceDetailsMgtDAOException { + + Connection conn; + PreparedStatement stmt = null; + ResultSet rs = null; + boolean hasLocation = false; + try { + conn = this.getConnection(); + String sql = "SELECT DEVICE_ID FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? " + + "LIMIT 1"; + stmt = conn.prepareStatement(sql); + stmt.setInt(1, deviceId); + stmt.setInt(2, enrollmentId); + rs = stmt.executeQuery(); + + if (rs.next()) { + hasLocation = true; + } + + return hasLocation; + } catch (SQLException e) { + throw new DeviceDetailsMgtDAOException("Error occurred while fetching the location of the registered devices.", e); + } finally { + DeviceManagementDAOUtil.cleanupResources(stmt, rs); + } +} + @Override public void deleteDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { 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 e9e5b0aaf5..eef40b97b2 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 @@ -367,6 +367,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { if (previousLocation == null) { deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); } else { + log.info("Update location on IOTS"); deviceDetailsDAO.updateDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); } deviceDetailsDAO.addDeviceLocationInfo(device, deviceLocation, @@ -390,8 +391,10 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { //Tracker update GPS Location if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { try { + log.info("--- Location Pushing to Traccar starts ---"); DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() .updateLocation(device, deviceLocation, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + log.info("--- Location Pushing to Traccar end ---"); } catch (ExecutionException e) { log.error("ExecutionException : " + e); //throw new RuntimeException(e); @@ -435,17 +438,44 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { DeviceLocation mostRecentDeviceLocation = deviceLocations.get(deviceLocations.size() - 1); mostRecentDeviceLocation.setDeviceId(device.getId()); DeviceManagementDAOFactory.beginTransaction(); - DeviceLocation previousLocation = deviceDetailsDAO.getDeviceLocation(device.getId(), + boolean previousLocation = deviceDetailsDAO.hasLocations(device.getId(), device.getEnrolmentInfo().getId()); - if (previousLocation == null) { - deviceDetailsDAO.addDeviceLocation(mostRecentDeviceLocation, device.getEnrolmentInfo().getId()); - } else { + if (previousLocation) { deviceDetailsDAO.updateDeviceLocation(mostRecentDeviceLocation, device.getEnrolmentInfo().getId()); + } else { + deviceDetailsDAO.addDeviceLocation(mostRecentDeviceLocation, device.getEnrolmentInfo().getId()); } deviceDetailsDAO.addDeviceLocationsInfo(device, deviceLocations, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + for (DeviceLocation deviceLocation: deviceLocations) { + //Tracker update GPS Location + if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { + try { + log.info("--- Location Pushing to Traccar starts ---"); + DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() + .updateLocation(device, deviceLocation, CarbonContext.getThreadLocalCarbonContext().getTenantId()); + log.info("--- Location Pushing to Traccar end ---"); + } 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 { + if(!HttpReportingUtil.isLocationPublishing()) { + log.info("Location publishing is disabled"); + } + if (!HttpReportingUtil.isTrackerEnabled()) { + log.info("Traccar is disabled"); + } + } + } + DeviceManagementDAOFactory.commitTransaction(); } catch (TransactionManagementException e) { throw new DeviceDetailsMgtException("Transactional error occurred while adding the device location " + 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 c777ea8799..ea0b3d4265 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 @@ -3979,7 +3979,10 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceLocation.setDistance(Double.parseDouble(distance)); deviceLocation.setSpeed(Float.parseFloat(speed)); deviceLocation.setBearing(Float.parseFloat(bearing)); +// deviceInformationManager.addDeviceLocation(device, deviceLocation); + log.info("--- Location Pushing to Traccar starts ---"); deviceInformationManager.addDeviceLocation(device, deviceLocation); + log.info("--- Location Pushing to Traccar end ---"); } catch (DeviceDetailsMgtException e) { //We are not failing the execution since this is not critical for the functionality. But logging as // a warning for reference. 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/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index bc51b1dfd5..90de53d6c8 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -130,7 +130,6 @@ public class TraccarClientImpl implements TraccarClient { request = builder.url(serverUri + publisherUrlWithContext).addHeader(authorization, authorizeKey).build(); response = client.newCall(request).execute(); - log.info("Live Location: " + response.body().string()); return response.body().string(); } } @@ -482,8 +481,6 @@ public class TraccarClientImpl implements TraccarClient { "&lon=" + deviceInfo.getLon() + "&bearing=" + deviceInfo.getBearing() + "&speed=" + deviceInfo.getSpeed() + "&ignition=true"; - log.info("Live Location: " + url); - executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.GET, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), Objects.requireNonNull(HttpReportingUtil.trackerServer()).split(":")[0] + ":" + -- 2.36.3 From d7f98a6f2839a8e6710eed1d2766e8e315fdca79 Mon Sep 17 00:00:00 2001 From: shamalka Date: Tue, 18 Oct 2022 11:21:44 +0530 Subject: [PATCH 06/12] Fix traccar client thredpool issue --- .../impl/DeviceManagementServiceImpl.java | 5 +- .../api/service/TraccarClientFactory.java | 765 ++++++++++++++++++ .../impl/DeviceAPIClientServiceImpl.java | 29 +- 3 files changed, 779 insertions(+), 20 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/TraccarClientFactory.java diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index b4edc538cc..5ac01b3fc3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -530,9 +530,9 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); + TrackerDeviceInfo trackerDevice; for (Device device : devices.getList()) { - TrackerDeviceInfo trackerDevice = DeviceAPIClientServiceImpl - .getTrackerDevice(device.getId(), tenantId); + trackerDevice = DeviceAPIClientServiceImpl.getTrackerDevice(device.getId(), tenantId); if (trackerDevice != null) { int traccarDeviceId = trackerDevice.getTraccarDeviceId(); boolean getPermission = DeviceAPIClientServiceImpl.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); @@ -541,7 +541,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { DeviceAPIClientServiceImpl.addTrackerUserDevicePermission(userId, traccarDeviceId); } } - } //Remove necessary List getAllUserDevices = 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 new file mode 100644 index 0000000000..f50b02a7b3 --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java @@ -0,0 +1,765 @@ +/* + * Copyright (c) 2022, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. 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.core.traccar.api.service; + +import okhttp3.ConnectionPool; +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; +import org.json.JSONObject; +import org.wso2.carbon.context.PrivilegedCarbonContext; +import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; +import org.wso2.carbon.device.mgt.common.TrackerGroupInfo; +import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo; +import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; +import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; +import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; +import org.wso2.carbon.device.mgt.core.dao.TrackerDAO; +import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.traccar.api.service.TraccarClient; +import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; +import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarUser; +import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarGateway; +import org.wso2.carbon.device.mgt.core.traccar.common.util.TraccarUtil; +import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager; +import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil; + +import java.io.IOException; +import java.sql.SQLException; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.util.Date; +import java.util.List; +import java.util.Objects; +import java.util.Random; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +public class TraccarClientFactory { + + private static TraccarClientFactory INSTANCE; + private static final Log log = LogFactory.getLog(TraccarClientFactory.class); + private static final int THREAD_POOL_SIZE = 50; + private final OkHttpClient client; + private final ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE); + final TraccarGateway traccarGateway = getTraccarGateway(); + final String endpoint = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.ENDPOINT).getValue(); + final String authorization = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.AUTHORIZATION).getValue(); + final String authorizationKey = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.AUTHORIZATION_KEY).getValue(); + final String defaultPort = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.DEFAULT_PORT).getValue(); + final String locationUpdatePort = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.LOCATION_UPDATE_PORT).getValue(); + + final String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); + private final TrackerDAO trackerDAO; + + private TraccarClientFactory() { + client = new OkHttpClient.Builder() + .connectTimeout(30, TimeUnit.SECONDS) + .writeTimeout(30, TimeUnit.SECONDS) + .readTimeout(45, TimeUnit.SECONDS) + .connectionPool(new ConnectionPool(100, 50, TimeUnit.SECONDS)) + .build(); + this.trackerDAO = TrackerManagementDAOFactory.getTrackerDAO(); + } + + public static TraccarClientFactory getInstance() { + if(INSTANCE == null) { + INSTANCE = new TraccarClientFactory(); + } + return INSTANCE; + } + + private class OkHttpClientThreadPool implements Callable { + final String publisherUrlWithContext; + final JSONObject payload; + private final String method; + private String authorizeKey; + private String serverUri; + + private OkHttpClientThreadPool(String publisherUrlWithContext, JSONObject payload, String method, + String authorizeKey, String serverUri) { + this.publisherUrlWithContext = publisherUrlWithContext; + this.payload = payload; + this.method = method; + this.authorizeKey = authorizeKey; + this.serverUri = serverUri; + } + + @Override + public String call() throws IOException { + RequestBody requestBody; + Request.Builder builder = new Request.Builder(); + Request request; + Response response; + + if (Objects.equals(method, TraccarHandlerConstants.Methods.POST)) { + requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8")); + builder = builder.post(requestBody); + } else if (Objects.equals(method, TraccarHandlerConstants.Methods.PUT)) { + requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8")); + builder = builder.put(requestBody); + } else if (Objects.equals(method, TraccarHandlerConstants.Methods.DELETE)) { + if (publisherUrlWithContext.contains("permission")) { + requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8")); + builder = builder.delete(requestBody); + } else { + builder = builder.delete(); + } + } + + request = builder.url(serverUri + publisherUrlWithContext).addHeader(authorization, authorizeKey).build(); + response = client.newCall(request).execute(); + return response.body().string(); + } + } + + public String fetchAllUsers() throws ExecutionException, InterruptedException { + String method = TraccarHandlerConstants.Methods.GET; + String url = defaultPort + "/api/users"; + + Future result = executor.submit(new OkHttpClientThreadPool(url, null, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + return result.get(); + } + + public String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { + String allUsers = fetchAllUsers(); //get all users + JSONArray fetchAllUsers = new JSONArray(allUsers); //loop users + for (int i = 0; i < fetchAllUsers.length(); i++) { + // if login is null then check the name or if login is not null then check the login + if (fetchAllUsers.getJSONObject(i).isNull("login")) { + if (Objects.equals(fetchAllUsers.getJSONObject(i).getString("name"), userName)) { + return fetchAllUsers.getJSONObject(i).toString(); + } + } else { + if (Objects.equals(fetchAllUsers.getJSONObject(i).getString("login"), userName) || + Objects.equals(fetchAllUsers.getJSONObject(i).getString("name"), userName)) { + return fetchAllUsers.getJSONObject(i).toString(); + } + } + } + + return TraccarHandlerConstants.Types.USER_NOT_FOUND; + } + + public String returnUser(String userName) throws TrackerManagementDAOException { + try { + String result = DeviceAPIClientServiceImpl.fetchUserInfo(userName); + Date today = new Date(); + LocalDateTime tomorrow = LocalDateTime.from(today.toInstant().atZone(ZoneId.of("UTC"))).plusDays(1); + String token = DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.TRACCAR_TOKEN); + + TraccarUser traccarUser = new TraccarUser(); + traccarUser.setToken(token); + + if (Objects.equals(result, TraccarHandlerConstants.Types.USER_NOT_FOUND)) { + //create user + log.info("Creating a user on Traccar client"); + traccarUser.setName(userName); + traccarUser.setLogin(userName); + traccarUser.setEmail(userName); + traccarUser.setPassword(DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.DEFAULT_RANDOM)); + traccarUser.setDeviceLimit(-1); + //traccarUser.setUserLimit(-1); + traccarUser.setExpirationTime(tomorrow.toString()); + DeviceAPIClientServiceImpl.createUser(traccarUser); + } else { + //update user + log.info("Updating the user on Traccar client"); + JSONObject obj = new JSONObject(result); + + traccarUser.setId(obj.getInt("id")); + traccarUser.setName(obj.getString("name")); + if (!obj.isNull("login")) { + traccarUser.setLogin(obj.getString("login")); + } + traccarUser.setEmail(obj.getString("email")); + traccarUser.setDeviceLimit(obj.getInt("deviceLimit")); + traccarUser.setUserLimit(obj.getInt("userLimit")); + traccarUser.setAdministrator(obj.getBoolean("administrator")); + traccarUser.setDisabled(obj.getBoolean("disabled")); + traccarUser.setReadonly(obj.getBoolean("readonly")); + if (!obj.getBoolean("administrator")) { + traccarUser.setExpirationTime(tomorrow.toString()); + } else if (!obj.isNull("expirationTime")) { + traccarUser.setExpirationTime(obj.getString("expirationTime")); + } + DeviceAPIClientServiceImpl.updateUser(traccarUser, obj.getInt("id")); + } + result = DeviceAPIClientServiceImpl.fetchUserInfo(userName); + return result; + } catch (InterruptedException | ExecutionException e) { + JSONObject obj = new JSONObject(); + String msg = "Error occurred while executing enrollment status of the device."; + obj.put("error", msg); + obj.put("e", e); + log.error(msg, e); + return obj.toString(); + } + } + + public String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { + String method = TraccarHandlerConstants.Methods.POST; + String url = defaultPort + "/api/users"; + JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser); + + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + return res.get(); + } + + public String updateUser(TraccarUser traccarUser, int userId) throws ExecutionException, InterruptedException { + String method = TraccarHandlerConstants.Methods.PUT; + String url = defaultPort + "/api/users/" + userId; + JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser); + + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + return res.get(); + } + + public void setPermission(int userId, int deviceId) + throws ExecutionException, InterruptedException, TrackerManagementDAOException { + JSONObject payload = new JSONObject(); + payload.put("userId", userId); + payload.put("deviceId", deviceId); + + String method = TraccarHandlerConstants.Methods.POST; + String url = defaultPort + "/api/permissions"; + + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + String result = res.get(); + + if (("").equals(result)) { + try { + TrackerManagementDAOFactory.beginTransaction(); + trackerDAO.addTrackerUserDevicePermission(userId, deviceId); + TrackerManagementDAOFactory.commitTransaction(); + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while mapping with deviceId ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TransactionManagementException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + } else { + log.error("Couldn't add the permission record: " + result); + } + } + + public void removePermission(int userId, int deviceId, int removeType) + throws TrackerManagementDAOException, ExecutionException, InterruptedException { + JSONObject payload = new JSONObject(); + payload.put("userId", userId); + payload.put("deviceId", deviceId); + + String method = TraccarHandlerConstants.Methods.DELETE; + String url = defaultPort + "/api/permissions"; + + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + + if (res.get() != null) { + try { + TrackerManagementDAOFactory.beginTransaction(); + trackerDAO.removeTrackerUserDevicePermission(deviceId, userId, removeType); + TrackerManagementDAOFactory.commitTransaction(); + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while mapping with deviceId ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TransactionManagementException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + } + } + + public List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) + throws TrackerManagementDAOException { + try { + TrackerManagementDAOFactory.openConnection(); + return trackerDAO.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + 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(); + } + } + + public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { + try { + TrackerManagementDAOFactory.openConnection(); + return trackerDAO.getTrackerDevice(deviceId, tenantId); + } catch (SQLException e) { + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TrackerManagementDAOException e) { + String msg = "Could not add new device location"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + } + + public boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) + throws TrackerManagementDAOException { + Boolean result = false; + try { + TrackerManagementDAOFactory.openConnection(); + result = trackerDAO.getUserIdofPermissionByDeviceIdNUserId(deviceId, userId); + } 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(); + } + return result; + } + + /** + * Add Traccar Device operation. + * + * @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 addDevice(TraccarDevice traccarDevice, int tenantId) throws + TrackerManagementDAOException, TrackerAlreadyExistException, 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) { + //add the device + String url = defaultPort + "/api/devices"; + JSONObject payload = TraccarUtil.TraccarDevicePayload(traccarDevice); + + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + String result = res.get(); + log.info("---------result--------"); + if (res.isDone() && result.charAt(0) == '{') { + JSONObject obj = new JSONObject(result); + if (obj.has("id")) { + int traccarDeviceId = obj.getInt("id"); + int deviceId = traccarDevice.getId(); + log.info("TraccarDeviceId - " + traccarDeviceId); + try { + TrackerManagementDAOFactory.beginTransaction(); + trackerDAO.addTrackerDevice(traccarDeviceId, deviceId, tenantId); + trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); + if (trackerDeviceInfo != null && trackerDeviceInfo.getStatus() == 0) { + trackerDAO.updateTrackerDeviceIdANDStatus(trackerDeviceInfo.getTraccarDeviceId(), deviceId, tenantId, 1); + } + TrackerManagementDAOFactory.commitTransaction(); + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while mapping with deviceId ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TransactionManagementException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + + JSONObject returnUserInfo = new JSONObject(returnUser(username)); + setPermission(returnUserInfo.getInt("id"), traccarDeviceId); + } + } + } else { + String msg = "The device already exist"; + log.error(msg); + throw new TrackerAlreadyExistException(msg); + } + } + + /** + * Add Device GPS Location operation. + * + * @param deviceInfo with DeviceIdentifier, Timestamp, Lat, Lon, Bearing, Speed, ignition + */ + public void updateLocation(TraccarDevice device, TraccarPosition deviceInfo, int tenantId) throws + TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { + TrackerDeviceInfo trackerDeviceInfo = null; + try { + TrackerManagementDAOFactory.openConnection(); + trackerDeviceInfo = trackerDAO.getTrackerDevice(device.getId(), tenantId); + } catch (SQLException e) { + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TrackerManagementDAOException e) { + String msg = "Could not add new device location"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + + + //check if the device is already exist before updating the location + if (trackerDeviceInfo == null) { + //add device if not exist + addDevice(device, tenantId); + } else { + //Update Location + if (log.isDebugEnabled()) { + log.info("Updating Location of the device: " + device.getId()); + } + String url = locationUpdatePort + "/?id=" + deviceInfo.getDeviceIdentifier() + + "×tamp=" + deviceInfo.getTimestamp() + "&lat=" + deviceInfo.getLat() + + "&lon=" + deviceInfo.getLon() + "&bearing=" + deviceInfo.getBearing() + + "&speed=" + deviceInfo.getSpeed() + "&ignition=true"; + + executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.GET, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + Objects.requireNonNull(HttpReportingUtil.trackerServer()).split(":")[0] + ":" + + Objects.requireNonNull(HttpReportingUtil.trackerServer()).split(":")[1] + ":")); + } + } + + /** + * Dis-enroll a Device operation. + * + * @param deviceId identified via deviceIdentifier + * @throws TrackerManagementDAOException Failed while dis-enroll a Traccar Device operation + */ + public void disEnrollDevice(int deviceId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { + TrackerDeviceInfo trackerDeviceInfo = null; + List trackerPermissionInfo = null; + try { + TrackerManagementDAOFactory.beginTransaction(); + trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); + log.info("deviceId - " + deviceId); + if (trackerDeviceInfo != null) { + trackerDAO.removeTrackerDevice(deviceId, tenantId); + TrackerManagementDAOFactory.commitTransaction(); + trackerPermissionInfo = trackerDAO.getUserIdofPermissionByDeviceId(trackerDeviceInfo.getTraccarDeviceId()); + } + } catch (TransactionManagementException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred establishing the DB connection"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while mapping with deviceId"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + log.info("--------Disenrolling Device with device id " + deviceId + " from traccar client--------"); + //Delete from traccar + if (trackerDeviceInfo != null) { + String method = TraccarHandlerConstants.Methods.DELETE; + String url = defaultPort + "/api/devices/" + trackerPermissionInfo.get(0).getTraccarDeviceId(); + + executor.submit(new OkHttpClientThreadPool(url, null, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + + //remove permissions + try { + removePermission( + trackerPermissionInfo.get(0).getTraccarUserId(), + trackerPermissionInfo.get(0).getTraccarDeviceId(), + TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE); + } catch (ExecutionException e) { + log.error("ExecutionException : " + e); + throw new ExecutionException(e); + } + } + } + + /** + * Add Traccar Device operation. + * + * @param groupInfo with groupName + * @throws TrackerManagementDAOException Failed while add Traccar Device the operation + */ + public void addGroup(TraccarGroups groupInfo, int groupId, int tenantId) throws + TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { + TrackerGroupInfo trackerGroupInfo = null; + try { + TrackerManagementDAOFactory.openConnection(); + trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); + if (trackerGroupInfo != null) { + String msg = "The group already exit"; + log.error(msg); + throw new TrackerAlreadyExistException(msg); + } + } catch (TrackerManagementDAOException e) { + String msg = "Error occurred while mapping with groupId."; + 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(); + } + + JSONObject payload = new JSONObject(); + payload.put("name", groupInfo.getName()); + payload.put("attributes", new JSONObject()); + + String method = TraccarHandlerConstants.Methods.POST; + String url = defaultPort + "/api/groups"; + + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + String result = res.get(); + log.info("---------result--------"); + if (res.isDone() && result.charAt(0) == '{') { + JSONObject obj = new JSONObject(result); + if (obj.has("id")) { + int traccarGroupId = obj.getInt("id"); + + try { + TrackerManagementDAOFactory.beginTransaction(); + trackerDAO.addTrackerGroup(traccarGroupId, groupId, tenantId); + trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); + if (trackerGroupInfo.getStatus() == 0) { + trackerDAO.updateTrackerGroupIdANDStatus(trackerGroupInfo.getTraccarGroupId(), groupId, tenantId, 1); + } + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while mapping with deviceId. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TransactionManagementException e) { + String msg = "Error occurred establishing the DB connection. "; + log.error(msg, e); + TrackerManagementDAOFactory.rollbackTransaction(); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + } else { + // TODO: Assumed the error message change if wrong + log.error("Response does not contains the key id: " + result); + } + } else { + // TODO: Assumed the error message change if wrong + log.error("Response does not contains a JSON object " + result); + } + } + + /** + * update Traccar Group operation. + * + * @param groupInfo with groupName + * @throws TrackerManagementDAOException Failed while add Traccar Device the operation + */ + public void updateGroup(TraccarGroups groupInfo, int groupId, int tenantId) throws + TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { + TrackerGroupInfo res = null; + try { + TrackerManagementDAOFactory.openConnection(); + res = trackerDAO.getTrackerGroup(groupId, tenantId); + } catch (SQLException e) { + String msg = "Error occurred establishing the DB connection. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TrackerManagementDAOException e) { + String msg = "Could not find traccar group details. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + + if ((res == null) || (res.getTraccarGroupId() == 0)) { + //add a new traccar group + addGroup(groupInfo, groupId, tenantId); + } else if (res != null && (res.getTraccarGroupId() != 0 && res.getStatus() == 0)) { + //update the traccargroupId and status + try { + TrackerManagementDAOFactory.beginTransaction(); + trackerDAO.updateTrackerGroupIdANDStatus(res.getTraccarGroupId(), groupId, tenantId, 1); + TrackerManagementDAOFactory.commitTransaction(); + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Could not update the traccar group. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TransactionManagementException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred establishing the DB connection. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + } else { + JSONObject obj = new JSONObject(res); + JSONObject payload = new JSONObject(); + payload.put("id", obj.getInt("traccarGroupId")); + payload.put("name", groupInfo.getName()); + payload.put("attributes", new JSONObject()); + + String method = TraccarHandlerConstants.Methods.PUT; + String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId"); + + executor.submit(new OkHttpClientThreadPool(url, payload, method, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + } + } + + /** + * Add Traccar Device operation. + * + * @param groupId + * @throws TrackerManagementDAOException Failed while add Traccar Device the operation + */ + public void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { + TrackerGroupInfo res = null; + JSONObject obj = null; + try { + TrackerManagementDAOFactory.beginTransaction(); + res = trackerDAO.getTrackerGroup(groupId, tenantId); + if (res != null) { + obj = new JSONObject(res); + if (obj != null) { + trackerDAO.removeTrackerGroup(obj.getInt("id")); + TrackerManagementDAOFactory.commitTransaction(); + + String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId"); + + executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.DELETE, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + } + } + } catch (TransactionManagementException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred establishing the DB connection. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while mapping with groupId. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + } + + public String generateRandomString(int len) { + String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; + Random rnd = new Random(); + StringBuilder sb = new StringBuilder(len); + for (int i = 0; i < len; i++) + sb.append(chars.charAt(rnd.nextInt(chars.length()))); + return sb.toString(); + } + + private TraccarGateway getTraccarGateway() { + return TraccarConfigurationManager.getInstance().getTraccarConfig().getTraccarGateway( + TraccarHandlerConstants.TraccarConfig.GATEWAY_NAME); + } + + public String authorizedKey(String trackerUser, String trackerPassword) { + String newAuthorizationKey = authorizationKey; + if (trackerUser != null && trackerPassword != null) { + newAuthorizationKey = trackerUser + ':' + trackerPassword; + + byte[] result = newAuthorizationKey.getBytes(); + byte[] res = Base64.encodeBase64(result); + newAuthorizationKey = "Basic " + new String(res); + } + return newAuthorizationKey; + } + + public String serverUrl(String serverUrl) { + String newServerUri = endpoint; + if (serverUrl != null) { + newServerUri = serverUrl; + } + return newServerUri; + } +} 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 f1f71cb99a..1361bbdc17 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 @@ -30,7 +30,7 @@ import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; -import org.wso2.carbon.device.mgt.core.traccar.api.service.addons.TraccarClientImpl; +import org.wso2.carbon.device.mgt.core.traccar.api.service.TraccarClientFactory; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; @@ -43,9 +43,9 @@ import java.util.concurrent.ExecutionException; public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { private static final Log log = LogFactory.getLog(DeviceAPIClientServiceImpl.class); + TraccarClientFactory client = TraccarClientFactory.getInstance(); public void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); String lastUpdatedTime = String.valueOf((new Date().getTime())); TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getName(), device.getDeviceIdentifier(), "online", "false", lastUpdatedTime, "", "", "", "", @@ -62,7 +62,6 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), deviceLocation.getUpdatedTime().getTime(), deviceLocation.getLatitude(), deviceLocation.getLongitude(), @@ -84,7 +83,6 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } public void disEnrollDevice(int deviceId, int tenantId) throws ExecutionException, InterruptedException{ - TraccarClientImpl client = new TraccarClientImpl(); try { client.disEnrollDevice(deviceId, tenantId); } catch (TrackerManagementDAOException e) { @@ -95,54 +93,51 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { public void addGroup(DeviceGroup group, int groupId, int tenantId) throws TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); TraccarGroups traccarGroups = new TraccarGroups(group.getName()); client.addGroup(traccarGroups, groupId, tenantId); } public void updateGroup(DeviceGroup group, int groupId, int tenantId) throws TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); TraccarGroups traccarGroups = new TraccarGroups(group.getName()); client.updateGroup(traccarGroups, groupId, tenantId); } public void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); client.deleteGroup(groupId, tenantId); } public static String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.fetchUserInfo(userName); } public static TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.getTrackerDevice(deviceId, tenantId); } public static boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws TrackerManagementDAOException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.getUserIdofPermissionByDeviceIdNUserId(deviceId, userId); } public static String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.createUser(traccarUser); } public static String updateUser(TraccarUser traccarUser, int userId) throws ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.updateUser(traccarUser, userId); } public static String returnUser(String userName) { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); try { return client.returnUser(userName); } catch (TrackerManagementDAOException e) { @@ -155,24 +150,24 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { public static void addTrackerUserDevicePermission(int userId, int deviceId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); client.setPermission(userId, deviceId); } public static void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws TrackerManagementDAOException, ExecutionException, InterruptedException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); client.removePermission(userId, deviceId, removeType); } public static List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) throws TrackerManagementDAOException { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); } public static String generateRandomString(int len) { - TraccarClientImpl client = new TraccarClientImpl(); + TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.generateRandomString(len); } -- 2.36.3 From c49a52937d1fa2b4149e4e96e9363419bed0e746 Mon Sep 17 00:00:00 2001 From: shamalka Date: Tue, 18 Oct 2022 11:26:23 +0530 Subject: [PATCH 07/12] Remove logs --- .../device/details/mgt/impl/DeviceInformationManagerImpl.java | 4 ---- 1 file changed, 4 deletions(-) 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 eef40b97b2..737d6a3ba3 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 @@ -391,10 +391,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { //Tracker update GPS Location if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { try { - log.info("--- Location Pushing to Traccar starts ---"); DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() .updateLocation(device, deviceLocation, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - log.info("--- Location Pushing to Traccar end ---"); } catch (ExecutionException e) { log.error("ExecutionException : " + e); //throw new RuntimeException(e); @@ -453,10 +451,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { //Tracker update GPS Location if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) { try { - log.info("--- Location Pushing to Traccar starts ---"); DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() .updateLocation(device, deviceLocation, CarbonContext.getThreadLocalCarbonContext().getTenantId()); - log.info("--- Location Pushing to Traccar end ---"); } catch (ExecutionException e) { log.error("ExecutionException : " + e); //throw new RuntimeException(e); -- 2.36.3 From f840ee97a89168765e26034e3b93610742aca7d2 Mon Sep 17 00:00:00 2001 From: shamalka Date: Mon, 24 Oct 2022 15:04:55 +0530 Subject: [PATCH 08/12] Fix issues --- .../details/mgt/dao/DeviceDetailsDAO.java | 2 +- .../mgt/dao/impl/DeviceDetailsDAOImpl.java | 37 ++--- .../impl/DeviceInformationManagerImpl.java | 21 ++- .../DeviceManagementProviderServiceImpl.java | 11 +- .../api/service/TraccarClientFactory.java | 144 +++++++++++------- .../api/service/addons/TraccarClientImpl.java | 4 +- 6 files changed, 124 insertions(+), 95 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java index 69bf4827cb..10c7a9f34f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/DeviceDetailsDAO.java @@ -105,7 +105,7 @@ public interface DeviceDetailsDAO { * @param deviceId - id of the device. * @param enrollmentId - enrolment id of the device. * @return - if device location exist - * @throws DeviceDetailsMgtDAOException + * @throws DeviceDetailsMgtDAOException if SQL error occurred while processing the query. */ boolean hasLocations(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java index d44df5f7d3..535a1cbb08 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/dao/impl/DeviceDetailsDAOImpl.java @@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO; @@ -381,31 +382,23 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO { @Override public boolean hasLocations(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { - - Connection conn; - PreparedStatement stmt = null; - ResultSet rs = null; - boolean hasLocation = false; try { - conn = this.getConnection(); - String sql = "SELECT DEVICE_ID FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? " + - "LIMIT 1"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, enrollmentId); - rs = stmt.executeQuery(); - - if (rs.next()) { - hasLocation = true; + Connection conn = this.getConnection(); + String sql = "SELECT DEVICE_ID FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? " + + "LIMIT 1"; + ResultSet rs; + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceId); + stmt.setInt(2, enrollmentId); + rs = stmt.executeQuery(); + return rs.next(); + } + } catch (SQLException e) { + String msg = "Error occurred while fetching the location of the registered devices."; + log.error(msg, e); + throw new DeviceDetailsMgtDAOException(msg, e); } - - return hasLocation; - } catch (SQLException e) { - throw new DeviceDetailsMgtDAOException("Error occurred while fetching the location of the registered devices.", e); - } finally { - DeviceManagementDAOUtil.cleanupResources(stmt, rs); } -} @Override public void deleteDeviceLocation(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException { 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 737d6a3ba3..585074894d 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 @@ -367,7 +367,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { if (previousLocation == null) { deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); } else { - log.info("Update location on IOTS"); deviceDetailsDAO.updateDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); } deviceDetailsDAO.addDeviceLocationInfo(device, deviceLocation, @@ -404,10 +403,14 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } } else { if(!HttpReportingUtil.isLocationPublishing()) { - log.info("Location publishing is disabled"); + if (log.isDebugEnabled()) { + log.debug("Location publishing is disabled"); + } } if (!HttpReportingUtil.isTrackerEnabled()) { - log.info("Traccar is disabled"); + if (log.isDebugEnabled()) { + log.info("Traccar is disabled"); + } } } //Tracker update GPS Location @@ -456,18 +459,22 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } catch (ExecutionException e) { log.error("ExecutionException : " + e); //throw new RuntimeException(e); - //Exception was not thrown due to being conflicted with non-traccar features + // NOTE: 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 + // NOTE: Exception was not thrown due to being conflicted with non-traccar features } } else { if(!HttpReportingUtil.isLocationPublishing()) { - log.info("Location publishing is disabled"); + if (log.isDebugEnabled()) { + log.debug("Location publishing is disabled"); + } } if (!HttpReportingUtil.isTrackerEnabled()) { - log.info("Traccar is disabled"); + if (log.isDebugEnabled()) { + log.info("Traccar is disabled"); + } } } } 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 ea0b3d4265..5ec33076b2 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 @@ -3980,13 +3980,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceLocation.setSpeed(Float.parseFloat(speed)); deviceLocation.setBearing(Float.parseFloat(bearing)); // deviceInformationManager.addDeviceLocation(device, deviceLocation); - log.info("--- Location Pushing to Traccar starts ---"); deviceInformationManager.addDeviceLocation(device, deviceLocation); - log.info("--- Location Pushing to Traccar end ---"); } catch (DeviceDetailsMgtException e) { - //We are not failing the execution since this is not critical for the functionality. But logging as - // a warning for reference. - //Exception was not thrown due to being conflicted with non-traccar features + /*** + * NOTE: + * We are not failing the execution since this is not critical for the functionality. But logging as + * a warning for reference. + * Exception was not thrown due to being conflicted with non-traccar features + */ log.warn("Error occurred while trying to add '" + device.getType() + "' device '" + device.getDeviceIdentifier() + "' (id:'" + device.getId() + "') location (lat:" + latitude + ", lon:" + longitude + ", altitude: " + altitude + 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 f50b02a7b3..547c4e8b25 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 @@ -64,6 +64,8 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class TraccarClientFactory { @@ -82,6 +84,9 @@ public class TraccarClientFactory { final String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); private final TrackerDAO trackerDAO; + /** + * This is the private constructor method as this class is a singleton + */ private TraccarClientFactory() { client = new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) @@ -92,6 +97,11 @@ public class TraccarClientFactory { this.trackerDAO = TrackerManagementDAOFactory.getTrackerDAO(); } + /** + * This method is used to initiate a singleton instance of this class. + * + * @return TraccarClientFactory instance + */ public static TraccarClientFactory getInstance() { if(INSTANCE == null) { INSTANCE = new TraccarClientFactory(); @@ -144,10 +154,9 @@ public class TraccarClientFactory { } public String fetchAllUsers() throws ExecutionException, InterruptedException { - String method = TraccarHandlerConstants.Methods.GET; String url = defaultPort + "/api/users"; - Future result = executor.submit(new OkHttpClientThreadPool(url, null, method, + Future result = executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.GET, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); return result.get(); @@ -191,7 +200,6 @@ public class TraccarClientFactory { traccarUser.setEmail(userName); traccarUser.setPassword(DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.DEFAULT_RANDOM)); traccarUser.setDeviceLimit(-1); - //traccarUser.setUserLimit(-1); traccarUser.setExpirationTime(tomorrow.toString()); DeviceAPIClientServiceImpl.createUser(traccarUser); } else { @@ -217,7 +225,6 @@ public class TraccarClientFactory { } DeviceAPIClientServiceImpl.updateUser(traccarUser, obj.getInt("id")); } - result = DeviceAPIClientServiceImpl.fetchUserInfo(userName); return result; } catch (InterruptedException | ExecutionException e) { JSONObject obj = new JSONObject(); @@ -230,22 +237,20 @@ public class TraccarClientFactory { } public String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { - String method = TraccarHandlerConstants.Methods.POST; String url = defaultPort + "/api/users"; JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser); - Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); return res.get(); } public String updateUser(TraccarUser traccarUser, int userId) throws ExecutionException, InterruptedException { - String method = TraccarHandlerConstants.Methods.PUT; String url = defaultPort + "/api/users/" + userId; JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser); - Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); return res.get(); @@ -257,10 +262,9 @@ public class TraccarClientFactory { payload.put("userId", userId); payload.put("deviceId", deviceId); - String method = TraccarHandlerConstants.Methods.POST; String url = defaultPort + "/api/permissions"; - Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); @@ -284,7 +288,9 @@ public class TraccarClientFactory { TrackerManagementDAOFactory.closeConnection(); } } else { - log.error("Couldn't add the permission record: " + result); + String msg = "Couldn't add the permission record: " + result; + log.error(msg); + throw new TrackerManagementDAOException(msg); } } @@ -294,10 +300,9 @@ public class TraccarClientFactory { payload.put("userId", userId); payload.put("deviceId", deviceId); - String method = TraccarHandlerConstants.Methods.DELETE; String url = defaultPort + "/api/permissions"; - Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.DELETE, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); @@ -319,6 +324,10 @@ public class TraccarClientFactory { } finally { TrackerManagementDAOFactory.closeConnection(); } + } else { + String msg = "Error occured while removing permission."; + log.error(msg); + throw new TrackerManagementDAOException(msg); } } @@ -328,7 +337,6 @@ public class TraccarClientFactory { TrackerManagementDAOFactory.openConnection(); return trackerDAO.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); } catch (TrackerManagementDAOException e) { - TrackerManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred while mapping with deviceId ."; log.error(msg, e); throw new TrackerManagementDAOException(msg, e); @@ -412,7 +420,7 @@ public class TraccarClientFactory { authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); - log.info("---------result--------"); + log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar."); if (res.isDone() && result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { @@ -508,36 +516,50 @@ public class TraccarClientFactory { List trackerPermissionInfo = null; try { TrackerManagementDAOFactory.beginTransaction(); - trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); - log.info("deviceId - " + deviceId); - if (trackerDeviceInfo != null) { - trackerDAO.removeTrackerDevice(deviceId, tenantId); - TrackerManagementDAOFactory.commitTransaction(); - trackerPermissionInfo = trackerDAO.getUserIdofPermissionByDeviceId(trackerDeviceInfo.getTraccarDeviceId()); + try { + TrackerManagementDAOFactory.openConnection(); + trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); + if (trackerDeviceInfo != null) { + trackerDAO.removeTrackerDevice(deviceId, tenantId); + TrackerManagementDAOFactory.commitTransaction(); + trackerPermissionInfo = trackerDAO.getUserIdofPermissionByDeviceId(trackerDeviceInfo.getTraccarDeviceId()); + } else { + String msg = "Tracker device for device id " + deviceId + " not found"; + log.error(msg); + throw new TrackerManagementDAOException(msg); + } + } catch (SQLException e) { + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (TrackerManagementDAOException e) { + String msg = "Could not add new device location"; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); } } catch (TransactionManagementException e) { TrackerManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred establishing the DB connection"; log.error(msg, e); throw new TrackerManagementDAOException(msg, e); - } catch (TrackerManagementDAOException e) { - TrackerManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while mapping with deviceId"; - log.error(msg, e); - throw new TrackerManagementDAOException(msg, e); - } finally { - TrackerManagementDAOFactory.closeConnection(); } - log.info("--------Disenrolling Device with device id " + deviceId + " from traccar client--------"); + log.info("Disenrolling Device with device id " + deviceId + " from traccar client"); //Delete from traccar if (trackerDeviceInfo != null) { - String method = TraccarHandlerConstants.Methods.DELETE; - String url = defaultPort + "/api/devices/" + trackerPermissionInfo.get(0).getTraccarDeviceId(); - executor.submit(new OkHttpClientThreadPool(url, null, method, - authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), - serverUrl(HttpReportingUtil.trackerServer()))); + if (trackerPermissionInfo.size() > 0) { + String url = defaultPort + "/api/devices/" + trackerPermissionInfo.get(0).getTraccarDeviceId(); + executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.DELETE, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + } else { + String msg = "Tracker permission mapping info not found"; + log.error(msg); + throw new TrackerManagementDAOException(msg); + } //remove permissions try { removePermission( @@ -545,9 +567,14 @@ public class TraccarClientFactory { trackerPermissionInfo.get(0).getTraccarDeviceId(), TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE); } catch (ExecutionException e) { - log.error("ExecutionException : " + e); - throw new ExecutionException(e); + String msg = "Error occured while removing tacker permissions "; + log.error(msg); + throw new ExecutionException(msg, e); } + } else { + String msg = "Tracker device not found"; + log.error(msg); + throw new TrackerManagementDAOException(msg); } } @@ -584,37 +611,36 @@ public class TraccarClientFactory { payload.put("name", groupInfo.getName()); payload.put("attributes", new JSONObject()); - String method = TraccarHandlerConstants.Methods.POST; String url = defaultPort + "/api/groups"; - Future res = executor.submit(new OkHttpClientThreadPool(url, payload, method, + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); - log.info("---------result--------"); + log.info("Group " + trackerGroupInfo.getGroupId() + " has been added to Traccar."); if (res.isDone() && result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { int traccarGroupId = obj.getInt("id"); - try { TrackerManagementDAOFactory.beginTransaction(); trackerDAO.addTrackerGroup(traccarGroupId, groupId, tenantId); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); if (trackerGroupInfo.getStatus() == 0) { trackerDAO.updateTrackerGroupIdANDStatus(trackerGroupInfo.getTraccarGroupId(), groupId, tenantId, 1); + TrackerManagementDAOFactory.commitTransaction(); } - } catch (TrackerManagementDAOException e) { - TrackerManagementDAOFactory.rollbackTransaction(); - String msg = "Error occurred while mapping with deviceId. "; - log.error(msg, e); - throw new TrackerManagementDAOException(msg, e); } catch (TransactionManagementException e) { String msg = "Error occurred establishing the DB connection. "; log.error(msg, e); TrackerManagementDAOFactory.rollbackTransaction(); throw new TrackerManagementDAOException(msg, e); - } finally { + } catch (TrackerManagementDAOException e) { + TrackerManagementDAOFactory.rollbackTransaction(); + String msg = "Error occurred while mapping with deviceId. "; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { TrackerManagementDAOFactory.closeConnection(); } } else { @@ -680,10 +706,9 @@ public class TraccarClientFactory { payload.put("name", groupInfo.getName()); payload.put("attributes", new JSONObject()); - String method = TraccarHandlerConstants.Methods.PUT; String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId"); - executor.submit(new OkHttpClientThreadPool(url, payload, method, + executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); } @@ -705,14 +730,20 @@ public class TraccarClientFactory { obj = new JSONObject(res); if (obj != null) { trackerDAO.removeTrackerGroup(obj.getInt("id")); - TrackerManagementDAOFactory.commitTransaction(); - String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId"); - executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.DELETE, authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); + TrackerManagementDAOFactory.commitTransaction(); + } else { + String msg = "Tracker group JSON object is null"; + log.error(msg); + throw new TrackerManagementDAOException(msg); } + } else { + String msg = "Tracker group not found"; + log.error(msg); + throw new TrackerManagementDAOException(msg); } } catch (TransactionManagementException e) { TrackerManagementDAOFactory.rollbackTransaction(); @@ -732,10 +763,7 @@ public class TraccarClientFactory { public String generateRandomString(int len) { String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; Random rnd = new Random(); - StringBuilder sb = new StringBuilder(len); - for (int i = 0; i < len; i++) - sb.append(chars.charAt(rnd.nextInt(chars.length()))); - return sb.toString(); + return IntStream.range(0, len).mapToObj(i -> String.valueOf(chars.charAt(rnd.nextInt(chars.length())))).collect(Collectors.joining()); } private TraccarGateway getTraccarGateway() { @@ -756,10 +784,10 @@ public class TraccarClientFactory { } public String serverUrl(String serverUrl) { - String newServerUri = endpoint; if (serverUrl != null) { - newServerUri = serverUrl; + return serverUrl; + } else { + return endpoint; } - return newServerUri; } } 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/TraccarClientImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java index 90de53d6c8..0e9264a325 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClientImpl.java @@ -403,7 +403,7 @@ public class TraccarClientImpl implements TraccarClient { authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); - log.info("---------result--------"); + log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar."); if (result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { @@ -582,7 +582,7 @@ public class TraccarClientImpl implements TraccarClient { authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), serverUrl(HttpReportingUtil.trackerServer()))); String result = res.get(); - log.info("---------result--------"); + log.info("Group " + trackerGroupInfo.getGroupId() + " has been added to Traccar."); if (res.isDone() && result.charAt(0) == '{') { JSONObject obj = new JSONObject(result); if (obj.has("id")) { -- 2.36.3 From e5095b287a41f46ae7970d72fb7c21a48bd92672 Mon Sep 17 00:00:00 2001 From: shamalka Date: Mon, 31 Oct 2022 15:15:06 +0530 Subject: [PATCH 09/12] Refactor traccar implementation --- .../impl/DeviceManagementServiceImpl.java | 20 ++++-- .../mgt/jaxrs/util/DeviceMgtAPIUtils.java | 14 ++++ .../core/dao/impl/tracker/TrackerDAOImpl.java | 28 ++++---- .../api/service/DeviceAPIClientService.java | 22 +++++++ .../api/service/TraccarClientFactory.java | 11 +++- .../impl/DeviceAPIClientServiceImpl.java | 66 +++++++++++-------- 6 files changed, 111 insertions(+), 50 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 5ac01b3fc3..1b85a4c9ba 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -102,6 +102,7 @@ import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.search.mgt.SearchMgtException; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; @@ -492,7 +493,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (HttpReportingUtil.isTrackerEnabled()) { String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - JSONObject obj = new JSONObject(DeviceAPIClientServiceImpl.returnUser(currentUser)); + JSONObject obj = new JSONObject(DeviceMgtAPIUtils.getDeviceAPIClientService().returnUser(currentUser)); if (obj.has("error")) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(obj.getString("error")).build(); @@ -502,6 +503,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { /*Get Device Id List*/ try { DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); + DeviceAPIClientService deviceAPIClientService = DeviceMgtAPIUtils.getDeviceAPIClientService(); DeviceAccessAuthorizationService deviceAccessAuthorizationService = DeviceMgtAPIUtils.getDeviceAccessAuthorizationService(); PaginationRequest request = new PaginationRequest(0, 0); @@ -532,21 +534,25 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { int tenantId = CarbonContext.getThreadLocalCarbonContext().getTenantId(); TrackerDeviceInfo trackerDevice; for (Device device : devices.getList()) { - trackerDevice = DeviceAPIClientServiceImpl.getTrackerDevice(device.getId(), tenantId); - if (trackerDevice != null) { + trackerDevice = deviceAPIClientService.getTrackerDevice(device.getId(), tenantId); + if(trackerDevice == null) { + String msg = "Tracker device not found for the device ID: " + device.getId(); + log.error(msg); + return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); + } else { int traccarDeviceId = trackerDevice.getTraccarDeviceId(); - boolean getPermission = DeviceAPIClientServiceImpl.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); + boolean getPermission = deviceAPIClientService.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); traccarValidIdList.add(traccarDeviceId); if (!getPermission) { - DeviceAPIClientServiceImpl.addTrackerUserDevicePermission(userId, traccarDeviceId); + deviceAPIClientService.addTrackerUserDevicePermission(userId, traccarDeviceId); } } } //Remove necessary List getAllUserDevices = - DeviceAPIClientServiceImpl.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); + deviceAPIClientService.getUserIdofPermissionByUserIdNIdList(userId, traccarValidIdList); for (TrackerPermissionInfo getAllUserDevice : getAllUserDevices) { - DeviceAPIClientServiceImpl.removeTrackerUserDevicePermission( + deviceAPIClientService.removeTrackerUserDevicePermission( getAllUserDevice.getTraccarUserId(), getAllUserDevice.getTraccarDeviceId(), TraccarHandlerConstants.Types.REMOVE_TYPE_SINGLE); 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 8861833f5a..92bc2790d4 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 @@ -87,6 +87,7 @@ import org.wso2.carbon.device.mgt.core.privacy.PrivacyComplianceProvider; import org.wso2.carbon.device.mgt.core.search.mgt.SearchManagerService; import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; import org.wso2.carbon.device.mgt.jaxrs.beans.DeviceTypeVersionWrapper; import org.wso2.carbon.device.mgt.jaxrs.beans.ErrorResponse; import org.wso2.carbon.device.mgt.jaxrs.beans.OperationStatusBean; @@ -592,6 +593,19 @@ public class DeviceMgtAPIUtils { return analyticsDataAPI; } + public static DeviceAPIClientService getDeviceAPIClientService() { + DeviceAPIClientService deviceAPIClientService; + PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext(); + deviceAPIClientService = (DeviceAPIClientService) ctx.getOSGiService( + DeviceAPIClientService.class, null); + if (deviceAPIClientService == null) { + String msg = "Device API Client service not initialized."; + log.error(msg); + throw new IllegalStateException(msg); + } + return deviceAPIClientService; + } + public static int getTenantId(String tenantDomain) throws DeviceManagementException { RealmService realmService = (RealmService) PrivilegedCarbonContext.getThreadLocalCarbonContext().getOSGiService(RealmService.class, null); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java index 43d21b4d0e..a3d1ccf8af 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java @@ -18,6 +18,7 @@ package org.wso2.carbon.device.mgt.core.dao.impl.tracker; +import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; @@ -26,13 +27,11 @@ import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOFactory; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.TrackerDAO; +import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.TrackerManagementDAOUtil; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; +import java.sql.*; import java.util.ArrayList; import java.util.List; @@ -104,27 +103,26 @@ public class TrackerDAOImpl implements TrackerDAO { @Override public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { - PreparedStatement stmt = null; - ResultSet rs = null; + TrackerDeviceInfo trackerDeviceInfo = null; try { Connection conn = TrackerManagementDAOFactory.getConnection(); String sql = "SELECT ID, TRACCAR_DEVICE_ID, DEVICE_ID, TENANT_ID, STATUS FROM DM_EXT_DEVICE_MAPPING WHERE " + "DEVICE_ID = ? AND TENANT_ID = ? ORDER BY ID DESC LIMIT 1"; - stmt = conn.prepareStatement(sql); - stmt.setInt(1, deviceId); - stmt.setInt(2, tenantId); - rs = stmt.executeQuery(); - if (rs.next()) { - return TrackerManagementDAOUtil.loadTrackerDevice(rs); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceId); + stmt.setInt(2, tenantId); + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + trackerDeviceInfo = TrackerManagementDAOUtil.loadTrackerDevice(rs); + } + } } - return null; } catch (SQLException e) { String msg = "Error occurred while retrieving data from the trackerDevice table "; log.error(msg, e); throw new TrackerManagementDAOException(msg, e); - } finally { - TrackerManagementDAOUtil.cleanupResources(stmt, rs); } + return trackerDeviceInfo; } @Override 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 605d4f7a46..73aca76059 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 @@ -20,10 +20,14 @@ package org.wso2.carbon.device.mgt.core.traccar.api.service; import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; +import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; + +import java.util.List; import java.util.concurrent.ExecutionException; public interface DeviceAPIClientService { @@ -81,4 +85,22 @@ public interface DeviceAPIClientService { */ void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException; + + String returnUser(String username); + + TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws + TrackerManagementDAOException; + + boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws + TrackerManagementDAOException; + + void addTrackerUserDevicePermission(int userId, int deviceId) throws + TrackerManagementDAOException, ExecutionException, InterruptedException; + + List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) throws + TrackerManagementDAOException; + + void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws + TrackerManagementDAOException, ExecutionException, InterruptedException; + } 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 547c4e8b25..65734d1f3c 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 @@ -352,7 +352,16 @@ public class TraccarClientFactory { public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { try { TrackerManagementDAOFactory.openConnection(); - return trackerDAO.getTrackerDevice(deviceId, tenantId); + TrackerDeviceInfo trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); + if(trackerDeviceInfo == null) { + String msg = "No tracker device is found upon the device ID of: " + deviceId; + if (log.isDebugEnabled()) { + log.debug(msg); + } + return null; + } else { + return trackerDeviceInfo; + } } catch (SQLException e) { String msg = "Error occurred establishing the DB connection ."; 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 1361bbdc17..6a113ca72e 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 @@ -45,6 +45,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { private static final Log log = LogFactory.getLog(DeviceAPIClientServiceImpl.class); TraccarClientFactory client = TraccarClientFactory.getInstance(); + @Override public void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException { String lastUpdatedTime = String.valueOf((new Date().getTime())); TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getName(), device.getDeviceIdentifier(), @@ -61,6 +62,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException { TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), deviceLocation.getUpdatedTime().getTime(), @@ -82,6 +84,7 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override public void disEnrollDevice(int deviceId, int tenantId) throws ExecutionException, InterruptedException{ try { client.disEnrollDevice(deviceId, tenantId); @@ -91,79 +94,88 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override public void addGroup(DeviceGroup group, int groupId, int tenantId) throws TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { TraccarGroups traccarGroups = new TraccarGroups(group.getName()); client.addGroup(traccarGroups, groupId, tenantId); } + @Override public void updateGroup(DeviceGroup group, int groupId, int tenantId) throws TrackerManagementDAOException, TrackerAlreadyExistException, ExecutionException, InterruptedException { TraccarGroups traccarGroups = new TraccarGroups(group.getName()); client.updateGroup(traccarGroups, groupId, tenantId); } + @Override public void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { client.deleteGroup(groupId, tenantId); } - public static String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { + @Override + public String returnUser(String userName) { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.fetchUserInfo(userName); + try { + return client.returnUser(userName); + } catch (TrackerManagementDAOException e) { + JSONObject obj = new JSONObject(); + String msg = "Error occurred while creating a user: "+ e; + obj.put("error", msg); + return obj.toString(); + } } - public static TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws + @Override + public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException { TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.getTrackerDevice(deviceId, tenantId); } - public static boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws + @Override + public boolean getUserIdofPermissionByDeviceIdNUserId(int deviceId, int userId) throws TrackerManagementDAOException { TraccarClientFactory client = TraccarClientFactory.getInstance(); return client.getUserIdofPermissionByDeviceIdNUserId(deviceId, userId); } - public static String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { + @Override + public void addTrackerUserDevicePermission(int userId, int deviceId) throws + TrackerManagementDAOException, ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.createUser(traccarUser); + client.setPermission(userId, deviceId); } - public static String updateUser(TraccarUser traccarUser, int userId) throws - ExecutionException, InterruptedException { + @Override + public List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) throws + TrackerManagementDAOException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.updateUser(traccarUser, userId); + return client.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); } - public static String returnUser(String userName) { + @Override + public void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws + TrackerManagementDAOException, ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - try { - return client.returnUser(userName); - } catch (TrackerManagementDAOException e) { - JSONObject obj = new JSONObject(); - String msg = "Error occurred while creating a user: "+ e; - obj.put("error", msg); - return obj.toString(); - } + client.removePermission(userId, deviceId, removeType); } - public static void addTrackerUserDevicePermission(int userId, int deviceId) throws - TrackerManagementDAOException, ExecutionException, InterruptedException { + public static String fetchUserInfo(String userName) throws ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - client.setPermission(userId, deviceId); + return client.fetchUserInfo(userName); } - public static void removeTrackerUserDevicePermission(int userId, int deviceId, int removeType) throws - TrackerManagementDAOException, ExecutionException, InterruptedException { + public static String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - client.removePermission(userId, deviceId, removeType); + return client.createUser(traccarUser); } - public static List getUserIdofPermissionByUserIdNIdList(int userId, List NotInDeviceIdList) throws - TrackerManagementDAOException { + public static String updateUser(TraccarUser traccarUser, int userId) throws + ExecutionException, InterruptedException { TraccarClientFactory client = TraccarClientFactory.getInstance(); - return client.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); + return client.updateUser(traccarUser, userId); } public static String generateRandomString(int len) { -- 2.36.3 From 084f83bc17bfc395516d8cb91a33d30a6435165f Mon Sep 17 00:00:00 2001 From: shamalka Date: Tue, 8 Nov 2022 13:15:01 +0530 Subject: [PATCH 10/12] Fix issues --- .../jaxrs/service/impl/DeviceManagementServiceImpl.java | 8 ++------ .../device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java | 9 ++++++--- .../core/traccar/api/service/DeviceAPIClientService.java | 2 +- .../core/traccar/api/service/TraccarClientFactory.java | 1 - .../api/service/impl/DeviceAPIClientServiceImpl.java | 4 ++-- 5 files changed, 11 insertions(+), 13 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index 1b85a4c9ba..cabd2f8b9b 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -493,7 +493,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (HttpReportingUtil.isTrackerEnabled()) { String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - JSONObject obj = new JSONObject(DeviceMgtAPIUtils.getDeviceAPIClientService().returnUser(currentUser)); + JSONObject obj = new JSONObject(DeviceAPIClientServiceImpl.returnUser(currentUser)); if (obj.has("error")) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(obj.getString("error")).build(); @@ -535,11 +535,7 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { TrackerDeviceInfo trackerDevice; for (Device device : devices.getList()) { trackerDevice = deviceAPIClientService.getTrackerDevice(device.getId(), tenantId); - if(trackerDevice == null) { - String msg = "Tracker device not found for the device ID: " + device.getId(); - log.error(msg); - return Response.status(Response.Status.NOT_FOUND).entity(msg).build(); - } else { + if(trackerDevice != null) { int traccarDeviceId = trackerDevice.getTraccarDeviceId(); boolean getPermission = deviceAPIClientService.getUserIdofPermissionByDeviceIdNUserId(traccarDeviceId, userId); traccarValidIdList.add(traccarDeviceId); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java index a3d1ccf8af..8066bc311c 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java @@ -31,7 +31,10 @@ import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; import org.wso2.carbon.device.mgt.core.dao.util.TrackerManagementDAOUtil; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; -import java.sql.*; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; import java.util.ArrayList; import java.util.List; @@ -106,8 +109,8 @@ public class TrackerDAOImpl implements TrackerDAO { TrackerDeviceInfo trackerDeviceInfo = null; try { Connection conn = TrackerManagementDAOFactory.getConnection(); - String sql = "SELECT ID, TRACCAR_DEVICE_ID, DEVICE_ID, TENANT_ID, STATUS FROM DM_EXT_DEVICE_MAPPING WHERE " + - "DEVICE_ID = ? AND TENANT_ID = ? ORDER BY ID DESC LIMIT 1"; + String sql = "SELECT DM_EXT_DEVICE_MAPPING.ID, DM_EXT_DEVICE_MAPPING.TRACCAR_DEVICE_ID, DM_EXT_DEVICE_MAPPING.DEVICE_ID, DM_EXT_DEVICE_MAPPING.TENANT_ID, DM_EXT_DEVICE_MAPPING.STATUS FROM DM_EXT_DEVICE_MAPPING JOIN DM_DEVICE ON DM_EXT_DEVICE_MAPPING.DEVICE_ID=DM_DEVICE.ID WHERE " + + "DM_EXT_DEVICE_MAPPING.DEVICE_ID = ? AND DM_EXT_DEVICE_MAPPING.TENANT_ID = ? ORDER BY DM_EXT_DEVICE_MAPPING.ID DESC LIMIT 1"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); 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 73aca76059..0aea251f31 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 @@ -86,7 +86,7 @@ public interface DeviceAPIClientService { void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException; - String returnUser(String username); +// String returnUser(String username); TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException; 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 65734d1f3c..c9ffbe9064 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 @@ -178,7 +178,6 @@ public class TraccarClientFactory { } } } - return TraccarHandlerConstants.Types.USER_NOT_FOUND; } 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 6a113ca72e..f6a0e3fc73 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 @@ -114,8 +114,8 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { client.deleteGroup(groupId, tenantId); } - @Override - public String returnUser(String userName) { +// @Override + public static String returnUser(String userName) { TraccarClientFactory client = TraccarClientFactory.getInstance(); try { return client.returnUser(userName); -- 2.36.3 From 8ad20279d2ff6170db86b568c2ca5759c50ade55 Mon Sep 17 00:00:00 2001 From: shamalka Date: Thu, 10 Nov 2022 20:48:10 +0530 Subject: [PATCH 11/12] Fix traccar loading issue --- .../mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java | 4 ++-- .../core/traccar/api/service/DeviceAPIClientService.java | 2 +- .../mgt/core/traccar/api/service/TraccarClientFactory.java | 3 ++- .../api/service/impl/DeviceAPIClientServiceImpl.java | 6 +++--- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index cabd2f8b9b..b8c626cb41 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -493,7 +493,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { if (HttpReportingUtil.isTrackerEnabled()) { String currentUser = CarbonContext.getThreadLocalCarbonContext().getUsername(); - JSONObject obj = new JSONObject(DeviceAPIClientServiceImpl.returnUser(currentUser)); + DeviceAPIClientService deviceAPIClientService = DeviceMgtAPIUtils.getDeviceAPIClientService(); + JSONObject obj = new JSONObject(deviceAPIClientService.returnUser(currentUser)); if (obj.has("error")) { return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(obj.getString("error")).build(); @@ -503,7 +504,6 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { /*Get Device Id List*/ try { DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); - DeviceAPIClientService deviceAPIClientService = DeviceMgtAPIUtils.getDeviceAPIClientService(); DeviceAccessAuthorizationService deviceAccessAuthorizationService = DeviceMgtAPIUtils.getDeviceAccessAuthorizationService(); PaginationRequest request = new PaginationRequest(0, 0); 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 0aea251f31..73aca76059 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 @@ -86,7 +86,7 @@ public interface DeviceAPIClientService { void deleteGroup(int groupId, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException; -// String returnUser(String username); + String returnUser(String username); TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException; 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 c9ffbe9064..193bdb211c 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 @@ -183,7 +183,7 @@ public class TraccarClientFactory { public String returnUser(String userName) throws TrackerManagementDAOException { try { - String result = DeviceAPIClientServiceImpl.fetchUserInfo(userName); + String result = fetchUserInfo(userName); Date today = new Date(); LocalDateTime tomorrow = LocalDateTime.from(today.toInstant().atZone(ZoneId.of("UTC"))).plusDays(1); String token = DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.TRACCAR_TOKEN); @@ -224,6 +224,7 @@ public class TraccarClientFactory { } DeviceAPIClientServiceImpl.updateUser(traccarUser, obj.getInt("id")); } + result = fetchUserInfo(userName); return result; } catch (InterruptedException | ExecutionException e) { JSONObject obj = new JSONObject(); 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 f6a0e3fc73..9095f3dd5b 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 @@ -114,11 +114,11 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { client.deleteGroup(groupId, tenantId); } -// @Override - public static String returnUser(String userName) { + @Override + public String returnUser(String username) { TraccarClientFactory client = TraccarClientFactory.getInstance(); try { - return client.returnUser(userName); + return client.returnUser(username); } catch (TrackerManagementDAOException e) { JSONObject obj = new JSONObject(); String msg = "Error occurred while creating a user: "+ e; -- 2.36.3 From 537fc9b8c4161e18a13d6a9492fb475ec44de5cc Mon Sep 17 00:00:00 2001 From: shamalka Date: Thu, 10 Nov 2022 21:00:08 +0530 Subject: [PATCH 12/12] Fix formatting issues --- .../mgt/core/dao/impl/tracker/TrackerDAOImpl.java | 12 ++++++++++-- .../service/DeviceManagementProviderServiceImpl.java | 1 - 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java index 8066bc311c..034ecbff77 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/dao/impl/tracker/TrackerDAOImpl.java @@ -109,8 +109,16 @@ public class TrackerDAOImpl implements TrackerDAO { TrackerDeviceInfo trackerDeviceInfo = null; try { Connection conn = TrackerManagementDAOFactory.getConnection(); - String sql = "SELECT DM_EXT_DEVICE_MAPPING.ID, DM_EXT_DEVICE_MAPPING.TRACCAR_DEVICE_ID, DM_EXT_DEVICE_MAPPING.DEVICE_ID, DM_EXT_DEVICE_MAPPING.TENANT_ID, DM_EXT_DEVICE_MAPPING.STATUS FROM DM_EXT_DEVICE_MAPPING JOIN DM_DEVICE ON DM_EXT_DEVICE_MAPPING.DEVICE_ID=DM_DEVICE.ID WHERE " + - "DM_EXT_DEVICE_MAPPING.DEVICE_ID = ? AND DM_EXT_DEVICE_MAPPING.TENANT_ID = ? ORDER BY DM_EXT_DEVICE_MAPPING.ID DESC LIMIT 1"; + String sql = "SELECT " + + "DM_EXT_DEVICE_MAPPING.ID, " + + "DM_EXT_DEVICE_MAPPING.TRACCAR_DEVICE_ID, " + + "DM_EXT_DEVICE_MAPPING.DEVICE_ID, " + + "DM_EXT_DEVICE_MAPPING.TENANT_ID, " + + "DM_EXT_DEVICE_MAPPING.STATUS " + + "FROM DM_EXT_DEVICE_MAPPING " + + "JOIN DM_DEVICE ON DM_EXT_DEVICE_MAPPING.DEVICE_ID=DM_DEVICE.ID " + + "WHERE DM_EXT_DEVICE_MAPPING.DEVICE_ID = ? AND DM_EXT_DEVICE_MAPPING.TENANT_ID = ? " + + "ORDER BY DM_EXT_DEVICE_MAPPING.ID DESC LIMIT 1"; try (PreparedStatement stmt = conn.prepareStatement(sql)) { stmt.setInt(1, deviceId); stmt.setInt(2, tenantId); 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 5ec33076b2..2f0439f8ca 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 @@ -3979,7 +3979,6 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv deviceLocation.setDistance(Double.parseDouble(distance)); deviceLocation.setSpeed(Float.parseFloat(speed)); deviceLocation.setBearing(Float.parseFloat(bearing)); -// deviceInformationManager.addDeviceLocation(device, deviceLocation); deviceInformationManager.addDeviceLocation(device, deviceLocation); } catch (DeviceDetailsMgtException e) { /*** -- 2.36.3