fix NullPointerException

feature/traccar-sync
Rushdi Mohamed 3 years ago
parent 5c14315b4a
commit bd164ea78f

@ -122,51 +122,55 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap
response = client.newCall(request).execute(); response = client.newCall(request).execute();
if(method==TraccarHandlerConstants.Methods.POST){ if(method==TraccarHandlerConstants.Methods.POST){
String result = response.body().string(); String result = response.body().string();
JSONObject obj = new JSONObject(result); log.info(result);
if (obj.has("id")){ if(result.charAt(0)=='{'){
int traccarId = obj.getInt("id"); JSONObject obj = new JSONObject(result);
try { if (obj.has("id")){
TrackerManagementDAOFactory.beginTransaction(); int traccarId = obj.getInt("id");
if(type==TraccarHandlerConstants.Types.DEVICE){ try {
trackerDAO.addTrackerDevice(traccarId, deviceId, tenantId); TrackerManagementDAOFactory.beginTransaction();
TrackerDeviceInfo res = trackerDAO.getTrackerDevice(deviceId, tenantId); if(type==TraccarHandlerConstants.Types.DEVICE){
if(res.getStatus()==0){ trackerDAO.addTrackerDevice(traccarId, deviceId, tenantId);
trackerDAO.updateTrackerDeviceIdANDStatus(res.getTraccarDeviceId(), deviceId, tenantId, 1); TrackerDeviceInfo res = trackerDAO.getTrackerDevice(deviceId, tenantId);
if(res.getStatus()==0){
trackerDAO.updateTrackerDeviceIdANDStatus(res.getTraccarDeviceId(), deviceId, tenantId, 1);
}
}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.GROUP){ TrackerManagementDAOFactory.commitTransaction();
trackerDAO.addTrackerGroup(traccarId, groupId, tenantId); } catch (JSONException e) {
TrackerGroupInfo res = trackerDAO.getTrackerGroup(groupId, tenantId); TrackerManagementDAOFactory.rollbackTransaction();
if(res.getStatus()==0){ String msg = "Error occurred on JSON object .";
trackerDAO.updateTrackerGroupIdANDStatus(res.getTraccarGroupId(), groupId, tenantId, 1); 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 = "Error occurred while mapping with deviceId .";
}else if(type==TraccarHandlerConstants.Types.GROUP){
msg = "Error occurred while mapping with groupId .";
} }
log.error(msg, e);
} finally {
TrackerManagementDAOFactory.closeConnection();
} }
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 = "Error occurred while mapping with deviceId .";
}else if(type==TraccarHandlerConstants.Types.GROUP){
msg = "Error occurred while mapping with groupId .";
}
log.error(msg, e);
} finally {
TrackerManagementDAOFactory.closeConnection();
} }
response.close();
}
if (log.isDebugEnabled()) {
log.debug("Successfully the request is proceed and communicated with Traccar");
} }
response.close();
}
if (log.isDebugEnabled()) {
log.debug("Successfully the request is proceed and communicated with Traccar");
} }
} catch (IOException e) { } catch (IOException e) {
log.error("Couldnt connect to traccar.", e); log.error("Couldnt connect to traccar.", e);
} }
@ -362,10 +366,12 @@ public class TraccarClient implements org.wso2.carbon.device.mgt.core.traccar.ap
TrackerManagementDAOFactory.closeConnection(); TrackerManagementDAOFactory.closeConnection();
} }
String context = defaultPort+"/api/devices/"+obj.getInt("traccarDeviceId"); if(obj != null){
Runnable trackerExecutor = new TrackerExecutor(obj.getInt("traccarDeviceId"), tenantId, endpoint, context, null, String context = defaultPort+"/api/devices/"+obj.getInt("traccarDeviceId");
TraccarHandlerConstants.Methods.DELETE, TraccarHandlerConstants.Types.DEVICE); Runnable trackerExecutor = new TrackerExecutor(obj.getInt("traccarDeviceId"), tenantId, endpoint, context, null,
executor.execute(trackerExecutor); TraccarHandlerConstants.Methods.DELETE, TraccarHandlerConstants.Types.DEVICE);
executor.execute(trackerExecutor);
}
} }
/** /**

Loading…
Cancel
Save