Merge pull request #13 from rasika90/master

Generalizing in memory map into any sensor reading
application-manager-new
Rasika Perera 9 years ago
commit fb21dc8b72

@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Analytics> <Analytics>
<Name>IoTServer_Sensor_Script</Name> <Name>IoTServer_Sensor_Script</Name>
<Script> <Script>
CREATE TEMPORARY TABLE DeviceTemperatureData USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_IOT_DEVICES_TEMPERATURE"); CREATE TEMPORARY TABLE DeviceTemperatureData USING CarbonAnalytics OPTIONS(tableName "ORG_WSO2_IOT_DEVICES_TEMPERATURE");
CREATE TEMPORARY TABLE DeviceTemperatureSummaryData USING CarbonAnalytics OPTIONS (tableName "DEVICE_TEMPERATURE_SUMMARY", schema "temperature FLOAT, deviceType STRING -i, deviceId STRING -i, owner STRING -i, time LONG -i",primaryKeys "deviceType, deviceId, owner, time"); CREATE TEMPORARY TABLE DeviceTemperatureSummaryData USING CarbonAnalytics OPTIONS (tableName "DEVICE_TEMPERATURE_SUMMARY", schema "temperature FLOAT, deviceType STRING -i, deviceId STRING -i, owner STRING -i, time LONG -i",primaryKeys "deviceType, deviceId, owner, time");
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); 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);
</Script> </Script>
<CronExpression>0 * * * * ?</CronExpression> <CronExpression>0 * * * * ?</CronExpression>
</Analytics> </Analytics>

@ -27,4 +27,6 @@ public class VirtualFireAlarmConstants {
public static final String BULB_CONTEXT = "/BULB/"; public static final String BULB_CONTEXT = "/BULB/";
public static final String SONAR_CONTEXT = "/HUMIDITY/"; public static final String SONAR_CONTEXT = "/HUMIDITY/";
public static final String TEMPERATURE_CONTEXT = "/TEMPERATURE/"; public static final String TEMPERATURE_CONTEXT = "/TEMPERATURE/";
public static final String SENSOR_TEMPERATURE = "temperature";
} }

@ -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.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.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.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.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.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.VirtualFireAlarmMQTTSubscriber;
@ -246,8 +247,7 @@ public class VirtualFireAlarmService {
device.setName(name); device.setName(name);
device.setType(VirtualFireAlarmConstants.DEVICE_TYPE); device.setType(VirtualFireAlarmConstants.DEVICE_TYPE);
boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment( boolean updated = deviceManagement.getDeviceManagementService().modifyEnrollment(device);
device);
if (updated) { if (updated) {
response.setStatus(Response.Status.OK.getStatusCode()); 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) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
return null; return null;
@ -607,11 +607,11 @@ public class VirtualFireAlarmService {
@GET @GET
@Consumes("application/json") @Consumes("application/json")
@Produces("application/json") @Produces("application/json")
public TemperatureRecord requestTemperature(@HeaderParam("owner") String owner, public SensorRecord 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) {
TemperatureRecord temperatureRecord = null; SensorRecord sensorRecord = null;
DeviceValidator deviceValidator = new DeviceValidator(); DeviceValidator deviceValidator = new DeviceValidator();
try { try {
@ -636,8 +636,9 @@ public class VirtualFireAlarmService {
response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode()); response.setStatus(Response.Status.PRECONDITION_FAILED.getStatusCode());
} }
String tString = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false); String tString = sendCommandViaHTTP(deviceHTTPEndpoint, VirtualFireAlarmConstants.TEMPERATURE_CONTEXT, false);
float temperature = Float.parseFloat(tString); String temperatureValue = tString;
DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis()); DataHolder.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue,
Calendar.getInstance().getTimeInMillis());
break; break;
case MQTT_PROTOCOL: case MQTT_PROTOCOL:
@ -651,13 +652,13 @@ public class VirtualFireAlarmService {
default: default:
response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode()); response.setStatus(Response.Status.NOT_ACCEPTABLE.getStatusCode());
} }
temperatureRecord = DataHolder.getThisInstance().getTemperature(deviceId); sensorRecord = DataHolder.getInstance().getSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE);
} catch (DeviceManagementException e) { } catch (DeviceManagementException e) {
response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()); response.setStatus(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
} }
response.setStatus(Response.Status.OK.getStatusCode()); response.setStatus(Response.Status.OK.getStatusCode());
return temperatureRecord; return sensorRecord;
} }
@Path("controller/push_temperature") @Path("controller/push_temperature")
@ -684,13 +685,38 @@ 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()); DataHolder.getInstance().setSensorRecord(deviceId,
VirtualFireAlarmConstants.SENSOR_TEMPERATURE,
String.valueOf(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());
} }
} }
@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 { private String sendCommandViaHTTP(final String deviceHTTPEndpoint, String urlContext, boolean fireAndForgot) throws DeviceManagementException {
String responseMsg = ""; String responseMsg = "";
@ -764,6 +790,8 @@ public class VirtualFireAlarmService {
return responseMsg; return responseMsg;
} }
private boolean sendCommandViaMQTT(String deviceOwner, String deviceId, String resource, String state) throws DeviceManagementException { private boolean sendCommandViaMQTT(String deviceOwner, String deviceId, String resource, String state) throws DeviceManagementException {
boolean result = false; boolean result = false;

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

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

@ -16,30 +16,69 @@
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.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.HashMap;
import java.util.Map; import java.util.Map;
public class DataHolder { public class DataHolder {
private static DataHolder thisInstance = new DataHolder(); private static DataHolder instance = new DataHolder();
private Map<String, TemperatureRecord> temperatureMap = new HashMap<>(); private Map<String, DeviceRecord> deviceMap = new HashMap<>();
private DataHolder() { private DataHolder() {
}
public static DataHolder getInstance() {
return instance;
} }
public static DataHolder getThisInstance() { public boolean setSensorRecord(String deviceId, String sensorName, String sensorValue, long time){
return thisInstance; DeviceRecord deviceRecord = new DeviceRecord(sensorName, sensorValue, time);
deviceMap.put(deviceId, deviceRecord);
return true;
} }
public TemperatureRecord getTemperature(String deviceId) { /**
return temperatureMap.get(deviceId); * 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);
} }
public void setTemperature(String deviceId, double temperature, long time){ /**
temperatureMap.put(deviceId, new TemperatureRecord(temperature, time)); * 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();
} }
} }

@ -66,8 +66,8 @@ public class VirtualFireAlarmMQTTSubscriber extends MqttSubscriber {
} }
} else if (message.toString().contains("TEMPERATURE")) { } 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]); String temperatureValue = message.toString().split(":")[1];
DataHolder.getThisInstance().setTemperature(deviceId, temperature, Calendar.getInstance().getTimeInMillis()); DataHolder.getInstance().setSensorRecord(deviceId, VirtualFireAlarmConstants.SENSOR_TEMPERATURE, temperatureValue, Calendar.getInstance().getTimeInMillis());
} else { } else {
log.info("MQTT: Message [" + message.toString() + "] topic: [" + topic + "]"); log.info("MQTT: Message [" + message.toString() + "] topic: [" + topic + "]");
} }

Loading…
Cancel
Save