pull/5/head
shamalka 2 years ago
parent 90793b7130
commit f840ee97a8

@ -105,7 +105,7 @@ public interface DeviceDetailsDAO {
* @param deviceId - id of the device. * @param deviceId - id of the device.
* @param enrollmentId - enrolment id of the device. * @param enrollmentId - enrolment id of the device.
* @return - if device location exist * @return - if device location exist
* @throws DeviceDetailsMgtDAOException * @throws DeviceDetailsMgtDAOException if SQL error occurred while processing the query.
*/ */
boolean hasLocations(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException; boolean hasLocations(int deviceId, int enrollmentId) throws DeviceDetailsMgtDAOException;

@ -25,6 +25,7 @@ import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.device.mgt.common.Device; import org.wso2.carbon.device.mgt.common.Device;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
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.DeviceManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.dao.util.DeviceManagementDAOUtil; 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.DeviceDetailsDAO;
@ -381,29 +382,21 @@ public class DeviceDetailsDAOImpl implements DeviceDetailsDAO {
@Override @Override
public boolean hasLocations(int deviceId, int enrollmentId) throws public boolean hasLocations(int deviceId, int enrollmentId) throws
DeviceDetailsMgtDAOException { DeviceDetailsMgtDAOException {
Connection conn;
PreparedStatement stmt = null;
ResultSet rs = null;
boolean hasLocation = false;
try { try {
conn = this.getConnection(); Connection conn = this.getConnection();
String sql = "SELECT DEVICE_ID FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? " + String sql = "SELECT DEVICE_ID FROM DM_DEVICE_LOCATION WHERE DEVICE_ID = ? AND ENROLMENT_ID = ? " +
"LIMIT 1"; "LIMIT 1";
stmt = conn.prepareStatement(sql); ResultSet rs;
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
stmt.setInt(1, deviceId); stmt.setInt(1, deviceId);
stmt.setInt(2, enrollmentId); stmt.setInt(2, enrollmentId);
rs = stmt.executeQuery(); rs = stmt.executeQuery();
return rs.next();
if (rs.next()) {
hasLocation = true;
} }
return hasLocation;
} catch (SQLException e) { } catch (SQLException e) {
throw new DeviceDetailsMgtDAOException("Error occurred while fetching the location of the registered devices.", e); String msg = "Error occurred while fetching the location of the registered devices.";
} finally { log.error(msg, e);
DeviceManagementDAOUtil.cleanupResources(stmt, rs); throw new DeviceDetailsMgtDAOException(msg, e);
} }
} }

@ -367,7 +367,6 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
if (previousLocation == null) { if (previousLocation == null) {
deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); deviceDetailsDAO.addDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId());
} else { } else {
log.info("Update location on IOTS");
deviceDetailsDAO.updateDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId()); deviceDetailsDAO.updateDeviceLocation(deviceLocation, device.getEnrolmentInfo().getId());
} }
deviceDetailsDAO.addDeviceLocationInfo(device, deviceLocation, deviceDetailsDAO.addDeviceLocationInfo(device, deviceLocation,
@ -404,12 +403,16 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} }
} else { } else {
if(!HttpReportingUtil.isLocationPublishing()) { if(!HttpReportingUtil.isLocationPublishing()) {
log.info("Location publishing is disabled"); if (log.isDebugEnabled()) {
log.debug("Location publishing is disabled");
}
} }
if (!HttpReportingUtil.isTrackerEnabled()) { if (!HttpReportingUtil.isTrackerEnabled()) {
if (log.isDebugEnabled()) {
log.info("Traccar is disabled"); log.info("Traccar is disabled");
} }
} }
}
//Tracker update GPS Location //Tracker update GPS Location
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
@ -456,21 +459,25 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
} catch (ExecutionException e) { } catch (ExecutionException e) {
log.error("ExecutionException : " + e); log.error("ExecutionException : " + e);
//throw new RuntimeException(e); //throw new RuntimeException(e);
//Exception was not thrown due to being conflicted with non-traccar features // NOTE: Exception was not thrown due to being conflicted with non-traccar features
} catch (InterruptedException e) { } catch (InterruptedException e) {
log.error("InterruptedException : " + e); log.error("InterruptedException : " + e);
//throw new RuntimeException(e); //throw new RuntimeException(e);
//Exception was not thrown due to being conflicted with non-traccar features // NOTE: Exception was not thrown due to being conflicted with non-traccar features
} }
} else { } else {
if(!HttpReportingUtil.isLocationPublishing()) { if(!HttpReportingUtil.isLocationPublishing()) {
log.info("Location publishing is disabled"); if (log.isDebugEnabled()) {
log.debug("Location publishing is disabled");
}
} }
if (!HttpReportingUtil.isTrackerEnabled()) { if (!HttpReportingUtil.isTrackerEnabled()) {
if (log.isDebugEnabled()) {
log.info("Traccar is disabled"); log.info("Traccar is disabled");
} }
} }
} }
}
DeviceManagementDAOFactory.commitTransaction(); DeviceManagementDAOFactory.commitTransaction();
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {

@ -3980,13 +3980,14 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
deviceLocation.setSpeed(Float.parseFloat(speed)); deviceLocation.setSpeed(Float.parseFloat(speed));
deviceLocation.setBearing(Float.parseFloat(bearing)); deviceLocation.setBearing(Float.parseFloat(bearing));
// deviceInformationManager.addDeviceLocation(device, deviceLocation); // deviceInformationManager.addDeviceLocation(device, deviceLocation);
log.info("--- Location Pushing to Traccar starts ---");
deviceInformationManager.addDeviceLocation(device, deviceLocation); deviceInformationManager.addDeviceLocation(device, deviceLocation);
log.info("--- Location Pushing to Traccar end ---");
} catch (DeviceDetailsMgtException e) { } catch (DeviceDetailsMgtException e) {
//We are not failing the execution since this is not critical for the functionality. But logging as /***
// a warning for reference. * NOTE:
//Exception was not thrown due to being conflicted with non-traccar features * We are not failing the execution since this is not critical for the functionality. But logging as
* a warning for reference.
* Exception was not thrown due to being conflicted with non-traccar features
*/
log.warn("Error occurred while trying to add '" + device.getType() + "' device '" + log.warn("Error occurred while trying to add '" + device.getType() + "' device '" +
device.getDeviceIdentifier() + "' (id:'" + device.getId() + "') location (lat:" + latitude + device.getDeviceIdentifier() + "' (id:'" + device.getId() + "') location (lat:" + latitude +
", lon:" + longitude + ", altitude: " + altitude + ", lon:" + longitude + ", altitude: " + altitude +

@ -64,6 +64,8 @@ import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.Future; import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class TraccarClientFactory { public class TraccarClientFactory {
@ -82,6 +84,9 @@ public class TraccarClientFactory {
final String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername(); final String username = PrivilegedCarbonContext.getThreadLocalCarbonContext().getUsername();
private final TrackerDAO trackerDAO; private final TrackerDAO trackerDAO;
/**
* This is the private constructor method as this class is a singleton
*/
private TraccarClientFactory() { private TraccarClientFactory() {
client = new OkHttpClient.Builder() client = new OkHttpClient.Builder()
.connectTimeout(30, TimeUnit.SECONDS) .connectTimeout(30, TimeUnit.SECONDS)
@ -92,6 +97,11 @@ public class TraccarClientFactory {
this.trackerDAO = TrackerManagementDAOFactory.getTrackerDAO(); this.trackerDAO = TrackerManagementDAOFactory.getTrackerDAO();
} }
/**
* This method is used to initiate a singleton instance of this class.
*
* @return TraccarClientFactory instance
*/
public static TraccarClientFactory getInstance() { public static TraccarClientFactory getInstance() {
if(INSTANCE == null) { if(INSTANCE == null) {
INSTANCE = new TraccarClientFactory(); INSTANCE = new TraccarClientFactory();
@ -144,10 +154,9 @@ public class TraccarClientFactory {
} }
public String fetchAllUsers() throws ExecutionException, InterruptedException { public String fetchAllUsers() throws ExecutionException, InterruptedException {
String method = TraccarHandlerConstants.Methods.GET;
String url = defaultPort + "/api/users"; String url = defaultPort + "/api/users";
Future<String> result = executor.submit(new OkHttpClientThreadPool(url, null, method, Future<String> result = executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.GET,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
return result.get(); return result.get();
@ -191,7 +200,6 @@ public class TraccarClientFactory {
traccarUser.setEmail(userName); traccarUser.setEmail(userName);
traccarUser.setPassword(DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.DEFAULT_RANDOM)); traccarUser.setPassword(DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.DEFAULT_RANDOM));
traccarUser.setDeviceLimit(-1); traccarUser.setDeviceLimit(-1);
//traccarUser.setUserLimit(-1);
traccarUser.setExpirationTime(tomorrow.toString()); traccarUser.setExpirationTime(tomorrow.toString());
DeviceAPIClientServiceImpl.createUser(traccarUser); DeviceAPIClientServiceImpl.createUser(traccarUser);
} else { } else {
@ -217,7 +225,6 @@ public class TraccarClientFactory {
} }
DeviceAPIClientServiceImpl.updateUser(traccarUser, obj.getInt("id")); DeviceAPIClientServiceImpl.updateUser(traccarUser, obj.getInt("id"));
} }
result = DeviceAPIClientServiceImpl.fetchUserInfo(userName);
return result; return result;
} catch (InterruptedException | ExecutionException e) { } catch (InterruptedException | ExecutionException e) {
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();
@ -230,22 +237,20 @@ public class TraccarClientFactory {
} }
public String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException { public String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException {
String method = TraccarHandlerConstants.Methods.POST;
String url = defaultPort + "/api/users"; String url = defaultPort + "/api/users";
JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser); JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser);
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method, Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
return res.get(); return res.get();
} }
public String updateUser(TraccarUser traccarUser, int userId) throws ExecutionException, InterruptedException { public String updateUser(TraccarUser traccarUser, int userId) throws ExecutionException, InterruptedException {
String method = TraccarHandlerConstants.Methods.PUT;
String url = defaultPort + "/api/users/" + userId; String url = defaultPort + "/api/users/" + userId;
JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser); JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser);
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method, Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
return res.get(); return res.get();
@ -257,10 +262,9 @@ public class TraccarClientFactory {
payload.put("userId", userId); payload.put("userId", userId);
payload.put("deviceId", deviceId); payload.put("deviceId", deviceId);
String method = TraccarHandlerConstants.Methods.POST;
String url = defaultPort + "/api/permissions"; String url = defaultPort + "/api/permissions";
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method, Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get(); String result = res.get();
@ -284,7 +288,9 @@ public class TraccarClientFactory {
TrackerManagementDAOFactory.closeConnection(); TrackerManagementDAOFactory.closeConnection();
} }
} else { } else {
log.error("Couldn't add the permission record: " + result); String msg = "Couldn't add the permission record: " + result;
log.error(msg);
throw new TrackerManagementDAOException(msg);
} }
} }
@ -294,10 +300,9 @@ public class TraccarClientFactory {
payload.put("userId", userId); payload.put("userId", userId);
payload.put("deviceId", deviceId); payload.put("deviceId", deviceId);
String method = TraccarHandlerConstants.Methods.DELETE;
String url = defaultPort + "/api/permissions"; String url = defaultPort + "/api/permissions";
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method, Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.DELETE,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
@ -319,6 +324,10 @@ public class TraccarClientFactory {
} finally { } finally {
TrackerManagementDAOFactory.closeConnection(); TrackerManagementDAOFactory.closeConnection();
} }
} else {
String msg = "Error occured while removing permission.";
log.error(msg);
throw new TrackerManagementDAOException(msg);
} }
} }
@ -328,7 +337,6 @@ public class TraccarClientFactory {
TrackerManagementDAOFactory.openConnection(); TrackerManagementDAOFactory.openConnection();
return trackerDAO.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList); return trackerDAO.getUserIdofPermissionByUserIdNIdList(userId, NotInDeviceIdList);
} catch (TrackerManagementDAOException e) { } catch (TrackerManagementDAOException e) {
TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while mapping with deviceId ."; String msg = "Error occurred while mapping with deviceId .";
log.error(msg, e); log.error(msg, e);
throw new TrackerManagementDAOException(msg, e); throw new TrackerManagementDAOException(msg, e);
@ -412,7 +420,7 @@ public class TraccarClientFactory {
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get(); String result = res.get();
log.info("---------result--------"); log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar.");
if (res.isDone() && result.charAt(0) == '{') { if (res.isDone() && result.charAt(0) == '{') {
JSONObject obj = new JSONObject(result); JSONObject obj = new JSONObject(result);
if (obj.has("id")) { if (obj.has("id")) {
@ -508,36 +516,50 @@ public class TraccarClientFactory {
List<TrackerPermissionInfo> trackerPermissionInfo = null; List<TrackerPermissionInfo> trackerPermissionInfo = null;
try { try {
TrackerManagementDAOFactory.beginTransaction(); TrackerManagementDAOFactory.beginTransaction();
try {
TrackerManagementDAOFactory.openConnection();
trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId); trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId);
log.info("deviceId - " + deviceId);
if (trackerDeviceInfo != null) { if (trackerDeviceInfo != null) {
trackerDAO.removeTrackerDevice(deviceId, tenantId); trackerDAO.removeTrackerDevice(deviceId, tenantId);
TrackerManagementDAOFactory.commitTransaction(); TrackerManagementDAOFactory.commitTransaction();
trackerPermissionInfo = trackerDAO.getUserIdofPermissionByDeviceId(trackerDeviceInfo.getTraccarDeviceId()); trackerPermissionInfo = trackerDAO.getUserIdofPermissionByDeviceId(trackerDeviceInfo.getTraccarDeviceId());
} else {
String msg = "Tracker device for device id " + deviceId + " not found";
log.error(msg);
throw new TrackerManagementDAOException(msg);
} }
} catch (TransactionManagementException e) { } catch (SQLException e) {
TrackerManagementDAOFactory.rollbackTransaction(); String msg = "Error occurred establishing the DB connection .";
String msg = "Error occurred establishing the DB connection";
log.error(msg, e); log.error(msg, e);
throw new TrackerManagementDAOException(msg, e); throw new TrackerManagementDAOException(msg, e);
} catch (TrackerManagementDAOException e) { } catch (TrackerManagementDAOException e) {
TrackerManagementDAOFactory.rollbackTransaction(); String msg = "Could not add new device location";
String msg = "Error occurred while mapping with deviceId";
log.error(msg, e); log.error(msg, e);
throw new TrackerManagementDAOException(msg, e); throw new TrackerManagementDAOException(msg, e);
} finally { } finally {
TrackerManagementDAOFactory.closeConnection(); TrackerManagementDAOFactory.closeConnection();
} }
log.info("--------Disenrolling Device with device id " + deviceId + " from traccar client--------"); } catch (TransactionManagementException e) {
TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred establishing the DB connection";
log.error(msg, e);
throw new TrackerManagementDAOException(msg, e);
}
log.info("Disenrolling Device with device id " + deviceId + " from traccar client");
//Delete from traccar //Delete from traccar
if (trackerDeviceInfo != null) { if (trackerDeviceInfo != null) {
String method = TraccarHandlerConstants.Methods.DELETE;
if (trackerPermissionInfo.size() > 0) {
String url = defaultPort + "/api/devices/" + trackerPermissionInfo.get(0).getTraccarDeviceId(); String url = defaultPort + "/api/devices/" + trackerPermissionInfo.get(0).getTraccarDeviceId();
executor.submit(new OkHttpClientThreadPool(url, null, method, executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.DELETE,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
} else {
String msg = "Tracker permission mapping info not found";
log.error(msg);
throw new TrackerManagementDAOException(msg);
}
//remove permissions //remove permissions
try { try {
removePermission( removePermission(
@ -545,9 +567,14 @@ public class TraccarClientFactory {
trackerPermissionInfo.get(0).getTraccarDeviceId(), trackerPermissionInfo.get(0).getTraccarDeviceId(),
TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE); TraccarHandlerConstants.Types.REMOVE_TYPE_MULTIPLE);
} catch (ExecutionException e) { } catch (ExecutionException e) {
log.error("ExecutionException : " + e); String msg = "Error occured while removing tacker permissions ";
throw new ExecutionException(e); log.error(msg);
throw new ExecutionException(msg, e);
} }
} else {
String msg = "Tracker device not found";
log.error(msg);
throw new TrackerManagementDAOException(msg);
} }
} }
@ -584,36 +611,35 @@ public class TraccarClientFactory {
payload.put("name", groupInfo.getName()); payload.put("name", groupInfo.getName());
payload.put("attributes", new JSONObject()); payload.put("attributes", new JSONObject());
String method = TraccarHandlerConstants.Methods.POST;
String url = defaultPort + "/api/groups"; String url = defaultPort + "/api/groups";
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method, Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.POST,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get(); String result = res.get();
log.info("---------result--------"); log.info("Group " + trackerGroupInfo.getGroupId() + " has been added to Traccar.");
if (res.isDone() && result.charAt(0) == '{') { if (res.isDone() && result.charAt(0) == '{') {
JSONObject obj = new JSONObject(result); JSONObject obj = new JSONObject(result);
if (obj.has("id")) { if (obj.has("id")) {
int traccarGroupId = obj.getInt("id"); int traccarGroupId = obj.getInt("id");
try { try {
TrackerManagementDAOFactory.beginTransaction(); TrackerManagementDAOFactory.beginTransaction();
trackerDAO.addTrackerGroup(traccarGroupId, groupId, tenantId); trackerDAO.addTrackerGroup(traccarGroupId, groupId, tenantId);
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId); trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
if (trackerGroupInfo.getStatus() == 0) { if (trackerGroupInfo.getStatus() == 0) {
trackerDAO.updateTrackerGroupIdANDStatus(trackerGroupInfo.getTraccarGroupId(), groupId, tenantId, 1); trackerDAO.updateTrackerGroupIdANDStatus(trackerGroupInfo.getTraccarGroupId(), groupId, tenantId, 1);
TrackerManagementDAOFactory.commitTransaction();
} }
} catch (TrackerManagementDAOException e) {
TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while mapping with deviceId. ";
log.error(msg, e);
throw new TrackerManagementDAOException(msg, e);
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
String msg = "Error occurred establishing the DB connection. "; String msg = "Error occurred establishing the DB connection. ";
log.error(msg, e); log.error(msg, e);
TrackerManagementDAOFactory.rollbackTransaction(); TrackerManagementDAOFactory.rollbackTransaction();
throw new TrackerManagementDAOException(msg, e); throw new TrackerManagementDAOException(msg, e);
} catch (TrackerManagementDAOException e) {
TrackerManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while mapping with deviceId. ";
log.error(msg, e);
throw new TrackerManagementDAOException(msg, e);
} finally { } finally {
TrackerManagementDAOFactory.closeConnection(); TrackerManagementDAOFactory.closeConnection();
} }
@ -680,10 +706,9 @@ public class TraccarClientFactory {
payload.put("name", groupInfo.getName()); payload.put("name", groupInfo.getName());
payload.put("attributes", new JSONObject()); payload.put("attributes", new JSONObject());
String method = TraccarHandlerConstants.Methods.PUT;
String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId"); String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId");
executor.submit(new OkHttpClientThreadPool(url, payload, method, executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
} }
@ -705,14 +730,20 @@ public class TraccarClientFactory {
obj = new JSONObject(res); obj = new JSONObject(res);
if (obj != null) { if (obj != null) {
trackerDAO.removeTrackerGroup(obj.getInt("id")); trackerDAO.removeTrackerGroup(obj.getInt("id"));
TrackerManagementDAOFactory.commitTransaction();
String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId"); String url = defaultPort + "/api/groups/" + obj.getInt("traccarGroupId");
executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.DELETE, executor.submit(new OkHttpClientThreadPool(url, null, TraccarHandlerConstants.Methods.DELETE,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
TrackerManagementDAOFactory.commitTransaction();
} else {
String msg = "Tracker group JSON object is null";
log.error(msg);
throw new TrackerManagementDAOException(msg);
} }
} else {
String msg = "Tracker group not found";
log.error(msg);
throw new TrackerManagementDAOException(msg);
} }
} catch (TransactionManagementException e) { } catch (TransactionManagementException e) {
TrackerManagementDAOFactory.rollbackTransaction(); TrackerManagementDAOFactory.rollbackTransaction();
@ -732,10 +763,7 @@ public class TraccarClientFactory {
public String generateRandomString(int len) { public String generateRandomString(int len) {
String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; String chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
Random rnd = new Random(); Random rnd = new Random();
StringBuilder sb = new StringBuilder(len); return IntStream.range(0, len).mapToObj(i -> String.valueOf(chars.charAt(rnd.nextInt(chars.length())))).collect(Collectors.joining());
for (int i = 0; i < len; i++)
sb.append(chars.charAt(rnd.nextInt(chars.length())));
return sb.toString();
} }
private TraccarGateway getTraccarGateway() { private TraccarGateway getTraccarGateway() {
@ -756,10 +784,10 @@ public class TraccarClientFactory {
} }
public String serverUrl(String serverUrl) { public String serverUrl(String serverUrl) {
String newServerUri = endpoint;
if (serverUrl != null) { if (serverUrl != null) {
newServerUri = serverUrl; return serverUrl;
} else {
return endpoint;
} }
return newServerUri;
} }
} }

@ -403,7 +403,7 @@ public class TraccarClientImpl implements TraccarClient {
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get(); String result = res.get();
log.info("---------result--------"); log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar.");
if (result.charAt(0) == '{') { if (result.charAt(0) == '{') {
JSONObject obj = new JSONObject(result); JSONObject obj = new JSONObject(result);
if (obj.has("id")) { if (obj.has("id")) {
@ -582,7 +582,7 @@ public class TraccarClientImpl implements TraccarClient {
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()), authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer()))); serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get(); String result = res.get();
log.info("---------result--------"); log.info("Group " + trackerGroupInfo.getGroupId() + " has been added to Traccar.");
if (res.isDone() && result.charAt(0) == '{') { if (res.isDone() && result.charAt(0) == '{') {
JSONObject obj = new JSONObject(result); JSONObject obj = new JSONObject(result);
if (obj.has("id")) { if (obj.has("id")) {

Loading…
Cancel
Save