Merge pull request #14 from rasika90/master

Migrating in memory sensor manager
application-manager-new
Rasika Perera 9 years ago
commit 42236f390f

@ -16,7 +16,6 @@
package org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
@ -41,30 +40,19 @@ 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.sensormgt.SensorDataManager;
import org.wso2.carbon.device.mgt.iot.common.sensormgt.SensorRecord;
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.dto.SensorRecord;
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.dto.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;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
import javax.ws.rs.HttpMethod;
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.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@ -78,11 +66,7 @@ import java.net.ProtocolException;
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;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
@ -638,12 +622,8 @@ public class VirtualFireAlarmService {
}
String tString = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false);
String temperatureValue = tString;
DataHolder.getInstance().setSensorRecord(deviceId,
VirtualFireAlarmConstants
.SENSOR_TEMPERATURE,
temperatureValue,
Calendar.getInstance()
.getTimeInMillis());
SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue,
Calendar.getInstance().getTimeInMillis());
break;
case MQTT_PROTOCOL:
@ -658,8 +638,7 @@ public class VirtualFireAlarmService {
default:
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
}
sensorRecord = DataHolder.getInstance().getSensorRecord(deviceId,
VirtualFireAlarmConstants.SENSOR_TEMPERATURE);
sensorRecord = SensorDataManager.getInstance().getSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE);
} catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
@ -692,38 +671,17 @@ public class VirtualFireAlarmService {
response.setStatus(Response.Status.CONFLICT.getStatusCode());
return;
}
DataHolder.getInstance().setSensorRecord(deviceId,
SensorDataManager.getInstance().setSensorRecord(deviceId,
VirtualFireAlarmConstants.SENSOR_TEMPERATURE,
String.valueOf(temperature),
Calendar.getInstance().getTimeInMillis());
if (!publishToDAS(dataMsg.owner, dataMsg.deviceId, dataMsg.value)) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
@Path("controller/set_sensor")
@POST
@Consumes("application/json")
@Produces("application/json")
public boolean setSensorReading(@HeaderParam("deviceId") String deviceId,
@HeaderParam("sensorName") String sensorName,
@HeaderParam("sensorValue") String sensorValue,
@Context HttpServletResponse response) {
return DataHolder.getInstance().setSensorRecord(deviceId, sensorName, sensorValue, Calendar.getInstance().getTimeInMillis());
}
@Path("controller/read_sensor")
@GET
@Consumes("application/json")
@Produces("application/json")
public SensorRecord readSensorReading(@HeaderParam("deviceId") String deviceId,
@HeaderParam("sensorName") String sensorName,
@Context HttpServletResponse response) {
return DataHolder.getInstance().getSensorRecord(deviceId, sensorName);
}
private String sendCommandViaHTTP(final String deviceHTTPEndpoint, String urlContext, boolean fireAndForgot) throws DeviceManagementException {
String responseMsg = "";

@ -1,38 +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.virtual.firealarm.service.impl.dto;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.HashMap;
import java.util.Map;
@XmlRootElement
public class DeviceRecord {
//all int, float, boolean should be converted into string
//when saving on the map
private Map<String, SensorRecord> sensorDataList = new HashMap<>();
public DeviceRecord(String sensorName, String sensorValue, long time) {
sensorDataList.put(sensorName, new SensorRecord(sensorValue, time));
}
@XmlElement
public Map<String, SensorRecord> getSensorDataList() {
return sensorDataList;
}
}

@ -1,24 +0,0 @@
package org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dto;
import javax.xml.bind.annotation.XmlElement;
public class SensorRecord {
private String sensorValue;
private long time;
public SensorRecord(String sensorValue, long time) {
this.sensorValue = sensorValue;
this.time = time;
}
@XmlElement
public String getSensorValue() {
return sensorValue;
}
@XmlElement
public long getTime() {
return time;
}
}

@ -1,84 +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.virtual.firealarm.service.impl.util;
import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dto.DeviceRecord;
import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dto.SensorRecord;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class DataHolder {
private static DataHolder instance = new DataHolder();
private Map<String, DeviceRecord> deviceMap = new HashMap<>();
private DataHolder() {
}
public static DataHolder getInstance() {
return instance;
}
public boolean setSensorRecord(String deviceId, String sensorName, String sensorValue, long time){
DeviceRecord deviceRecord = new DeviceRecord(sensorName, sensorValue, time);
deviceMap.put(deviceId, deviceRecord);
return true;
}
/**
* Returns last updated sensor records list for a device
* @param deviceId
* @return
*/
public SensorRecord[] getSensorRecords(String deviceId){
Collection<SensorRecord> list = deviceMap.get(deviceId).getSensorDataList().values();
return list.toArray(new SensorRecord[list.size()]);
}
/**
* Returns last updated sensor record for a device's sensor
* @param deviceId
* @param sensorName
* @return
*/
public SensorRecord getSensorRecord(String deviceId, String sensorName){
return deviceMap.get(deviceId).getSensorDataList().get(sensorName);
}
/**
* Returns last updated sensor value for a device's sensor
* @param deviceId
* @param sensorName
* @return
*/
public String getSensorRecordValue(String deviceId, String sensorName){
return deviceMap.get(deviceId).getSensorDataList().get(sensorName).getSensorValue();
}
/**
* Returns last updated sensor value reading time for a device's sensor
* @param deviceId
* @param sensorName
* @return
*/
public long getSensorRecordTime(String deviceId, String sensorName){
return deviceMap.get(deviceId).getSensorDataList().get(sensorName).getTime();
}
}

@ -6,6 +6,7 @@ import org.eclipse.paho.client.mqttv3.MqttMessage;
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.common.sensormgt.SensorDataManager;
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;
@ -67,7 +68,7 @@ public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber {
} else if (message.toString().contains("TEMPERATURE")) {
log.info("MQTT: Reply Message [" + message.toString() + "] topic: [" + topic + "]");
String temperatureValue = message.toString().split(":")[1];
DataHolder.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue, Calendar.getInstance().getTimeInMillis());
SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue, Calendar.getInstance().getTimeInMillis());
} else {
log.info("MQTT: Message [" + message.toString() + "] topic: [" + topic + "]");
}

Loading…
Cancel
Save