Skip to content

Commit

Permalink
Add basic sanity check for range of measurement items.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Oct 15, 2023
1 parent 293148a commit 7c2b0f6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ public final class SensorData {
// start with a simple map containing one Double value per item
private final Map<ESensorItem, Double> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}

0 comments on commit 7c2b0f6

Please sign in to comment.