From bf9bfd40b59bd09c8c04560dc35c4f2c286f4bbe Mon Sep 17 00:00:00 2001 From: ayyoob Date: Sun, 25 Oct 2015 20:17:51 +0530 Subject: [PATCH] few changes after testing --- .../impl/AndroidSenseControllerService.java | 118 ------ .../impl/AndroidSenseManagerService.java | 186 --------- .../service/impl/AndroidSenseService.java | 362 ++++++++++++++++++ .../src/main/webapp/WEB-INF/cxf-servlet.xml | 18 +- .../src/main/webapp/WEB-INF/web.xml | 50 +++ .../service/impl/FireAlarmManagerService.java | 351 ----------------- 6 files changed, 416 insertions(+), 669 deletions(-) delete mode 100644 modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java delete mode 100644 modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java create mode 100644 modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseService.java delete mode 100644 modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java diff --git a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java deleted file mode 100644 index 2e1c16b9..00000000 --- a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseControllerService.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.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.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; - -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 */ - @Path("/sensordata") - @POST - @Consumes(MediaType.APPLICATION_JSON) - 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); - - - 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)}; - break; - case "battery" : streamDef = BATTERY_STREAM_DEFINITION; - payloadData = new Object[]{Float.parseFloat(sensor.value)}; - 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; - 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; - }else if(androidSensorId==5){ - streamDef = LIGHT_STREAM_DEFINITION; - payloadData = new Object[]{Float.parseFloat(sensor.value)}; - } - }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()); - } - } - - } - - } - -} diff --git a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java deleted file mode 100644 index 986a067c..00000000 --- a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseManagerService.java +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.iot.sample.android.sense.service.impl; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.iot.sample.android.sense.plugin.constants.AndroidSenseConstants; -import org.wso2.carbon.device.mgt.iot.common.DeviceManagement; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.FormParam; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.Response; -import java.util.Date; - -public class AndroidSenseManagerService { - - - - private static Log log = LogFactory.getLog(AndroidSenseManagerService.class); - //TODO; replace this tenant domain - private final String SUPER_TENANT = "carbon.super"; - @Context //injected response proxy supporting multiple thread - private HttpServletResponse response; - - @Path("/device") - @PUT - public boolean register(@FormParam("deviceId") String deviceId, - @FormParam("owner") String owner) { - - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - try { - if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - response.setStatus(Response.Status.CONFLICT.getStatusCode()); - return true; - } - Device device = new Device(); - device.setDeviceIdentifier(deviceId); - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - - enrolmentInfo.setDateOfEnrolment(new Date().getTime()); - enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); - String name = owner + " android " + deviceId; - device.setName(name); - device.setType(AndroidSenseConstants.DEVICE_TYPE); - enrolmentInfo.setOwner(owner); - device.setEnrolmentInfo(enrolmentInfo); - boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); - - if (added) { - response.setStatus(Response.Status.OK.getStatusCode()); - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - } - - return added; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - } - - @Path("/device/{device_id}") - @DELETE - public void removeDevice(@PathParam("device_id") String deviceId, - @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - try { - boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(deviceIdentifier); - if (removed) { - response.setStatus(Response.Status.OK.getStatusCode()); - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - } - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - - } finally { - deviceManagement.endTenantFlow(); - } - - - } - - @Path("/device/{device_id}") - @POST - public boolean updateDevice(@PathParam("device_id") String deviceId, - @FormParam("name") String name, - @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - try { - Device device = deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); - device.setDeviceIdentifier(deviceId); - device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); - - device.setName(name); - device.setType(AndroidSenseConstants.DEVICE_TYPE); - - boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device); - - - if (updated) { - response.setStatus(Response.Status.OK.getStatusCode()); - - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - - } - return updated; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("/device/{device_id}") - @GET - @Consumes("application/json") - @Produces("application/json") - public Device getDevice(@PathParam("device_id") String deviceId) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); - - try { - return deviceManagement.getDeviceManagementService().getDevice( - deviceIdentifier); - - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return null; - } finally { - deviceManagement.endTenantFlow(); - } - - } - -} diff --git a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseService.java b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseService.java new file mode 100644 index 00000000..4babba76 --- /dev/null +++ b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/android/sense/service/impl/AndroidSenseService.java @@ -0,0 +1,362 @@ +/* + * Copyright (c) 2015, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. + * + * Licensed 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.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.common.Device; +import org.wso2.carbon.device.mgt.common.DeviceIdentifier; +import org.wso2.carbon.device.mgt.common.DeviceManagementException; +import org.wso2.carbon.device.mgt.common.EnrolmentInfo; +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.common.DeviceManagement; +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.DELETE; +import javax.ws.rs.FormParam; +import javax.ws.rs.GET; +import javax.ws.rs.HeaderParam; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.Date; + +public class AndroidSenseService { + + + + private static Log log = LogFactory.getLog(AndroidSenseService.class); + //TODO; replace this tenant domain + private final String SUPER_TENANT = "carbon.super"; + 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"; + + private static final String SENSOR_LIGHT = "light"; + private static final String SENSOR_GPS = "gps"; + private static final String SENSOR_BATTERY = "battery"; + private static final String SENSOR_MAGNETIC = "magnetic"; + + @Context //injected response proxy supporting multiple thread + private HttpServletResponse response; + + + + @Path("manager/device") + @PUT + public boolean register(@FormParam("deviceId") String deviceId, + @FormParam("owner") String owner) { + + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + try { + if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { + response.setStatus(Response.Status.CONFLICT.getStatusCode()); + return true; + } + Device device = new Device(); + device.setDeviceIdentifier(deviceId); + EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); + + enrolmentInfo.setDateOfEnrolment(new Date().getTime()); + enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); + enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); + enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); + String name = owner + " android " + deviceId; + device.setName(name); + device.setType(AndroidSenseConstants.DEVICE_TYPE); + enrolmentInfo.setOwner(owner); + device.setEnrolmentInfo(enrolmentInfo); + boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); + + if (added) { + response.setStatus(Response.Status.OK.getStatusCode()); + } else { + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + } + + return added; + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return false; + } finally { + deviceManagement.endTenantFlow(); + } + } + + @Path("manager/device/remove/{device_id}") + @DELETE + public void removeDevice(@PathParam("device_id") String deviceId, + @Context HttpServletResponse response) { + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + try { + boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( + deviceIdentifier); + if (removed) { + response.setStatus(Response.Status.OK.getStatusCode()); + + } else { + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + + } + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } finally { + deviceManagement.endTenantFlow(); + } + + } + + @Path("manager/device/update/{device_id}") + @POST + public boolean updateDevice(@PathParam("device_id") String deviceId, + @QueryParam("name") String name, + @Context HttpServletResponse response) { + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + try { + Device device = deviceManagement.getDeviceManagementService().getDevice( + deviceIdentifier); + device.setDeviceIdentifier(deviceId); + + // device.setDeviceTypeId(deviceTypeId); + device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); + + device.setName(name); + device.setType(AndroidSenseConstants.DEVICE_TYPE); + + boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device); + + if (updated) { + response.setStatus(Response.Status.OK.getStatusCode()); + + } else { + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + + } + return updated; + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return false; + } finally { + deviceManagement.endTenantFlow(); + } + + } + + @Path("manager/device/{device_id}") + @GET + @Consumes("application/json") + @Produces("application/json") + public Device getDevice(@PathParam("device_id") String deviceId) { + + DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); + DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); + deviceIdentifier.setId(deviceId); + deviceIdentifier.setType(AndroidSenseConstants.DEVICE_TYPE); + + try { + return deviceManagement.getDeviceManagementService().getDevice( + deviceIdentifier); + + } catch (DeviceManagementException e) { + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + return null; + } finally { + deviceManagement.endTenantFlow(); + } + + } + + /* Service to push all the sensor data collected by the Android + Called by the Android device */ + @Path("controller/sensordata") + @POST + @Consumes(MediaType.APPLICATION_JSON) + 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); + + + SensorJSON[] sensorData = dataMsg.values; + String streamDef = null; + Object payloadData[] = null; + String sensorName = null; + + for(SensorJSON sensor: sensorData){ + switch (sensor.key){ + case "battery" : streamDef = BATTERY_STREAM_DEFINITION; + payloadData = new Object[]{Float.parseFloat(sensor.value)}; + sensorName = SENSOR_BATTERY; + 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; + sensorName = SENSOR_GPS; + 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; + sensorName = SENSOR_MAGNETIC; + }else if(androidSensorId==5){ + streamDef = LIGHT_STREAM_DEFINITION; + sensorName = SENSOR_LIGHT; + payloadData = new Object[]{Float.parseFloat(sensor.value)}; + } + }catch(NumberFormatException e){ + continue; + } + + } + Object metdaData[] = {dataMsg.owner, AndroidSenseConstants.DEVICE_TYPE, dataMsg.deviceId, sensor.time}; + + if(streamDef!=null && payloadData!=null && payloadData.length > 0) { + try { + SensorDataManager.getInstance().setSensorRecord(dataMsg.deviceId, sensorName, sensor.value, sensor.time); + deviceAnalyticsService.publishEvent(streamDef, "1.0.0", metdaData, new Object[0], payloadData); + } catch (DataPublisherConfigurationException e) { + response.setStatus(Response.Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode()); + } + } + + } + + } + + + //TODO below enpoints needs to be removed and cep websocket have to be added + @Path("controller/readlight") + @GET + @Consumes("application/json") + @Produces("application/json") + public SensorRecord readLight(@HeaderParam("owner") String owner, + @HeaderParam("deviceId") String deviceId, + @Context HttpServletResponse response) { + SensorRecord sensorRecord = null; + + try{ + sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, SENSOR_LIGHT); + } catch (DeviceControllerException e){ + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } + + response.setStatus(Response.Status.OK.getStatusCode()); + return sensorRecord; + } + + @Path("controller/readbattery") + @GET + @Consumes("application/json") + @Produces("application/json") + public SensorRecord readBattery(@HeaderParam("owner") String owner, + @HeaderParam("deviceId") String deviceId, + @Context HttpServletResponse response) { + SensorRecord sensorRecord = null; + + try{ + sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, SENSOR_BATTERY); + } catch (DeviceControllerException e){ + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } + + response.setStatus(Response.Status.OK.getStatusCode()); + return sensorRecord; + } + + @Path("controller/readgps") + @GET + @Consumes("application/json") + @Produces("application/json") + public SensorRecord readGPS(@HeaderParam("owner") String owner, + @HeaderParam("deviceId") String deviceId, + @Context HttpServletResponse response) { + SensorRecord sensorRecord = null; + + try{ + sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, SENSOR_GPS); + } catch (DeviceControllerException e){ + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } + + response.setStatus(Response.Status.OK.getStatusCode()); + return sensorRecord; + } + + @Path("controller/readmagnetic") + @GET + @Consumes("application/json") + @Produces("application/json") + public SensorRecord readMagnetic(@HeaderParam("owner") String owner, + @HeaderParam("deviceId") String deviceId, + @Context HttpServletResponse response) { + SensorRecord sensorRecord = null; + + try{ + sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, SENSOR_MAGNETIC); + } catch (DeviceControllerException e){ + response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); + } + + response.setStatus(Response.Status.OK.getStatusCode()); + return sensorRecord; + } + +} diff --git a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml index b5024a3a..e67c36e7 100644 --- a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml +++ b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/cxf-servlet.xml @@ -24,26 +24,16 @@ http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd"> - + + - - + - - - - - - - - - diff --git a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/web.xml b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/web.xml index f4864580..52270088 100755 --- a/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/web.xml +++ b/modules/samples/android_sense/src/org.wso2.carbon.device.mgt.iot.sample.android.sense.service.impl/src/main/webapp/WEB-INF/web.xml @@ -19,5 +19,55 @@ /* + + isAdminService + false + + + doAuthentication + false + + + + + managed-api-enabled + true + + + managed-api-owner + admin + + + managed-api-name + android_sense + + + managed-api-endpoint + http://localhost:9763/android_sense + + + managed-api-version + 1.0.0 + + + managed-api-context + /android_sense + + + managed-api-isSecured + true + + + + + + + + + + + + + diff --git a/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java b/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java deleted file mode 100644 index ccc647d8..00000000 --- a/modules/samples/firealarm/src/org.wso2.carbon.device.mgt.iot.sample.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/firealarm/service/impl/FireAlarmManagerService.java +++ /dev/null @@ -1,351 +0,0 @@ -/* - * Copyright (c) 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. - * - * Licensed 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.iot.sample.firealarm.service.impl; - - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.wso2.carbon.device.mgt.common.Device; -import org.wso2.carbon.device.mgt.common.DeviceIdentifier; -import org.wso2.carbon.device.mgt.common.DeviceManagementException; -import org.wso2.carbon.device.mgt.common.EnrolmentInfo; -import org.wso2.carbon.device.mgt.iot.common.DeviceManagement; -import org.wso2.carbon.device.mgt.iot.common.apimgt.AccessTokenInfo; -import org.wso2.carbon.device.mgt.iot.common.apimgt.TokenClient; -import org.wso2.carbon.device.mgt.iot.common.controlqueue.xmpp.XmppAccount; -import org.wso2.carbon.device.mgt.iot.common.controlqueue.xmpp.XmppConfig; -import org.wso2.carbon.device.mgt.iot.common.controlqueue.xmpp.XmppServerClient; -import org.wso2.carbon.device.mgt.iot.common.exception.AccessTokenException; -import org.wso2.carbon.device.mgt.iot.common.exception.DeviceControllerException; -import org.wso2.carbon.device.mgt.iot.common.util.ZipArchive; -import org.wso2.carbon.device.mgt.iot.common.util.ZipUtil; -import org.wso2.carbon.device.mgt.iot.sample.firealarm.plugin.constants.FireAlarmConstants; - -import javax.servlet.http.HttpServletResponse; -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; -import javax.ws.rs.Path; -import javax.ws.rs.PathParam; -import javax.ws.rs.Produces; -import javax.ws.rs.QueryParam; -import javax.ws.rs.core.Context; -import javax.ws.rs.core.Response; -import java.nio.ByteBuffer; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.UUID; - -//@Path("/FireAlarmDeviceManager") -public class FireAlarmManagerService { - - private static Log log = LogFactory.getLog(FireAlarmManagerService.class); - - //TODO; replace this tenant domain - private final String SUPER_TENANT = "carbon.super"; - @Context //injected response proxy supporting multiple thread - private HttpServletResponse response; - - @Path("/device/register") - @PUT - public boolean register(@QueryParam("deviceId") String deviceId, - @QueryParam("name") String name, @QueryParam("owner") String owner) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); - try { - if (deviceManagement.getDeviceManagementService().isEnrolled(deviceIdentifier)) { - response.setStatus(Response.Status.CONFLICT.getStatusCode()); - return false; - } - - Device device = new Device(); - device.setDeviceIdentifier(deviceId); - EnrolmentInfo enrolmentInfo = new EnrolmentInfo(); - - enrolmentInfo.setDateOfEnrolment(new Date().getTime()); - enrolmentInfo.setDateOfLastUpdate(new Date().getTime()); - enrolmentInfo.setStatus(EnrolmentInfo.Status.ACTIVE); - enrolmentInfo.setOwnership(EnrolmentInfo.OwnerShip.BYOD); - - device.setName(name); - device.setType(FireAlarmConstants.DEVICE_TYPE); - enrolmentInfo.setOwner(owner); - device.setEnrolmentInfo(enrolmentInfo); - boolean added = deviceManagement.getDeviceManagementService().enrollDevice(device); - - if (added) { - response.setStatus(Response.Status.OK.getStatusCode()); - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - } - - return added; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - } - - @Path("/device/remove/{device_id}") - @DELETE - public void removeDevice(@PathParam("device_id") String deviceId, - @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); - try { - boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( - deviceIdentifier); - if (removed) { - response.setStatus(Response.Status.OK.getStatusCode()); - - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - - } - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("/device/update/{device_id}") - @POST - public boolean updateDevice(@PathParam("device_id") String deviceId, - @QueryParam("name") String name, - @Context HttpServletResponse response) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); - try { - Device device = deviceManagement.getDeviceManagementService().getDevice( - deviceIdentifier); - device.setDeviceIdentifier(deviceId); - - // device.setDeviceTypeId(deviceTypeId); - device.getEnrolmentInfo().setDateOfLastUpdate(new Date().getTime()); - - device.setName(name); - device.setType(FireAlarmConstants.DEVICE_TYPE); - - boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment( - device); - - if (updated) { - response.setStatus(Response.Status.OK.getStatusCode()); - - } else { - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - - } - return updated; - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return false; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("/device/{device_id}") - @GET - @Consumes("application/json") - @Produces("application/json") - public Device getDevice(@PathParam("device_id") String deviceId) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); - deviceIdentifier.setId(deviceId); - deviceIdentifier.setType(FireAlarmConstants.DEVICE_TYPE); - - try { - return deviceManagement.getDeviceManagementService().getDevice(deviceIdentifier); - - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return null; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("/devices/{username}") - @GET - @Consumes("application/json") - @Produces("application/json") - public Device[] getFirealarmDevices(@PathParam("username") String username) { - - DeviceManagement deviceManagement = new DeviceManagement(SUPER_TENANT); - - try { - List userDevices = - deviceManagement.getDeviceManagementService().getDevicesOfUser( - username); - ArrayList userDevicesforFirealarm = new ArrayList(); - for (Device device : userDevices) { - - if (device.getType().equals(FireAlarmConstants.DEVICE_TYPE) && - device.getEnrolmentInfo().getStatus().equals( - EnrolmentInfo.Status.ACTIVE)) { - userDevicesforFirealarm.add(device); - - } - } - - return userDevicesforFirealarm.toArray(new Device[]{}); - } catch (DeviceManagementException e) { - response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return null; - } finally { - deviceManagement.endTenantFlow(); - } - - } - - @Path("/device/{sketch_type}/download") - @GET - @Produces("application/octet-stream") - public Response downloadSketch(@QueryParam("owner") String owner, - @QueryParam("deviceName") String customDeviceName, - @PathParam("sketch_type") String sketchType) { - - ZipArchive zipFile = null; - try { - zipFile = createDownloadFile(owner, customDeviceName, sketchType); - Response.ResponseBuilder rb = Response.ok(zipFile.getZipFile()); - rb.header("Content-Disposition", - "attachment; filename=\"" + zipFile.getFileName() + "\""); - return rb.build(); - } catch (IllegalArgumentException ex) { - return Response.status(400).entity(ex.getMessage()).build();//bad request - } catch (DeviceManagementException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (AccessTokenException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (DeviceControllerException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } - - } - - @Path("/device/{sketch_type}/generate_link") - @GET - public Response generateSketchLink(@QueryParam("owner") String owner, - @QueryParam("deviceName") String customDeviceName, - @PathParam("sketch_type") String sketchType) { - - ZipArchive zipFile = null; - try { - zipFile = createDownloadFile(owner, customDeviceName, sketchType); - Response.ResponseBuilder rb = Response.ok(zipFile.getDeviceId()); - return rb.build(); - } catch (IllegalArgumentException ex) { - return Response.status(400).entity(ex.getMessage()).build();//bad request - } catch (DeviceManagementException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (AccessTokenException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } catch (DeviceControllerException ex) { - return Response.status(500).entity(ex.getMessage()).build(); - } - - } - - private ZipArchive createDownloadFile(String owner, String customDeviceName, String sketchType) - throws DeviceManagementException, AccessTokenException, DeviceControllerException { - if (owner == null) { - throw new IllegalArgumentException("Error on createDownloadFile() Owner is null!"); - } - - //create new device id - String deviceId = shortUUID(); - - TokenClient accessTokenClient = new TokenClient(FireAlarmConstants.DEVICE_TYPE); - AccessTokenInfo accessTokenInfo = null; - - accessTokenInfo = accessTokenClient.getAccessToken(owner, deviceId); - - //create token - String accessToken = accessTokenInfo.getAccess_token(); - String refreshToken = accessTokenInfo.getRefresh_token(); - //adding registering data - - XmppAccount newXmppAccount = new XmppAccount(); - newXmppAccount.setAccountName(owner + "_" + deviceId); - newXmppAccount.setUsername(deviceId); - newXmppAccount.setPassword(accessToken); - newXmppAccount.setEmail(deviceId + "@wso2.com"); - - XmppServerClient xmppServerClient = new XmppServerClient(); - xmppServerClient.initControlQueue(); - boolean status; - if(XmppConfig.getInstance().isEnabled()) { - status = xmppServerClient.createXMPPAccount(newXmppAccount); - - if (!status) { - String msg = - "XMPP Account was not created for device - " + deviceId + " of owner - " + - owner + ". XMPP might have been disabled in org.wso2.carbon.device.mgt.iot.common.config.server.configs"; - log.warn(msg); - throw new DeviceManagementException(msg); - } - } - - String deviceName = customDeviceName + "_" + deviceId; - status = register(deviceId, deviceName, owner); - - if (!status) { - String msg = "Error occurred while registering the device with " + "id: " + deviceId - + " owner:" + owner; - throw new DeviceManagementException(msg); - } - - - ZipUtil ziputil = new ZipUtil(); - ZipArchive zipFile = null; - - zipFile = ziputil.downloadSketch(owner,SUPER_TENANT, sketchType, deviceId, deviceName, accessToken, refreshToken); - zipFile.setDeviceId(deviceId); - return zipFile; - } - - private static String shortUUID() { - UUID uuid = UUID.randomUUID(); - long l = ByteBuffer.wrap(uuid.toString().getBytes(StandardCharsets.UTF_8)).getLong(); - return Long.toString(l, Character.MAX_RADIX); - } - -}