From 53f4fd7ebcf1a35b06ef01f9b6c5a6f60afc8ef0 Mon Sep 17 00:00:00 2001 From: Rushdi Date: Wed, 9 Mar 2022 23:31:26 +0530 Subject: [PATCH] Add add/delete a group from traccar --- .../GroupManagementProviderServiceImpl.java | 27 +++++++ .../api/service/DeviceAPIClientService.java | 8 ++ .../api/service/addons/TrackerClient.java | 75 ++++++++++++++++--- .../impl/TraccarAPIClientServiceImpl.java | 6 ++ .../core/traccar/common/TraccarClient.java | 4 + 5 files changed, 111 insertions(+), 9 deletions(-) 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 2db3bd5f58..66167f837f 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 7ef5f777d0..e187ddf7df 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 4d4f7ada43..548e585a0b 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