From 7c2b0f680c0ef943385116cca3167572f959b1ac Mon Sep 17 00:00:00 2001 From: Bertrik Sikken Date: Sun, 15 Oct 2023 16:42:37 +0200 Subject: [PATCH] Add basic sanity check for range of measurement items. --- .../java/nl/bertriksikken/pm/ESensorItem.java | 60 ++++++++++++------- .../java/nl/bertriksikken/pm/SensorData.java | 6 +- .../nl/bertriksikken/pm/json/JsonDecoder.java | 2 +- .../nl/bertriksikken/pm/SensorDataTest.java | 33 ++++++++++ 4 files changed, 77 insertions(+), 24 deletions(-) create mode 100644 sensor-data-bridge/src/test/java/nl/bertriksikken/pm/SensorDataTest.java diff --git a/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/ESensorItem.java b/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/ESensorItem.java index 2e7bcaf..b363b94 100644 --- a/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/ESensorItem.java +++ b/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/ESensorItem.java @@ -2,45 +2,61 @@ public enum ESensorItem { - PM10("Particulate matter PM10 (aka P1), in ug/m3"), - PM2_5("Particulate matter PM2.5 (aka P2), in ug/m3"), - PM4_0("Particulate matter PM4 (aka P4), in ug/m3"), - PM1_0("Particulate matter PM1.0 (aka P0), in ug/m3"), + PM10("Particulate matter PM10 (aka P1), in ug/m3", 0), + PM2_5("Particulate matter PM2.5 (aka P2), in ug/m3", 0), + PM4_0("Particulate matter PM4 (aka P4), in ug/m3", 0), + PM1_0("Particulate matter PM1.0 (aka P0), in ug/m3", 0), - PM10_N("Particulate matter PM10, in #/cm3"), - PM4_0_N("Particulate matter PM4, in #/cm3"), - PM2_5_N("Particulate matter PM2.5, in #/cm3"), - PM1_0_N("Particulate matter PM1.0, in #/cm3"), - PM0_5_N("Particulate matter PM0.5, in #/cm3"), + PM10_N("Particulate matter PM10, in #/cm3", 0), + PM4_0_N("Particulate matter PM4, in #/cm3", 0), + PM2_5_N("Particulate matter PM2.5, in #/cm3", 0), + PM1_0_N("Particulate matter PM1.0, in #/cm3", 0), + PM0_5_N("Particulate matter PM0.5, in #/cm3", 0), PM_TPS("Typical particle size, in um"), - HUMI("Relative humidity, in percent"), - TEMP("Temperature, in degrees Celcius"), - PRESSURE("Atmospheric pressure, in Pa"), + HUMI("Relative humidity, in percent", 0, 100), + TEMP("Temperature, in degrees Celcius", -100, 100), + PRESSURE("Atmospheric pressure, in Pa", 0, 1E6), - POS_LAT("Latitude in degrees"), - POS_LON("Longitude in degrees"), + POS_LAT("Latitude in degrees", -90, 90), + POS_LON("Longitude in degrees", -180, 180), POS_ALT("Altitude in meters"), - NOISE_LA_EQ("Noise avg (dBA)"), - NOISE_LA_MIN("Noise min (dBA)"), - NOISE_LA_MAX("Noise max (dBA)"), + NOISE_LA_EQ("Noise avg (dBA)", 0, 200), + NOISE_LA_MIN("Noise min (dBA)", 0, 200), + NOISE_LA_MAX("Noise max (dBA)", 0, 200), - NO2("NO2 concentration (unit?)"), - RADIATION("Radiation (unit?)"), + NO2("NO2 concentration (unit?)", 0), + RADIATION("Radiation (unit?)", 0), - LORA_SF("Spreading factor"), + LORA_SF("Spreading factor", 6, 12), LORA_SNR("Signal-to-noise ratio"), LORA_RSSI("Signal strength, in dBm"); + private double minValue; + private double maxValue; private String description; - private ESensorItem(String description) { + private ESensorItem(String description, double minValue, double maxValue) { this.description = description; + this.minValue = minValue; + this.maxValue = maxValue; } - + + private ESensorItem(String description, double minValue) { + this(description, minValue, Double.POSITIVE_INFINITY); + } + + private ESensorItem(String description) { + this(description, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); + } + public String getDescription() { return description; } + + public boolean inRange(Double value) { + return Double.isFinite(value) && (value >= minValue) && (value <= maxValue); + } } diff --git a/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/SensorData.java b/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/SensorData.java index 222a6a1..059823e 100644 --- a/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/SensorData.java +++ b/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/SensorData.java @@ -11,8 +11,12 @@ public final class SensorData { // start with a simple map containing one Double value per item private final Map items = new LinkedHashMap<>(); - public void addValue(ESensorItem item, Double value) { + public boolean addValue(ESensorItem item, Double value) { + if (!item.inRange(value)) { + return false; + } items.put(item, value); + return true; } public boolean hasValue(ESensorItem item) { diff --git a/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/json/JsonDecoder.java b/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/json/JsonDecoder.java index 5821da6..a17317d 100644 --- a/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/json/JsonDecoder.java +++ b/sensor-data-bridge/src/main/java/nl/bertriksikken/pm/json/JsonDecoder.java @@ -25,7 +25,7 @@ public void parse(JsonNode config, String json, SensorData data) throws JsonProc for (JsonDecoderItem item : jsonDecoderConfig) { JsonNode field = node.at(item.path); double value = field.asDouble(Double.NaN); - if (Double.isFinite(value)) { + if (item.item.inRange(value)) { data.addValue(item.item, value); } } diff --git a/sensor-data-bridge/src/test/java/nl/bertriksikken/pm/SensorDataTest.java b/sensor-data-bridge/src/test/java/nl/bertriksikken/pm/SensorDataTest.java new file mode 100644 index 0000000..4e80d68 --- /dev/null +++ b/sensor-data-bridge/src/test/java/nl/bertriksikken/pm/SensorDataTest.java @@ -0,0 +1,33 @@ +package nl.bertriksikken.pm; + +import org.junit.Assert; +import org.junit.Test; + +public final class SensorDataTest { + + @Test + public void testInRange() { + SensorData sensorData = new SensorData(); + + // value in range + Assert.assertTrue(sensorData.addValue(ESensorItem.HUMI, 100.0)); + Assert.assertTrue(sensorData.hasValue(ESensorItem.HUMI)); + Assert.assertEquals(100.0, sensorData.getValue(ESensorItem.HUMI), 0.1); + + // value out of range + Assert.assertFalse(sensorData.addValue(ESensorItem.PM10, -1.0)); + Assert.assertFalse(sensorData.hasValue(ESensorItem.PM10)); + + // invalid value + Assert.assertFalse(sensorData.addValue(ESensorItem.PM2_5, Double.NaN)); + Assert.assertFalse(sensorData.hasValue(ESensorItem.PM2_5)); + } + + @Test + public void testString() { + SensorData sensorData = new SensorData(); + String s = sensorData.toString(); + Assert.assertEquals("{}", s); + } + +}