|
|
|
@ -17,12 +17,23 @@
|
|
|
|
|
package org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl;
|
|
|
|
|
import org.apache.commons.logging.Log;
|
|
|
|
|
import org.apache.commons.logging.LogFactory;
|
|
|
|
|
import org.wso2.carbon.context.PrivilegedCarbonContext;
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.exception.DataPublisherConfigurationException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.analytics.service.DeviceAnalyticsService;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.common.exception.DeviceControllerException;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.common.sensormgt.SensorDataManager;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.common.sensormgt.SensorRecord;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.constants.AndroidSenseConstants;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl.util.DeviceJSON;
|
|
|
|
|
import org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl.util.SensorJSON;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
|
|
import javax.ws.rs.Consumes;
|
|
|
|
|
import javax.ws.rs.GET;
|
|
|
|
|
import javax.ws.rs.HeaderParam;
|
|
|
|
|
import javax.ws.rs.POST;
|
|
|
|
|
import javax.ws.rs.Path;
|
|
|
|
|
import javax.ws.rs.Produces;
|
|
|
|
|
import javax.ws.rs.core.Context;
|
|
|
|
|
import javax.ws.rs.core.MediaType;
|
|
|
|
|
import javax.ws.rs.core.Response;
|
|
|
|
@ -32,6 +43,10 @@ public class AndroidSenseControllerService {
|
|
|
|
|
private static Log log = LogFactory.getLog(AndroidSenseControllerService.class);
|
|
|
|
|
@Context //injected response proxy supporting multiple thread
|
|
|
|
|
private HttpServletResponse response;
|
|
|
|
|
private static final String BATTERY_STREAM_DEFINITION = "org.wso2.iot.devices.battery";
|
|
|
|
|
private static final String LIGHT_STREAM_DEFINITION = "org.wso2.iot.devices.light";
|
|
|
|
|
private static final String GPS_STREAM_DEFINITION = "org.wso2.iot.devices.gps";
|
|
|
|
|
private static final String MAGNETIC_STREAM_DEFINITION = "org.wso2.iot.devices.magnetic";
|
|
|
|
|
|
|
|
|
|
/* Service to push all the sensor data collected by the Android
|
|
|
|
|
Called by the Android device */
|
|
|
|
@ -41,29 +56,150 @@ public class AndroidSenseControllerService {
|
|
|
|
|
public void pushSensorData(final DeviceJSON dataMsg, @Context HttpServletResponse response) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
PrivilegedCarbonContext.startTenantFlow();
|
|
|
|
|
PrivilegedCarbonContext ctx = PrivilegedCarbonContext.getThreadLocalCarbonContext();
|
|
|
|
|
ctx.setTenantDomain("carbon.super", true);
|
|
|
|
|
DeviceAnalyticsService deviceAnalyticsService = (DeviceAnalyticsService) ctx.getOSGiService(
|
|
|
|
|
DeviceAnalyticsService.class, null);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String temperature = dataMsg.owner; //TEMP
|
|
|
|
|
log.info("Recieved Sensor Data Values: " + temperature);
|
|
|
|
|
SensorJSON[] sensorData = dataMsg.values;
|
|
|
|
|
String streamDef = null;
|
|
|
|
|
Object payloadData[] = null;
|
|
|
|
|
|
|
|
|
|
for(SensorJSON sensor: sensorData){
|
|
|
|
|
switch (sensor.key){
|
|
|
|
|
case "light" : streamDef = LIGHT_STREAM_DEFINITION;
|
|
|
|
|
payloadData = new Object[]{Float.parseFloat(sensor.value)};
|
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(dataMsg.deviceId,"light",sensor.value,sensor.time);
|
|
|
|
|
break;
|
|
|
|
|
case "battery" : streamDef = BATTERY_STREAM_DEFINITION;
|
|
|
|
|
payloadData = new Object[]{Float.parseFloat(sensor.value)};
|
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(dataMsg.deviceId,"battery",sensor.value,sensor.time);
|
|
|
|
|
break;
|
|
|
|
|
case "GPS" : streamDef = GPS_STREAM_DEFINITION;
|
|
|
|
|
String gpsValue =sensor.value;
|
|
|
|
|
String gpsValues[]=gpsValue.split(",");
|
|
|
|
|
Float gpsValuesF[]= new Float[2];
|
|
|
|
|
gpsValuesF[0] = Float.parseFloat(gpsValues[0]);
|
|
|
|
|
gpsValuesF[1] = Float.parseFloat(gpsValues[0]);
|
|
|
|
|
payloadData = gpsValuesF;
|
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(dataMsg.deviceId,"gps",sensor.value,sensor.time);
|
|
|
|
|
break;
|
|
|
|
|
default :
|
|
|
|
|
try {
|
|
|
|
|
int androidSensorId = Integer.parseInt(sensor.key);
|
|
|
|
|
|
|
|
|
|
if(androidSensorId==2){
|
|
|
|
|
streamDef = MAGNETIC_STREAM_DEFINITION;
|
|
|
|
|
String value =sensor.value;
|
|
|
|
|
String valuesM[]=value.split(",");
|
|
|
|
|
Float gValuesF[]= new Float[1];
|
|
|
|
|
gValuesF[0] = Float.parseFloat(valuesM[0])*Float.parseFloat(valuesM[0])*Float.parseFloat(valuesM[0]);
|
|
|
|
|
|
|
|
|
|
payloadData = gValuesF;
|
|
|
|
|
SensorDataManager.getInstance().setSensorRecord(dataMsg.deviceId,"magnetic",sensor.value,sensor.time);
|
|
|
|
|
}
|
|
|
|
|
}catch(NumberFormatException e){
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Object metdaData[] = {dataMsg.owner, AndroidSenseConstants.DEVICE_TYPE, dataMsg.deviceId, sensor.time};
|
|
|
|
|
|
|
|
|
|
if(streamDef!=null && payloadData!=null && payloadData.length > 0) {
|
|
|
|
|
try {
|
|
|
|
|
deviceAnalyticsService.publishEvent(streamDef, "1.0.0",
|
|
|
|
|
metdaData, new Object[0], payloadData);
|
|
|
|
|
} catch (DataPublisherConfigurationException e) {
|
|
|
|
|
response.setStatus(Response.Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (log.isDebugEnabled()) {
|
|
|
|
|
log.debug("Recieved Temperature Data Value: " + temperature + " degrees C");
|
|
|
|
|
}
|
|
|
|
|
//try {
|
|
|
|
|
boolean result = false;
|
|
|
|
|
// result=DeviceController.pushBamData(dataMsg.owner, AndroidSenseConstants.DEVICE_TYPE,
|
|
|
|
|
// dataMsg.deviceId,
|
|
|
|
|
// System.currentTimeMillis(), "DeviceData",
|
|
|
|
|
// temperature, "TEMPERATURE");
|
|
|
|
|
|
|
|
|
|
if (!result) {
|
|
|
|
|
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
|
|
|
|
|
@Path("controller/readgps")
|
|
|
|
|
@GET
|
|
|
|
|
@Consumes("application/json")
|
|
|
|
|
@Produces("application/json")
|
|
|
|
|
public SensorRecord requestTemperature(@HeaderParam("owner") String owner,
|
|
|
|
|
@HeaderParam("deviceId") String deviceId,
|
|
|
|
|
@HeaderParam("protocol") String protocol,
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
SensorRecord sensorRecord = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return SensorDataManager.getInstance().getSensorRecord(deviceId,"gps");
|
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
//return result;
|
|
|
|
|
// } catch (UnauthorizedException e) {
|
|
|
|
|
// response.setStatus(HttpStatus.SC_UNAUTHORIZED);
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sensorRecord;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Path("controller/readlight")
|
|
|
|
|
@GET
|
|
|
|
|
@Consumes("application/json")
|
|
|
|
|
@Produces("application/json")
|
|
|
|
|
public SensorRecord readLight(@HeaderParam("owner") String owner,
|
|
|
|
|
@HeaderParam("deviceId") String deviceId,
|
|
|
|
|
@HeaderParam("protocol") String protocol,
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
SensorRecord sensorRecord = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return SensorDataManager.getInstance().getSensorRecord(deviceId,"light");
|
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sensorRecord;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Path("controller/readmagnetic")
|
|
|
|
|
@GET
|
|
|
|
|
@Consumes("application/json")
|
|
|
|
|
@Produces("application/json")
|
|
|
|
|
public SensorRecord readMagnetic(@HeaderParam("owner") String owner,
|
|
|
|
|
@HeaderParam("deviceId") String deviceId,
|
|
|
|
|
@HeaderParam("protocol") String protocol,
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
SensorRecord sensorRecord = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return SensorDataManager.getInstance().getSensorRecord(deviceId,"magnetic");
|
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sensorRecord;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Path("controller/readbattery")
|
|
|
|
|
@GET
|
|
|
|
|
@Consumes("application/json")
|
|
|
|
|
@Produces("application/json")
|
|
|
|
|
public SensorRecord readBattery(@HeaderParam("owner") String owner,
|
|
|
|
|
@HeaderParam("deviceId") String deviceId,
|
|
|
|
|
@HeaderParam("protocol") String protocol,
|
|
|
|
|
@Context HttpServletResponse response) {
|
|
|
|
|
SensorRecord sensorRecord = null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return SensorDataManager.getInstance().getSensorRecord(deviceId,"battery");
|
|
|
|
|
} catch (DeviceControllerException e) {
|
|
|
|
|
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return sensorRecord;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|