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 193bdb211c..1175b80f24 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,52 @@ 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 + String msg = "Tracker device for device id " + traccarDevice.getId() + " not found in local database"; + log.error(msg); + throw new TrackerManagementDAOException(msg); + } + } + /** * 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..d791f40219 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.getName(), device.getDeviceIdentifier(), null,null,null,null,null,null,null,null,null); + 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/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..72cb86e9d5 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,14 @@ 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()); +// add other params if needed +// as long as its null it wintt be overitten + return payload; + } }