|
|
@ -19,7 +19,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
package org.wso2.carbon.device.mgt.core.traccar.api.service.addons;
|
|
|
|
package org.wso2.carbon.device.mgt.core.traccar.api.service.addons;
|
|
|
|
|
|
|
|
|
|
|
|
import com.google.gson.Gson;
|
|
|
|
import okhttp3.ConnectionPool;
|
|
|
|
import okhttp3.MediaType;
|
|
|
|
import okhttp3.MediaType;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.OkHttpClient;
|
|
|
|
import okhttp3.Request;
|
|
|
|
import okhttp3.Request;
|
|
|
@ -30,136 +30,192 @@ import org.apache.commons.logging.LogFactory;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
|
|
import org.wso2.carbon.device.mgt.common.event.config.EventConfigurationException;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarClient;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarClient;
|
|
|
|
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.TraccarPosition;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition;
|
|
|
|
|
|
|
|
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.core.config.TraccarConfigurationManager;
|
|
|
|
import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager;
|
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import java.util.concurrent.ExecutorService;
|
|
|
|
|
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.ENDPOINT;
|
|
|
|
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.ENDPOINT;
|
|
|
|
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.AUTHORIZATION;
|
|
|
|
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.AUTHORIZATION;
|
|
|
|
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.AUTHORIZATION_KEY;
|
|
|
|
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.AUTHORIZATION_KEY;
|
|
|
|
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.MAIN_ENDPOINT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class TrackerClient implements TraccarClient {
|
|
|
|
public class TrackerClient implements TraccarClient {
|
|
|
|
private static final Log log = LogFactory.getLog(TrackerClient.class);
|
|
|
|
private static final Log log = LogFactory.getLog(TrackerClient.class);
|
|
|
|
|
|
|
|
private static final int THREAD_POOL_SIZE = 50;
|
|
|
|
|
|
|
|
private final OkHttpClient client;
|
|
|
|
|
|
|
|
private final ExecutorService executor = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
final TraccarGateway traccarGateway = getTraccarGateway();
|
|
|
|
|
|
|
|
final String endpoint = traccarGateway.getPropertyByName(ENDPOINT).getValue();
|
|
|
|
|
|
|
|
final String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue();
|
|
|
|
|
|
|
|
final String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public TrackerClient() {
|
|
|
|
|
|
|
|
client = new OkHttpClient.Builder()
|
|
|
|
|
|
|
|
.connectTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
|
|
|
.writeTimeout(30, TimeUnit.SECONDS)
|
|
|
|
|
|
|
|
.readTimeout(45, TimeUnit.SECONDS)
|
|
|
|
|
|
|
|
.connectionPool(new ConnectionPool(50,30,TimeUnit.SECONDS))
|
|
|
|
|
|
|
|
.build();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Request getDeviceByDeviceIdentifier(String deviceId) {
|
|
|
|
private class TrackerExecutor implements Runnable {
|
|
|
|
//device identifier matches with traccar uniqueId
|
|
|
|
final JSONObject payload;
|
|
|
|
//Retrieve the traccar Gateway by passing the Gateway name
|
|
|
|
final String context;
|
|
|
|
TraccarGateway traccarGateway = getTraccarGateway();
|
|
|
|
final String publisherUrl;
|
|
|
|
|
|
|
|
private final String method;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private TrackerExecutor(String publisherUrl, String context, JSONObject payload, String method) {
|
|
|
|
|
|
|
|
this.payload = payload;
|
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
|
|
|
|
this.publisherUrl = publisherUrl;
|
|
|
|
|
|
|
|
this.method = method;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Retrieve the properties in the Traccar Gateway by passing the property name
|
|
|
|
public void run() {
|
|
|
|
String endpoint = traccarGateway.getPropertyByName(MAIN_ENDPOINT).getValue();
|
|
|
|
RequestBody requestBody;
|
|
|
|
String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue();
|
|
|
|
Request.Builder builder = new Request.Builder();
|
|
|
|
String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue();
|
|
|
|
Request request;
|
|
|
|
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
|
|
|
if(method=="post"){
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), payload.toString());
|
|
|
|
.url(endpoint+"/devices?uniqueId="+deviceId)
|
|
|
|
builder = builder.post(requestBody);
|
|
|
|
.method("GET", null)
|
|
|
|
}else if(method=="delete"){
|
|
|
|
|
|
|
|
builder = builder.delete();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
request = builder.url(publisherUrl + context)
|
|
|
|
.addHeader(authorization, authorizationKey)
|
|
|
|
.addHeader(authorization, authorizationKey)
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
return request;
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
client.newCall(request).execute();
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
|
|
|
log.debug("Successfully the request is proceed and communicated with Traccar");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
log.error("Error occurred", e);
|
|
|
|
|
|
|
|
|
|
|
|
public String updateLocation(TraccarPosition deviceInfo) throws IOException {
|
|
|
|
}
|
|
|
|
//Retrieve the traccar Gateway by passing the Gateway name
|
|
|
|
}
|
|
|
|
TraccarGateway traccarGateway = getTraccarGateway();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//Retrieve the properties in the Traccar Gateway by passing the property name
|
|
|
|
/**
|
|
|
|
String endpoint = traccarGateway.getPropertyByName(ENDPOINT).getValue();
|
|
|
|
* Add Traccar Device operation.
|
|
|
|
|
|
|
|
* @param deviceInfo with DeviceName UniqueId, Status, Disabled LastUpdate, PositionId, GroupId
|
|
|
|
|
|
|
|
* Model, Contact, Category, fenceIds
|
|
|
|
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device the operation
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public void addDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException {
|
|
|
|
|
|
|
|
try{
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
|
|
String context = "8082/api/devices";
|
|
|
|
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(endpoint, context, payload, "post");
|
|
|
|
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
|
|
|
|
log.info("Device successfully enorolled on traccar");
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
|
|
|
String msg="Could not enroll traccar device";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
|
|
|
/**
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
* Add Device GPS Location operation.
|
|
|
|
.url(endpoint+"id="+deviceInfo.getDeviceIdentifier()+
|
|
|
|
* @param deviceInfo with DeviceIdentifier, Timestamp, Lat, Lon, Bearing, Speed, ignition
|
|
|
|
"×tamp="+deviceInfo.getTimestamp()+
|
|
|
|
*/
|
|
|
|
"&lat="+deviceInfo.getLat()+"&lon="+deviceInfo.getLon()+
|
|
|
|
public void updateLocation(TraccarPosition deviceInfo) throws TraccarConfigurationException {
|
|
|
|
"&bearing="+deviceInfo.getBearing()+"&speed="+deviceInfo.getSpeed()+"&ignition=true")
|
|
|
|
try{
|
|
|
|
.method("GET", null)
|
|
|
|
String context = "5055/?id="+deviceInfo.getDeviceIdentifier()+"×tamp="+deviceInfo.getTimestamp()+
|
|
|
|
.build();
|
|
|
|
"&lat="+deviceInfo.getLat()+"&lon="+deviceInfo.getLon()+"&bearing="+deviceInfo.getBearing()+
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
"&speed="+deviceInfo.getSpeed()+"&ignition=true";
|
|
|
|
log.info(String.valueOf(response));
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(endpoint, context, null, "get");
|
|
|
|
return String.valueOf(response);
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
}
|
|
|
|
log.info("Device GPS location added on traccar");
|
|
|
|
|
|
|
|
}catch (Exception e){
|
|
|
|
public String addDevice(TraccarDevice deviceInfo) throws IOException{
|
|
|
|
String msg="Could not add GPS location";
|
|
|
|
//Retrieve the traccar Gateway by passing the Gateway name
|
|
|
|
log.error(msg, e);
|
|
|
|
TraccarGateway traccarGateway = getTraccarGateway();
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
//Retrieve the properties in the Traccar Gateway by passing the property name
|
|
|
|
}
|
|
|
|
String endpoint = traccarGateway.getPropertyByName(MAIN_ENDPOINT).getValue();
|
|
|
|
|
|
|
|
String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue();
|
|
|
|
/**
|
|
|
|
String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue();
|
|
|
|
* Add Device GPS Location operation.
|
|
|
|
|
|
|
|
* @param deviceIdentifier
|
|
|
|
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
|
|
|
* @return device info
|
|
|
|
MediaType mediaType = MediaType.parse("application/json");
|
|
|
|
* @throws TraccarConfigurationException Failed while add Traccar Device location operation
|
|
|
|
|
|
|
|
*/
|
|
|
|
JSONObject data = new JSONObject();
|
|
|
|
public String getDeviceByDeviceIdentifier(String deviceIdentifier) throws TraccarConfigurationException {
|
|
|
|
data.put("name", deviceInfo.getDeviceName());
|
|
|
|
try {
|
|
|
|
data.put("uniqueId", deviceInfo.getUniqueId());
|
|
|
|
String context = "8082/api/devices?uniqueId="+deviceIdentifier;
|
|
|
|
data.put("status", deviceInfo.getStatus());
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(endpoint, context, null, "get");
|
|
|
|
data.put("disabled", deviceInfo.getDisabled());
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
data.put("lastUpdate", deviceInfo.getLastUpdate());
|
|
|
|
|
|
|
|
data.put("positionId", deviceInfo.getPositionId());
|
|
|
|
|
|
|
|
data.put("groupId", deviceInfo.getGroupId());
|
|
|
|
|
|
|
|
data.put("phone", deviceInfo.getPhone());
|
|
|
|
|
|
|
|
data.put("model", deviceInfo.getModel());
|
|
|
|
|
|
|
|
data.put("contact", deviceInfo.getContact());
|
|
|
|
|
|
|
|
data.put("category", deviceInfo.getCategory());
|
|
|
|
|
|
|
|
List<String> geofenceIds = new ArrayList<>();
|
|
|
|
|
|
|
|
data.put("geofenceIds", geofenceIds);
|
|
|
|
|
|
|
|
data.put("attributes", new JSONObject());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
RequestBody body = RequestBody.create(mediaType, data.toString());
|
|
|
|
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
Request request = new Request.Builder()
|
|
|
|
.url(endpoint+"/devices")
|
|
|
|
.url(endpoint+context)
|
|
|
|
.method("POST", body)
|
|
|
|
|
|
|
|
.addHeader("Content-Type", "application/json")
|
|
|
|
|
|
|
|
.addHeader(authorization, authorizationKey)
|
|
|
|
.addHeader(authorization, authorizationKey)
|
|
|
|
.build();
|
|
|
|
.build();
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
Response response = client.newCall(request).execute();
|
|
|
|
|
|
|
|
String result = response.body().string();
|
|
|
|
return String.valueOf(response);
|
|
|
|
log.info("Device info found");
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
|
|
|
|
String msg="Could not find device information";
|
|
|
|
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, e);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public String disDevice(TraccarDevice deviceInfo) throws IOException {
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Dis-enroll a Device operation.
|
|
|
|
//Retrieve the traccar Gateway by passing the Gateway name
|
|
|
|
* @param deviceInfo identified via deviceIdentifier
|
|
|
|
TraccarGateway traccarGateway = getTraccarGateway();
|
|
|
|
* @throws TraccarConfigurationException Failed while dis-enroll a Traccar Device operation
|
|
|
|
|
|
|
|
*/
|
|
|
|
//Retrieve the properties in the Traccar Gateway by passing the property name
|
|
|
|
public void disDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException {
|
|
|
|
String endpoint = traccarGateway.getPropertyByName(MAIN_ENDPOINT).getValue();
|
|
|
|
try{
|
|
|
|
String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue();
|
|
|
|
String result = getDeviceByDeviceIdentifier(deviceInfo.getDeviceIdentifier());
|
|
|
|
String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
OkHttpClient client = new OkHttpClient();
|
|
|
|
|
|
|
|
Request deviceDetails = getDeviceByDeviceIdentifier(deviceInfo.getDeviceIdentifier());
|
|
|
|
|
|
|
|
Response response = client.newCall(deviceDetails).execute();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String result = response.body().string();
|
|
|
|
|
|
|
|
String jsonData ="{"+ "\"geodata\": "+ result+ "}";
|
|
|
|
String jsonData ="{"+ "\"geodata\": "+ result+ "}";
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
JSONObject obj = new JSONObject(jsonData);
|
|
|
|
JSONObject obj = new JSONObject(jsonData);
|
|
|
|
JSONArray geodata = obj.getJSONArray("geodata");
|
|
|
|
JSONArray geodata = obj.getJSONArray("geodata");
|
|
|
|
JSONObject jsonResponse = geodata.getJSONObject(0);
|
|
|
|
JSONObject jsonResponse = geodata.getJSONObject(0);
|
|
|
|
|
|
|
|
|
|
|
|
OkHttpClient client1 = new OkHttpClient();
|
|
|
|
String context = "8082/api/devices/"+jsonResponse.getInt("id");
|
|
|
|
Request request1 = new Request.Builder()
|
|
|
|
Runnable trackerExecutor = new TrackerExecutor(endpoint, context, null, "delete");
|
|
|
|
.url(endpoint+"/devices/"+jsonResponse.getInt("id")).delete()
|
|
|
|
executor.execute(trackerExecutor);
|
|
|
|
.addHeader(authorization, authorizationKey).build();
|
|
|
|
log.info("Device successfully dis-enrolled");
|
|
|
|
Response response1 = client1.newCall(request1).execute();
|
|
|
|
|
|
|
|
log.info(String.valueOf(response1));
|
|
|
|
|
|
|
|
return String.valueOf(response1);
|
|
|
|
|
|
|
|
}catch (JSONException e){
|
|
|
|
}catch (JSONException e){
|
|
|
|
log.info("Delete Error "+e);
|
|
|
|
String msg = "Could not find the device infomation to dis-enroll the device";
|
|
|
|
return String.valueOf(e);
|
|
|
|
log.error(msg, e);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg);
|
|
|
|
|
|
|
|
}catch (TraccarConfigurationException ex){
|
|
|
|
|
|
|
|
String msg = "Could not find the device infomation to dis-enroll the device";
|
|
|
|
|
|
|
|
log.error(msg, ex);
|
|
|
|
|
|
|
|
throw new TraccarConfigurationException(msg, ex);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|