Adding new apis for device informations

revert-70aa11f8
geethkokila 9 years ago
parent 61ef87c658
commit c7dd386c94

@ -19,14 +19,17 @@
package org.wso2.carbon.device.mgt.jaxrs.api;
import io.swagger.annotations.*;
import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* Device information related operations.
@ -55,6 +58,26 @@ public interface DeviceInformation {
@ApiParam(name = "id", value = "Provide the device identifier", required = true)
@PathParam("id") String id);
@POST
@Path("{list}")
@ApiOperation(
produces = MediaType.APPLICATION_JSON,
httpMethod = "POST",
value = "Get devices information from the supplied device identifies",
notes = "This will return device information such as CPU usage, memory usage etc for supplied device " +
"identifiers.",
response = DeviceInfo.class,
responseContainer = "List")
@ApiResponses(value = {
@ApiResponse(code = 200, message = ""),
@ApiResponse(code = 400, message = ""),
@ApiResponse(code = 400, message = ""),
@ApiResponse(code = 500, message = "Internal Server Error")
})
Response getDevicesInfo(@ApiParam(name = "deviceIdentifiers", value = "List of device identifiers",
required = true) List<DeviceIdentifier> deviceIdentifiers);
@GET
@Path("location/{type}/{id}")
@ApiOperation(

@ -30,9 +30,11 @@ import org.wso2.carbon.device.mgt.jaxrs.api.DeviceInformation;
import org.wso2.carbon.device.mgt.jaxrs.api.util.DeviceMgtAPIUtils;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
import java.util.List;
@SuppressWarnings("NonJaxWsWebServices")
public class DeviceInformationImpl implements DeviceInformation {
@ -59,6 +61,23 @@ public class DeviceInformationImpl implements DeviceInformation {
}
@POST
@Path("list")
public Response getDevicesInfo(List<DeviceIdentifier> deviceIdentifiers) {
DeviceInformationManager informationManager;
List<DeviceInfo> deviceInfos;
try {
informationManager = DeviceMgtAPIUtils.getDeviceInformationManagerService();
deviceInfos = informationManager.getDevicesInfo(deviceIdentifiers);
} catch (DeviceDetailsMgtException e) {
String msg = "Error occurred while getting the device information.";
log.error(msg, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(msg).build();
}
return Response.status(Response.Status.OK).entity(deviceInfos).build();
}
@GET
@Path("location/{type}/{id}")
public Response getDeviceLocation(@PathParam("type") String type, @PathParam("id") String id) {

@ -876,6 +876,13 @@
<method>GET</method>
</Permission>
<Permission>
<name>Device Information</name>
<path>/device-mgt/admin/information/list</path>
<url>/information/list</url>
<method>POST</method>
</Permission>
<Permission>
<name>Device Search</name>
<path>/device-mgt/admin/search</path>

@ -23,6 +23,8 @@ import org.wso2.carbon.device.mgt.common.DeviceIdentifier;
import org.wso2.carbon.device.mgt.common.device.details.DeviceInfo;
import org.wso2.carbon.device.mgt.common.device.details.DeviceLocation;
import java.util.List;
/**
* This class will manage the storing of device details related generic information such as cpu/memory utilization, battery level,
* plugged in to a power source or operation on battery.
@ -47,6 +49,14 @@ public interface DeviceInformationManager {
*/
DeviceInfo getDeviceInfo(DeviceIdentifier deviceIdentifier) throws DeviceDetailsMgtException;
/**
* This method will return device information for the supplied devices list.
* @param deviceIdentifiers
* @return List of device info objects
* @throws DeviceDetailsMgtException
*/
List<DeviceInfo> getDevicesInfo(List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException;
/**
* This method will manage storing the device location as latitude, longitude, address, zip, country etc..
* @param deviceLocation - Device location object.

@ -35,6 +35,10 @@ import org.wso2.carbon.device.mgt.core.device.details.mgt.dao.DeviceDetailsMgtDA
import org.wso2.carbon.device.mgt.core.internal.DeviceManagementDataHolder;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DeviceInformationManagerImpl implements DeviceInformationManager {
@ -97,6 +101,43 @@ public class DeviceInformationManagerImpl implements DeviceInformationManager {
}
}
@Override
public List<DeviceInfo> getDevicesInfo(List<DeviceIdentifier> deviceIdentifiers) throws DeviceDetailsMgtException {
List<DeviceInfo> deviceInfos = new ArrayList<>();
Map<String, DeviceIdentifier> identifierMap = new HashMap<>();
for (DeviceIdentifier identifier : deviceIdentifiers) {
identifierMap.put(identifier.getId(), identifier);
}
try {
List<Integer> deviceIds = new ArrayList<>();
List<Device> devices = DeviceManagementDataHolder.getInstance().
getDeviceManagementProvider().getAllDevices();
for (Device device : devices) {
if (identifierMap.containsKey(device.getDeviceIdentifier()) &&
device.getType().equals(identifierMap.get(device.getDeviceIdentifier()))) {
deviceIds.add(device.getId());
}
}
DeviceManagementDAOFactory.openConnection();
for(Integer id : deviceIds) {
DeviceInfo deviceInfo = deviceDetailsDAO.getDeviceInformation(id);
deviceInfo.setDeviceDetailsMap(deviceDetailsDAO.getDeviceProperties(id));
deviceInfos.add(deviceInfo);
}
} catch (SQLException e) {
throw new DeviceDetailsMgtException("SQL error occurred while retrieving devices from database.", e);
} catch (DeviceManagementException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving the devices.", e);
} catch (DeviceDetailsMgtDAOException e) {
throw new DeviceDetailsMgtException("Exception occurred while retrieving devices details.", e);
} finally {
DeviceManagementDAOFactory.closeConnection();
}
return deviceInfos;
}
@Override
public void addDeviceLocation(DeviceLocation deviceLocation) throws DeviceDetailsMgtException {

Loading…
Cancel
Save