Add add/delete a group from traccar

feature/traccar-sync
Rushdi Mohamed 3 years ago
parent 1dd2e35f89
commit 53f4fd7ebc

@ -35,6 +35,7 @@
package org.wso2.carbon.device.mgt.core.service;
import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -64,6 +65,7 @@ import org.wso2.carbon.device.mgt.core.event.config.GroupAssignmentEventOperatio
import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager;
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.operation.mgt.OperationMgtConstants;
import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.user.api.UserRealm;
import org.wso2.carbon.user.api.UserStoreException;
@ -131,7 +133,24 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
this.groupDAO.addGroupProperties(deviceGroup, updatedGroupID, tenantId);
}
GroupManagementDAOFactory.commitTransaction();
//add new group in traccar
try {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.addGroup(deviceGroup);
} catch (TraccarConfigurationException e) {
log.error("Error while disenrolling a device from Traccar " + e);
}
//add new group in traccar
} else {
//check if a group exist or not in traccar if not existing then add
/*try {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.addGroup(deviceGroup);
} catch (TraccarConfigurationException e) {
log.error("Error while disenrolling a device from Traccar " + e);
}*/
//check if a group exist or not in traccar
throw new GroupAlreadyExistException("Group exist with name " + deviceGroup.getName());
}
} catch (GroupManagementDAOException e) {
@ -283,6 +302,14 @@ public class GroupManagementProviderServiceImpl implements GroupManagementProvid
if (log.isDebugEnabled()) {
log.debug("DeviceGroup " + deviceGroup.getName() + " removed.");
}
//add new group in traccar
try {
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService()
.deleteGroup(deviceGroup);
} catch (TraccarConfigurationException e) {
log.error("Error while disenrolling a device from Traccar " + e);
}
//add new group in traccar
return true;
} catch (GroupManagementDAOException e) {
GroupManagementDAOFactory.rollbackTransaction();

@ -63,4 +63,12 @@ public interface DeviceAPIClientService {
* @throws TraccarConfigurationException errors thrown while adding a group traccar configuration
*/
void addGroup(DeviceGroup group) throws TraccarConfigurationException;
/**
* Delete a device Traccar configuration records
*
* @param group to be add a group
* @throws TraccarConfigurationException errors thrown while adding a group traccar configuration
*/
void deleteGroup(DeviceGroup group) throws TraccarConfigurationException;
}

@ -42,6 +42,7 @@ import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationM
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
@ -123,7 +124,7 @@ public class TrackerClient implements TraccarClient {
*/
public void addDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException {
try{
JSONObject payload = new JSONObject();
JSONObject payload = new JSONObject();
payload.put("name", deviceInfo.getDeviceName());
payload.put("uniqueId", deviceInfo.getUniqueId());
payload.put("status", deviceInfo.getStatus());
@ -205,11 +206,6 @@ public class TrackerClient implements TraccarClient {
String result = getDeviceByDeviceIdentifier(deviceInfo.getDeviceIdentifier());
String jsonData ="{"+ "\"geodata\": "+ result+ "}";
log.info("======================");
log.info("result");
log.info(result);
log.info(deviceInfo.getDeviceIdentifier());
log.info("===========================");
JSONObject obj = new JSONObject(jsonData);
JSONArray geodata = obj.getJSONArray("geodata");
JSONObject jsonResponse = geodata.getJSONObject(0);
@ -219,11 +215,11 @@ public class TrackerClient implements TraccarClient {
executor.execute(trackerExecutor);
log.info("Device successfully dis-enrolled");
}catch (JSONException e){
String msg = "Could not find the device infomation to dis-enroll the device";
String msg = "Could not find the device information to dis-enroll the device";
log.error(msg, e);
throw new TraccarConfigurationException(msg);
}catch (TraccarConfigurationException ex){
String msg = "Could not find the device infomation to dis-enroll the device";
String msg = "Could not find the device information to dis-enroll the device";
log.error(msg, ex);
throw new TraccarConfigurationException(msg, ex);
}
@ -243,7 +239,7 @@ public class TrackerClient implements TraccarClient {
String context = defaultPort+"/api/groups";
Runnable trackerExecutor = new TrackerExecutor(endpoint, context, payload, "post");
executor.execute(trackerExecutor);
log.info("Group successfully on traccar");
log.info("Group successfully added on traccar");
}catch (Exception e){
String msg="Could not add a traccar group";
log.error(msg, e);
@ -251,6 +247,67 @@ public class TrackerClient implements TraccarClient {
}
}
/**
* Add Device GPS Location operation.
* @return all groups
* @throws TraccarConfigurationException Failed while add Traccar Device location operation
*/
@Override
public String getAllGroups() throws TraccarConfigurationException {
try {
String context = defaultPort+"/api/groups?all=true";
Runnable trackerExecutor = new TrackerExecutor(endpoint, context, null, "get");
executor.execute(trackerExecutor);
Request request = new Request.Builder()
.url(endpoint+context)
.addHeader(authorization, authorizationKey)
.build();
Response response = client.newCall(request).execute();
String result = response.body().string();
return result;
} catch (IOException e) {
String msg="Could not find device information";
log.error(msg, e);
throw new TraccarConfigurationException(msg, e);
}
}
/**
* Add Traccar Device operation.
* @param groupInfo with groupName
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
*/
public void deleteGroup(TraccarGroups groupInfo) throws TraccarConfigurationException {
try{
String result = getAllGroups();
String jsonData ="{"+ "\"groupdata\": "+ result+ "}";
JSONObject obj = new JSONObject(jsonData);
JSONArray geodata = obj.getJSONArray("groupdata");
for(int i=0; i<geodata.length();i++){
JSONObject jsonResponse = geodata.getJSONObject(i);
log.info(jsonResponse.getString("name"));
log.info(jsonResponse.getInt("id"));
if(Objects.equals(jsonResponse.getString("name"), groupInfo.getName())){
String context = defaultPort+"/api/groups/"+jsonResponse.getInt("id");
Runnable trackerExecutor = new TrackerExecutor(endpoint, context, null, "delete");
executor.execute(trackerExecutor);
log.info("Traccar group successfully deleted");
break;
}
}
}catch (JSONException e){
String msg = "Could not find the device information to dis-enroll the device";
log.error(msg, e);
throw new TraccarConfigurationException(msg);
}catch (TraccarConfigurationException ex){
String msg = "Could not find the device information to dis-enroll the device";
log.error(msg, ex);
throw new TraccarConfigurationException(msg, ex);
}
}
private TraccarGateway getTraccarGateway(){
return TraccarConfigurationManager.getInstance().getTraccarConfig().getTraccarGateway(
TraccarHandlerConstants.GATEWAY_NAME);

@ -63,4 +63,10 @@ public class TraccarAPIClientServiceImpl implements DeviceAPIClientService {
client.addGroup(traccarGroups);
}
public void deleteGroup(DeviceGroup group) throws TraccarConfigurationException {
TrackerClient client = new TrackerClient();
TraccarGroups traccarGroups = new TraccarGroups(group.getName());
client.deleteGroup(traccarGroups);
}
}

@ -35,4 +35,8 @@ public interface TraccarClient {
void disDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException;
void addGroup(TraccarGroups groupInfo) throws TraccarConfigurationException;
void deleteGroup(TraccarGroups groupInfo) throws TraccarConfigurationException;
String getAllGroups() throws TraccarConfigurationException;
}

Loading…
Cancel
Save