Update with Traccar Dis-Enrolling a Device via UEM

master
Rushdi Mohamed 3 years ago
parent c4d75234b0
commit 8d99123ec1

@ -45,7 +45,7 @@ import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import org.wso2.carbon.device.mgt.core.report.mgt.Constants;
import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition;
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
import org.wso2.carbon.device.mgt.core.util.HttpReportingUtil;
import org.wso2.carbon.user.api.UserStoreException;
@ -391,7 +391,7 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
}
//Traccar update lat lon
TraccarDeviceInfo trackerinfo = new TraccarDeviceInfo(device.getDeviceIdentifier(),
TraccarPosition trackerinfo = new TraccarPosition(device.getDeviceIdentifier(),
deviceLocation.getUpdatedTime().getTime(),
deviceLocation.getLatitude(), deviceLocation.getLongitude(),
deviceLocation.getBearing(), deviceLocation.getSpeed());

@ -119,7 +119,8 @@ import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataDAO;
import org.wso2.carbon.device.mgt.core.metadata.mgt.dao.MetadataManagementDAOFactory;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.DeviceAPIClientServiceImpl;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo;
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.util.DeviceManagerUtil;
import org.wso2.carbon.email.sender.core.ContentProviderInfo;
import org.wso2.carbon.email.sender.core.EmailContext;
@ -396,7 +397,7 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
//Traccar update Latitude Longitude
String lastUpdatedTime = String.valueOf((new Date().getTime()));
TraccarDeviceInfo traccarDeviceInfo = new TraccarDeviceInfo(device.getName(), device.getDeviceIdentifier(),
TraccarDevice traccarDeviceInfo = new TraccarDevice(device.getName(), device.getDeviceIdentifier(),
"online", "false", lastUpdatedTime, "", "", "", "",
"", "");
DeviceAPIClientServiceImpl dac= new DeviceAPIClientServiceImpl();
@ -566,6 +567,15 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv
deviceDAO.updateDevice(device, tenantId);
DeviceManagementDAOFactory.commitTransaction();
this.removeDeviceFromCache(deviceId);
//Traccar update Latitude Longitude
TraccarDevice traccarDeviceInfo = new TraccarDevice(device.getDeviceIdentifier());
DeviceAPIClientServiceImpl dac= new DeviceAPIClientServiceImpl();
String deviceAPIClientResponse=dac.disDevice(traccarDeviceInfo);
if (log.isDebugEnabled()) {
log.debug("Disenroll Device "+ new Gson().toJson(deviceAPIClientResponse));
}
//Traccar update Latitude Longitude
} catch (DeviceManagementDAOException e) {
DeviceManagementDAOFactory.rollbackTransaction();
String msg = "Error occurred while dis-enrolling '" + deviceId.getType() +

@ -19,15 +19,16 @@
package org.wso2.carbon.device.mgt.core.traccar.api.service;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition;
import java.io.IOException;
public interface DeviceAPIClientService {
String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException;
String updateLocation(TraccarPosition deviceInfo) throws IOException;
String addDevice(TraccarDeviceInfo deviceInfo) throws IOException;
String addDevice(TraccarDevice deviceInfo) throws IOException;
String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException;
String disDevice(TraccarDevice deviceInfo) throws IOException;
}

@ -19,6 +19,7 @@
package org.wso2.carbon.device.mgt.core.traccar.api.service.addons;
import com.google.gson.Gson;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
@ -26,10 +27,13 @@ import okhttp3.RequestBody;
import okhttp3.Response;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
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.beans.TraccarDeviceInfo;
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.config.TraccarGateway;
import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager;
@ -45,7 +49,26 @@ import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConst
public class TrackerClient implements TraccarClient {
private static final Log log = LogFactory.getLog(TrackerClient.class);
public String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException{
public Request getDeviceByDeviceIdentifier(String deviceId) {
//device identifier matches with traccar uniqueId
//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(MAIN_ENDPOINT).getValue();
String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue();
String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue();
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
.url(endpoint+"/devices?uniqueId="+deviceId)
.method("GET", null)
.addHeader(authorization, authorizationKey)
.build();
return request;
}
public String updateLocation(TraccarPosition deviceInfo) throws IOException {
//Retrieve the traccar Gateway by passing the Gateway name
TraccarGateway traccarGateway = getTraccarGateway();
@ -65,8 +88,7 @@ public class TrackerClient implements TraccarClient {
return String.valueOf(response);
}
public String addDevice(TraccarDeviceInfo deviceInfo) throws IOException{
public String addDevice(TraccarDevice deviceInfo) throws IOException{
//Retrieve the traccar Gateway by passing the Gateway name
TraccarGateway traccarGateway = getTraccarGateway();
@ -95,7 +117,6 @@ public class TrackerClient implements TraccarClient {
data.put("attributes", new JSONObject());
RequestBody body = RequestBody.create(mediaType, data.toString());
Request request = new Request.Builder()
.url(endpoint+"/devices")
.method("POST", body)
@ -107,8 +128,7 @@ public class TrackerClient implements TraccarClient {
return String.valueOf(response);
}
// /TODO FIX THIS WITH GET REQUEST
public String deleteDevice(TraccarDeviceInfo deviceInfo){
public String disDevice(TraccarDevice deviceInfo) throws IOException {
//Retrieve the traccar Gateway by passing the Gateway name
TraccarGateway traccarGateway = getTraccarGateway();
@ -118,22 +138,33 @@ public class TrackerClient implements TraccarClient {
String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue();
String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue();
/*
OkHttpClient client = new OkHttpClient().newBuilder().build();
MediaType mediaType = MediaType.parse("text/plain");
RequestBody body = RequestBody.create(mediaType, "");
Request request = new Request.Builder()
.url("endpoint+"/devices/"+deviceInfo)
.method("DELETE", body)
.addHeader(authorization, authorizationKey)
.build();
Response response = client.newCall(request).execute();
*/
return "";
OkHttpClient client = new OkHttpClient();
Request deviceDetails = getDeviceByDeviceIdentifier(deviceInfo.getDeviceIdentifier());
Response response = client.newCall(deviceDetails).execute();
String result = response.body().string();
String jsonData ="{"+ "\"geodata\": "+ result+ "}";
try {
JSONObject obj = new JSONObject(jsonData);
JSONArray geodata = obj.getJSONArray("geodata");
JSONObject jsonResponse = geodata.getJSONObject(0);
OkHttpClient client1 = new OkHttpClient();
Request request1 = new Request.Builder()
.url(endpoint+"/devices/"+jsonResponse.getInt("id")).delete()
.addHeader(authorization, authorizationKey).build();
Response response1 = client1.newCall(request1).execute();
log.info(String.valueOf(response1));
return String.valueOf(response1);
} catch (JSONException e) {
log.info("Delete Error "+e);
return String.valueOf(e);
}
}
private TraccarGateway getTraccarGateway(){
return TraccarConfigurationManager.getInstance().getTraccarConfig().getTraccarGateway(TraccarHandlerConstants.GATEWAY_NAME);
return TraccarConfigurationManager.getInstance().getTraccarConfig().getTraccarGateway(
TraccarHandlerConstants.GATEWAY_NAME);
}
}

@ -21,24 +21,25 @@ package org.wso2.carbon.device.mgt.core.traccar.api.service.impl;
import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService;
import org.wso2.carbon.device.mgt.core.traccar.api.service.addons.TrackerClient;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition;
import java.io.IOException;
public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
public String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException {
public String updateLocation(TraccarPosition deviceInfo) throws IOException {
TrackerClient client = new TrackerClient();
return (client.updateLocation(deviceInfo));
}
public String addDevice(TraccarDeviceInfo deviceInfo) throws IOException {
public String addDevice(TraccarDevice deviceInfo) throws IOException {
TrackerClient client = new TrackerClient();
return (client.addDevice(deviceInfo));
}
public String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException {
public String disDevice(TraccarDevice deviceInfo) throws IOException {
TrackerClient client = new TrackerClient();
return (client.deleteDevice(deviceInfo));
return (client.disDevice(deviceInfo));
}
}

@ -19,15 +19,19 @@
package org.wso2.carbon.device.mgt.core.traccar.common;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo;
import okhttp3.Request;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice;
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition;
import java.io.IOException;
public interface TraccarClient {
String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException;
String updateLocation(TraccarPosition deviceInfo) throws IOException;
String addDevice(TraccarDeviceInfo deviceInfo) throws IOException;
String addDevice(TraccarDevice deviceInfo) throws IOException;
String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException;
Request getDeviceByDeviceIdentifier(String deviceInfo) throws IOException;
String disDevice(TraccarDevice deviceInfo) throws IOException;
}

@ -0,0 +1,106 @@
/*
* Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
*
* Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
*
* Licensed under the Entgra Commercial License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://entgra.io/licenses/entgra-commercial/1.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.traccar.common.beans;
public class TraccarDevice {
private String deviceIdentifier;
private String deviceName;
private String uniqueId;
private String status;
private String disabled;
private String lastUpdate;
private String positionId;
private String groupId;
private String phone;
private String model;
private String contact;
private String category;
public TraccarDevice(String deviceIdentifier){
this.deviceIdentifier =deviceIdentifier;
}
public TraccarDevice(String deviceName, String uniqueId, String status, String disabled, String lastUpdate,
String positionId, String groupId, String phone, String model, String contact,
String category){
this.deviceName =deviceName;
this.uniqueId=uniqueId;
this.status=status;
this.disabled =disabled;
this.lastUpdate =lastUpdate;
this.positionId =positionId;
this.groupId =groupId;
this.phone =phone;
this.model =model;
this.contact =contact;
this.category =category;
}
public TraccarDevice(){ }
public String getDeviceIdentifier() {return deviceIdentifier;}
public void setDeviceIdentifier(String deviceIdentifier) {this.deviceIdentifier = deviceIdentifier;}
public String getDeviceName() {return deviceName;}
public void setDeviceName(String deviceName) {this.deviceName = deviceName;}
public String getUniqueId() {return uniqueId;}
public void setUniqueId(String uniqueId) {this.uniqueId = uniqueId;}
public String getStatus() {return status;}
public void setStatus(String status) {this.status = status;}
public String getDisabled() {return disabled;}
public void setDisabled(String disabled) {this.disabled = disabled;}
public String getLastUpdate() {return lastUpdate;}
public void setLastUpdate(String lastUpdate) {this.lastUpdate = lastUpdate;}
public String getPhone() {return phone;}
public void setPhone(String phone) {this.phone = phone;}
public String getModel() {return model;}
public void setModel(String model) {this.model = model;}
public String getContact() {return contact;}
public void setContact(String contact) {this.contact = contact;}
public String getCategory() {return category;}
public void setCategory(String category) {this.category = category;}
public String getPositionId() {return positionId;}
public void setPositionId(String positionId) {this.positionId = positionId;}
public String getGroupId() {return groupId;}
public void setGroupId(String groupId) {this.groupId = groupId;}
}

@ -1,206 +0,0 @@
/*
* Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
*
* Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
*
* Licensed under the Entgra Commercial License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://entgra.io/licenses/entgra-commercial/1.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.traccar.common.beans;
public class TraccarDeviceInfo {
private String deviceIdentifier;
private Long timestamp;
private Double lat;
private Double lon;
private Float bearing;
private Float speed;
private String deviceName;
private String uniqueId;
private String status;
private String disabled;
private String lastUpdate;
private String positionId;
private String groupId;
private String phone;
private String model;
private String contact;
private String category;
public TraccarDeviceInfo(String deviceIdentifier, long timestamp, Double lat, Double lon,
float bearing, float speed){
this.deviceIdentifier =deviceIdentifier;
this.timestamp=timestamp;
this.lat=lat;
this.lon =lon;
this.bearing =bearing;
this.speed =speed;
}
public TraccarDeviceInfo(String deviceName, String uniqueId, String status, String disabled, String lastUpdate,
String positionId, String groupId, String phone, String model, String contact,
String category){
this.deviceName =deviceName;
this.uniqueId=uniqueId;
this.status=status;
this.disabled =disabled;
this.lastUpdate =lastUpdate;
this.positionId =positionId;
this.groupId =groupId;
this.phone =phone;
this.model =model;
this.contact =contact;
this.category =category;
}
public TraccarDeviceInfo(){ }
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
public Float getBearing() {
return bearing;
}
public void setBearing(Float bearing) {
this.bearing = bearing;
}
public Float getSpeed() {
return speed;
}
public void setSpeed(Float speed) {
this.speed = speed;
}
public String getDeviceName() {
return deviceName;
}
public void setDeviceName(String deviceName) {
this.deviceName = deviceName;
}
public String getUniqueId() {
return uniqueId;
}
public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDisabled() {
return disabled;
}
public void setDisabled(String disabled) {
this.disabled = disabled;
}
public String getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(String lastUpdate) {
this.lastUpdate = lastUpdate;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(String deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
public String getPositionId() {
return positionId;
}
public void setPositionId(String positionId) {
this.positionId = positionId;
}
public String getGroupId() {
return groupId;
}
public void setGroupId(String groupId) {
this.groupId = groupId;
}
}

@ -0,0 +1,90 @@
/*
* Copyright (C) 2018 - 2022 Entgra (Pvt) Ltd, Inc - All Rights Reserved.
*
* Unauthorised copying/redistribution of this file, via any medium is strictly prohibited.
*
* Licensed under the Entgra Commercial License, Version 1.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://entgra.io/licenses/entgra-commercial/1.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.wso2.carbon.device.mgt.core.traccar.common.beans;
public class TraccarPosition {
private String deviceIdentifier;
private Long timestamp;
private Double lat;
private Double lon;
private Float bearing;
private Float speed;
public TraccarPosition(String deviceIdentifier, long timestamp, Double lat, Double lon,
float bearing, float speed){
this.deviceIdentifier =deviceIdentifier;
this.timestamp=timestamp;
this.lat=lat;
this.lon =lon;
this.bearing =bearing;
this.speed =speed;
}
public TraccarPosition(){ }
public String getDeviceIdentifier() {
return deviceIdentifier;
}
public void setDeviceIdentifier(String deviceIdentifier) {
this.deviceIdentifier = deviceIdentifier;
}
public Long getTimestamp() {
return timestamp;
}
public void setTimestamp(Long timestamp) {
this.timestamp = timestamp;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
public Float getBearing() {
return bearing;
}
public void setBearing(Float bearing) {
this.bearing = bearing;
}
public Float getSpeed() {
return speed;
}
public void setSpeed(Float speed) {
this.speed = speed;
}
}
Loading…
Cancel
Save