From 1983f3a55d944cb92a6cb4df43c79e128b0f7e7f Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Sun, 17 Jul 2016 00:16:33 +0200 Subject: [PATCH] Added data report interval so the null values in the graphs are specific to different protocols --- src/se/hal/intf/HalEventData.java | 2 +- src/se/hal/intf/HalSensorData.java | 19 +++++++++++--- src/se/hal/page/MapHttpPage.java | 10 ++++--- src/se/hal/plugin/nutups/NutUpsDevice.java | 7 ++++- .../raspberry/RPiPowerConsumptionSensor.java | 8 +++++- .../raspberry/RPiTemperatureSensor.java | 8 +++++- .../plugin/tellstick/TellstickProtocol.java | 2 -- .../tellstick/protocols/NexaSelfLearning.java | 3 ++- .../tellstick/protocols/Oregon0x1A2D.java | 12 ++++++++- .../hal/util/AggregateDataListSqlResult.java | 26 ++++++++++++++----- 10 files changed, 76 insertions(+), 21 deletions(-) mode change 100644 => 100755 src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java mode change 100644 => 100755 src/se/hal/plugin/raspberry/RPiTemperatureSensor.java diff --git a/src/se/hal/intf/HalEventData.java b/src/se/hal/intf/HalEventData.java index dc0c068a..8efaf588 100755 --- a/src/se/hal/intf/HalEventData.java +++ b/src/se/hal/intf/HalEventData.java @@ -15,6 +15,6 @@ public interface HalEventData { * This method needs to be implemented. * NOTE: it should not compare data and timestamp, only static or unique data for the event type. */ - boolean equals(Object obj); + boolean equals(HalEventData obj); } diff --git a/src/se/hal/intf/HalSensorData.java b/src/se/hal/intf/HalSensorData.java index cc276096..a2211783 100755 --- a/src/se/hal/intf/HalSensorData.java +++ b/src/se/hal/intf/HalSensorData.java @@ -14,13 +14,26 @@ public interface HalSensorData { double getData(); + + /** + * @return the intended data reporting interval in milliseconds. + */ + long getDataInterval(); + + /** + * @return which aggregation method that should be used to aggregate the reported data. + */ AggregationMethod getAggregationMethod(); + + /** + * @return the Controller class where SensorData should be registered on + */ Class getSensorController(); /** - * This method needs to be implemented. - * NOTE: it should not compare data and timestamp, only static or unique data for the event type. + * NOTE: it should only static or unique data for the sensor type. + * This method is used to associate reported data with registered sensors */ - boolean equals(Object obj); + boolean equals(HalSensorData obj); } diff --git a/src/se/hal/page/MapHttpPage.java b/src/se/hal/page/MapHttpPage.java index a1146130..1a366241 100755 --- a/src/se/hal/page/MapHttpPage.java +++ b/src/se/hal/page/MapHttpPage.java @@ -63,9 +63,13 @@ public class MapHttpPage extends HalHttpPage implements HalHttpPage.HalJsonPage } else if (request.containsKey("bgimage")) { if (bgImage == null) loadBgImage(); - out.setHeader("Content-Type", "image/"+bgType); - out.setHeader("Content-Length", ""+bgImage.length); - out.write(bgImage); + if (bgImage == null) + out.setStatusCode(404); + else { + out.setHeader("Content-Type", "image/" + bgType); + out.setHeader("Content-Length", "" + bgImage.length); + out.write(bgImage); + } } else { // Run default Hal behaviour super.respond(out, header, session, cookie, request); } diff --git a/src/se/hal/plugin/nutups/NutUpsDevice.java b/src/se/hal/plugin/nutups/NutUpsDevice.java index c5e761b0..e5b4cc36 100755 --- a/src/se/hal/plugin/nutups/NutUpsDevice.java +++ b/src/se/hal/plugin/nutups/NutUpsDevice.java @@ -37,7 +37,12 @@ public class NutUpsDevice implements PowerConsumptionSensorData{ } @Override - public boolean equals(Object obj){ + public long getDataInterval(){ + return 60*1000; // 1 min + } + + @Override + public boolean equals(HalSensorData obj){ if (obj instanceof NutUpsDevice) return deviceId != null && deviceId.equals(((NutUpsDevice)obj).deviceId); return false; diff --git a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java b/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java old mode 100644 new mode 100755 index 938f49c5..e3ddb3b3 --- a/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java +++ b/src/se/hal/plugin/raspberry/RPiPowerConsumptionSensor.java @@ -1,6 +1,7 @@ package se.hal.plugin.raspberry; import se.hal.intf.HalSensorController; +import se.hal.intf.HalSensorData; import se.hal.struct.PowerConsumptionSensorData; import zutil.ui.Configurator; @@ -32,6 +33,11 @@ public class RPiPowerConsumptionSensor implements PowerConsumptionSensorData { return data; } + @Override + public long getDataInterval(){ + return 60*1000; // 1 min + } + @Override public AggregationMethod getAggregationMethod() { return AggregationMethod.SUM; @@ -42,7 +48,7 @@ public class RPiPowerConsumptionSensor implements PowerConsumptionSensorData { return RPiController.class; } - public boolean equals(Object obj){ + public boolean equals(HalSensorData obj){ if(!(obj instanceof RPiPowerConsumptionSensor)) return false; return ((RPiPowerConsumptionSensor)obj).gpioPin == gpioPin; diff --git a/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java b/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java old mode 100644 new mode 100755 index f090a079..af700e18 --- a/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java +++ b/src/se/hal/plugin/raspberry/RPiTemperatureSensor.java @@ -1,6 +1,7 @@ package se.hal.plugin.raspberry; import se.hal.intf.HalSensorController; +import se.hal.intf.HalSensorData; import se.hal.struct.TemperatureSensorData; import zutil.ui.Configurator; @@ -31,6 +32,11 @@ public class RPiTemperatureSensor implements TemperatureSensorData { return data; } + @Override + public long getDataInterval() { + return 10*60*1000; // 10 min + } + @Override public AggregationMethod getAggregationMethod() { return AggregationMethod.AVERAGE; @@ -41,7 +47,7 @@ public class RPiTemperatureSensor implements TemperatureSensorData { return RPiController.class; } - public boolean equals(Object obj){ + public boolean equals(HalSensorData obj){ if(obj instanceof RPiTemperatureSensor) return obj == this; return false; diff --git a/src/se/hal/plugin/tellstick/TellstickProtocol.java b/src/se/hal/plugin/tellstick/TellstickProtocol.java index aeee9c3d..0a601416 100755 --- a/src/se/hal/plugin/tellstick/TellstickProtocol.java +++ b/src/se/hal/plugin/tellstick/TellstickProtocol.java @@ -66,6 +66,4 @@ public abstract class TellstickProtocol { public abstract String encode(); public abstract void decode(byte[] data); - public abstract boolean equals(Object obj); - } diff --git a/src/se/hal/plugin/tellstick/protocols/NexaSelfLearning.java b/src/se/hal/plugin/tellstick/protocols/NexaSelfLearning.java index 612363bf..c704853f 100755 --- a/src/se/hal/plugin/tellstick/protocols/NexaSelfLearning.java +++ b/src/se/hal/plugin/tellstick/protocols/NexaSelfLearning.java @@ -22,6 +22,7 @@ package se.hal.plugin.tellstick.protocols; +import se.hal.intf.HalEventData; import se.hal.plugin.tellstick.TellstickGroupProtocol; import se.hal.plugin.tellstick.TellstickProtocol; import se.hal.struct.SwitchEventData; @@ -147,7 +148,7 @@ public class NexaSelfLearning extends TellstickProtocol } @Override - public boolean equals(Object obj){ + public boolean equals(HalEventData obj){ if(obj instanceof NexaSelfLearning) return ((NexaSelfLearning) obj).house == house && ((NexaSelfLearning) obj).group == group && diff --git a/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java b/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java index f9262fa1..b7512835 100755 --- a/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java +++ b/src/se/hal/plugin/tellstick/protocols/Oregon0x1A2D.java @@ -1,5 +1,6 @@ package se.hal.plugin.tellstick.protocols; +import se.hal.intf.HalSensorData; import se.hal.plugin.tellstick.TellstickProtocol; import se.hal.struct.PowerConsumptionSensorData; import zutil.log.LogUtil; @@ -15,15 +16,19 @@ public class Oregon0x1A2D extends TellstickProtocol implements PowerConsumptionS @Configurator.Configurable("Address") private int address = 0; + @Configurator.Configurable("Report Interval(ms)") + private int interval = 60*1000; // default 1 min private double temperature = 0; private double humidity = 0; + public Oregon0x1A2D(){ super("oregon", "0x1A2D"); } + @Override public String encode() { return null; @@ -63,7 +68,7 @@ public class Oregon0x1A2D extends TellstickProtocol implements PowerConsumptionS } - public boolean equals(Object obj){ + public boolean equals(HalSensorData obj){ if(! (obj instanceof Oregon0x1A2D)) return false; return ((Oregon0x1A2D)obj).address == this.address; @@ -90,6 +95,11 @@ public class Oregon0x1A2D extends TellstickProtocol implements PowerConsumptionS return temperature; } + @Override + public long getDataInterval() { + return interval; + } + @Override public AggregationMethod getAggregationMethod() { return AggregationMethod.SUM; diff --git a/src/se/hal/util/AggregateDataListSqlResult.java b/src/se/hal/util/AggregateDataListSqlResult.java index 47299289..54f7cd84 100755 --- a/src/se/hal/util/AggregateDataListSqlResult.java +++ b/src/se/hal/util/AggregateDataListSqlResult.java @@ -54,15 +54,23 @@ public class AggregateDataListSqlResult implements SQLResultHandler handleQueryResult(Statement stmt, ResultSet result) throws SQLException { ArrayList list = new ArrayList<>(); long previousTimestampEnd = -1; - while(result.next()){ + while (result.next()){ int id = result.getInt("id"); long timestampStart = result.getLong("timestamp_start"); @@ -70,18 +78,22 @@ public class AggregateDataListSqlResult implements SQLResultHandler sensor.getDeviceData().getDataInterval()) { + // Add null data point to list if one or more periods of data is missing before this + if (previousTimestampEnd != -1 && previousTimestampEnd + 1 < timestampStart) { + list.add(new AggregateData(id, previousTimestampEnd + 1, null /*Float.NaN*/, username)); + } } list.add(new AggregateData(id, timestampEnd, (estimatedData/1000f), username)); //add this data point to list - //update previous end timestamp + // Update previous end timestamp previousTimestampEnd = timestampEnd; } return list;