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 2f0439f8ca..d6cf2d7fc3 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -527,6 +527,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv DeviceManagementDAOFactory.closeConnection(); } extractDeviceLocationToUpdate(device); + //enroll Traccar device + if (HttpReportingUtil.isTrackerEnabled()) { + try { + int tenantId = this.getTenantId(); + DeviceManagementDataHolder.getInstance().getDeviceAPIClientService().modifyDevice(device, tenantId); + } catch (ExecutionException e) { + log.error("ExecutionException : " + e); + //throw new RuntimeException(e); + //Exception was not thrown due to being conflicted with non-traccar features + } catch (InterruptedException e) { + log.error("InterruptedException : " + e); + //throw new RuntimeException(e); + //Exception was not thrown due to being conflicted with non-traccar features + } + } else { + log.info("Traccar is disabled"); + } + //enroll Traccar device return status; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java index 73aca76059..5dacc735d1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java @@ -50,6 +50,14 @@ public interface DeviceAPIClientService { */ void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException; + /** + * Updates device Traccar configuration records (like device name) + * + * @params device to be modifies + * @throws TrackerManagementDAOException errors thrown while modifing a traccar device + */ + void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException; + /** * Delete a device Traccar configuration records * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java index e0124c53ef..bffa6f5e85 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClientFactory.java @@ -469,6 +469,54 @@ public class TraccarClientFactory { } } + /** + * Modify the Device + * + * @param traccarDevice with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId + * Model, Contact, Category, fenceIds + * @throws TrackerManagementDAOException Failed while add Traccar Device the operation + */ + public void modifyDevice(TraccarDevice traccarDevice, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException { + TrackerDeviceInfo trackerDeviceInfo = null; + try { + TrackerManagementDAOFactory.openConnection(); + trackerDeviceInfo = trackerDAO.getTrackerDevice(traccarDevice.getId(), tenantId); + } catch (TrackerManagementDAOException e) { + String msg = "Error occurred while mapping with deviceId ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred establishing the DB connection ."; + log.error(msg, e); + throw new TrackerManagementDAOException(msg, e); + } finally { + TrackerManagementDAOFactory.closeConnection(); + } + if (trackerDeviceInfo != null) { + log.info("Preparing to rename device (" + traccarDevice.getId() + " to taccar server"); + String url = defaultPort + "/api/devices/" + trackerDeviceInfo.getTraccarDeviceId(); + JSONObject payload = TraccarUtil.TraccarDevicePayload(traccarDevice, trackerDeviceInfo.getTraccarDeviceId()); + Future res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT, + authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), + serverUrl(HttpReportingUtil.trackerServer()))); + String result = res.get(); + log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar."); + if (res.isDone() && result.charAt(0) == '{') { + log.info("Succesfully renamed Traccar Device - " + traccarDevice.getId() + "in server"); + } else { + log.info("Failed to rename Traccar Device" + traccarDevice.getId() + "in server"); + } + } else { +// forward to add device + try { + addDevice(traccarDevice, tenantId); + } catch (TrackerAlreadyExistException e) { + String msg = "The device already exist"; + log.error(msg, e); + } + } + } + /** * Add Device GPS Location operation. * diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java index 9095f3dd5b..6b955d028d 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java @@ -62,6 +62,17 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { } } + @Override + public void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException { + TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getDeviceIdentifier(), device.getName()); + try { + client.modifyDevice(traccarDevice, tenantId); + } catch (TrackerManagementDAOException e) { + String msg = "Error occurred while mapping with deviceId"; + log.error(msg, e); + } + } + @Override public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException { TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java index 163ae4f3a5..7464876664 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java @@ -55,6 +55,12 @@ public class TraccarDevice { this.category =category; } + public TraccarDevice(int id, String uniqueId, String deviceName) { + this.id = id; + this.uniqueId = uniqueId; + this.deviceName = deviceName; + } + public TraccarDevice(){ } public int getId() { return id; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java index 986b10624d..ed32984d63 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/util/TraccarUtil.java @@ -66,4 +66,12 @@ public class TraccarUtil { payload.put("attributes", new JSONObject()); return payload; } + + public static JSONObject TraccarDevicePayload(TraccarDevice deviceInfo, int id) { + JSONObject payload = new JSONObject(); + payload.put("id", id); + payload.put("name", deviceInfo.getDeviceName()); + payload.put("uniqueId", deviceInfo.getUniqueId()); + return payload; + } }