diff --git a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/VirtualFireAlarmService.java b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/VirtualFireAlarmService.java index 16adabd5..6bc7fef4 100644 --- a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/VirtualFireAlarmService.java +++ b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/VirtualFireAlarmService.java @@ -43,14 +43,12 @@ 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.virtual.firealarm.plugin.constants - .VirtualFireAlarmConstants; -import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util.DeviceJSON; -import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util - .VirtualFireAlarmMQTTSubscriber; -import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util - .VirtualFireAlarmXMPPConnector; -import org.wso2.carbon.utils.CarbonUtils; +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.constants.VirtualFireAlarmConstants; +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dao.TemperatureRecord; +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util.DataHolder; +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dao.DeviceJSON; +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util.VirtualFireAlarmMQTTSubscriber; +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util.VirtualFireAlarmXMPPConnector; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -79,6 +77,7 @@ import java.net.URL; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.Calendar; import java.util.Date; import java.util.List; import java.util.UUID; @@ -209,8 +208,8 @@ public class VirtualFireAlarmService { deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE); try { boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( - deviceIdentifier); - if (removed) { + deviceIdentifier); + if (removed) { response.setStatus(Response.Status.OK.getStatusCode()); } else { @@ -505,7 +504,9 @@ public class VirtualFireAlarmService { String protocolString = protocol.toUpperCase(); String callUrlPattern = VirtualFireAlarmConstants.BULB_CONTEXT + switchToState; - log.info("Sending request to switch-bulb of device [" + deviceId + "] via " + protocolString); + if (log.isDebugEnabled()) { + log.debug("Sending request to switch-bulb of device [" + deviceId + "] via " + protocolString); + } try { switch (protocolString) { @@ -559,9 +560,12 @@ public class VirtualFireAlarmService { } String protocolString = protocol.toUpperCase(); - log.info("Sending request to read sonar value of device [" + deviceId + "] via " + protocolString); - try { + if (log.isDebugEnabled()) { + log.debug("Sending request to read sonar value of device [" + deviceId + "] via " + protocolString); + } + + try { switch (protocolString) { case HTTP_PROTOCOL: String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); @@ -601,40 +605,40 @@ public class VirtualFireAlarmService { @Path("controller/readtemperature") @GET - public String requestTemperature(@HeaderParam("owner") String owner, + @Consumes("application/json") + @Produces("application/json") + public TemperatureRecord requestTemperature(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId, @HeaderParam("protocol") String protocol, @Context HttpServletResponse response) { - String replyMsg = ""; + TemperatureRecord temperatureRecord = 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"; } } catch (DeviceManagementException e) { - replyMsg = e.getErrorMessage(); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return replyMsg; } String protocolString = protocol.toUpperCase(); - log.info("Sending request to read virtual-firealarm-temperature of device [" + deviceId + "] via " + protocolString); + if (log.isDebugEnabled()) { + log.debug("Sending request to read virtual-firealarm-temperature of device [" + deviceId + "] via " + protocolString); + } try { switch (protocolString) { 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 = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false); - break; + } + String tString = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false); + float temperature = Float.parseFloat(tString); + DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis()); + break; case MQTT_PROTOCOL: sendCommandViaMQTT(owner, deviceId, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT.replace("/", ""), ""); @@ -645,20 +649,16 @@ public class VirtualFireAlarmService { break; default: - replyMsg = "Requested protocol '" + protocolString + "' is not supported"; - response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); - return replyMsg; - } + response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); + } + temperatureRecord = DataHolder.getThisInstance().getTemperature(deviceId); } catch (DeviceManagementException e) { - replyMsg = e.getErrorMessage(); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); - return replyMsg; - } + } response.setStatus(Response.Status.OK.getStatusCode()); - replyMsg = "The current temperature of the device is " + replyMsg; - return replyMsg; - } + return temperatureRecord; + } @Path("controller/push_temperature") @POST @@ -684,7 +684,7 @@ public class VirtualFireAlarmService { response.setStatus(Response.Status.CONFLICT.getStatusCode()); return; } - + DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis()); if (!publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } diff --git a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/DeviceJSON.java b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/dao/DeviceJSON.java similarity index 96% rename from modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/DeviceJSON.java rename to modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/dao/DeviceJSON.java index 62ec64fa..34d6054c 100644 --- a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/DeviceJSON.java +++ b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/dao/DeviceJSON.java @@ -1,4 +1,4 @@ -package org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util; +package org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dao; import org.codehaus.jackson.annotate.JsonIgnoreProperties; diff --git a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/dao/TemperatureRecord.java b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/dao/TemperatureRecord.java new file mode 100644 index 00000000..3b3074c9 --- /dev/null +++ b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/dao/TemperatureRecord.java @@ -0,0 +1,41 @@ +/* + * 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.virtual.firealarm.service.impl.dao; + +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +@XmlRootElement +public class TemperatureRecord { + private double temperature; + private long time; + + public TemperatureRecord(double temperature, long time) { + this.temperature = temperature; + this.time = time; + } + + @XmlElement + public double getTemperature() { + return temperature; + } + + @XmlElement + public long getTime() { + return time; + } +} \ No newline at end of file diff --git a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/DataHolder.java b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/DataHolder.java new file mode 100644 index 00000000..e7b9c8db --- /dev/null +++ b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/DataHolder.java @@ -0,0 +1,45 @@ +/* + * 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.virtual.firealarm.service.impl.util; + +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dao.TemperatureRecord; + +import java.util.HashMap; +import java.util.Map; + +public class DataHolder { + + private static DataHolder thisInstance = new DataHolder(); + private Map temperatureMap = new HashMap<>(); + + private DataHolder() { + + } + + public static DataHolder getThisInstance() { + return thisInstance; + } + + public TemperatureRecord getTemperature(String deviceId) { + return temperatureMap.get(deviceId); + } + + public void setTemperature(String deviceId, double temperature, long time){ + temperatureMap.put(deviceId, new TemperatureRecord(temperature, time)); + } + +} diff --git a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/VirtualFireAlarmMQTTSubscriber.java b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/VirtualFireAlarmMQTTSubscriber.java index ae299877..7a980700 100644 --- a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/VirtualFireAlarmMQTTSubscriber.java +++ b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/service/impl/util/VirtualFireAlarmMQTTSubscriber.java @@ -3,18 +3,14 @@ package org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.uti import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.eclipse.paho.client.mqttv3.MqttMessage; -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.DeviceManagementException; import org.wso2.carbon.device.mgt.iot.common.controlqueue.mqtt.MqttConfig; import org.wso2.carbon.device.mgt.iot.common.controlqueue.mqtt.MqttSubscriber; -import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.constants - .VirtualFireAlarmConstants; +import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.constants.VirtualFireAlarmConstants; import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.VirtualFireAlarmService; -import javax.ws.rs.core.Response; import java.io.File; +import java.util.Calendar; import java.util.UUID; public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber { @@ -68,10 +64,14 @@ public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber { if(log.isDebugEnabled()) { log.debug("MQTT Subscriber: Published data to DAS successfully."); } - } else { - log.info("MQTT: Reply Message [" + message.toString() + "] topic: [" + topic + "]"); - } - } + } else if (message.toString().contains("TEMPERATURE")) { + log.info("MQTT: Reply Message [" + message.toString() + "] topic: [" + topic + "]"); + float temperature = Float.parseFloat(message.toString().split(":")[1]); + DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis()); + } else { + log.info("MQTT: Message [" + message.toString() + "] topic: [" + topic + "]"); + } + } private void retryMQTTSubscription() { Thread retryToSubscribe = new Thread() {