diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerAlreadyExistException.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/TrackerAlreadyExistException.java similarity index 96% rename from components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerAlreadyExistException.java rename to components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/TrackerAlreadyExistException.java index 45b8d31063a..bf27865da97 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/TrackerAlreadyExistException.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/exceptions/TrackerAlreadyExistException.java @@ -16,7 +16,7 @@ * under the License. */ -package org.wso2.carbon.device.mgt.common; +package org.wso2.carbon.device.mgt.common.exceptions; /** * This class represents a custom exception specified for group management diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClient.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClient.java index 76cb22a8b9f..1be320687b2 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClient.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/TraccarClient.java @@ -19,10 +19,12 @@ package org.wso2.carbon.device.mgt.core.traccar.api.service; -import org.wso2.carbon.device.mgt.common.TrackerAlreadyExistException; +import org.json.JSONObject; +import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarUser; import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; public interface TraccarClient { @@ -45,4 +47,13 @@ public interface TraccarClient { throws TraccarConfigurationException, TrackerAlreadyExistException; void deleteGroup(int traccarGroupId, int tenantId) throws TraccarConfigurationException; + + void fetchAllUsers(String createUser, TraccarUser traccarUser, int deviceId) throws TraccarConfigurationException; + + void createUser(TraccarUser traccarUser, String method, int deviceId) throws TraccarConfigurationException; + + void updateUser(JSONObject traccarUser) throws TraccarConfigurationException; + + void setPermission(int userId, int deviceId) 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/TraccarClient.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 similarity index 73% rename from components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClient.java rename to 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 bcce5f4d879..124b16f0444 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TraccarClient.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 @@ -19,6 +19,7 @@ package org.wso2.carbon.device.mgt.core.traccar.api.service.addons; +import com.google.gson.Gson; import okhttp3.ConnectionPool; import okhttp3.MediaType; import okhttp3.OkHttpClient; @@ -27,19 +28,23 @@ import okhttp3.RequestBody; import okhttp3.Response; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.wso2.carbon.context.PrivilegedCarbonContext; import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo; import org.wso2.carbon.device.mgt.common.TrackerGroupInfo; import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException; -import org.wso2.carbon.device.mgt.common.TrackerAlreadyExistException; +import org.wso2.carbon.device.mgt.common.exceptions.TrackerAlreadyExistException; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException; import org.wso2.carbon.device.mgt.core.dao.TrackerDAO; import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOFactory; +import org.wso2.carbon.device.mgt.core.traccar.api.service.TraccarClient; import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarUser; import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarGateway; import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager; @@ -49,16 +54,16 @@ import java.sql.SQLException; import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.Random; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; -public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.api.service.TraccarClient { - private static final Log log = LogFactory.getLog(TraccarClient.class); +public class TraccarClientImpl implements TraccarClient { + private static final Log log = LogFactory.getLog(TraccarClientImpl.class); private static final int THREAD_POOL_SIZE = 50; private final OkHttpClient client; private final ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE); - final TraccarGateway traccarGateway = getTraccarGateway(); final String endpoint = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.ENDPOINT).getValue(); final String authorization = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.AUTHORIZATION).getValue(); @@ -66,9 +71,10 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap final String defaultPort = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.DEFAULT_PORT).getValue(); final String locationUpdatePort = traccarGateway.getPropertyByName(TraccarHandlerConstants.TraccarConfig.LOCATION_UPDATE_PORT).getValue(); + final String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); private final TrackerDAO trackerDAO; - public TraccarClient() { + public TraccarClientImpl() { client = new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .writeTimeout(30, TimeUnit.SECONDS) @@ -123,7 +129,15 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap if(method==TraccarHandlerConstants.Methods.POST){ String result = response.body().string(); log.info(result); - if(result.charAt(0)=='{'){ + if(type==TraccarHandlerConstants.Types.PERMISSION){ + String msg =""; + if(result == ""){ + msg ="Successfully the device is assigned to the user"; + }else{ + msg = "Error occurred while fetching users ."; + } + log.info(msg); + }else if(result.charAt(0)=='{'){ JSONObject obj = new JSONObject(result); if (obj.has("id")){ int traccarId = obj.getInt("id"); @@ -134,6 +148,20 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap TrackerDeviceInfo res = trackerDAO.getTrackerDevice(deviceId, tenantId); if(res.getStatus()==0){ trackerDAO.updateTrackerDeviceIdANDStatus(res.getTraccarDeviceId(), deviceId, tenantId, 1); + + TraccarUser traccarUser = new TraccarUser(); + traccarUser.setName(username); + traccarUser.setLogin(username); + traccarUser.setEmail(username); + traccarUser.setPassword(generateRandomString(10)); + traccarUser.setToken(generateRandomString(32)); + traccarUser.setDeviceLimit(-1); + + log.info("============================="); + log.info(new Gson().toJson(traccarUser)); + log.info("============================="); + fetchAllUsers(TraccarHandlerConstants.Types.USER_CREATE_WITH_INSERT_DEVICE, traccarUser, traccarId); + } }else if(type==TraccarHandlerConstants.Types.GROUP){ trackerDAO.addTrackerGroup(traccarId, groupId, tenantId); @@ -141,6 +169,16 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap if(res.getStatus()==0){ trackerDAO.updateTrackerGroupIdANDStatus(res.getTraccarGroupId(), groupId, tenantId, 1); } + }else if(type==TraccarHandlerConstants.Types.USER_CREATE){ + log.info("============="); + log.info("User inserted"); + log.info("============="); + }else if(type==TraccarHandlerConstants.Types.USER_CREATE_WITH_INSERT_DEVICE){ + int userId = traccarId; + log.info("============="); + log.info("User inserted and setting to create session"); + log.info("============="); + setPermission(userId, deviceId); } TrackerManagementDAOFactory.commitTransaction(); } catch (JSONException e) { @@ -158,6 +196,10 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap msg = "Error occurred while mapping with deviceId ."; }else if(type==TraccarHandlerConstants.Types.GROUP){ msg = "Error occurred while mapping with groupId ."; + }else if(type==TraccarHandlerConstants.Types.USER){ + msg = "Error occurred while fetching users ."; + }else if(type==TraccarHandlerConstants.Types.PERMISSION){ + msg = "Error occurred while assigning the device to the user ."; } log.error(msg, e); } finally { @@ -169,9 +211,55 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap if (log.isDebugEnabled()) { log.debug("Successfully the request is proceed and communicated with Traccar"); } + }else if(method==TraccarHandlerConstants.Methods.GET){ + if(type!=TraccarHandlerConstants.Types.DEVICE){ + response = client.newCall(request).execute(); + String result = response.body().string(); + + JSONArray fetchAllUsers = new JSONArray(result); + int userAvailability = 0; + for(int i=0; i