Device names in endpoint-mgt and traccar are not consistent - Fix (#21)

Fix: [https://roadmap.entgra.net/issues/9656](https://roadmap.entgra.net/issues/9656)

When renaming an agent, the update wasn't send to the traccar server.
Added new methods and combined it with existing agent modification code

Co-authored-by: Deenath Geegange <deenathgeeganage@gmail.com>
Co-authored-by: Pahansith Gunathilake <pahansith@entgra.io>
Reviewed-on: community/device-mgt-core#21
Co-authored-by: Deenath  Geeganage <deenathgeeganage@gmail.com>
Co-committed-by: Deenath  Geeganage <deenathgeeganage@gmail.com>
build-status-badge
Deenath Geeganage 2 years ago committed by Pahansith Gunathilake
parent 58ea13095e
commit 7e914aeafd

@ -527,6 +527,24 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
DeviceManagementDAOFactory.closeConnection(); DeviceManagementDAOFactory.closeConnection();
} }
extractDeviceLocationToUpdate(device); extractDeviceLocationToUpdate(device);
//enroll Traccar device
if (HttpReportingUtil.isTrackerEnabled()) {
try {
int tenantId = this.getTenantId();
DeviceManagementDataHolder.getInstance().getDeviceAPIClientService().modifyDevice(device, tenantId);
} catch (ExecutionException e) {
log.error("ExecutionException : " + e);
//throw new RuntimeException(e);
//Exception was not thrown due to being conflicted with non-traccar features
} catch (InterruptedException e) {
log.error("InterruptedException : " + e);
//throw new RuntimeException(e);
//Exception was not thrown due to being conflicted with non-traccar features
}
} else {
log.info("Traccar is disabled");
}
//enroll Traccar device
return status; return status;
} }

@ -50,6 +50,14 @@ public interface DeviceAPIClientService {
*/ */
void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException; void addDevice(Device device, int tenantId) throws ExecutionException, InterruptedException;
/**
* Updates device Traccar configuration records (like device name)
*
* @params device to be modifies
* @throws TrackerManagementDAOException errors thrown while modifing a traccar device
*/
void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException;
/** /**
* Delete a device Traccar configuration records * Delete a device Traccar configuration records
* *

@ -469,6 +469,54 @@ public class TraccarClientFactory {
} }
} }
/**
* Modify the Device
*
* @param traccarDevice with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId
* Model, Contact, Category, fenceIds
* @throws TrackerManagementDAOException Failed while add Traccar Device the operation
*/
public void modifyDevice(TraccarDevice traccarDevice, int tenantId) throws TrackerManagementDAOException, ExecutionException, InterruptedException {
TrackerDeviceInfo trackerDeviceInfo = null;
try {
TrackerManagementDAOFactory.openConnection();
trackerDeviceInfo = trackerDAO.getTrackerDevice(traccarDevice.getId(), tenantId);
} catch (TrackerManagementDAOException e) {
String msg = "Error occurred while mapping with deviceId .";
log.error(msg, e);
throw new TrackerManagementDAOException(msg, e);
} catch (SQLException e) {
String msg = "Error occurred establishing the DB connection .";
log.error(msg, e);
throw new TrackerManagementDAOException(msg, e);
} finally {
TrackerManagementDAOFactory.closeConnection();
}
if (trackerDeviceInfo != null) {
log.info("Preparing to rename device (" + traccarDevice.getId() + " to taccar server");
String url = defaultPort + "/api/devices/" + trackerDeviceInfo.getTraccarDeviceId();
JSONObject payload = TraccarUtil.TraccarDevicePayload(traccarDevice, trackerDeviceInfo.getTraccarDeviceId());
Future<String> res = executor.submit(new OkHttpClientThreadPool(url, payload, TraccarHandlerConstants.Methods.PUT,
authorizedKey(HttpReportingUtil.trackerUser(), HttpReportingUtil.trackerPassword()),
serverUrl(HttpReportingUtil.trackerServer())));
String result = res.get();
log.info("Device " + traccarDevice.getDeviceIdentifier() + " has been added to Traccar.");
if (res.isDone() && result.charAt(0) == '{') {
log.info("Succesfully renamed Traccar Device - " + traccarDevice.getId() + "in server");
} else {
log.info("Failed to rename Traccar Device" + traccarDevice.getId() + "in server");
}
} else {
// forward to add device
try {
addDevice(traccarDevice, tenantId);
} catch (TrackerAlreadyExistException e) {
String msg = "The device already exist";
log.error(msg, e);
}
}
}
/** /**
* Add Device GPS Location operation. * Add Device GPS Location operation.
* *

@ -62,6 +62,17 @@ public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
} }
} }
@Override
public void modifyDevice(Device device, int tenantId) throws ExecutionException, InterruptedException {
TraccarDevice traccarDevice = new TraccarDevice(device.getId(), device.getDeviceIdentifier(), device.getName());
try {
client.modifyDevice(traccarDevice, tenantId);
} catch (TrackerManagementDAOException e) {
String msg = "Error occurred while mapping with deviceId";
log.error(msg, e);
}
}
@Override @Override
public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException { public void updateLocation(Device device, DeviceLocation deviceLocation, int tenantId) throws ExecutionException, InterruptedException {
TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(),

@ -55,6 +55,12 @@ public class TraccarDevice {
this.category =category; this.category =category;
} }
public TraccarDevice(int id, String uniqueId, String deviceName) {
this.id = id;
this.uniqueId = uniqueId;
this.deviceName = deviceName;
}
public TraccarDevice(){ } public TraccarDevice(){ }
public int getId() { return id; } public int getId() { return id; }

@ -66,4 +66,12 @@ public class TraccarUtil {
payload.put("attributes", new JSONObject()); payload.put("attributes", new JSONObject());
return payload; return payload;
} }
public static JSONObject TraccarDevicePayload(TraccarDevice deviceInfo, int id) {
JSONObject payload = new JSONObject();
payload.put("id", id);
payload.put("name", deviceInfo.getDeviceName());
payload.put("uniqueId", deviceInfo.getUniqueId());
return payload;
}
} }

Loading…
Cancel
Save