diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java index 2db3bd5f584..66167f837f1 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/GroupManagementProviderServiceImpl.java @@ -35,6 +35,7 @@ package org.wso2.carbon.device.mgt.core.service; +import com.google.gson.Gson; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -64,6 +65,7 @@ import org.wso2.carbon.device.mgt.core.event.config.GroupAssignmentEventOperatio import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager; import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants; +import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.user.api.UserRealm; import org.wso2.carbon.user.api.UserStoreException; @@ -131,7 +133,24 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid this.groupDAO.addGroupProperties(deviceGroup, updatedGroupID, tenantId); } GroupManagementDAOFactory.commitTransaction(); + + //add new group in traccar + try { + DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() + .addGroup(deviceGroup); + } catch (TraccarConfigurationException e) { + log.error("Error while disenrolling a device from Traccar " + e); + } + //add new group in traccar } else { + //check if a group exist or not in traccar if not existing then add + /*try { + DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() + .addGroup(deviceGroup); + } catch (TraccarConfigurationException e) { + log.error("Error while disenrolling a device from Traccar " + e); + }*/ + //check if a group exist or not in traccar throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName()); } } catch (GroupManagementDAOException e) { @@ -283,6 +302,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid if (log.isDebugEnabled()) { log.debug("DeviceGroup " + deviceGroup.getName() + " removed."); } + //add new group in traccar + try { + DeviceManagementDataHolder.getInstance().getDeviceAPIClientService() + .deleteGroup(deviceGroup); + } catch (TraccarConfigurationException e) { + log.error("Error while disenrolling a device from Traccar " + e); + } + //add new group in traccar return true; } catch (GroupManagementDAOException e) { GroupManagementDAOFactory.rollbackTransaction(); 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 7ef5f777d0f..e187ddf7df8 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 @@ -63,4 +63,12 @@ public interface DeviceAPIClientService { * @throws TraccarConfigurationException errors thrown while adding a group traccar configuration */ void addGroup(DeviceGroup group) throws TraccarConfigurationException; + + /** + * Delete a device Traccar configuration records + * + * @param group to be add a group + * @throws TraccarConfigurationException errors thrown while adding a group traccar configuration + */ + void deleteGroup(DeviceGroup group) throws TraccarConfigurationException; } 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/TrackerClient.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java index 4d4f7ada436..548e585a0bb 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java @@ -42,6 +42,7 @@ import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationM import java.io.IOException; import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -123,7 +124,7 @@ public class TrackerClient implements TraccarClient { */ public void addDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException { try{ - JSONObject payload = new JSONObject(); + JSONObject payload = new JSONObject(); payload.put("name", deviceInfo.getDeviceName()); payload.put("uniqueId", deviceInfo.getUniqueId()); payload.put("status", deviceInfo.getStatus()); @@ -205,11 +206,6 @@ public class TrackerClient implements TraccarClient { String result = getDeviceByDeviceIdentifier(deviceInfo.getDeviceIdentifier()); String jsonData ="{"+ "\"geodata\": "+ result+ "}"; - log.info("======================"); - log.info("result"); - log.info(result); - log.info(deviceInfo.getDeviceIdentifier()); - log.info("==========================="); JSONObject obj = new JSONObject(jsonData); JSONArray geodata = obj.getJSONArray("geodata"); JSONObject jsonResponse = geodata.getJSONObject(0); @@ -219,11 +215,11 @@ public class TrackerClient implements TraccarClient { executor.execute(trackerExecutor); log.info("Device successfully dis-enrolled"); }catch (JSONException e){ - String msg = "Could not find the device infomation to dis-enroll the device"; + String msg = "Could not find the device information to dis-enroll the device"; log.error(msg, e); throw new TraccarConfigurationException(msg); }catch (TraccarConfigurationException ex){ - String msg = "Could not find the device infomation to dis-enroll the device"; + String msg = "Could not find the device information to dis-enroll the device"; log.error(msg, ex); throw new TraccarConfigurationException(msg, ex); } @@ -243,7 +239,7 @@ public class TrackerClient implements TraccarClient { String context = defaultPort+"/api/groups"; Runnable trackerExecutor = new TrackerExecutor(endpoint, context, payload, "post"); executor.execute(trackerExecutor); - log.info("Group successfully on traccar"); + log.info("Group successfully added on traccar"); }catch (Exception e){ String msg="Could not add a traccar group"; log.error(msg, e); @@ -251,6 +247,67 @@ public class TrackerClient implements TraccarClient { } } + /** + * Add Device GPS Location operation. + * @return all groups + * @throws TraccarConfigurationException Failed while add Traccar Device location operation + */ + @Override + public String getAllGroups() throws TraccarConfigurationException { + try { + String context = defaultPort+"/api/groups?all=true"; + Runnable trackerExecutor = new TrackerExecutor(endpoint, context, null, "get"); + executor.execute(trackerExecutor); + Request request = new Request.Builder() + .url(endpoint+context) + .addHeader(authorization, authorizationKey) + .build(); + Response response = client.newCall(request).execute(); + String result = response.body().string(); + return result; + } catch (IOException e) { + String msg="Could not find device information"; + log.error(msg, e); + throw new TraccarConfigurationException(msg, e); + } + } + + /** + * Add Traccar Device operation. + * @param groupInfo with groupName + * @throws TraccarConfigurationException Failed while add Traccar Device the operation + */ + public void deleteGroup(TraccarGroups groupInfo) throws TraccarConfigurationException { + try{ + String result = getAllGroups(); + String jsonData ="{"+ "\"groupdata\": "+ result+ "}"; + + JSONObject obj = new JSONObject(jsonData); + JSONArray geodata = obj.getJSONArray("groupdata"); + + for(int i=0; i