From 50e90fa8de7c9c119c9dcdd513f9a685911e5eed Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 23 Oct 2015 01:04:32 +0530 Subject: [PATCH 1/2] generalizing inmemory map into any sensor --- ...mperatureRecord.java => DeviceRecord.java} | 4 ++-- .../service/impl/dao/SensorRecord.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) rename 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 => DeviceRecord.java} (92%) create mode 100644 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/SensorRecord.java 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/DeviceRecord.java similarity index 92% 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/dao/TemperatureRecord.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/DeviceRecord.java index 3b3074c9..c4358ae2 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/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/DeviceRecord.java @@ -20,11 +20,11 @@ import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement -public class TemperatureRecord { +public class DeviceRecord { private double temperature; private long time; - public TemperatureRecord(double temperature, long time) { + public DeviceRecord(double temperature, long time) { this.temperature = temperature; this.time = 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/dao/SensorRecord.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/SensorRecord.java new file mode 100644 index 00000000..e52e722c --- /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/SensorRecord.java @@ -0,0 +1,24 @@ +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; + } + +} \ No newline at end of file From e31fda92a695850471bb6633f3888531124907b1 Mon Sep 17 00:00:00 2001 From: Rasika Perera Date: Fri, 23 Oct 2015 01:04:55 +0530 Subject: [PATCH 2/2] generalizing inmemory map into any sensor --- .../Temperature_Sensor_Script.xml | 12 ++-- .../constants/VirtualFireAlarmConstants.java | 2 + .../service/impl/VirtualFireAlarmService.java | 50 ++++++++++++---- .../service/impl/dao/DeviceRecord.java | 21 +++---- .../service/impl/util/DataHolder.java | 57 ++++++++++++++++--- .../util/VirtualFireAlarmMQTTSubscriber.java | 4 +- 6 files changed, 106 insertions(+), 40 deletions(-) diff --git a/modules/distribution/src/statistics/carbonapps/Temperature_Sensor/Sparkscripts_1.0.0/Temperature_Sensor_Script.xml b/modules/distribution/src/statistics/carbonapps/Temperature_Sensor/Sparkscripts_1.0.0/Temperature_Sensor_Script.xml index b604b5ab..ee6a7195 100644 --- a/modules/distribution/src/statistics/carbonapps/Temperature_Sensor/Sparkscripts_1.0.0/Temperature_Sensor_Script.xml +++ b/modules/distribution/src/statistics/carbonapps/Temperature_Sensor/Sparkscripts_1.0.0/Temperature_Sensor_Script.xml @@ -1,13 +1,13 @@ IoTServer_Sensor_Script - + + insert overwrite table DeviceTemperatureSummaryData select temperature, meta_deviceType as deviceType, meta_deviceId as deviceId, meta_owner as owner, cast(meta_time/1000 as BIGINT)as time from DeviceTemperatureData group by temperature, meta_deviceType, meta_deviceId, meta_owner, cast(meta_time/1000 as BIGINT); + 0 * * * * ? diff --git a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/plugin/constants/VirtualFireAlarmConstants.java b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/plugin/constants/VirtualFireAlarmConstants.java index e780ee83..64e9d8fe 100644 --- a/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/plugin/constants/VirtualFireAlarmConstants.java +++ b/modules/samples/virtual_firealarm/src/org.wso2.carbon.device.mgt.iot.sample.virtual.firealarm.plugin.impl/src/main/java/org/wso2/carbon/device/mgt/iot/sample/virtual/firealarm/plugin/constants/VirtualFireAlarmConstants.java @@ -27,4 +27,6 @@ public class VirtualFireAlarmConstants { public static final String BULB_CONTEXT = "/BULB/"; public static final String SONAR_CONTEXT = "/HUMIDITY/"; public static final String TEMPERATURE_CONTEXT = "/TEMPERATURE/"; + + public static final String SENSOR_TEMPERATURE = "temperature"; } 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 6bc7fef4..403aabab 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 @@ -44,7 +44,8 @@ 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.dao.TemperatureRecord; +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; @@ -246,8 +247,7 @@ public class VirtualFireAlarmService { device.setName(name); device.setType(VirtualFireAlarmConstants.DEVICE_TYPE); - boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment( - device); + boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device); if (updated) { response.setStatus(Response.Status.OK.getStatusCode()); @@ -312,7 +312,7 @@ public class VirtualFireAlarmService { } } - return userDevicesforFirealarm.toArray(new Device[]{}); + return userDevicesforFirealarm.toArray(new Device[] {}); } catch (DeviceManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); return null; @@ -607,11 +607,11 @@ public class VirtualFireAlarmService { @GET @Consumes("application/json") @Produces("application/json") - public TemperatureRecord requestTemperature(@HeaderParam("owner") String owner, + public SensorRecord requestTemperature(@HeaderParam("owner") String owner, @HeaderParam("deviceId") String deviceId, @HeaderParam("protocol") String protocol, @Context HttpServletResponse response) { - TemperatureRecord temperatureRecord = null; + SensorRecord sensorRecord = null; DeviceValidator deviceValidator = new DeviceValidator(); try { @@ -636,8 +636,9 @@ public class VirtualFireAlarmService { response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); } String tString = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false); - float temperature = Float.parseFloat(tString); - DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis()); + String temperatureValue = tString; + DataHolder.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue, + Calendar.getInstance().getTimeInMillis()); break; case MQTT_PROTOCOL: @@ -651,13 +652,13 @@ public class VirtualFireAlarmService { default: response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); } - temperatureRecord = DataHolder.getThisInstance().getTemperature(deviceId); + sensorRecord = DataHolder.getInstance().getSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE); } catch (DeviceManagementException e) { response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); } response.setStatus(Response.Status.OK.getStatusCode()); - return temperatureRecord; + return sensorRecord; } @Path("controller/push_temperature") @@ -684,13 +685,38 @@ public class VirtualFireAlarmService { response.setStatus(Response.Status.CONFLICT.getStatusCode()); return; } - DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis()); + DataHolder.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 = ""; @@ -764,6 +790,8 @@ public class VirtualFireAlarmService { return responseMsg; } + + private boolean sendCommandViaMQTT(String deviceOwner, String deviceId, String resource, String state) throws DeviceManagementException { boolean result = false; 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/DeviceRecord.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/DeviceRecord.java index c4358ae2..49f679fd 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/dao/DeviceRecord.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/DeviceRecord.java @@ -18,24 +18,21 @@ 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 { - private double temperature; - private long time; + //all int, float, boolean should be converted into string + //when saving on the map + private Map sensorDataList = new HashMap<>(); - public DeviceRecord(double temperature, long time) { - this.temperature = temperature; - this.time = time; + public DeviceRecord(String sensorName, String sensorValue, long time) { + sensorDataList.put(sensorName, new SensorRecord(sensorValue, time)); } @XmlElement - public double getTemperature() { - return temperature; - } - - @XmlElement - public long getTime() { - return time; + public Map getSensorDataList() { + return sensorDataList; } } \ 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 index e7b9c8db..9d97902b 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/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 @@ -16,30 +16,69 @@ 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 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 thisInstance = new DataHolder(); - private Map temperatureMap = new HashMap<>(); + private static DataHolder instance = new DataHolder(); + private Map 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 list = deviceMap.get(deviceId).getSensorDataList().values(); + return list.toArray(new SensorRecord[list.size()]); } - public static DataHolder getThisInstance() { - return thisInstance; + /** + * 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); } - public TemperatureRecord getTemperature(String deviceId) { - return temperatureMap.get(deviceId); + /** + * 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(); } - public void setTemperature(String deviceId, double temperature, long time){ - temperatureMap.put(deviceId, new TemperatureRecord(temperature, time)); + /** + * 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(); } } 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 7a980700..08014b6c 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 @@ -66,8 +66,8 @@ public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber { } } 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()); + String temperatureValue = message.toString().split(":")[1]; + DataHolder.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue, Calendar.getInstance().getTimeInMillis()); } else { log.info("MQTT: Message [" + message.toString() + "] topic: [" + topic + "]"); }