diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java index 19a6c64ef2c..e019716bdf4 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/device/details/mgt/impl/DeviceInformationManagerImpl.java @@ -18,7 +18,6 @@ package org.wso2.carbon.device.mgt.core.device.details.mgt.impl; -import com.google.gson.Gson; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -44,15 +43,13 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA 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.TraccarDevice; -import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.TraccarAPIClientServiceImpl; import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; 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; -import java.io.IOException; import java.sql.SQLException; import java.util.ArrayList; import java.util.Calendar; @@ -393,12 +390,8 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager { } //Traccar update GPS Location - TraccarPosition trackerinfo = new TraccarPosition(device.getDeviceIdentifier(), - deviceLocation.getUpdatedTime().getTime(), - deviceLocation.getLatitude(), deviceLocation.getLongitude(), - deviceLocation.getBearing(), deviceLocation.getSpeed()); - DeviceAPIClientServiceImpl dac= new DeviceAPIClientServiceImpl(); - dac.updateLocation(trackerinfo); + DeviceAPIClientService dac= DeviceManagementDataHolder.getInstance().getDeviceAPIClientService(); + dac.updateLocation(device, deviceLocation); //Traccar update GPS Location DeviceManagementDAOFactory.commitTransaction(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java index 5cb1a1c9ab6..6a69cb02ba5 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementDataHolder.java @@ -41,6 +41,7 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.status.task.DeviceStatusTaskManagerService; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; import org.wso2.carbon.email.sender.core.service.EmailSenderService; import org.wso2.carbon.ntask.core.service.TaskService; import org.wso2.carbon.registry.core.service.RegistryService; @@ -83,6 +84,7 @@ public class DeviceManagementDataHolder { private GeoFenceEventOperationManager geoFenceEventOperationManager; private ExecutorService eventConfigExecutors; private OperationTimeoutTaskManagerService operationTimeoutTaskManagerService; + private DeviceAPIClientService deviceAPIClientService; private final Map deviceStatusTaskPluginConfigs = Collections.synchronizedMap( new HashMap<>()); @@ -349,4 +351,12 @@ public class DeviceManagementDataHolder { OperationTimeoutTaskManagerService operationTimeoutTaskManagerService) { this.operationTimeoutTaskManagerService = operationTimeoutTaskManagerService; } + + public DeviceAPIClientService getDeviceAPIClientService() { + return deviceAPIClientService; + } + + public void setDeviceAPIClientService(DeviceAPIClientService deviceAPIClientService) { + this.deviceAPIClientService = deviceAPIClientService; + } } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java index 4c031163aa7..fec7cbcbf34 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/internal/DeviceManagementServiceComponent.java @@ -78,6 +78,8 @@ import org.wso2.carbon.device.mgt.core.service.DeviceManagementProviderServiceIm import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderService; import org.wso2.carbon.device.mgt.core.service.GroupManagementProviderServiceImpl; import org.wso2.carbon.device.mgt.core.task.DeviceTaskManagerService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; +import org.wso2.carbon.device.mgt.core.traccar.api.service.impl.TraccarAPIClientServiceImpl; import org.wso2.carbon.device.mgt.core.util.DeviceManagementSchemaInitializer; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.device.mgt.core.util.DeviceMgtTenantMgtListener; @@ -302,6 +304,11 @@ public class DeviceManagementServiceComponent { DeviceManagementDataHolder.getInstance().setDeviceManagementProvider(deviceManagementProvider); bundleContext.registerService(DeviceManagementProviderService.class.getName(), deviceManagementProvider, null); + /* Registering Device API Client Service */ + DeviceAPIClientService deviceAPIClientService = new TraccarAPIClientServiceImpl(); + DeviceManagementDataHolder.getInstance().setDeviceAPIClientService(deviceAPIClientService); + bundleContext.registerService(DeviceAPIClientService.class.getName(), deviceAPIClientService, null); + /* Registering Group Management Service */ GroupManagementProviderService groupManagementProvider = new GroupManagementProviderServiceImpl(); String defaultGroups = diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java index 4e283160594..851bb3050c9 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderService.java @@ -677,7 +677,7 @@ public interface DeviceManagementProviderService { boolean modifyEnrollment(Device device) throws DeviceManagementException; - boolean enrollDevice(Device device) throws DeviceManagementException, IOException; + boolean enrollDevice(Device device) throws DeviceManagementException; boolean saveConfiguration(PlatformConfiguration configuration) throws DeviceManagementException; diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java index 20fa0406a44..845e335b404 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/service/DeviceManagementProviderServiceImpl.java @@ -118,9 +118,7 @@ import org.wso2.carbon.device.mgt.core.internal.PluginInitializationListener; 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.TraccarDevice; -import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; +import org.wso2.carbon.device.mgt.core.traccar.api.service.DeviceAPIClientService; import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; import org.wso2.carbon.device.mgt.core.util.DeviceManagerUtil; import org.wso2.carbon.email.sender.core.ContentProviderInfo; @@ -397,13 +395,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv } //enroll Traccar device - String lastUpdatedTime = String.valueOf((new Date().getTime())); - TraccarDevice traccarDeviceInfo = new TraccarDevice(device.getName(), device.getDeviceIdentifier(), - "online", "false", lastUpdatedTime, "", "", "", "", - "", ""); - DeviceAPIClientServiceImpl dac= new DeviceAPIClientServiceImpl(); try { - dac.addDevice(traccarDeviceInfo); + DeviceAPIClientService dac= DeviceManagementDataHolder.getInstance().getDeviceAPIClientService(); + dac.addDevice(device); } catch (TraccarConfigurationException e) { log.error("Error on Traccar add device" + e); //e.printStackTrace(); @@ -572,10 +566,9 @@ public class DeviceManagementProviderServiceImpl implements DeviceManagementProv this.removeDeviceFromCache(deviceId); //disenroll Traccar device - TraccarDevice traccarDeviceInfo = new TraccarDevice(device.getDeviceIdentifier()); - DeviceAPIClientServiceImpl dac= new DeviceAPIClientServiceImpl(); try { - dac.disDevice(traccarDeviceInfo); + DeviceAPIClientService dac= DeviceManagementDataHolder.getInstance().getDeviceAPIClientService(); + dac.disDevice(device.getDeviceIdentifier()); } catch (TraccarConfigurationException e) { log.error("Error on Traccar disenroll a device" + e); //e.printStackTrace(); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java index 49084a800b1..7ef5f777d0f 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/DeviceAPIClientService.java @@ -19,37 +19,48 @@ package org.wso2.carbon.device.mgt.core.traccar.api.service; -import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice; +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; -import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; +import java.io.IOException; + public interface DeviceAPIClientService { /** - * Create device Traccar configuration records + * Add GPS location of a device Traccar configuration records * - * @param deviceInfo to be added - * @throws TraccarConfigurationException errors thrown while creating a device traccar configuration + * @param device to be added to update location of the device + * @param deviceLocation to be added to update location of the device + * @throws TraccarConfigurationException errors thrown while inserting location of a device traccar configuration */ - void addDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException; + void updateLocation(Device device, DeviceLocation deviceLocation) throws TraccarConfigurationException; /** - * Add GPS location of a device Traccar configuration records + * Create device Traccar configuration records * - * @param deviceInfo to be added to update location of the device - * @throws TraccarConfigurationException errors thrown while inserting location of a device traccar configuration + * @param device to be added + * @throws TraccarConfigurationException errors thrown while creating a device traccar configuration */ - void updateLocation(TraccarPosition deviceInfo) throws TraccarConfigurationException; + void addDevice(Device device) throws TraccarConfigurationException; /** * Delete a device Traccar configuration records * - * @param deviceInfo to be delete a device + * @param deviceIdentifier to be delete a device * @throws TraccarConfigurationException errors thrown while deleting a device traccar configuration */ - void disDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException; + void disDevice(String deviceIdentifier) throws TraccarConfigurationException; - //String addGroup(TraccarGroups groupInfo) throws IOException; + /** + * Delete a device Traccar configuration records + * + * @param group to be add a group + * @throws TraccarConfigurationException errors thrown while adding a group traccar configuration + */ + void addGroup(DeviceGroup group) throws TraccarConfigurationException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java index c2aec979a20..62091212311 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/addons/TrackerClient.java @@ -30,10 +30,10 @@ import org.apache.commons.logging.LogFactory; import org.json.JSONArray; import org.json.JSONException; 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.TraccarHandlerConstants; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; 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; @@ -166,13 +166,14 @@ public class TrackerClient implements TraccarClient { /** * Add Device GPS Location operation. - * @param deviceIdentifier + * @param deviceId * @return device info * @throws TraccarConfigurationException Failed while add Traccar Device location operation */ - public String getDeviceByDeviceIdentifier(String deviceIdentifier) throws TraccarConfigurationException { + @Override + public String getDeviceByDeviceIdentifier(String deviceId) throws TraccarConfigurationException { try { - String context = "8082/api/devices?uniqueId="+deviceIdentifier; + String context = "8082/api/devices?uniqueId="+ deviceId; Runnable trackerExecutor = new TrackerExecutor(endpoint, context, null, "get"); executor.execute(trackerExecutor); Request request = new Request.Builder() @@ -200,6 +201,11 @@ public class TrackerClient implements TraccarClient { String result = getDeviceByDeviceIdentifier(deviceInfo.getDeviceIdentifier()); String jsonData ="{"+ "\"geodata\": "+ result+ "}"; + log.info("======================"); + log.info("result"); + log.info(result); + log.info(deviceInfo.getDeviceIdentifier()); + log.info("==========================="); JSONObject obj = new JSONObject(jsonData); JSONArray geodata = obj.getJSONArray("geodata"); JSONObject jsonResponse = geodata.getJSONObject(0); @@ -219,6 +225,28 @@ public class TrackerClient implements TraccarClient { } } + /** + * Add Traccar Device operation. + * @param groupInfo with groupName + * @throws TraccarConfigurationException Failed while add Traccar Device the operation + */ + public void addGroup(TraccarGroups groupInfo) throws TraccarConfigurationException { + try{ + JSONObject payload = new JSONObject(); + payload.put("name", groupInfo.getName()); + payload.put("attributes", new JSONObject()); + + String context = "8082/api/groups"; + Runnable trackerExecutor = new TrackerExecutor(endpoint, context, payload, "post"); + executor.execute(trackerExecutor); + log.info("Group successfully on traccar"); + }catch (Exception e){ + String msg="Could not add a traccar group"; + log.error(msg, e); + throw new TraccarConfigurationException(msg, e); + } + } + private TraccarGateway getTraccarGateway(){ return TraccarConfigurationManager.getInstance().getTraccarConfig().getTraccarGateway( TraccarHandlerConstants.GATEWAY_NAME); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java deleted file mode 100644 index 2cbbf57e5ea..00000000000 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/DeviceAPIClientServiceImpl.java +++ /dev/null @@ -1,59 +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.api.service.impl; - -import org.wso2.carbon.device.mgt.common.event.config.EventMetaData; -import org.wso2.carbon.device.mgt.core.event.config.EventOperationExecutor; -import org.wso2.carbon.device.mgt.core.geo.task.GeoFenceEventOperationManager; -import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder; -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.TraccarDevice; -import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; -import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; -import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; - -import java.io.IOException; -import java.util.List; - -public class DeviceAPIClientServiceImpl implements DeviceAPIClientService { - - - public void addDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException { - TrackerClient client = new TrackerClient(); - client.addDevice(deviceInfo); - } - - public void updateLocation(TraccarPosition deviceInfo) throws TraccarConfigurationException { - TrackerClient client = new TrackerClient(); - client.updateLocation(deviceInfo); - } - - public void disDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException { - TrackerClient client = new TrackerClient(); - client.disDevice(deviceInfo); - } - - /*public String addGroup(TraccarGroups groupInfo) throws IOException { - TrackerClient client = new TrackerClient(); - return (client.addGroup(groupInfo)); - }*/ - -} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/TraccarAPIClientServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/TraccarAPIClientServiceImpl.java new file mode 100644 index 00000000000..a64cf6d31be --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/api/service/impl/TraccarAPIClientServiceImpl.java @@ -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.api.service.impl; + +import org.wso2.carbon.device.mgt.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; +import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroup; +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.TraccarDevice; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; +import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; + +import java.util.Date; + +public class TraccarAPIClientServiceImpl implements DeviceAPIClientService { + + + public void addDevice(Device device) throws TraccarConfigurationException { + TrackerClient client = new TrackerClient(); + String lastUpdatedTime = String.valueOf((new Date().getTime())); + TraccarDevice traccarDeviceInfo = new TraccarDevice(device.getName(), device.getDeviceIdentifier(), + "online", "false", lastUpdatedTime, "", "", "", "", + "", ""); + client.addDevice(traccarDeviceInfo); + } + + public void updateLocation(Device device, DeviceLocation deviceLocation) throws TraccarConfigurationException { + TrackerClient client = new TrackerClient(); + TraccarPosition traccarPosition = new TraccarPosition(device.getDeviceIdentifier(), + deviceLocation.getUpdatedTime().getTime(), + deviceLocation.getLatitude(), deviceLocation.getLongitude(), + deviceLocation.getBearing(), deviceLocation.getSpeed()); + client.updateLocation(traccarPosition); + } + + public void disDevice(String deviceIdentifier) throws TraccarConfigurationException { + TrackerClient client = new TrackerClient(); + TraccarDevice traccarDeviceInfo = new TraccarDevice(deviceIdentifier); + client.disDevice(traccarDeviceInfo); + } + + public void addGroup(DeviceGroup group) throws TraccarConfigurationException { + TrackerClient client = new TrackerClient(); + TraccarGroups traccarGroups = new TraccarGroups(group.getName()); + client.addGroup(traccarGroups); + } + +} diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java index 28c7ceaac15..abac0303d31 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/TraccarClient.java @@ -20,20 +20,19 @@ package org.wso2.carbon.device.mgt.core.traccar.common; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarDevice; +import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarGroups; import org.wso2.carbon.device.mgt.core.traccar.common.beans.TraccarPosition; import org.wso2.carbon.device.mgt.core.traccar.common.config.TraccarConfigurationException; -import java.io.IOException; - public interface TraccarClient { - void addDevice(TraccarDevice deviceInfo) throws IOException, TraccarConfigurationException; + void addDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException; - void updateLocation(TraccarPosition deviceInfo) throws IOException, TraccarConfigurationException; + void updateLocation(TraccarPosition deviceInfo) throws TraccarConfigurationException; - String getDeviceByDeviceIdentifier(String deviceInfo) throws IOException, TraccarConfigurationException; + String getDeviceByDeviceIdentifier(String deviceId) throws TraccarConfigurationException; - void disDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException, IOException; + void disDevice(TraccarDevice deviceInfo) throws TraccarConfigurationException; - //String addGroup(TraccarGroups groupInfo) throws IOException; + void addGroup(TraccarGroups groupInfo) throws TraccarConfigurationException; } diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java index 29b4bc50098..07793340769 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.core/src/main/java/org/wso2/carbon/device/mgt/core/traccar/common/beans/TraccarDevice.java @@ -19,6 +19,8 @@ package org.wso2.carbon.device.mgt.core.traccar.common.beans; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; + public class TraccarDevice { private String deviceIdentifier; private String deviceName;