Improve device location history API

corrective-policy
shamalka 4 years ago
parent 574119df68
commit 90ed562647

@ -551,7 +551,12 @@ public interface DeviceManagementService {
value = "Define the time to finish getting the geo location history of the device in " + value = "Define the time to finish getting the geo location history of the device in " +
"milliseconds.", "milliseconds.",
required = true) 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 @GET
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)

@ -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.DeviceLocation;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocationHistory; 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.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.DeviceManagementException;
import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException; import org.wso2.carbon.device.mgt.common.exceptions.DeviceTypeNotFoundException;
import org.wso2.carbon.device.mgt.common.exceptions.InvalidConfigurationException; import org.wso2.carbon.device.mgt.common.exceptions.InvalidConfigurationException;
@ -510,7 +511,8 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
@PathParam("deviceType") String deviceType, @PathParam("deviceType") String deviceType,
@PathParam("deviceId") String deviceId, @PathParam("deviceId") String deviceId,
@QueryParam("from") long from, @QueryParam("from") long from,
@QueryParam("to") long to) { @QueryParam("to") long to,
@QueryParam("type") String type) {
try { try {
RequestValidationUtil.validateDeviceIdentifier(deviceType, deviceId); RequestValidationUtil.validateDeviceIdentifier(deviceType, deviceId);
DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService(); DeviceManagementProviderService dms = DeviceMgtAPIUtils.getDeviceManagementService();
@ -555,17 +557,32 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
deviceLocationHistorySnapshots); deviceLocationHistorySnapshots);
List<List<DeviceLocationHistorySnapshot>> locationHistorySnapshotList = new ArrayList<>(); List<List<DeviceLocationHistorySnapshot>> locationHistorySnapshotList = new ArrayList<>();
List<Object> pathsArray = new ArrayList<>();
DeviceLocationHistorySnapshotWrapper snapshotWrapper = new DeviceLocationHistorySnapshotWrapper();
while (!deviceLocationHistorySnapshotsQueue.isEmpty()) { while (!deviceLocationHistorySnapshotsQueue.isEmpty()) {
List<DeviceLocationHistorySnapshot> snapshots = new ArrayList<>(); List<DeviceLocationHistorySnapshot> snapshots = new ArrayList<>();
// Make a copy of remaining snapshots // Make a copy of remaining snapshots
List<DeviceLocationHistorySnapshot> cachedSnapshots = new ArrayList<>( List<DeviceLocationHistorySnapshot> cachedSnapshots = new ArrayList<>(
deviceLocationHistorySnapshotsQueue); deviceLocationHistorySnapshotsQueue);
List<Object> locationPoint = new ArrayList<>();
for (int i = 0; i < cachedSnapshots.size(); i++) { for (int i = 0; i < cachedSnapshots.size(); i++) {
DeviceLocationHistorySnapshot currentSnapshot = deviceLocationHistorySnapshotsQueue.poll(); DeviceLocationHistorySnapshot currentSnapshot = deviceLocationHistorySnapshotsQueue.poll();
snapshots.add(currentSnapshot); 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()) { if (!deviceLocationHistorySnapshotsQueue.isEmpty()) {
DeviceLocationHistorySnapshot nextSnapshot = deviceLocationHistorySnapshotsQueue.peek(); 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() if (nextSnapshot.getUpdatedTime().getTime() - currentSnapshot.getUpdatedTime().getTime()
> operationFrequency) { > operationFrequency) {
break; break;
@ -576,7 +593,21 @@ public class DeviceManagementServiceImpl implements DeviceManagementService {
} }
DeviceLocationHistory deviceLocationHistory = new DeviceLocationHistory(); DeviceLocationHistory deviceLocationHistory = new DeviceLocationHistory();
deviceLocationHistory.setLocationHistorySnapshots(locationHistorySnapshotList); 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) { } catch (DeviceManagementException e) {
String msg = "Error occurred while fetching the device information."; String msg = "Error occurred while fetching the device information.";
log.error(msg, e); log.error(msg, e);

@ -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<Object> pathSnapshot;
public DeviceLocationHistory getFullSnapshot() {
return fullSnapshot;
}
public void setFullSnapshot(DeviceLocationHistory fullSnapshot) {
this.fullSnapshot = fullSnapshot;
}
public List<Object> getPathSnapshot() {
return pathSnapshot;
}
public void setPathSnapshot(List<Object> pathSnapshot) {
this.pathSnapshot = pathSnapshot;
}
}
Loading…
Cancel
Save