Fixed the bugs

feature/traccar-sync
Rushdi Mohamed 3 years ago
parent d1f2bdb296
commit 44ac5c8a8f

@ -16,7 +16,7 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.common.group.mgt;
package org.wso2.carbon.device.mgt.common;
/**
* This class represents a custom exception specified for group management

@ -28,6 +28,7 @@ public class TrackerDeviceInfo implements Serializable {
private int traccarDeviceId;
private int deviceId;
private int tenantId;
private int status;
public TrackerDeviceInfo() {
}
@ -69,4 +70,12 @@ public class TrackerDeviceInfo implements Serializable {
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}

@ -28,6 +28,7 @@ public class TrackerGroupInfo implements Serializable {
private int traccarGroupId;
private int groupId;
private int tenantId;
private int status;
public TrackerGroupInfo() {
}
@ -69,4 +70,12 @@ public class TrackerGroupInfo implements Serializable {
public void setTenantId(int tenantId) {
this.tenantId = tenantId;
}
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
}

@ -27,12 +27,10 @@ import org.wso2.carbon.device.mgt.common.exceptions.UnsupportedDatabaseEngineExc
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.impl.*;
import org.wso2.carbon.device.mgt.core.dao.impl.event.GenericEventConfigDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.GenericDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.OracleDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.PostgreSQLDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.device.SQLServerDeviceDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.event.H2EventConfigDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsDAO;
import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.impl.DeviceDetailsDAOImpl;

@ -23,16 +23,80 @@ import org.wso2.carbon.device.mgt.common.TrackerGroupInfo;
public interface TrackerDAO {
Boolean addTraccarDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException;
/**
* Add new Device.
* @param traccarDeviceId to be added.
* @param deviceId of the device.
* @param tenantId of the group.
* @return boolean value.
* @throws TrackerManagementDAOException
*/
Boolean addTrackerDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException;
int removeTraccarDevice(int deviceId, int tenantId) throws TrackerManagementDAOException;
/**
* get trackerDevice info.
* @param groupId of the device.
* @param tenantId of the group.
* @return Tracker Device Info.
* @throws TrackerManagementDAOException
*/
TrackerDeviceInfo getTrackerDevice(int groupId, int tenantId) throws TrackerManagementDAOException;
TrackerDeviceInfo getTraccarDevice(int groupId, int tenantId) throws TrackerManagementDAOException;
/**
* update trackerDevice status and traccarDeviceId.
* @param traccarDeviceId of the Device.
* @param deviceId of the device.
* @param tenantId of the group.
* @param status of the device.
* @return Tracker Device Info.
* @throws TrackerManagementDAOException
*/
Boolean updateTrackerDeviceIdANDStatus(int traccarDeviceId, int deviceId, int tenantId, int status) throws TrackerManagementDAOException;
Boolean addTraccarGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException;
/**
* Remove a Device.
* @param deviceId of the device.
* @param tenantId of the group.
* @return sql execution result.
* @throws TrackerManagementDAOException
*/
int removeTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException;
int removeTraccarGroup(int id) throws TrackerManagementDAOException;
/**
* Add new Group.
* @param traccarGroupId to be added.
* @param groupId of the group.
* @param tenantId of the group.
* @return boolean value.
* @throws TrackerManagementDAOException
*/
Boolean addTrackerGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException;
TrackerGroupInfo getTraccarGroup(int groupId, int tenantId) throws TrackerManagementDAOException;
/**
* Update status and traccarGroupId of a Group.
* @param traccarGroupId to be added.
* @param groupId of the group.
* @param tenantId of the group.
* @param status of the group.
* @return boolean value.
* @throws TrackerManagementDAOException
*/
Boolean updateTrackerGroupIdANDStatus(int traccarGroupId, int groupId, int tenantId, int status) throws TrackerManagementDAOException;
/**
* Remove a Group.
* @param id mapping table.
* @throws TrackerManagementDAOException
*/
int removeTrackerGroup(int id) throws TrackerManagementDAOException;
/**
* get trackerGroup info.
* @param groupId of the device.
* @param tenantId of the group.
* @return Tracker Device Info.
* @throws TrackerManagementDAOException
*/
TrackerGroupInfo getTrackerGroup(int groupId, int tenantId) throws TrackerManagementDAOException;
}

