Skip to content

Commit

Permalink
Add unit to ESensorItem.
Browse files Browse the repository at this point in the history
  • Loading branch information
bertrik committed Dec 9, 2023
1 parent f9c23ee commit 7923f54
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,62 +1,70 @@
package nl.bertriksikken.pm;

import java.util.Locale;

public enum ESensorItem {
PM10("Particulate matter PM10 (aka P1)", "ug/m3", 0),
PM2_5("Particulate matter PM2.5 (aka P2)", "ug/m3", 0),
PM4_0("Particulate matter PM4 (aka P4)", "ug/m3", 0),
PM1_0("Particulate matter PM1.0 (aka P0)", "ug/m3", 0),

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", 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),
PM10_N("Particulate matter PM10", "#/cm3", 0),
PM4_0_N("Particulate matter PM4", "#/cm3", 0),
PM2_5_N("Particulate matter PM2.5", "#/cm3", 0),
PM1_0_N("Particulate matter PM1.0", "#/cm3", 0),
PM0_5_N("Particulate matter PM0.5", "#/cm3", 0),

PM_TPS("Typical particle size, in um"),
PM_TPS("Typical particle size", "um"),

HUMIDITY("Relative humidity, in percent", 0, 100),
TEMPERATURE("Temperature, in degrees Celcius", -100, 100),
PRESSURE("Atmospheric pressure, in Pa", 0, 1E6),
HUMIDITY("Relative humidity", "%", 0, 100),
TEMPERATURE("Temperature", "degC", -100, 100),
PRESSURE("Atmospheric pressure", "Pa", 0, 1E6),

POS_LAT("Latitude in degrees", -90, 90),
POS_LON("Longitude in degrees", -180, 180),
POS_ALT("Altitude in meters"),
POS_LAT("Latitude", "deg", -90, 90),
POS_LON("Longitude", "deg", -180, 180),
POS_ALT("Altitude", "m"),

NOISE_LA_EQ("Noise avg (dBA)", 0, 200),
NOISE_LA_MIN("Noise min (dBA)", 0, 200),
NOISE_LA_MAX("Noise max (dBA)", 0, 200),
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?)", 0),
RADIATION("Radiation (unit?)", 0),
NO2("NO2 concentration", "?", 0),
RADIATION("Radiation", "?", 0),

LORA_SF("Spreading factor", 6, 12),
LORA_SNR("Signal-to-noise ratio"),
LORA_RSSI("Signal strength, in dBm");
LORA_SF("Spreading factor", "", 6, 12),
LORA_SNR("Signal-to-noise ratio", "dB"),
LORA_RSSI("Signal strength", "dBm");

private String description;
private String unit;
private double minValue;
private double maxValue;
private String description;

private ESensorItem(String description, double minValue, double maxValue) {
private ESensorItem(String description, String unit, double minValue, double maxValue) {
this.description = description;
this.unit = unit;
this.minValue = minValue;
this.maxValue = maxValue;
}

private ESensorItem(String description, double minValue) {
this(description, minValue, Double.POSITIVE_INFINITY);
private ESensorItem(String description, String unit, double minValue) {
this(description, unit, minValue, Double.POSITIVE_INFINITY);
}

private ESensorItem(String description) {
this(description, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
private ESensorItem(String description, String unit) {
this(description, unit, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
}

public String getDescription() {
return description;
@Override
public String toString() {
return String.format(Locale.ROOT, "%s (%s)", description, unit);
}

public boolean inRange(Double value) {
return Double.isFinite(value) && (value >= minValue) && (value <= maxValue);
}

public String format(Number value) {
return String.format(Locale.ROOT, "%s=%s%s", name(), value, unit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.stream.Collectors;

/**
* Collection of measurement items.
Expand Down Expand Up @@ -33,7 +34,8 @@ public Number get(ESensorItem item) {

@Override
public String toString() {
return items.toString();
return items.entrySet().stream().map(entry -> entry.getKey().format(entry.getValue()))
.collect(Collectors.joining(",", "{", "}"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ public void testString() {
SensorData sensorData = new SensorData();
String s = sensorData.toString();
Assert.assertEquals("{}", s);

// value in range
sensorData.addValue(ESensorItem.PM2_5, 2.5);
sensorData.addValue(ESensorItem.PM10, 10.0);
sensorData.addValue(ESensorItem.HUMIDITY, 12.3);
Assert.assertEquals("{PM2_5=2.5ug/m3,PM10=10.0ug/m3,HUMIDITY=12.3%}", sensorData.toString());
}

}

0 comments on commit 7923f54

Please sign in to comment.