forked from community/device-mgt-core
parent
8636767cba
commit
c4d75234b0
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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.api.service;
|
||||
|
||||
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface DeviceAPIClientService {
|
||||
|
||||
String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException;
|
||||
|
||||
String addDevice(TraccarDeviceInfo deviceInfo) throws IOException;
|
||||
|
||||
String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException;
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
/*
|
||||
* 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.api.service.addons;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import okhttp3.Response;
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
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.config.TraccarGateway;
|
||||
import org.wso2.carbon.device.mgt.core.traccar.core.config.TraccarConfigurationManager;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
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_KEY;
|
||||
import static org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants.MAIN_ENDPOINT;
|
||||
|
||||
public class TrackerClient implements TraccarClient {
|
||||
private static final Log log = LogFactory.getLog(TrackerClient.class);
|
||||
|
||||
public String updateLocation(TraccarDeviceInfo 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();
|
||||
|
||||
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
||||
Request request = new Request.Builder()
|
||||
.url(endpoint+"id="+deviceInfo.getDeviceIdentifier()+
|
||||
"×tamp="+deviceInfo.getTimestamp()+
|
||||
"&lat="+deviceInfo.getLat()+"&lon="+deviceInfo.getLon()+
|
||||
"&bearing="+deviceInfo.getBearing()+"&speed="+deviceInfo.getSpeed()+"&ignition=true")
|
||||
.method("GET", null)
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
log.info(String.valueOf(response));
|
||||
return String.valueOf(response);
|
||||
}
|
||||
|
||||
public String addDevice(TraccarDeviceInfo 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(MAIN_ENDPOINT).getValue();
|
||||
String authorization = traccarGateway.getPropertyByName(AUTHORIZATION).getValue();
|
||||
String authorizationKey = traccarGateway.getPropertyByName(AUTHORIZATION_KEY).getValue();
|
||||
|
||||
OkHttpClient client = new OkHttpClient().newBuilder().build();
|
||||
MediaType mediaType = MediaType.parse("application/json");
|
||||
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("name", deviceInfo.getDeviceName());
|
||||
data.put("uniqueId", deviceInfo.getUniqueId());
|
||||
data.put("status", deviceInfo.getStatus());
|
||||
data.put("disabled", deviceInfo.getDisabled());
|
||||
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()
|
||||
.url(endpoint+"/devices")
|
||||
.method("POST", body)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.addHeader(authorization, authorizationKey)
|
||||
.build();
|
||||
Response response = client.newCall(request).execute();
|
||||
|
||||
return String.valueOf(response);
|
||||
}
|
||||
|
||||
// /TODO FIX THIS WITH GET REQUEST
|
||||
public String deleteDevice(TraccarDeviceInfo deviceInfo){
|
||||
|
||||
//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();
|
||||
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 "";
|
||||
}
|
||||
|
||||
private TraccarGateway getTraccarGateway(){
|
||||
return TraccarConfigurationManager.getInstance().getTraccarConfig().getTraccarGateway(TraccarHandlerConstants.GATEWAY_NAME);
|
||||
}
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* 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.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 java.io.IOException;
|
||||
|
||||
public class DeviceAPIClientServiceImpl implements DeviceAPIClientService {
|
||||
|
||||
public String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException {
|
||||
TrackerClient client = new TrackerClient();
|
||||
return (client.updateLocation(deviceInfo));
|
||||
}
|
||||
|
||||
public String addDevice(TraccarDeviceInfo deviceInfo) throws IOException {
|
||||
TrackerClient client = new TrackerClient();
|
||||
return (client.addDevice(deviceInfo));
|
||||
}
|
||||
|
||||
public String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException {
|
||||
TrackerClient client = new TrackerClient();
|
||||
return (client.deleteDevice(deviceInfo));
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDeviceInfo;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public interface TraccarClient {
|
||||
|
||||
String updateLocation(TraccarDeviceInfo deviceInfo) throws IOException;
|
||||
|
||||
String addDevice(TraccarDeviceInfo deviceInfo) throws IOException;
|
||||
|
||||
String deleteDevice(TraccarDeviceInfo deviceInfo) throws IOException;
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
public class TraccarHandlerConstants {
|
||||
public static final String TRACCAR_CONFIG_XML_NAME = "traccar-config.xml";
|
||||
public static final String GATEWAY_NAME = "sample";
|
||||
public static final String MAIN_ENDPOINT = "api-endpoint";
|
||||
public static final String ENDPOINT = "add-location-api-endpoint";
|
||||
public static final String AUTHORIZATION = "authorization";
|
||||
public static final String AUTHORIZATION_KEY = "authorization-key";
|
||||
}
|
@ -0,0 +1,206 @@
|
||||
/*
|
||||
* 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,49 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlValue;
|
||||
|
||||
@XmlRootElement(name = "Property")
|
||||
public class Property {
|
||||
|
||||
private String name;
|
||||
private String value;
|
||||
|
||||
@XmlAttribute(name = "name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@XmlValue
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.util.List;
|
||||
|
||||
@XmlRootElement(name = "TraccarConfiguration")
|
||||
public class TraccarConfiguration {
|
||||
|
||||
private List<TraccarGateway> traccarGateways;
|
||||
|
||||
@XmlElementWrapper(name = "Gateways")
|
||||
@XmlElement(name = "Gateway")
|
||||
public List<TraccarGateway> getTraccarGateways() {
|
||||
return traccarGateways;
|
||||
}
|
||||
|
||||
public void setTraccarGateways(List<TraccarGateway> traccarGateways) {
|
||||
this.traccarGateways = traccarGateways;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve the default Traccar Gateway as defined in the Traccar configuration.
|
||||
* @return default {@link TraccarGateway}
|
||||
*/
|
||||
public TraccarGateway getDefaultTraccarGateway() {
|
||||
for (TraccarGateway traccarGateway : traccarGateways) {
|
||||
if (traccarGateway.isDefault()) {
|
||||
return traccarGateway;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve Traccar Gateway by the provided Gateway Name
|
||||
* @param gatewayName has the name of the Gateway to be retrieved
|
||||
* @return retrieved {@link TraccarGateway}
|
||||
*/
|
||||
public TraccarGateway getTraccarGateway(String gatewayName) {
|
||||
for (TraccarGateway traccarGateway : traccarGateways) {
|
||||
if (gatewayName.equals(traccarGateway.getName())) {
|
||||
return traccarGateway;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,87 @@
|
||||
/*
|
||||
* 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.config;
|
||||
|
||||
import javax.xml.bind.annotation.XmlAttribute;
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlElementWrapper;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import java.util.List;
|
||||
|
||||
@XmlRootElement(name = "Gateway")
|
||||
public class TraccarGateway {
|
||||
|
||||
private String name;
|
||||
private String extensionClass;
|
||||
private boolean isDefault;
|
||||
private List<Property> properties;
|
||||
|
||||
@XmlAttribute(name = "name")
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@XmlAttribute(name = "extensionClass")
|
||||
public String getExtensionClass() {
|
||||
return extensionClass;
|
||||
}
|
||||
|
||||
public void setExtensionClass(String extensionClass) {
|
||||
this.extensionClass = extensionClass;
|
||||
}
|
||||
|
||||
@XmlAttribute(name = "isDefault")
|
||||
public boolean isDefault() {
|
||||
return isDefault;
|
||||
}
|
||||
|
||||
public void setDefault(boolean aDefault) {
|
||||
isDefault = aDefault;
|
||||
}
|
||||
|
||||
@XmlElementWrapper(name = "Properties")
|
||||
@XmlElement(name = "Property")
|
||||
public List<Property> getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
public void setProperties(List<Property> properties) {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrives the Property based on the provided property name
|
||||
* @param propertyName has the name of the Property to be retrieved
|
||||
* @return retrieved {@link Property}
|
||||
*/
|
||||
public Property getPropertyByName(String propertyName) {
|
||||
for (Property property : properties) {
|
||||
if (propertyName.equals(property.getName())) {
|
||||
return property;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* 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.core.config;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.w3c.dom.Document;
|
||||
import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException;
|
||||
import org.wso2.carbon.device.mgt.core.traccar.common.TraccarHandlerConstants;
|
||||
import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfiguration;
|
||||
import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil;
|
||||
import org.wso2.carbon.utils.CarbonUtils;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
import java.io.File;
|
||||
|
||||
|
||||
public class TraccarConfigurationManager {
|
||||
private static final Log log = LogFactory.getLog(TraccarConfigurationManager.class);
|
||||
private static TraccarConfigurationManager traccarConfigurationManager;
|
||||
private TraccarConfiguration traccarConfiguration;
|
||||
private static final String CarbonUtilsFile = CarbonUtils.getCarbonConfigDirPath() + File.separator;
|
||||
private static final String TRACCAR_CONFIG_PATH = CarbonUtilsFile + TraccarHandlerConstants.TRACCAR_CONFIG_XML_NAME;
|
||||
|
||||
/**
|
||||
* Retrieve an instance of {@link TraccarConfigurationManager}
|
||||
* @return an instance of {@link TraccarConfigurationManager}
|
||||
*/
|
||||
public static TraccarConfigurationManager getInstance() {
|
||||
if (traccarConfigurationManager == null) {
|
||||
synchronized (TraccarConfigurationManager.class) {
|
||||
if (traccarConfigurationManager == null) {
|
||||
traccarConfigurationManager = new TraccarConfigurationManager();
|
||||
}
|
||||
}
|
||||
}
|
||||
return traccarConfigurationManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the Traccar Configuration through the provided configuration location
|
||||
* @param configLocation has the path of the Traccar configuration file
|
||||
* @throws DeviceManagementException throws when there are any errors during the initialization of
|
||||
* Traccar configuration
|
||||
*/
|
||||
public synchronized void initConfig(String configLocation) throws DeviceManagementException {
|
||||
try {
|
||||
File traccarConfig = new File(configLocation);
|
||||
Document doc = DeviceManagerUtil.convertToDocument(traccarConfig);
|
||||
|
||||
//Un-marshaling Traccar configuration
|
||||
JAXBContext traccarContext = JAXBContext.newInstance(TraccarConfiguration.class);
|
||||
Unmarshaller unmarshaller = traccarContext.createUnmarshaller();
|
||||
this.traccarConfiguration = (TraccarConfiguration) unmarshaller.unmarshal(doc);
|
||||
} catch (JAXBException e) {
|
||||
String msg = "Error occurred while initializing Traccar config '" + configLocation + "'";
|
||||
log.error(msg, e);
|
||||
throw new DeviceManagementException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize the Traccar Configuration through the traccar-config.xml file in the TRACCAR_CONFIG_PATH
|
||||
* @throws DeviceManagementException throws when there are any errors during the initialization of
|
||||
* Traccar configuration
|
||||
*/
|
||||
public void initConfig() throws DeviceManagementException {
|
||||
this.initConfig(TRACCAR_CONFIG_PATH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the initialized {@link TraccarConfiguration}
|
||||
* @return the initialized {@link TraccarConfiguration}
|
||||
*/
|
||||
public TraccarConfiguration getTraccarConfig() {
|
||||
try{
|
||||
initConfig();
|
||||
}catch (Exception e){
|
||||
log.error("TraccarConfiguration:", e);
|
||||
}
|
||||
return traccarConfiguration;
|
||||
}
|
||||
}
|
Loading…
Reference in new issue