@ -26,7 +26,7 @@ import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementExcepti
import org.wso2.carbon.device.mgt.common.exceptions.UnsupportedDatabaseEngineException;
import org.wso2.carbon.device.mgt.core.config.datasource.DataSourceConfig;
import org.wso2.carbon.device.mgt.core.config.datasource.JNDILookupDefinition;
import org.wso2.carbon.device.mgt.core.dao.impl.TrackerDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.impl.tracker.TrackerDAOImpl;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil;
import javax.sql.DataSource;

@ -16,7 +16,7 @@
* under the License.
*
*/
package org.wso2.carbon.device.mgt.core.dao.impl;
package org.wso2.carbon.device.mgt.core.dao.impl.tracker;
import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo;
import org.wso2.carbon.device.mgt.common.TrackerGroupInfo;
@ -33,7 +33,7 @@ import java.sql.SQLException;
public class TrackerDAOImpl implements TrackerDAO {
@Override
public Boolean addTraccarDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException {
public Boolean addTrackerDevice(int traccarDeviceId, int deviceId, int tenantId) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = TrackerManagementDAOFactory.getConnection();
@ -53,7 +53,29 @@ public class TrackerDAOImpl implements TrackerDAO {
}
@Override
public int removeTraccarDevice(int deviceId, int tenantId) throws TrackerManagementDAOException {
public Boolean updateTrackerDeviceIdANDStatus(int traccarDeviceId, int deviceId, int tenantId, int status) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = TrackerManagementDAOFactory.getConnection();
String sql = "UPDATE DM_EXT_DEVICE_MAPPING SET STATUS=?, TRACCAR_DEVICE_ID=? WHERE DEVICE_ID=? AND TENANT_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, status);
stmt.setInt(2, traccarDeviceId);
stmt.setInt(3, deviceId);
stmt.setInt(4, tenantId);
stmt.execute();
return true;
} catch (SQLException e) {
String msg = "Error occurred while updating trackerDevice mapping table";
throw new TrackerManagementDAOException(msg, e);
} finally {
TrackerManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public int removeTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
int status = -1;
@ -70,20 +92,20 @@ public class TrackerDAOImpl implements TrackerDAO {
}
return status;
} catch (SQLException e) {
throw new TrackerManagementDAOException("Error occurred while removing traccar device", e);
throw new TrackerManagementDAOException("Error occurred while removing trackerDevice", e);
} finally {
TrackerManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public TrackerDeviceInfo getTraccarDevice(int deviceId, int tenantId) throws TrackerManagementDAOException {
public TrackerDeviceInfo getTrackerDevice(int deviceId, int tenantId) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
TrackerDeviceInfo trackerDeviceInfo = null;
try {
Connection conn = TrackerManagementDAOFactory.getConnection();
String sql = "SELECT ID, TRACCAR_DEVICE_ID, DEVICE_ID, TENANT_ID FROM DM_EXT_DEVICE_MAPPING WHERE " +
String sql = "SELECT ID, TRACCAR_DEVICE_ID, DEVICE_ID, TENANT_ID, STATUS FROM DM_EXT_DEVICE_MAPPING WHERE " +
"DEVICE_ID = ? AND TENANT_ID = ? ORDER BY ID DESC LIMIT 1";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, deviceId);
@ -94,23 +116,24 @@ public class TrackerDAOImpl implements TrackerDAO {
}
return trackerDeviceInfo;
} catch (SQLException e) {
throw new TrackerManagementDAOException("Error occurred while retrieving the traccar device information ", e);
throw new TrackerManagementDAOException("Error occurred while retrieving the trackerDevice information ", e);
} finally {
TrackerManagementDAOUtil.cleanupResources(stmt, rs);
}
}
@Override
public Boolean addTraccarGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException {
public Boolean addTrackerGroup(int traccarGroupId, int groupId, int tenantId) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
int status = 1 ;
try {
Connection conn = TrackerManagementDAOFactory.getConnection();
String sql = "INSERT INTO DM_EXT_GROUP_MAPPING(TRACCAR_GROUP_ID, GROUP_ID, TENANT_ID) VALUES(?, ?, ?)";
String sql = "INSERT INTO DM_EXT_GROUP_MAPPING(TRACCAR_GROUP_ID, GROUP_ID, TENANT_ID, STATUS) VALUES(?, ?, ?, ?)";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, traccarGroupId);
stmt.setInt(2, groupId);
stmt.setInt(3, tenantId);
stmt.setInt(4, status);
stmt.execute();
return true;
@ -123,7 +146,29 @@ public class TrackerDAOImpl implements TrackerDAO {
}
@Override
public int removeTraccarGroup(int id) throws TrackerManagementDAOException {
public Boolean updateTrackerGroupIdANDStatus(int traccarGroupId, int groupId, int tenantId, int status) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
try {
Connection conn = TrackerManagementDAOFactory.getConnection();
String sql = "UPDATE DM_EXT_GROUP_MAPPING SET STATUS=?, TRACCAR_GROUP_ID=? WHERE GROUP_ID=? AND TENANT_ID=?";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, status);
stmt.setInt(2, traccarGroupId);
stmt.setInt(3, groupId);
stmt.setInt(4, tenantId);
stmt.execute();
return true;
} catch (SQLException e) {
String msg = "Error occurred while updating traccar group mapping table";
throw new TrackerManagementDAOException(msg, e);
} finally {
TrackerManagementDAOUtil.cleanupResources(stmt, null);
}
}
@Override
public int removeTrackerGroup(int id) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
int status = -1;
@ -146,14 +191,14 @@ public class TrackerDAOImpl implements TrackerDAO {
}
@Override
public TrackerGroupInfo getTraccarGroup(int groupId, int tenantId) throws TrackerManagementDAOException {
public TrackerGroupInfo getTrackerGroup(int groupId, int tenantId) throws TrackerManagementDAOException {
PreparedStatement stmt = null;
ResultSet rs = null;
TrackerGroupInfo trackerGroupInfo = null;
try {
Connection conn = TrackerManagementDAOFactory.getConnection();
String sql = "SELECT ID, TRACCAR_GROUP_ID, GROUP_ID, TENANT_ID FROM DM_EXT_GROUP_MAPPING WHERE " +
"GROUP_ID = ? AND TENANT_ID = ?";
String sql = "SELECT ID, TRACCAR_GROUP_ID, GROUP_ID, TENANT_ID, STATUS FROM DM_EXT_GROUP_MAPPING WHERE " +
"GROUP_ID = ? AND TENANT_ID = ? ORDER BY ID DESC LIMIT 1";
stmt = conn.prepareStatement(sql);
stmt.setInt(1, groupId);
stmt.setInt(2, tenantId);
@ -175,6 +220,7 @@ public class TrackerDAOImpl implements TrackerDAO {
trackerGroupInfo.setTraccarGroupId(rs.getInt("TRACCAR_GROUP_ID"));
trackerGroupInfo.setGroupId(rs.getInt("GROUP_ID"));
trackerGroupInfo.setTenantId(rs.getInt("TENANT_ID"));
trackerGroupInfo.setStatus(rs.getInt("STATUS"));
return trackerGroupInfo;
}
@ -184,6 +230,7 @@ public class TrackerDAOImpl implements TrackerDAO {
trackerDeviceInfo.setTraccarDeviceId(rs.getInt("TRACCAR_DEVICE_ID"));
trackerDeviceInfo.setDeviceId(rs.getInt("DEVICE_ID"));
trackerDeviceInfo.setTenantId(rs.getInt("TENANT_ID"));
trackerDeviceInfo.setStatus(rs.getInt("STATUS"));
return trackerDeviceInfo;
}
}

@ -386,12 +386,12 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
// );
}
//Traccar update GPS Location
//Tracker update GPS Location
if (HttpReportingUtil.isLocationPublishing() && HttpReportingUtil.isTrackerEnabled()) {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.updateLocation(device, deviceLocation, CarbonContext.getThreadLocalCarbonContext().getTenantId());
}
//Traccar update GPS Location
//Tracker update GPS Location
DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {

@ -80,7 +80,7 @@ import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl;
import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.TraccarAPIClientServiceImpl;
import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl;
import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.DeviceMgtTenantMgtListener;
@ -307,7 +307,7 @@ public class DeviceManagementServiceComponent {
bundleContext.registerService(DeviceManagementProviderService.class.getName(), deviceManagementProvider, null);
/* Registering Device API Client Service */
DeviceAPIClientService deviceAPIClientService = new TraccarAPIClientServiceImpl();
DeviceAPIClientService deviceAPIClientService = new DeviceAPIClientServiceImpl();
DeviceManagementDataHolder.getInstance().setDeviceAPIClientService(deviceAPIClientService);
bundleContext.registerService(DeviceAPIClientService.class.getName(), deviceAPIClientService, null);

@ -43,7 +43,6 @@ import org.wso2.carbon.context.CarbonContext;
import org.wso2.carbon.context.PrivilegedCarbonContext;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.TrackerGroupInfo;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceNotFoundException;
import org.wso2.carbon.device.mgt.common.GroupPaginationRequest;
@ -55,20 +54,16 @@ import org.wso2.carbon.device.mgt.common.group.mgt.GroupAlreadyExistException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.GroupNotExistException;
import org.wso2.carbon.device.mgt.common.group.mgt.RoleDoesNotExistException;
import org.wso2.carbon.device.mgt.common.group.mgt.TrackerAlreadyExistException;
import org.wso2.carbon.device.mgt.core.dao.DeviceDAO;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.GroupDAO;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOException;
import org.wso2.carbon.device.mgt.core.dao.GroupManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.TrackerDAO;
import org.wso2.carbon.device.mgt.core.dao.TrackerManagementDAOException;
import org.wso2.carbon.device.mgt.core.event.config.GroupAssignmentEventOperationExecutor;
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.device.mgt.core.util.HttpReportingUtil;
import org.wso2.carbon.user.api.UserRealm;

@ -17,9 +17,9 @@
* under the License.
*/
package org.wso2.carbon.device.mgt.core.traccar.common;
package org.wso2.carbon.device.mgt.core.traccar.api.service;
import org.wso2.carbon.device.mgt.common.group.mgt.TrackerAlreadyExistException;
import org.wso2.carbon.device.mgt.common.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;

@ -19,7 +19,6 @@
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;
@ -28,16 +27,14 @@ import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONException;
import org.json.JSONObject;
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.group.mgt.TrackerAlreadyExistException;
import org.wso2.carbon.device.mgt.common.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.common.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;
@ -55,8 +52,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class TrackerClient implements TraccarClient {
private static final Log log = LogFactory.getLog(TrackerClient.class);
public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.api.service.TraccarClient {
private static final Log log = LogFactory.getLog(TraccarClient.class);
private static final int THREAD_POOL_SIZE = 50;
private final OkHttpClient client;
private final ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
@ -70,7 +67,7 @@ public class TrackerClient implements TraccarClient {
private final TrackerDAO trackerDAO;
public TrackerClient() {
public TraccarClient() {
client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS)
.writeTimeout(30, TimeUnit.SECONDS)
@ -82,6 +79,7 @@ public class TrackerClient implements TraccarClient {
private class TrackerExecutor implements Runnable {
final int deviceId;
final int groupId;
final int tenantId;
final JSONObject payload;
final String context;
@ -89,9 +87,10 @@ public class TrackerClient implements TraccarClient {
private final String method;
private final String type;
private TrackerExecutor(int deviceId, int tenantId, String publisherUrl, String context, JSONObject payload,
private TrackerExecutor(int id, int tenantId, String publisherUrl, String context, JSONObject payload,
String method, String type) {
this.deviceId = deviceId;
this.deviceId = id;
this.groupId = id;
this.tenantId = tenantId;
this.payload = payload;
this.context = context;
@ -122,14 +121,23 @@ public class TrackerClient implements TraccarClient {
response = client.newCall(request).execute();
if(method==TraccarHandlerConstants.Methods.POST){
JSONObject obj = new JSONObject(response.body().string());
response.close();
if (obj != null){
int traccarId = obj.getInt("id");
try {
TrackerManagementDAOFactory.beginTransaction();
if(type==TraccarHandlerConstants.Types.DEVICE){
trackerDAO.addTraccarDevice(traccarId, deviceId, tenantId);
trackerDAO.addTrackerDevice(traccarId, deviceId, tenantId);
TrackerDeviceInfo res = trackerDAO.getTrackerDevice(deviceId, tenantId);
if(res.getStatus()==0){
trackerDAO.updateTrackerDeviceIdANDStatus(res.getTraccarDeviceId(), deviceId, tenantId, 1);
}
}else if(type==TraccarHandlerConstants.Types.GROUP){
trackerDAO.addTraccarGroup(traccarId, deviceId, tenantId);
trackerDAO.addTrackerGroup(traccarId, groupId, tenantId);
TrackerGroupInfo res = trackerDAO.getTrackerGroup(groupId, tenantId);
if(res.getStatus()==0){
trackerDAO.updateTrackerGroupIdANDStatus(res.getTraccarGroupId(), groupId, tenantId, 1);
}
}
TrackerManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
@ -154,7 +162,7 @@ public class TrackerClient implements TraccarClient {
log.debug("Successfully the request is proceed and communicated with Traccar");
}
} catch (IOException e) {
log.error("Error occurred", e);
log.error("Couldnt connect to traccar.", e);
}
}
}
@ -169,9 +177,11 @@ public class TrackerClient implements TraccarClient {
TrackerDeviceInfo res = null;
try {
TrackerManagementDAOFactory.openConnection();
res = trackerDAO.getTraccarDevice(deviceInfo.getId(), tenantId);
res = trackerDAO.getTrackerDevice(deviceInfo.getId(), tenantId);
if(res!=null){
throw new TrackerAlreadyExistException("The device already exit");
String msg = "The device already exit";
log.error(msg);
throw new TrackerAlreadyExistException(msg);
}
} catch (TrackerManagementDAOException e) {
String msg = "Error occurred while mapping with deviceId .";
@ -193,7 +203,7 @@ public class TrackerClient implements TraccarClient {
}
/**
* Add Traccar Device operation.
* update Traccar Device operation.
* @param deviceInfo with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId
* Model, Contact, Category, fenceIds
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
@ -202,7 +212,7 @@ public class TrackerClient implements TraccarClient {
TrackerDeviceInfo res = null;
try {
TrackerManagementDAOFactory.openConnection();
res = trackerDAO.getTraccarDevice(deviceInfo.getId(), tenantId);
res = trackerDAO.getTrackerDevice(deviceInfo.getId(), tenantId);
} catch (TrackerManagementDAOException e) {
String msg = "Error occurred while mapping with deviceId .";
log.error(msg, e);
@ -215,13 +225,12 @@ public class TrackerClient implements TraccarClient {
TrackerManagementDAOFactory.closeConnection();
}
if(res==null){
if ((res==null) || (res.getTraccarDeviceId()==0)){
try {
TrackerClient trackerClient = new TrackerClient();
TraccarDevice device = deviceInfo;
String lastUpdatedTime = String.valueOf((new Date().getTime()));
device.setLastUpdate(lastUpdatedTime);
trackerClient.addDevice(deviceInfo, tenantId);
addDevice(deviceInfo, tenantId);
} catch (TraccarConfigurationException e) {
String msg = "Error occurred while mapping with groupId";
log.error(msg, e);
@ -231,6 +240,21 @@ public class TrackerClient implements TraccarClient {
log.error(msg, e);
throw new TrackerAlreadyExistException(msg, e);
}
}else if (res!=null && (res.getTraccarDeviceId()!=0 && res.getStatus()==0)){
//update the traccarGroupId and status
try {
TrackerManagementDAOFactory.beginTransaction();
trackerDAO.updateTrackerDeviceIdANDStatus(res.getTraccarDeviceId(), deviceInfo.getId(), tenantId, 1);
TrackerManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Error occurred establishing the DB connection .";
log.error(msg, e);
} catch (TrackerManagementDAOException e) {
String msg="Could not add the traccar group";
log.error(msg, e);
} finally{
TrackerManagementDAOFactory.closeConnection();
}
}else{
JSONObject payload = payload(deviceInfo);
String context = defaultPort+"/api/devices";
@ -267,7 +291,7 @@ public class TrackerClient implements TraccarClient {
TrackerDeviceInfo res = null;
try {
TrackerManagementDAOFactory.openConnection();
res = trackerDAO.getTraccarDevice(device.getId(), tenantId);
res = trackerDAO.getTrackerDevice(device.getId(), tenantId);
} catch (SQLException e) {
String msg = "Error occurred establishing the DB connection .";
log.error(msg, e);
@ -280,13 +304,7 @@ public class TrackerClient implements TraccarClient {
if (res == null){
try {
TrackerClient trackerClient = new TrackerClient();
trackerClient.addDevice(device, tenantId);
log.info("====================================");
log.info(device);
log.info(trackerClient);
log.info(tenantId);
log.info("====================================");
addDevice(device, tenantId);
} catch (TraccarConfigurationException e) {
String msg = "Error occurred while mapping with groupId";
log.error(msg, e);
@ -314,17 +332,18 @@ public class TrackerClient implements TraccarClient {
* @throws TraccarConfigurationException Failed while dis-enroll a Traccar Device operation
*/
public void disEndrollDevice(int deviceId, int tenantId) throws TraccarConfigurationException {
TrackerDeviceInfo res = null;
JSONObject obj = null;
try {
TrackerManagementDAOFactory.beginTransaction();
TrackerDeviceInfo res = trackerDAO.getTraccarDevice(deviceId, tenantId);
JSONObject obj = new JSONObject(res);
trackerDAO.removeTraccarDevice(deviceId, tenantId);
String context = defaultPort+"/api/devices/"+obj.getInt("traccarDeviceId");
Runnable trackerExecutor = new TrackerExecutor(obj.getInt("traccarDeviceId"), tenantId, endpoint, context, null,
TraccarHandlerConstants.Methods.DELETE, TraccarHandlerConstants.Types.DEVICE);
executor.execute(trackerExecutor);
TrackerManagementDAOFactory.commitTransaction();
res = trackerDAO.getTrackerDevice(deviceId, tenantId);
if(res!=null){
obj = new JSONObject(res);
if(obj!=null){
trackerDAO.removeTrackerDevice(deviceId, tenantId);
TrackerManagementDAOFactory.commitTransaction();
}
}
} catch (TransactionManagementException e) {
TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred establishing the DB connection";
@ -336,6 +355,11 @@ public class TrackerClient implements TraccarClient {
} finally {
TrackerManagementDAOFactory.closeConnection();
}
String context = defaultPort+"/api/devices/"+obj.getInt("traccarDeviceId");
Runnable trackerExecutor = new TrackerExecutor(obj.getInt("traccarDeviceId"), tenantId, endpoint, context, null,
TraccarHandlerConstants.Methods.DELETE, TraccarHandlerConstants.Types.DEVICE);
executor.execute(trackerExecutor);
}
/**
@ -344,12 +368,14 @@ public class TrackerClient implements TraccarClient {
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
*/
public void addGroup(TraccarGroups groupInfo, int groupId, int tenantId) throws TraccarConfigurationException, TrackerAlreadyExistException {
TrackerGroupInfo res = null;
try {
TrackerManagementDAOFactory.openConnection();
TrackerGroupInfo res = trackerDAO.getTraccarGroup(groupId, tenantId);
if(res!=null){
throw new TrackerAlreadyExistException("The group already exit");
res = trackerDAO.getTrackerGroup(groupId, tenantId);
if (res!=null){
String msg = "The group already exit";
log.error(msg);
throw new TrackerAlreadyExistException(msg);
}
} catch (TrackerManagementDAOException e) {
String msg = "Error occurred while mapping with deviceId .";
@ -363,18 +389,20 @@ public class TrackerClient implements TraccarClient {
TrackerManagementDAOFactory.closeConnection();
}
JSONObject payload = new JSONObject();
payload.put("name", groupInfo.getName());
payload.put("attributes", new JSONObject());
if (res==null){
JSONObject payload = new JSONObject();
payload.put("name", groupInfo.getName());
payload.put("attributes", new JSONObject());
String context = defaultPort+"/api/groups";
Runnable trackerExecutor = new TrackerExecutor(groupId, tenantId, endpoint, context, payload,
TraccarHandlerConstants.Methods.POST, TraccarHandlerConstants.Types.GROUP);
executor.execute(trackerExecutor);
String context = defaultPort+"/api/groups";
Runnable trackerExecutor = new TrackerExecutor(groupId, tenantId, endpoint, context, payload,
TraccarHandlerConstants.Methods.POST, TraccarHandlerConstants.Types.GROUP);
executor.execute(trackerExecutor);
}
}
/**
* Add Traccar Device operation.
* update Traccar Group operation.
* @param groupInfo with groupName
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
*/
@ -382,21 +410,21 @@ public class TrackerClient implements TraccarClient {
TrackerGroupInfo res = null;
try {
TrackerManagementDAOFactory.openConnection();
res = trackerDAO.getTraccarGroup(groupId, tenantId);
res = trackerDAO.getTrackerGroup(groupId, tenantId);
} catch (SQLException e) {
String msg = "Error occurred establishing the DB connection .";
log.error(msg, e);
} catch (TrackerManagementDAOException e) {
String msg="Could not update the traccar group";
String msg="Could not find traccar group details";
log.error(msg, e);
} finally{
TrackerManagementDAOFactory.closeConnection();
}
if (res == null){
if ((res==null) || (res.getTraccarGroupId()==0)){
//add a new traccar group
try {
TrackerClient trackerClient = new TrackerClient();
trackerClient.addGroup(groupInfo, groupId, tenantId);
addGroup(groupInfo, groupId, tenantId);
} catch (TraccarConfigurationException e) {
String msg = "Error occurred while mapping with groupId";
log.error(msg, e);
@ -406,6 +434,21 @@ public class TrackerClient implements TraccarClient {
log.error(msg, e);
throw new TrackerAlreadyExistException(msg, e);
}
}else if (res!=null && (res.getTraccarGroupId()!=0 && res.getStatus()==0)){
//update the traccargroupId and status
try {
TrackerManagementDAOFactory.beginTransaction();
trackerDAO.updateTrackerGroupIdANDStatus(res.getTraccarGroupId(), groupId, tenantId, 1);
TrackerManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) {
String msg = "Error occurred establishing the DB connection .";
log.error(msg, e);
} catch (TrackerManagementDAOException e) {
String msg="Could not add the traccar group";
log.error(msg, e);
} finally{
TrackerManagementDAOFactory.closeConnection();
}
}else{
JSONObject obj = new JSONObject(res);
JSONObject payload = new JSONObject();
@ -426,17 +469,18 @@ public class TrackerClient implements TraccarClient {
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
*/
public void deleteGroup(int groupId, int tenantId) throws TraccarConfigurationException {
TrackerGroupInfo res = null;
JSONObject obj = null;
try {
TrackerManagementDAOFactory.beginTransaction();
TrackerGroupInfo res = trackerDAO.getTraccarGroup(groupId, tenantId);
JSONObject obj = new JSONObject(res);
trackerDAO.removeTraccarGroup(obj.getInt("id"));
String context = defaultPort+"/api/groups/"+obj.getInt("traccarGroupId");
Runnable trackerExecutor = new TrackerExecutor(obj.getInt("traccarGroupId"), tenantId, endpoint, context,
null, TraccarHandlerConstants.Methods.DELETE, TraccarHandlerConstants.Types.GROUP);
executor.execute(trackerExecutor);
TrackerManagementDAOFactory.commitTransaction();
res = trackerDAO.getTrackerGroup(groupId, tenantId);
if(res!=null){
obj = new JSONObject(res);
if(obj!=null){
trackerDAO.removeTrackerGroup(obj.getInt("id"));
TrackerManagementDAOFactory.commitTransaction();
}
}
} catch (TransactionManagementException e) {
TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred establishing the DB connection";
@ -448,6 +492,13 @@ public class TrackerClient implements TraccarClient {
} finally {
TrackerManagementDAOFactory.closeConnection();
}
if(obj!=null){
String context = defaultPort+"/api/groups/"+obj.getInt("traccarGroupId");
Runnable trackerExecutor = new TrackerExecutor(obj.getInt("traccarGroupId"), tenantId, endpoint, context,
null, TraccarHandlerConstants.Methods.DELETE, TraccarHandlerConstants.Types.GROUP);
executor.execute(trackerExecutor);
}
}
private TraccarGateway getTraccarGateway(){

@ -24,9 +24,9 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup;
import org.wso2.carbon.device.mgt.common.group.mgt.TrackerAlreadyExistException;
import org.wso2.carbon.device.mgt.common.TrackerAlreadyExistException;
import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.addons.TrackerClient;
import org.wso2.carbon.device.mgt.core.traccar.api.service.addons.TraccarClient;
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;
@ -34,12 +34,12 @@ import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfiguratio
import java.util.Date;
public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
private static final Log log = LogFactory.getLog(TraccarAPIClientServiceImpl.class);
private static final Log log = LogFactory.getLog(DeviceAPIClientServiceImpl.class);
public void addDevice(Device device, int tenantId) {
TrackerClient client = new TrackerClient();
TraccarClient client = new TraccarClient();
String lastUpdatedTime = String.valueOf((new Date().getTime()));
TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getName(), device.getDeviceIdentifier(),
"online", "false", lastUpdatedTime, "", "", "", "",
@ -56,7 +56,7 @@ public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
}
public void updateDevice(Device device, int tenantId) {
TrackerClient client = new TrackerClient();
TraccarClient client = new TraccarClient();
String lastUpdatedTime = String.valueOf((new Date().getTime()));
TraccarDevice traccarDeviceInfo = new TraccarDevice(device.getId(), device.getName(), device.getDeviceIdentifier(),
"online", "false", lastUpdatedTime, "", "", "", "",
@ -73,7 +73,7 @@ public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
}
public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) {
TrackerClient client = new TrackerClient();
TraccarClient client = new TraccarClient();
TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(),
deviceLocation.getUpdatedTime().getTime(),
deviceLocation.getLatitude(), deviceLocation.getLongitude(),
@ -95,7 +95,7 @@ public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
}
public void disEndrollDevice(int deviceId, int tenantId) {
TrackerClient client = new TrackerClient();
TraccarClient client = new TraccarClient();
try {
client.disEndrollDevice(deviceId, tenantId);
} catch (TraccarConfigurationException e) {
@ -105,7 +105,7 @@ public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
}
public void addGroup(DeviceGroup group, int groupId, int tenantId) {
TrackerClient client = new TrackerClient();
TraccarClient client = new TraccarClient();
TraccarGroups traccarGroups = new TraccarGroups(group.getName());
try {
client.addGroup(traccarGroups, groupId, tenantId);
@ -119,7 +119,7 @@ public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
}
public void updateGroup(DeviceGroup group, int groupId, int tenantId) {
TrackerClient client = new TrackerClient();
TraccarClient client = new TraccarClient();
TraccarGroups traccarGroups = new TraccarGroups(group.getName());
try {
client.updateGroup(traccarGroups, groupId, tenantId);
@ -133,7 +133,7 @@ public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
}
public void deleteGroup(int groupId, int tenantId) {
TrackerClient client = new TrackerClient();
TraccarClient client = new TraccarClient();
try {
client.deleteGroup(groupId, tenantId);
} catch (TraccarConfigurationException e) {
Loading…
Cancel
Save