diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java index 60799bf5a2..2166e5c587 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/api/DeviceManagementService.java @@ -551,7 +551,12 @@ public interface DeviceManagementService { value = "Define the time to finish getting the geo location history of the device in " + "milliseconds.", required = true) - @QueryParam("to") long to); + @QueryParam("to") long to, + @ApiParam( + name = "type", + value = "Defines how the output should be.", + required = true) + @QueryParam("type") String type); @GET @Produces(MediaType.APPLICATION_JSON) diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java index bd04c963bd..731b4131b7 100644 --- a/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java +++ b/components/device-mgt/org.wso2.carbon.device.mgt.api/src/main/java/org/wso2/carbon/device/mgt/jaxrs/service/impl/DeviceManagementServiceImpl.java @@ -64,6 +64,7 @@ import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistory; import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshot; +import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistorySnapshotWrapper; import org.wso2.carbon.device.mgt.common.exceptions.DeviceManagementException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidConfigurationException; @@ -510,7 +511,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { @PathParam("deviceType") String deviceType, @PathParam("deviceId") String deviceId, @QueryParam("from") long from, - @QueryParam("to") long to) { + @QueryParam("to") long to, + @QueryParam("type") String type) { try { RequestValidationUtil.validateDeviceIdentifier(deviceType, deviceId); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); @@ -555,17 +557,32 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { deviceLocationHistorySnapshots); List> locationHistorySnapshotList = new ArrayList<>(); + List pathsArray = new ArrayList<>(); + DeviceLocationHistorySnapshotWrapper snapshotWrapper = new DeviceLocationHistorySnapshotWrapper(); while (!deviceLocationHistorySnapshotsQueue.isEmpty()) { List snapshots = new ArrayList<>(); // Make a copy of remaining snapshots List cachedSnapshots = new ArrayList<>( deviceLocationHistorySnapshotsQueue); + List locationPoint = new ArrayList<>(); for (int i = 0; i < cachedSnapshots.size(); i++) { DeviceLocationHistorySnapshot currentSnapshot = deviceLocationHistorySnapshotsQueue.poll(); snapshots.add(currentSnapshot); + if (currentSnapshot != null) { + locationPoint.add(currentSnapshot.getLatitude()); + locationPoint.add(currentSnapshot.getLongitude()); + locationPoint.add(currentSnapshot.getUpdatedTime()); + pathsArray.add(new ArrayList<>(locationPoint)); + locationPoint.clear(); + } if (!deviceLocationHistorySnapshotsQueue.isEmpty()) { DeviceLocationHistorySnapshot nextSnapshot = deviceLocationHistorySnapshotsQueue.peek(); + locationPoint.add(nextSnapshot.getLatitude()); + locationPoint.add(nextSnapshot.getLongitude()); + locationPoint.add(nextSnapshot.getUpdatedTime()); + pathsArray.add(new ArrayList<>(locationPoint)); + locationPoint.clear(); if (nextSnapshot.getUpdatedTime().getTime() - currentSnapshot.getUpdatedTime().getTime() > operationFrequency) { break; @@ -576,7 +593,21 @@ public class DeviceManagementServiceImpl implements DeviceManagementService { } DeviceLocationHistory deviceLocationHistory = new DeviceLocationHistory(); deviceLocationHistory.setLocationHistorySnapshots(locationHistorySnapshotList); - return Response.status(Response.Status.OK).entity(deviceLocationHistory).build(); + if (type != null) { + if (type.equals("path")) { + snapshotWrapper.setPathSnapshot(pathsArray); + } else if (type.equals("full")) { + snapshotWrapper.setFullSnapshot(deviceLocationHistory); + } else { + String msg = "Invalid type, use either 'path' or 'full'"; + log.error(msg); + return Response.status(Response.Status.BAD_REQUEST).entity(new ErrorResponse.ErrorResponseBuilder() + .setCode(Response.Status.BAD_REQUEST.getStatusCode()).setMessage(msg)).build(); + } + } else { + snapshotWrapper.setFullSnapshot(deviceLocationHistory); + } + return Response.status(Response.Status.OK).entity(snapshotWrapper).build(); } catch (DeviceManagementException e) { String msg = "Error occurred while fetching the device information."; log.error(msg, e); diff --git a/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceLocationHistorySnapshotWrapper.java b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceLocationHistorySnapshotWrapper.java new file mode 100644 index 0000000000..37ef54aa0b --- /dev/null +++ b/components/device-mgt/org.wso2.carbon.device.mgt.common/src/main/java/org/wso2/carbon/device/mgt/common/device/details/DeviceLocationHistorySnapshotWrapper.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2020, Entgra (pvt) Ltd. (http://entgra.io) All Rights Reserved. + * + * Entgra (pvt) Ltd. licenses this file to you under the Apache License, + * Version 2.0 (the "License"); you may not use this file except + * in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.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.common.device.details; + +import java.util.List; + +public class DeviceLocationHistorySnapshotWrapper { + + private DeviceLocationHistory fullSnapshot; + private List pathSnapshot; + + public DeviceLocationHistory getFullSnapshot() { + return fullSnapshot; + } + + public void setFullSnapshot(DeviceLocationHistory fullSnapshot) { + this.fullSnapshot = fullSnapshot; + } + + public List getPathSnapshot() { + return pathSnapshot; + } + + public void setPathSnapshot(List pathSnapshot) { + this.pathSnapshot = pathSnapshot; + } +}