|
|
@ -29,17 +29,18 @@ import okhttp3.Response;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.TrackerDeviceInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.TrackerGroupInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.TrackerGroupInfo;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.TrackerPermissionInfo;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.TransactionManagementException;
|
|
|
|
import org.wso2.carbon.device.mgt.common.exceptions.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.TrackerManagementDAOException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.dao.TrackerDAO;
|
|
|
|
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.dao.TrackerManagementDAOFactory;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.api.service.TraccarClient;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.api.service.TraccarClient;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants;
|
|
|
|
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.TraccarDevice;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups;
|
|
|
@ -47,16 +48,20 @@ 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.beans.TraccarUser;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException;
|
|
|
|
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.common.config.TraccarGateway;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.util.TraccarUtil;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.sql.SQLException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.time.LocalDateTime;
|
|
|
|
|
|
|
|
import java.time.ZoneId;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Objects;
|
|
|
|
import java.util.Random;
|
|
|
|
import java.util.Random;
|
|
|
|
|
|
|
|
import java.util.concurrent.Callable;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
import java.util.concurrent.Future;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
public class TraccarClientImpl implements TraccarClient {
|
|
|
|
public class TraccarClientImpl implements TraccarClient {
|
|
|
@ -79,246 +84,240 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.writeTimeout(30, TimeUnit.SECONDS)
|
|
|
|
.readTimeout(45, TimeUnit.SECONDS)
|
|
|
|
.readTimeout(45, TimeUnit.SECONDS)
|
|
|
|
.connectionPool(new ConnectionPool(50,30,TimeUnit.SECONDS))
|
|
|
|
.connectionPool(new ConnectionPool(100,50,TimeUnit.SECONDS))
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
this.trackerDAO = TrackerManagementDAOFactory.getTrackerDAO();
|
|
|
|
this.trackerDAO = TrackerManagementDAOFactory.getTrackerDAO();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private class TrackerExecutor implements Runnable {
|
|
|
|
private class OkHttpClientThreadPool implements Callable {
|
|
|
|
final int deviceId;
|
|
|
|
final String publisherUrlWithContext;
|
|
|
|
final int groupId;
|
|
|
|
|
|
|
|
final int tenantId;
|
|
|
|
|
|
|
|
final JSONObject payload;
|
|
|
|
final JSONObject payload;
|
|
|
|
final String context;
|
|
|
|
|
|
|
|
final String publisherUrl;
|
|
|
|
|
|
|
|
private final String method;
|
|
|
|
private final String method;
|
|
|
|
private final String type;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TrackerExecutor(int id, int tenantId, String publisherUrl, String context, JSONObject payload,
|
|
|
|
private OkHttpClientThreadPool(String publisherUrlWithContext, JSONObject payload, String method) {
|
|
|
|
String method, String type) {
|
|
|
|
this.publisherUrlWithContext = publisherUrlWithContext;
|
|
|
|
this.deviceId = id;
|
|
|
|
|
|
|
|
this.groupId = id;
|
|
|
|
|
|
|
|
this.tenantId = tenantId;
|
|
|
|
|
|
|
|
this.payload = payload;
|
|
|
|
this.payload = payload;
|
|
|
|
this.context = context;
|
|
|
|
|
|
|
|
this.publisherUrl = publisherUrl;
|
|
|
|
|
|
|
|
this.method = method;
|
|
|
|
this.method = method;
|
|
|
|
this.type = type;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void run() {
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public String call() throws Exception {
|
|
|
|
RequestBody requestBody;
|
|
|
|
RequestBody requestBody;
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
Request request;
|
|
|
|
Request request;
|
|
|
|
Response response;
|
|
|
|
Response response;
|
|
|
|
|
|
|
|
|
|
|
|
if(method==TraccarHandlerConstants.Methods.POST){
|
|
|
|
if(Objects.equals(method, TraccarHandlerConstants.Methods.POST)){
|
|
|
|
requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8"));
|
|
|
|
requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8"));
|
|
|
|
builder = builder.post(requestBody);
|
|
|
|
builder = builder.post(requestBody);
|
|
|
|
}else if(method==TraccarHandlerConstants.Methods.PUT){
|
|
|
|
}else if(Objects.equals(method, TraccarHandlerConstants.Methods.PUT)){
|
|
|
|
requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8"));
|
|
|
|
requestBody = RequestBody.create(payload.toString(), MediaType.parse("application/json; charset=utf-8"));
|
|
|
|
builder = builder.put(requestBody);
|
|
|
|
builder = builder.put(requestBody);
|
|
|
|
}else if(method==TraccarHandlerConstants.Methods.DELETE){
|
|
|
|
}else if(Objects.equals(method, TraccarHandlerConstants.Methods.DELETE)){
|
|
|
|
builder = builder.delete();
|
|
|
|
builder = builder.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
request = builder.url(publisherUrl + context).addHeader(authorization, authorizationKey).build();
|
|
|
|
request = builder.url(endpoint+publisherUrlWithContext).addHeader(authorization, authorizationKey).build();
|
|
|
|
|
|
|
|
response = client.newCall(request).execute();
|
|
|
|
|
|
|
|
return response.body().string();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String fetchAllUsers() throws ExecutionException, InterruptedException {
|
|
|
|
|
|
|
|
String method = TraccarHandlerConstants.Methods.GET;
|
|
|
|
|
|
|
|
String url = defaultPort+"/api/users";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<String> result = executor.submit(new OkHttpClientThreadPool(url, null, method));
|
|
|
|
|
|
|
|
String res = result.get();
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
|
|
|
|
return res;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
public String fetchUserInfo(String userName) throws ExecutionException, InterruptedException {
|
|
|
|
response = client.newCall(request).execute();
|
|
|
|
String allUsers = fetchAllUsers(); //get all users
|
|
|
|
if(method==TraccarHandlerConstants.Methods.POST){
|
|
|
|
JSONArray fetchAllUsers = new JSONArray(allUsers); //loop users
|
|
|
|
String result = response.body().string();
|
|
|
|
for(int i=0; i<fetchAllUsers.length();i++){
|
|
|
|
log.info(result);
|
|
|
|
//if login is null then check the name or if login is not null then check the login
|
|
|
|
if(type==TraccarHandlerConstants.Types.PERMISSION){
|
|
|
|
if((!fetchAllUsers.getJSONObject(i).isNull("login") &&
|
|
|
|
String msg ="";
|
|
|
|
fetchAllUsers.getJSONObject(i).getString("login").equals(userName)) ||
|
|
|
|
if(result == ""){
|
|
|
|
(fetchAllUsers.getJSONObject(i).isNull("login") &&
|
|
|
|
msg ="Successfully the device is assigned to the user";
|
|
|
|
fetchAllUsers.getJSONObject(i).getString("name").equals(userName)) ){
|
|
|
|
}else{
|
|
|
|
|
|
|
|
msg = "Error occurred while fetching users .";
|
|
|
|
return fetchAllUsers.getJSONObject(i).toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
log.info(msg);
|
|
|
|
}
|
|
|
|
}else if(result.charAt(0)=='{'){
|
|
|
|
|
|
|
|
JSONObject obj = new JSONObject(result);
|
|
|
|
|
|
|
|
if (obj.has("id")){
|
|
|
|
|
|
|
|
int traccarId = obj.getInt("id");
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
|
|
|
|
if(type==TraccarHandlerConstants.Types.DEVICE){
|
|
|
|
|
|
|
|
trackerDAO.addTrackerDevice(traccarId, deviceId, tenantId);
|
|
|
|
|
|
|
|
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("=============================");
|
|
|
|
|
|
|
|
//device is available
|
|
|
|
|
|
|
|
//device is not available
|
|
|
|
|
|
|
|
//user is available
|
|
|
|
|
|
|
|
//user is not available
|
|
|
|
|
|
|
|
fetchAllUsers(TraccarHandlerConstants.Types.USER_CREATE_WITH_INSERT_DEVICE, traccarUser, traccarId);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else if(type==TraccarHandlerConstants.Types.GROUP){
|
|
|
|
|
|
|
|
trackerDAO.addTrackerGroup(traccarId, groupId, tenantId);
|
|
|
|
|
|
|
|
TrackerGroupInfo res = trackerDAO.getTrackerGroup(groupId, tenantId);
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred on JSON object .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred establishing the DB connection .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = null;
|
|
|
|
|
|
|
|
if(type==TraccarHandlerConstants.Types.DEVICE){
|
|
|
|
|
|
|
|
msg = "Already device with deviceId " + deviceId + " exists" ;
|
|
|
|
|
|
|
|
}else if(type==TraccarHandlerConstants.Types.GROUP){
|
|
|
|
|
|
|
|
msg = "Already the group with groupId - " + groupId + " exists!";
|
|
|
|
|
|
|
|
}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." + traccarId + deviceId;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
response.close();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
int userId = 0;
|
|
|
|
|
|
|
|
for(int i=0; i<fetchAllUsers.length();i++){
|
|
|
|
|
|
|
|
if(fetchAllUsers.getJSONObject(i).getString("login").equals(username)){
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//TODO :: when user is available then assgin the device to the user
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
userAvailability=1;
|
|
|
|
|
|
|
|
log.info(fetchAllUsers.getJSONObject(i));
|
|
|
|
|
|
|
|
log.info(new Gson().toJson(fetchAllUsers.getJSONObject(i)));
|
|
|
|
|
|
|
|
log.info("Token: "+fetchAllUsers.getJSONObject(i).getString("token"));
|
|
|
|
|
|
|
|
userId = fetchAllUsers.getJSONObject(i).getInt("id");
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(type==TraccarHandlerConstants.Types.USER_CREATE_WITH_INSERT_DEVICE){
|
|
|
|
return TraccarHandlerConstants.Types.USER_NOT_FOUND;
|
|
|
|
if(userAvailability==0){
|
|
|
|
}
|
|
|
|
log.info("============");
|
|
|
|
|
|
|
|
log.info("Creating User");
|
|
|
|
|
|
|
|
TraccarUser traccarUser = (TraccarUser) payload.get("data");
|
|
|
|
|
|
|
|
log.info(traccarUser);
|
|
|
|
|
|
|
|
log.info("============");
|
|
|
|
|
|
|
|
createUser(traccarUser, type, deviceId);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
if(userId!=0){
|
|
|
|
|
|
|
|
log.info("=============");
|
|
|
|
|
|
|
|
log.info("User inserted and setting to create session");
|
|
|
|
|
|
|
|
log.info("=============");
|
|
|
|
|
|
|
|
setPermission(userId, deviceId);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
log.info("UserId is null");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else if(type==TraccarHandlerConstants.Types.USER_CREATE){
|
|
|
|
|
|
|
|
/*if(userAvailability==1){
|
|
|
|
|
|
|
|
log.info("Update User");
|
|
|
|
|
|
|
|
log.info(payload);
|
|
|
|
|
|
|
|
log.info(new Gson().toJson(payload));
|
|
|
|
|
|
|
|
updateUser(payload);
|
|
|
|
|
|
|
|
log.info("Update User");
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String returnUser(String userName) throws TraccarConfigurationException{
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
String result = DeviceAPIClientServiceImpl.fetchUserInfo(userName);
|
|
|
|
|
|
|
|
Date today = new Date();
|
|
|
|
|
|
|
|
LocalDateTime tomorrow = LocalDateTime.from(today.toInstant().atZone(ZoneId.of("UTC"))).plusDays(1);
|
|
|
|
|
|
|
|
String token = DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.TRACCAR_TOKEN);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TraccarUser traccarUser = new TraccarUser();
|
|
|
|
|
|
|
|
traccarUser.setToken(token);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(Objects.equals(result, TraccarHandlerConstants.Types.USER_NOT_FOUND)){
|
|
|
|
|
|
|
|
//create user
|
|
|
|
|
|
|
|
traccarUser.setName(userName);
|
|
|
|
|
|
|
|
traccarUser.setLogin(userName);
|
|
|
|
|
|
|
|
traccarUser.setEmail(userName);
|
|
|
|
|
|
|
|
traccarUser.setPassword(DeviceAPIClientServiceImpl.generateRandomString(TraccarHandlerConstants.Types.DEFAULT_RANDOM));
|
|
|
|
|
|
|
|
traccarUser.setDeviceLimit(-1);
|
|
|
|
|
|
|
|
traccarUser.setUserLimit(-1);
|
|
|
|
|
|
|
|
traccarUser.setExpirationTime(tomorrow.toString());
|
|
|
|
|
|
|
|
DeviceAPIClientServiceImpl.createUser(traccarUser);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
//update user
|
|
|
|
|
|
|
|
JSONObject obj = new JSONObject(result);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
traccarUser.setId(obj.getInt("id"));
|
|
|
|
|
|
|
|
traccarUser.setName(obj.getString("name"));
|
|
|
|
|
|
|
|
traccarUser.setLogin(obj.getString("login"));
|
|
|
|
|
|
|
|
traccarUser.setEmail(obj.getString("email"));
|
|
|
|
|
|
|
|
traccarUser.setDeviceLimit(obj.getInt("deviceLimit"));
|
|
|
|
|
|
|
|
traccarUser.setUserLimit(obj.getInt("userLimit"));
|
|
|
|
|
|
|
|
traccarUser.setAdministrator(obj.getBoolean("administrator"));
|
|
|
|
|
|
|
|
traccarUser.setDisabled(obj.getBoolean("disabled"));
|
|
|
|
|
|
|
|
traccarUser.setReadonly(obj.getBoolean("readonly"));
|
|
|
|
|
|
|
|
if(!obj.getBoolean("administrator")){
|
|
|
|
|
|
|
|
traccarUser.setExpirationTime(tomorrow.toString());
|
|
|
|
|
|
|
|
}else if(!obj.isNull("expirationTime")){
|
|
|
|
|
|
|
|
traccarUser.setExpirationTime(obj.getString("expirationTime"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
DeviceAPIClientServiceImpl.updateUser(traccarUser, obj.getInt("id"));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
result = DeviceAPIClientServiceImpl.fetchUserInfo(userName);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} catch (InterruptedException | ExecutionException e) {
|
|
|
|
|
|
|
|
JSONObject obj=new JSONObject();
|
|
|
|
|
|
|
|
String msg = "Error occurred while executing enrollment status of the device.";
|
|
|
|
|
|
|
|
obj.put("error",msg);
|
|
|
|
|
|
|
|
obj.put("e",e);
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
return obj.toString();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String createUser(TraccarUser traccarUser) throws ExecutionException, InterruptedException {
|
|
|
|
|
|
|
|
String method = TraccarHandlerConstants.Methods.POST;
|
|
|
|
|
|
|
|
String url = defaultPort+"/api/users";
|
|
|
|
|
|
|
|
JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method));
|
|
|
|
|
|
|
|
String result = res.get();
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String updateUser(TraccarUser traccarUser, int userId) throws ExecutionException, InterruptedException {
|
|
|
|
|
|
|
|
String method = TraccarHandlerConstants.Methods.PUT;
|
|
|
|
|
|
|
|
String url = defaultPort+"/api/users/"+userId;
|
|
|
|
|
|
|
|
JSONObject payload = TraccarUtil.TraccarUserPayload(traccarUser);
|
|
|
|
|
|
|
|
|
|
|
|
} catch (IOException | TraccarConfigurationException e) {
|
|
|
|
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method));
|
|
|
|
log.error("Couldnt connect to traccar.", e);
|
|
|
|
String result = res.get();
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setPermission(int userId, int deviceId) throws TraccarConfigurationException, ExecutionException, InterruptedException {
|
|
|
|
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
|
|
|
|
payload.put("userId", userId);
|
|
|
|
|
|
|
|
payload.put("deviceId", deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String method = TraccarHandlerConstants.Methods.POST;
|
|
|
|
|
|
|
|
String url = defaultPort+"/api/permissions";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method));
|
|
|
|
|
|
|
|
String result = res.get();
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(result!=null){
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
|
|
|
|
trackerDAO.addTrackerUssrDevicePermission(userId, deviceId);
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred establishing the DB connection .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
public void removePermission(int userId, int deviceId) throws TraccarConfigurationException, ExecutionException, InterruptedException {
|
|
|
|
* Add Traccar Device operation.
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
* @param deviceInfo with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId
|
|
|
|
payload.put("userId", userId);
|
|
|
|
* Model, Contact, Category, fenceIds
|
|
|
|
payload.put("deviceId", deviceId);
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
|
|
|
|
*/
|
|
|
|
String method = TraccarHandlerConstants.Methods.DELETE;
|
|
|
|
public void addDevice(TraccarDevice deviceInfo, int tenantId) throws TraccarConfigurationException, TrackerAlreadyExistException {
|
|
|
|
String url = defaultPort+"/api/permissions";
|
|
|
|
TrackerDeviceInfo res = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method));
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
String result = res.get();
|
|
|
|
res = trackerDAO.getTrackerDevice(deviceInfo.getId(), tenantId);
|
|
|
|
//executor.shutdown();
|
|
|
|
if(res!=null){
|
|
|
|
|
|
|
|
String msg = "The device already exit";
|
|
|
|
if(result!=null){
|
|
|
|
log.error(msg);
|
|
|
|
try {
|
|
|
|
throw new TrackerAlreadyExistException(msg);
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
|
|
|
|
trackerDAO.removeTrackerUssrDevicePermission(userId, deviceId);
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred establishing the DB connection .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} catch (SQLException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred establishing the DB connection .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public String fetchAllDevices() throws ExecutionException, InterruptedException {
|
|
|
|
|
|
|
|
String method = TraccarHandlerConstants.Methods.GET;
|
|
|
|
|
|
|
|
String url = defaultPort+"/api/devices";
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject payload = payload(deviceInfo);
|
|
|
|
Future<String> result = executor.submit(new OkHttpClientThreadPool(url, null, method));
|
|
|
|
String context = defaultPort+"/api/devices";
|
|
|
|
String res = result.get();
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(deviceInfo.getId(), tenantId, endpoint, context, payload,
|
|
|
|
//executor.shutdown();
|
|
|
|
TraccarHandlerConstants.Methods.POST, TraccarHandlerConstants.Types.DEVICE);
|
|
|
|
return res;
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* update Traccar Device operation.
|
|
|
|
* Add Traccar Device operation.
|
|
|
|
* @param deviceInfo with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId
|
|
|
|
* @param traccarDevice with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId
|
|
|
|
* Model, Contact, Category, fenceIds
|
|
|
|
* Model, Contact, Category, fenceIds
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void updateDevice(TraccarDevice deviceInfo, int tenantId) throws TraccarConfigurationException, TrackerAlreadyExistException {
|
|
|
|
public void addDevice(TraccarDevice traccarDevice, int tenantId) throws
|
|
|
|
TrackerDeviceInfo res = null;
|
|
|
|
TraccarConfigurationException, TrackerAlreadyExistException, ExecutionException, InterruptedException {
|
|
|
|
|
|
|
|
TrackerDeviceInfo trackerDeviceInfo = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
res = trackerDAO.getTrackerDevice(deviceInfo.getId(), tenantId);
|
|
|
|
trackerDeviceInfo = trackerDAO.getTrackerDevice(traccarDevice.getId(), tenantId);
|
|
|
|
|
|
|
|
if(trackerDeviceInfo!=null){
|
|
|
|
|
|
|
|
String msg = "The device already exist";
|
|
|
|
|
|
|
|
log.error(msg);
|
|
|
|
|
|
|
|
throw new TrackerAlreadyExistException(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
@ -331,105 +330,92 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((res==null) || (res.getTraccarDeviceId()==0)){
|
|
|
|
if(trackerDeviceInfo==null){
|
|
|
|
try {
|
|
|
|
//add the device
|
|
|
|
TraccarDevice device = deviceInfo;
|
|
|
|
String method = TraccarHandlerConstants.Methods.POST;
|
|
|
|
String lastUpdatedTime = String.valueOf((new Date().getTime()));
|
|
|
|
String url = defaultPort+"/api/devices";
|
|
|
|
device.setLastUpdate(lastUpdatedTime);
|
|
|
|
JSONObject payload = TraccarUtil.TraccarDevicePayload(traccarDevice);
|
|
|
|
addDevice(deviceInfo, tenantId);
|
|
|
|
|
|
|
|
} catch (TraccarConfigurationException e) {
|
|
|
|
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method));
|
|
|
|
String msg = "Error occurred while mapping with groupId";
|
|
|
|
String result = res.get();
|
|
|
|
log.error(msg, e);
|
|
|
|
if(result.charAt(0)=='{'){
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
JSONObject obj = new JSONObject(result);
|
|
|
|
} catch (TrackerAlreadyExistException e) {
|
|
|
|
if (obj.has("id")){
|
|
|
|
String msg = "The group already exist";
|
|
|
|
int traccarDeviceId = obj.getInt("id");
|
|
|
|
log.error(msg, e);
|
|
|
|
int deviceId = traccarDevice.getId();
|
|
|
|
throw new TrackerAlreadyExistException(msg, e);
|
|
|
|
try {
|
|
|
|
}
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
}else if (res!=null && (res.getTraccarDeviceId()!=0 && res.getStatus()==0)){
|
|
|
|
trackerDAO.addTrackerDevice(traccarDeviceId, deviceId, tenantId);
|
|
|
|
//update the traccarGroupId and status
|
|
|
|
trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId);
|
|
|
|
try {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
if(trackerDeviceInfo.getStatus()==0){
|
|
|
|
trackerDAO.updateTrackerDeviceIdANDStatus(res.getTraccarDeviceId(), deviceInfo.getId(), tenantId, 1);
|
|
|
|
trackerDAO.updateTrackerDeviceIdANDStatus(trackerDeviceInfo.getTraccarDeviceId(), deviceId, tenantId, 1);
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
}
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
String msg = "Error occurred establishing the DB connection .";
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
log.error(msg, e);
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
String msg="Could not add the traccar group";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
} finally{
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg = "Error occurred establishing the DB connection .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject returnUser = new JSONObject(returnUser(username));
|
|
|
|
|
|
|
|
setPermission(returnUser.getInt("id"),deviceId);
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
log.error("Something went wrong_1: " + result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
log.error("Something went wrong_2: " + result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
|
|
|
|
JSONObject payload = payload(deviceInfo);
|
|
|
|
|
|
|
|
String context = defaultPort+"/api/devices";
|
|
|
|
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(deviceInfo.getId(), tenantId, endpoint, context, payload,
|
|
|
|
|
|
|
|
TraccarHandlerConstants.Methods.PUT, TraccarHandlerConstants.Types.DEVICE);
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//executor.shutdown();
|
|
|
|
|
|
|
|
|
|
|
|
private JSONObject payload(TraccarDevice deviceInfo){
|
|
|
|
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
|
|
|
|
payload.put("name", deviceInfo.getDeviceName());
|
|
|
|
|
|
|
|
payload.put("uniqueId", deviceInfo.getUniqueId());
|
|
|
|
|
|
|
|
payload.put("status", deviceInfo.getStatus());
|
|
|
|
|
|
|
|
payload.put("disabled", deviceInfo.getDisabled());
|
|
|
|
|
|
|
|
payload.put("lastUpdate", deviceInfo.getLastUpdate());
|
|
|
|
|
|
|
|
payload.put("positionId", deviceInfo.getPositionId());
|
|
|
|
|
|
|
|
payload.put("groupId", deviceInfo.getGroupId());
|
|
|
|
|
|
|
|
payload.put("phone", deviceInfo.getPhone());
|
|
|
|
|
|
|
|
payload.put("model", deviceInfo.getModel());
|
|
|
|
|
|
|
|
payload.put("contact", deviceInfo.getContact());
|
|
|
|
|
|
|
|
payload.put("category", deviceInfo.getCategory());
|
|
|
|
|
|
|
|
List<String> geoFenceIds = new ArrayList<>();
|
|
|
|
|
|
|
|
payload.put("geofenceIds", geoFenceIds);
|
|
|
|
|
|
|
|
payload.put("attributes", new JSONObject());
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Add Device GPS Location operation.
|
|
|
|
* Add Device GPS Location operation.
|
|
|
|
* @param deviceInfo with DeviceIdentifier, Timestamp, Lat, Lon, Bearing, Speed, ignition
|
|
|
|
* @param deviceInfo with DeviceIdentifier, Timestamp, Lat, Lon, Bearing, Speed, ignition
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void updateLocation(TraccarDevice device, TraccarPosition deviceInfo, int tenantId) throws TraccarConfigurationException, TrackerAlreadyExistException {
|
|
|
|
public void updateLocation(TraccarDevice device, TraccarPosition deviceInfo, int tenantId) throws TraccarConfigurationException, TrackerAlreadyExistException, ExecutionException, InterruptedException {
|
|
|
|
TrackerDeviceInfo res = null;
|
|
|
|
TrackerDeviceInfo trackerDeviceInfo = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
res = trackerDAO.getTrackerDevice(device.getId(), tenantId);
|
|
|
|
trackerDeviceInfo = trackerDAO.getTrackerDevice(device.getId(), tenantId);
|
|
|
|
|
|
|
|
log.info(trackerDeviceInfo);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException 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);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
String msg="Could add new device location";
|
|
|
|
String msg="Could not add new device location";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
} finally{
|
|
|
|
} finally{
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (res == null){
|
|
|
|
//check if the device is already exist before updating the location
|
|
|
|
try {
|
|
|
|
if (trackerDeviceInfo == null){
|
|
|
|
addDevice(device, tenantId);
|
|
|
|
//add device if not exist
|
|
|
|
} catch (TraccarConfigurationException e) {
|
|
|
|
log.info(trackerDeviceInfo);
|
|
|
|
String msg = "Error occurred add the new device";
|
|
|
|
addDevice(device, tenantId);
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} catch (TrackerAlreadyExistException e) {
|
|
|
|
|
|
|
|
String msg = "The device already exist";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TrackerAlreadyExistException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
}else{
|
|
|
|
String context = locationUpdatePort+"/?id="+deviceInfo.getDeviceIdentifier()+"×tamp="+deviceInfo.getTimestamp()+
|
|
|
|
//Update Location
|
|
|
|
"&lat="+deviceInfo.getLat()+"&lon="+deviceInfo.getLon()+"&bearing="+deviceInfo.getBearing()+
|
|
|
|
String method = TraccarHandlerConstants.Methods.GET;
|
|
|
|
|
|
|
|
String url = locationUpdatePort+"/?id="+deviceInfo.getDeviceIdentifier()+
|
|
|
|
|
|
|
|
"×tamp="+deviceInfo.getTimestamp()+"&lat="+deviceInfo.getLat()+
|
|
|
|
|
|
|
|
"&lon="+deviceInfo.getLon()+"&bearing="+deviceInfo.getBearing()+
|
|
|
|
"&speed="+deviceInfo.getSpeed()+"&ignition=true";
|
|
|
|
"&speed="+deviceInfo.getSpeed()+"&ignition=true";
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(0, 0, endpoint, context, null,
|
|
|
|
|
|
|
|
TraccarHandlerConstants.Methods.GET, TraccarHandlerConstants.Types.DEVICE);
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
|
|
|
|
log.info("Device GPS location added on traccar");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executor.submit(new OkHttpClientThreadPool(url, null, method));
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -437,36 +423,54 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
* @param deviceId identified via deviceIdentifier
|
|
|
|
* @param deviceId identified via deviceIdentifier
|
|
|
|
* @throws TraccarConfigurationException Failed while dis-enroll a Traccar Device operation
|
|
|
|
* @throws TraccarConfigurationException Failed while dis-enroll a Traccar Device operation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void disEndrollDevice(int deviceId, int tenantId) throws TraccarConfigurationException {
|
|
|
|
public void disEnrollDevice(int deviceId, int tenantId) throws TraccarConfigurationException {
|
|
|
|
TrackerDeviceInfo res = null;
|
|
|
|
TrackerDeviceInfo trackerDeviceInfo = null;
|
|
|
|
JSONObject obj = null;
|
|
|
|
TrackerPermissionInfo trackerPermissionInfo = null;
|
|
|
|
|
|
|
|
JSONObject trackerDevice = null;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
res = trackerDAO.getTrackerDevice(deviceId, tenantId);
|
|
|
|
trackerDeviceInfo = trackerDAO.getTrackerDevice(deviceId, tenantId);
|
|
|
|
if(res!=null){
|
|
|
|
trackerPermissionInfo = trackerDAO.getUserIdofPermissionByDeviceId(deviceId);
|
|
|
|
obj = new JSONObject(res);
|
|
|
|
if(trackerDeviceInfo!=null){
|
|
|
|
if(obj!=null){
|
|
|
|
trackerDevice = new JSONObject(trackerDeviceInfo);
|
|
|
|
trackerDAO.removeTrackerDevice(deviceId, tenantId);
|
|
|
|
trackerDAO.removeTrackerDevice(deviceId, tenantId);
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
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 TraccarConfigurationException(msg, e);
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
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 TraccarConfigurationException(msg, e);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(obj != null){
|
|
|
|
//Delete from traccar
|
|
|
|
String context = defaultPort+"/api/devices/"+obj.getInt("traccarDeviceId");
|
|
|
|
if(trackerDevice!=null){
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(obj.getInt("traccarDeviceId"), tenantId, endpoint, context, null,
|
|
|
|
String method = TraccarHandlerConstants.Methods.DELETE;
|
|
|
|
TraccarHandlerConstants.Methods.DELETE, TraccarHandlerConstants.Types.DEVICE);
|
|
|
|
String url = defaultPort+"/api/devices/"+trackerDevice.getInt("traccarDeviceId");
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
executor.submit(new OkHttpClientThreadPool(url, null, method));
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
|
|
|
|
//remove permissions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
log.info(new Gson().toJson(trackerPermissionInfo));
|
|
|
|
|
|
|
|
log.info(trackerPermissionInfo);
|
|
|
|
|
|
|
|
if(trackerPermissionInfo!=null){
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
removePermission(trackerPermissionInfo.getTraccarUserId(),deviceId);
|
|
|
|
|
|
|
|
} catch (ExecutionException e) {
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -475,38 +479,71 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
* @param groupInfo with groupName
|
|
|
|
* @param groupInfo with groupName
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void addGroup(TraccarGroups groupInfo, int groupId, int tenantId) throws TraccarConfigurationException, TrackerAlreadyExistException {
|
|
|
|
public void addGroup(TraccarGroups groupInfo, int groupId, int tenantId) throws
|
|
|
|
TrackerGroupInfo res = null;
|
|
|
|
TraccarConfigurationException, TrackerAlreadyExistException, ExecutionException, InterruptedException {
|
|
|
|
|
|
|
|
TrackerGroupInfo trackerGroupInfo = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
res = trackerDAO.getTrackerGroup(groupId, tenantId);
|
|
|
|
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
|
|
|
|
if (res!=null){
|
|
|
|
if (trackerGroupInfo!=null){
|
|
|
|
String msg = "The group already exit";
|
|
|
|
String msg = "The group already exit";
|
|
|
|
log.error(msg);
|
|
|
|
log.error(msg);
|
|
|
|
throw new TrackerAlreadyExistException(msg);
|
|
|
|
throw new TrackerAlreadyExistException(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
String msg = "Error occurred while mapping with groupId.";
|
|
|
|
log.error(msg, e);
|
|
|
|
log.error(msg, e);
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
} catch (SQLException e) {
|
|
|
|
} catch (SQLException 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);
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (res==null){
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
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";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, method));
|
|
|
|
|
|
|
|
String result = res.get();
|
|
|
|
|
|
|
|
|
|
|
|
String context = defaultPort+"/api/groups";
|
|
|
|
if(result.charAt(0)=='{'){
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(groupId, tenantId, endpoint, context, payload,
|
|
|
|
JSONObject obj = new JSONObject(result);
|
|
|
|
TraccarHandlerConstants.Methods.POST, TraccarHandlerConstants.Types.GROUP);
|
|
|
|
if (obj.has("id")){
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
int traccarGroupId = obj.getInt("id");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
|
|
|
|
trackerDAO.addTrackerGroup(traccarGroupId, groupId, tenantId);
|
|
|
|
|
|
|
|
trackerGroupInfo = trackerDAO.getTrackerGroup(groupId, tenantId);
|
|
|
|
|
|
|
|
if(trackerGroupInfo.getStatus()==0){
|
|
|
|
|
|
|
|
trackerDAO.updateTrackerGroupIdANDStatus(trackerGroupInfo.getTraccarGroupId(), groupId, tenantId, 1);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while mapping with deviceId .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred establishing the DB connection .";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
log.error("Something went wrong_1: " + result);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else{
|
|
|
|
|
|
|
|
log.error("Something went wrong_2: " + result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -514,7 +551,8 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
* @param groupInfo with groupName
|
|
|
|
* @param groupInfo with groupName
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void updateGroup(TraccarGroups groupInfo, int groupId, int tenantId) throws TraccarConfigurationException, TrackerAlreadyExistException {
|
|
|
|
public void updateGroup(TraccarGroups groupInfo, int groupId, int tenantId) throws
|
|
|
|
|
|
|
|
TraccarConfigurationException, TrackerAlreadyExistException, ExecutionException, InterruptedException {
|
|
|
|
TrackerGroupInfo res = null;
|
|
|
|
TrackerGroupInfo res = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
|
TrackerManagementDAOFactory.openConnection();
|
|
|
@ -531,29 +569,23 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
|
|
|
|
|
|
|
|
if ((res==null) || (res.getTraccarGroupId()==0)){
|
|
|
|
if ((res==null) || (res.getTraccarGroupId()==0)){
|
|
|
|
//add a new traccar group
|
|
|
|
//add a new traccar group
|
|
|
|
try {
|
|
|
|
addGroup(groupInfo, groupId, tenantId);
|
|
|
|
addGroup(groupInfo, groupId, tenantId);
|
|
|
|
|
|
|
|
} catch (TraccarConfigurationException e) {
|
|
|
|
|
|
|
|
String msg = "Error occurred while mapping with groupId";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
} catch (TrackerAlreadyExistException e) {
|
|
|
|
|
|
|
|
String msg = "The group already exist";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TrackerAlreadyExistException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}else if (res!=null && (res.getTraccarGroupId()!=0 && res.getStatus()==0)){
|
|
|
|
}else if (res!=null && (res.getTraccarGroupId()!=0 && res.getStatus()==0)){
|
|
|
|
//update the traccargroupId and status
|
|
|
|
//update the traccargroupId and status
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
TrackerManagementDAOFactory.beginTransaction();
|
|
|
|
trackerDAO.updateTrackerGroupIdANDStatus(res.getTraccarGroupId(), groupId, tenantId, 1);
|
|
|
|
trackerDAO.updateTrackerGroupIdANDStatus(res.getTraccarGroupId(), groupId, tenantId, 1);
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
|
|
|
|
TrackerManagementDAOFactory.rollbackTransaction();
|
|
|
|
|
|
|
|
String msg="Could not update the traccar group";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
} catch (TransactionManagementException 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);
|
|
|
|
} catch (TrackerManagementDAOException e) {
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
String msg="Could not add the traccar group";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
} finally{
|
|
|
|
} finally{
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -564,10 +596,11 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
payload.put("name", groupInfo.getName());
|
|
|
|
payload.put("name", groupInfo.getName());
|
|
|
|
payload.put("attributes", new JSONObject());
|
|
|
|
payload.put("attributes", new JSONObject());
|
|
|
|
|
|
|
|
|
|
|
|
String context = defaultPort+"/api/groups/"+obj.getInt("traccarGroupId");
|
|
|
|
String method = TraccarHandlerConstants.Methods.PUT;
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(groupId, tenantId, endpoint, context, payload,
|
|
|
|
String url = defaultPort+"/api/groups/"+obj.getInt("traccarGroupId");
|
|
|
|
TraccarHandlerConstants.Methods.PUT, TraccarHandlerConstants.Types.GROUP);
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
executor.submit(new OkHttpClientThreadPool(url, payload, method));
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -576,7 +609,7 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
* @param groupId
|
|
|
|
* @param groupId
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public void deleteGroup(int groupId, int tenantId) throws TraccarConfigurationException {
|
|
|
|
public void deleteGroup(int groupId, int tenantId) throws TraccarConfigurationException, ExecutionException, InterruptedException {
|
|
|
|
TrackerGroupInfo res = null;
|
|
|
|
TrackerGroupInfo res = null;
|
|
|
|
JSONObject obj = null;
|
|
|
|
JSONObject obj = null;
|
|
|
|
try {
|
|
|
|
try {
|
|
|
@ -587,6 +620,12 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
if(obj!=null){
|
|
|
|
if(obj!=null){
|
|
|
|
trackerDAO.removeTrackerGroup(obj.getInt("id"));
|
|
|
|
trackerDAO.removeTrackerGroup(obj.getInt("id"));
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
TrackerManagementDAOFactory.commitTransaction();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String method = TraccarHandlerConstants.Methods.DELETE;
|
|
|
|
|
|
|
|
String url = defaultPort+"/api/groups/"+obj.getInt("traccarGroupId");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
executor.submit(new OkHttpClientThreadPool(url, null, method));
|
|
|
|
|
|
|
|
//executor.shutdown();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
|
} catch (TransactionManagementException e) {
|
|
|
@ -600,66 +639,6 @@ public class TraccarClientImpl implements TraccarClient {
|
|
|
|
} finally {
|
|
|
|
} finally {
|
|
|
|
TrackerManagementDAOFactory.closeConnection();
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void fetchAllUsers(String type, TraccarUser traccarUser, int deviceId) throws TraccarConfigurationException {
|
|
|
|
|
|
|
|
String context = defaultPort+"/api/users/";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
|
|
|
|
payload.put("data", traccarUser);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(deviceId, 0, endpoint, context,
|
|
|
|
|
|
|
|
payload, TraccarHandlerConstants.Methods.GET, type);
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private JSONObject TraccarUserPayload(TraccarUser traccarUser){
|
|
|
|
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
|
|
|
|
payload.put("name", traccarUser.getName());
|
|
|
|
|
|
|
|
payload.put("login", traccarUser.getLogin());
|
|
|
|
|
|
|
|
payload.put("email", traccarUser.getEmail());
|
|
|
|
|
|
|
|
payload.put("password", traccarUser.getPassword());
|
|
|
|
|
|
|
|
payload.put("token", traccarUser.getToken());
|
|
|
|
|
|
|
|
payload.put("administrator", traccarUser.getAdministrator());
|
|
|
|
|
|
|
|
payload.put("deviceLimit", traccarUser.getDeviceLimit());
|
|
|
|
|
|
|
|
payload.put("userLimit", traccarUser.getUserLimit());
|
|
|
|
|
|
|
|
payload.put("disabled", traccarUser.getDisabled());
|
|
|
|
|
|
|
|
payload.put("deviceReadonly", traccarUser.getDeviceReadonly());
|
|
|
|
|
|
|
|
return payload;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public void createUser(TraccarUser traccarUser, String type, int deviceId) throws TraccarConfigurationException {
|
|
|
|
|
|
|
|
JSONObject payload = TraccarUserPayload(traccarUser);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String context = defaultPort+"/api/users";
|
|
|
|
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(deviceId, 0, endpoint, context, payload,
|
|
|
|
|
|
|
|
TraccarHandlerConstants.Methods.POST, type);
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void updateUser(JSONObject traccarUser) throws TraccarConfigurationException {
|
|
|
|
|
|
|
|
/*JSONObject payload = traccarUser;
|
|
|
|
|
|
|
|
String context = defaultPort+"/api/users";
|
|
|
|
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(0, 0, endpoint, context, payload,
|
|
|
|
|
|
|
|
TraccarHandlerConstants.Methods.PUT, TraccarHandlerConstants.Types.USER);
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);*/
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void setPermission(int userId, int deviceId) throws TraccarConfigurationException {
|
|
|
|
|
|
|
|
JSONObject payload = new JSONObject();
|
|
|
|
|
|
|
|
payload.put("userId", userId);
|
|
|
|
|
|
|
|
payload.put("deviceId", deviceId);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String context = defaultPort+"/api/permissions";
|
|
|
|
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(deviceId, 0, endpoint, context, payload,
|
|
|
|
|
|
|
|
TraccarHandlerConstants.Methods.POST, TraccarHandlerConstants.Types.PERMISSION);
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String generateRandomString(int len) {
|
|
|
|
public String generateRandomString(int len) {
|
|
|
|