Merge pull request #11 from charithag/master

Add in memory hash table to hold last received temperature data
application-manager-new
Charitha Goonetilleke 9 years ago
commit e88019a56c

@ -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.exception.DeviceControllerException;
import org.wso2.carbon.device.mgt.iot.common.util.ZipArchive; 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.common.util.ZipUtil;
import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.constants import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.constants.VirtualFireAlarmConstants;
.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.DeviceJSON; 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.util import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dao.DeviceJSON;
.VirtualFireAlarmMQTTSubscriber; 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 import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.util.VirtualFireAlarmXMPPConnector;
.VirtualFireAlarmXMPPConnector;
import org.wso2.carbon.utils.CarbonUtils;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -79,6 +77,7 @@ import java.net.URL;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
@ -209,8 +208,8 @@ public class VirtualFireAlarmService {
deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE); deviceIdentifier.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
try { try {
boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice( boolean removed = deviceManagement.getDeviceManagementService().disenrollDevice(
deviceIdentifier); deviceIdentifier);
if (removed) { if (removed) {
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
} else { } else {
@ -505,7 +504,9 @@ public class VirtualFireAlarmService {
String protocolString = protocol.toUpperCase(); String protocolString = protocol.toUpperCase();
String callUrlPattern = VirtualFireAlarmConstants.BULB_CONTEXT + switchToState; 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 { try {
switch (protocolString) { switch (protocolString) {
@ -559,9 +560,12 @@ public class VirtualFireAlarmService {
} }
String protocolString = protocol.toUpperCase(); 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) { switch (protocolString) {
case HTTP_PROTOCOL: case HTTP_PROTOCOL:
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
@ -601,40 +605,40 @@ public class VirtualFireAlarmService {
@Path("controller/readtemperature") @Path("controller/readtemperature")
@GET @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("deviceId") String deviceId,
@HeaderParam("protocol") String protocol, @HeaderParam("protocol") String protocol,
@Context HttpServletResponse response) { @Context HttpServletResponse response) {
String replyMsg = ""; TemperatureRecord temperatureRecord = null;
DeviceValidator deviceValidator = new DeviceValidator(); DeviceValidator deviceValidator = new DeviceValidator();
try { try {
if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE))) { if (!deviceValidator.isExist(owner, SUPER_TENANT, new DeviceIdentifier(deviceId, VirtualFireAlarmConstants.DEVICE_TYPE))) {
response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode()); response.setStatus(Response.Status.UNAUTHORIZED.getStatusCode());
return "Unauthorized Access";
} }
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
replyMsg = e.getErrorMessage();
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return replyMsg;
} }
String protocolString = protocol.toUpperCase(); 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 { try {
switch (protocolString) { switch (protocolString) {
case HTTP_PROTOCOL: case HTTP_PROTOCOL:
String deviceHTTPEndpoint = deviceToIpMap.get(deviceId); String deviceHTTPEndpoint = deviceToIpMap.get(deviceId);
if (deviceHTTPEndpoint == null) { if (deviceHTTPEndpoint == null) {
replyMsg = "IP not registered for device: " + deviceId + " of owner: " + owner;
response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
return replyMsg; }
} String tString = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false);
float temperature = Float.parseFloat(tString);
replyMsg = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false); DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis());
break; break;
case MQTT_PROTOCOL: case MQTT_PROTOCOL:
sendCommandViaMQTT(owner, deviceId, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT.replace("/", ""), ""); sendCommandViaMQTT(owner, deviceId, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT.replace("/", ""), "");
@ -645,20 +649,16 @@ public class VirtualFireAlarmService {
break; break;
default: default:
replyMsg = "Requested protocol '" + protocolString + "' is not supported"; response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); }
return replyMsg; temperatureRecord = DataHolder.getThisInstance().getTemperature(deviceId);
}
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
replyMsg = e.getErrorMessage();
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return replyMsg; }
}
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
replyMsg = "The current temperature of the device is " + replyMsg; return temperatureRecord;
return replyMsg; }
}
@Path("controller/push_temperature") @Path("controller/push_temperature")
@POST @POST
@ -684,7 +684,7 @@ public class VirtualFireAlarmService {
response.setStatus(Response.Status.CONFLICT.getStatusCode()); response.setStatus(Response.Status.CONFLICT.getStatusCode());
return; return;
} }
DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis());
if (!publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) { if (!publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} }

@ -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; import org.codehaus.jackson.annotate.JsonIgnoreProperties;

@ -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;
}
}

@ -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<String, TemperatureRecord> 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));
}
}

@ -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.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.eclipse.paho.client.mqttv3.MqttMessage; 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.common.DeviceManagementException;
import org.wso2.carbon.device.mgt.iot.common.controlqueue.mqtt.MqttConfig; 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.common.controlqueue.mqtt.MqttSubscriber;
import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.constants import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.constants.VirtualFireAlarmConstants;
.VirtualFireAlarmConstants;
import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.VirtualFireAlarmService; 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.io.File;
import java.util.Calendar;
import java.util.UUID; import java.util.UUID;
public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber { public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber {
@ -68,10 +64,14 @@ public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber {
if(log.isDebugEnabled()) { if(log.isDebugEnabled()) {
log.debug("MQTT Subscriber: Published data to DAS successfully."); log.debug("MQTT Subscriber: Published data to DAS successfully.");
} }
} else { } else if (message.toString().contains("TEMPERATURE")) {
log.info("MQTT: Reply Message [" + message.toString() + "] topic: [" + topic + "]"); 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() { private void retryMQTTSubscription() {
Thread retryToSubscribe = new Thread() { Thread retryToSubscribe = new Thread() {

Loading…
Cancel
Save