From 739090b42d472f5c20a69bd44d327cd0305d3204 Mon Sep 17 00:00:00 2001 From: shamalka Date: Wed, 28 Sep 2022 11:46:09 +0530 Subject: [PATCH] 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] + ":" +