Adding Android sense API for batch analytics

revert-dabc3590
lasantha 8 years ago
parent 032b9a524c
commit 98e0b38244

@ -34,9 +34,6 @@ import io.swagger.annotations.ResponseHeader;
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.annotations.Tag;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
@ -73,178 +70,6 @@ import javax.ws.rs.core.Response;
description = "This carries all the resources related to the Android sense device management functionalities.")
public interface AndroidSenseService {
/**
* End point to send the key words to the device
*
* @param deviceId The registered device Id.
* @param keywords The key words to be sent. (Comma separated values)
*/
@POST
@Path("device/{deviceId}/words")
@ApiOperation(
httpMethod = "POST",
value = "End point to send the key words to the device",
notes = "",
tags = "android_sense",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = AndroidSenseConstants.SCOPE, value = "perm:android-sense:enroll")
})
}
)
@ApiResponses(value = {
@ApiResponse(
code = 200,
message = "OK.",
response = Response.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid Device Identifiers found.",
response = Response.class),
@ApiResponse(
code = 401,
message = "Unauthorized. \n Unauthorized request."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while executing command operation to"
+ " send keywords",
response = Response.class)
})
Response sendKeyWords(
@ApiParam(
name = "deviceId",
value = "The registered device Id.",
required = true)
@PathParam("deviceId") String deviceId,
@ApiParam(
name = "keywords",
value = "The key words to be sent. (Comma separated values)",
required = true)
@QueryParam("keywords") String keywords);
/**
* End point to send the key words to the device
*
* @param deviceId The registered device Id.
* @param threshold The key words to be sent. (Comma separated values)
*/
@POST
@Path("device/{deviceId}/words/threshold")
@ApiOperation(
httpMethod = "POST",
value = "End point to send threshold value to the device",
notes = "",
tags = "android_sense",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = AndroidSenseConstants.SCOPE, value = "perm:android-sense:enroll")
})
}
)
@ApiResponses(value = {
@ApiResponse(
code = 200,
message = "OK.",
response = Response.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid Device Identifiers found.",
response = Response.class),
@ApiResponse(
code = 401,
message = "Unauthorized. \n Unauthorized request."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while executing command operation to"
+ " send threashold",
response = Response.class)
})
Response sendThreshold(
@ApiParam(
name = "deviceId",
value = "The registered device Id.",
required = true)
@PathParam("deviceId") String deviceId,
@ApiParam(
name = "threshold",
value = "Threshold to be sent.",
required = true)
@QueryParam("threshold") String threshold);
/**
* End point to remove key words from the device
*/
@DELETE
@Path("device/{deviceId}/words")
@ApiOperation(
httpMethod = "DELETE",
value = "Remove key words from the device",
notes = "",
response = Response.class,
tags = "android_sense",
extensions = {
@Extension(properties = {
@ExtensionProperty(name = AndroidSenseConstants.SCOPE, value = "perm:android-sense:enroll")
})
}
)
@ApiResponses(value = {
@ApiResponse(
code = 200,
message = "OK.",
response = Response.class,
responseHeaders = {
@ResponseHeader(
name = "Content-Type",
description = "The content type of the body"),
@ResponseHeader(
name = "Last-Modified",
description = "Date and time the resource was last modified.\n" +
"Used by caches, or in conditional requests."),
}),
@ApiResponse(
code = 400,
message = "Bad Request. \n Invalid Device Identifiers found.",
response = Response.class),
@ApiResponse(
code = 401,
message = "Unauthorized. \n Unauthorized request."),
@ApiResponse(
code = 500,
message = "Internal Server Error. \n Error occurred while executing command operation to"
+ " send threashold",
response = Response.class)
})
Response removeKeyWords(
@ApiParam(
name = "deviceId",
value = "The registered device Id.",
required = true)
@PathParam("deviceId") String deviceId,
@ApiParam(
name = "words",
value = "The key words to be sent. (Comma separated values)",
required = true)
@QueryParam("words") String words);
/**
* Retrieve Sensor data for the device type
*/

