migrating in memory sensor manager

application-manager-new
Rasika Perera 9 years ago
parent e31fda92a6
commit 6fabb28b1c

@ -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,29 +40,18 @@ 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.dao.DeviceRecord;
import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dao.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.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;
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;
@ -77,11 +65,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;
@ -637,7 +621,7 @@ public class VirtualFireAlarmService {
}
String tString = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false);
String temperatureValue = tString;
DataHolder.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue,
SensorDataManager.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue,
Calendar.getInstance().getTimeInMillis());
break;
@ -652,7 +636,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());
}
@ -685,7 +669,7 @@ 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());
@ -695,28 +679,6 @@ public class VirtualFireAlarmService {
}
@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.dao;
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.dao;
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.dao.DeviceRecord;
import org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.service.impl.dao.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