Merge pull request 'taccar server not updating when device modified - fix (Bug #9656)' (#6) from traccar_consistency_issue into master

Reviewed-on: deenath/device-mgt-core#6
pull/21/head
Deenath Geeganage 2 years ago
commit 3ac643359f

@ -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,52 @@ 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
String msg = "Tracker device for device id " + traccarDevice.getId() + " not found in local database";
log.error(msg);
throw new TrackerManagementDAOException(msg);
}
}
/** /**
* 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.getName(), device.getDeviceIdentifier(), null,null,null,null,null,null,null,null,null);
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(),

@ -66,4 +66,14 @@ 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());
// add other params if needed
// as long as its null it wintt be overitten
return payload;
}
} }

Loading…
Cancel
Save