@ -27,9 +27,6 @@ import org.wso2.carbon.device.mgt.common.*;
import org.wso2.carbon.device.mgt.common.authorization.DeviceAccessAuthorizationException;
import org.wso2.carbon.device.mgt.common.configuration.mgt.ConfigurationManagementException;
import org.wso2.carbon.device.mgt.common.group.mgt.DeviceGroupConstants;
import org.wso2.carbon.device.mgt.common.operation.mgt.Operation;
import org.wso2.carbon.device.mgt.common.operation.mgt.OperationManagementException;
import org.wso2.carbon.device.mgt.core.operation.mgt.CommandOperation;
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.constants.AndroidSenseConstants;
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.APIUtil;
import org.wso2.carbon.device.mgt.iot.androidsense.service.impl.util.AndroidConfiguration;
@ -40,131 +37,14 @@ import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;
/**
* The api for
* This api is for Android Sense Device Type
*/
public class AndroidSenseServiceImpl implements AndroidSenseService {
private static Log log = LogFactory.getLog(AndroidSenseServiceImpl.class);
@Path("device/{deviceId}/words")
@POST
public Response sendKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("keywords") String keywords) {
try {
if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
AndroidSenseConstants.DEVICE_TYPE))) {
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
String publishTopic = APIUtil.getAuthenticatedUserTenantDomain()
+ "/" + AndroidSenseConstants.DEVICE_TYPE + "/" + deviceId + "/command/words";
Operation commandOp = new CommandOperation();
commandOp.setCode("keywords");
commandOp.setType(Operation.Type.COMMAND);
commandOp.setEnabled(true);
commandOp.setPayLoad(keywords);
Properties props = new Properties();
props.setProperty(AndroidSenseConstants.MQTT_ADAPTER_TOPIC_PROPERTY_NAME, publishTopic);
commandOp.setProperties(props);
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(new DeviceIdentifier(deviceId, AndroidSenseConstants.DEVICE_TYPE));
APIUtil.getDeviceManagementService().addOperation(AndroidSenseConstants.DEVICE_TYPE, commandOp,
deviceIdentifiers);
return Response.ok().build();
} catch (InvalidDeviceException e) {
String msg = "Invalid Device Identifiers found.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (DeviceAccessAuthorizationException e) {
log.error(e.getErrorMessage(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
} catch (OperationManagementException e) {
log.error("Error occurred while executing command operation to send keywords", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Path("device/{deviceId}/words/threshold")
@POST
public Response sendThreshold(@PathParam("deviceId") String deviceId, @QueryParam("threshold") String threshold) {
try {
if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
AndroidSenseConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
String publishTopic = APIUtil.getAuthenticatedUserTenantDomain()
+ "/" + AndroidSenseConstants.DEVICE_TYPE + "/" + deviceId + "/command/threshold";
Operation commandOp = new CommandOperation();
commandOp.setCode("threshold");
commandOp.setType(Operation.Type.COMMAND);
commandOp.setEnabled(true);
commandOp.setPayLoad(threshold);
Properties props = new Properties();
props.setProperty(AndroidSenseConstants.MQTT_ADAPTER_TOPIC_PROPERTY_NAME, publishTopic);
commandOp.setProperties(props);
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(new DeviceIdentifier(deviceId, AndroidSenseConstants.DEVICE_TYPE));
APIUtil.getDeviceManagementService().addOperation(AndroidSenseConstants.DEVICE_TYPE, commandOp,
deviceIdentifiers);
return Response.ok().build();
} catch (InvalidDeviceException e) {
String msg = "Invalid Device Identifiers found.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (DeviceAccessAuthorizationException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
} catch (OperationManagementException e) {
log.error("Error occurred while executing command operation to set threashold", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Path("device/{deviceId}/words")
@DELETE
public Response removeKeyWords(@PathParam("deviceId") String deviceId, @QueryParam("words") String words) {
try {
if (!APIUtil.getDeviceAccessAuthorizationService().isUserAuthorized(new DeviceIdentifier(deviceId,
AndroidSenseConstants.DEVICE_TYPE), DeviceGroupConstants.Permissions.DEFAULT_OPERATOR_PERMISSIONS)) {
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
String publishTopic = APIUtil.getAuthenticatedUserTenantDomain()
+ "/" + AndroidSenseConstants.DEVICE_TYPE + "/" + deviceId + "/command/remove";
Operation commandOp = new CommandOperation();
commandOp.setCode("remove");
commandOp.setType(Operation.Type.COMMAND);
commandOp.setEnabled(true);
commandOp.setPayLoad(words);
Properties props = new Properties();
props.setProperty(AndroidSenseConstants.MQTT_ADAPTER_TOPIC_PROPERTY_NAME, publishTopic);
commandOp.setProperties(props);
List<DeviceIdentifier> deviceIdentifiers = new ArrayList<>();
deviceIdentifiers.add(new DeviceIdentifier(deviceId, AndroidSenseConstants.DEVICE_TYPE));
APIUtil.getDeviceManagementService().addOperation(AndroidSenseConstants.DEVICE_TYPE, commandOp,
deviceIdentifiers);
return Response.ok().build();
} catch (InvalidDeviceException e) {
String msg = "Invalid Device Identifiers found.";
log.error(msg, e);
return Response.status(Response.Status.BAD_REQUEST).build();
} catch (DeviceAccessAuthorizationException e) {
log.error(e.getErrorMessage(), e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).build();
} catch (OperationManagementException e) {
log.error("Error occurred while executing command operation to remove words", e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}
@Path("stats/{deviceId}/sensors/{sensorName}")
@GET
@Consumes("application/json")
@ -173,11 +53,7 @@ public class AndroidSenseServiceImpl implements AndroidSenseService {
@QueryParam("from") long from, @QueryParam("to") long to) {
String fromDate = String.valueOf(from);
String toDate = String.valueOf(to);
String query = "deviceId:" + deviceId + " AND deviceType:" +
AndroidSenseConstants.DEVICE_TYPE + " AND time : [" + fromDate + " TO " + toDate + "]";
if (sensor.equals(AndroidSenseConstants.SENSOR_WORDCOUNT)) {
query = "deviceId:" + deviceId;
}
String query = "meta_deviceId:" + deviceId + " AND meta_timestamp : [" + fromDate + " TO " + toDate + "]";
String sensorTableName = getSensorEventTableName(sensor);
try {
@ -186,14 +62,10 @@ public class AndroidSenseServiceImpl implements AndroidSenseService {
return Response.status(Response.Status.UNAUTHORIZED.getStatusCode()).build();
}
List<SensorRecord> sensorDatas;
if (!sensor.equals(AndroidSenseConstants.SENSOR_WORDCOUNT)) {
List<SortByField> sortByFields = new ArrayList<>();
SortByField sortByField = new SortByField("time", SortType.ASC);
sortByFields.add(sortByField);
sensorDatas = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
} else {
sensorDatas = APIUtil.getAllEventsForDevice(sensorTableName, query, null);
}
List<SortByField> sortByFields = new ArrayList<>();
SortByField sortByField = new SortByField("meta_timestamp", SortType.ASC);
sortByFields.add(sortByField);
sensorDatas = APIUtil.getAllEventsForDevice(sensorTableName, query, sortByFields);
return Response.ok().entity(sensorDatas).build();
} catch (AnalyticsException e) {
String errorMsg = "Error on retrieving stats on table " + sensorTableName + " with query " + query;
@ -212,37 +84,31 @@ public class AndroidSenseServiceImpl implements AndroidSenseService {
String sensorEventTableName;
switch (sensorName) {
case AndroidSenseConstants.SENSOR_ACCELEROMETER:
sensorEventTableName = "DEVICE_ACCELEROMETER_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_ACCELEROMETER_TABLE;
break;
case AndroidSenseConstants.SENSOR_BATTERY:
sensorEventTableName = "DEVICE_BATTERY_SUMMARY";
break;
case AndroidSenseConstants.SENSOR_GPS:
sensorEventTableName = "DEVICE_GPS_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_BATTERY_TABLE;
break;
case AndroidSenseConstants.SENSOR_GRAVITY:
sensorEventTableName = "DEVICE_GRAVITY_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_GRAVITY_TABLE;
break;
case AndroidSenseConstants.SENSOR_GYROSCOPE:
sensorEventTableName = "DEVICE_GYROSCOPE_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_GYROSCOPE_TABLE;
break;
case AndroidSenseConstants.SENSOR_LIGHT:
sensorEventTableName = "DEVICE_LIGHT_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_LIGHT_TABLE;
break;
case AndroidSenseConstants.SENSOR_MAGNETIC:
sensorEventTableName = "DEVICE_MAGNETIC_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_MAGNETIC_TABLE;
break;
case AndroidSenseConstants.SENSOR_PRESSURE:
sensorEventTableName = "DEVICE_PRESSURE_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_PRESSURE_TABLE;
break;
case AndroidSenseConstants.SENSOR_PROXIMITY:
sensorEventTableName = "DEVICE_PROXIMITY_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_PROXIMITY_TABLE;
break;
case AndroidSenseConstants.SENSOR_ROTATION:
sensorEventTableName = "DEVICE_ROTATION_SUMMARY";
break;
case AndroidSenseConstants.SENSOR_WORDCOUNT:
sensorEventTableName = "WORD_COUNT_SUMMARY";
sensorEventTableName = AndroidSenseConstants.SENSOR_ROTATION_TABLE;
break;
default:
sensorEventTableName = "";
@ -292,4 +158,4 @@ public class AndroidSenseServiceImpl implements AndroidSenseService {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()).entity(false).build();
}
}
}
}
Loading…
Cancel
Save