From 9e7e0aa845566afe3180599a79a02ea27921ea4b Mon Sep 17 00:00:00 2001 From: charithag Date: Tue, 22 Dec 2015 15:44:51 +0530 Subject: [PATCH] Fix issue in service ep --- .../constants/VirtualFireAlarmConstants.java | 1 + .../service/VirtualFireAlarmService.java | 45 +++++++++---------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java index 4b9226779b..a6415fe6e0 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/plugin/constants/VirtualFireAlarmConstants.java @@ -31,4 +31,5 @@ public class VirtualFireAlarmConstants { public static final String TEMPERATURE_CONTEXT = "/TEMPERATURE/"; public static final String SENSOR_TEMP = "temperature"; + public static final String SENSOR_HUMIDITY = "humidity"; } diff --git a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java index 81be8d1342..9c968edf84 100644 --- a/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java +++ b/components/device-mgt-iot-virtualfirealarm/org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/virtualfirealarm/service/VirtualFireAlarmService.java @@ -648,7 +648,6 @@ public class VirtualFireAlarmService { /** - * * @param owner * @param deviceId * @param protocol @@ -657,25 +656,23 @@ public class VirtualFireAlarmService { */ @Path("controller/readhumidity") @GET - @Feature( code="readhumidity", name="Humidity", type = "monitor", - description="Read Humidity Readings from Virtual Fire Alarm") - public String requestHumidity(@HeaderParam("owner") String owner, - @HeaderParam("deviceId") String deviceId, - @HeaderParam("protocol") String protocol, - @Context HttpServletResponse response) { - String replyMsg = ""; - + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @Feature(code = "readhumidity", name = "Humidity", type = "monitor", + description = "Read Humidity Readings from Virtual Fire Alarm") + public SensorRecord requestHumidity(@HeaderParam("owner") String owner, + @HeaderParam("deviceId") String deviceId, + @HeaderParam("protocol") String protocol, + @Context HttpServletResponse response) { + SensorRecord sensorRecord = null; DeviceValidator deviceValidator = new DeviceValidator(); try { if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier( deviceId, VirtualFireAlarmConstants.DEVICE_TYPE))) { response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); - return "Unauthorized Access Attempt"; } } catch (DeviceManagementException e) { - replyMsg = e.getErrorMessage(); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return replyMsg; } String protocolString = protocol.toUpperCase(); @@ -689,14 +686,16 @@ public class VirtualFireAlarmService { case HTTP_PROTOCOL: String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); if (deviceHTTPEndpoint == null) { - replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner; response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); - return replyMsg; } - replyMsg = VirtualFireAlarmServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, - VirtualFireAlarmConstants.HUMIDITY_CONTEXT, - false); + String humidityValue = VirtualFireAlarmServiceUtils.sendCommandViaHTTP(deviceHTTPEndpoint, + VirtualFireAlarmConstants.HUMIDITY_CONTEXT, + false); + SensorDataManager.getInstance().setSensorRecord(deviceId, + VirtualFireAlarmConstants.SENSOR_TEMP, + humidityValue, + Calendar.getInstance().getTimeInMillis()); break; case MQTT_PROTOCOL: @@ -710,22 +709,18 @@ public class VirtualFireAlarmService { break; default: - replyMsg = "Requested protocol '" + protocolString + "' is not supported"; response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - return replyMsg; } - } catch (DeviceManagementException | TransportHandlerException e) { - replyMsg = e.getMessage(); + sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, + VirtualFireAlarmConstants.SENSOR_HUMIDITY); + } catch (DeviceManagementException | DeviceControllerException | TransportHandlerException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return replyMsg; } response.setStatus(Response.Status.OK.getStatusCode()); - replyMsg = "The current humidity reading of the device is " + replyMsg; - return replyMsg; + return sensorRecord; } - /** * * @param owner