86 lines
1.8 KiB
Java
86 lines
1.8 KiB
Java
package de.ph87.data.value;
|
|
|
|
import com.fasterxml.jackson.core.*;
|
|
import com.fasterxml.jackson.databind.*;
|
|
import lombok.*;
|
|
|
|
import java.io.*;
|
|
import java.util.*;
|
|
|
|
public enum Unit {
|
|
TEMPERATURE_C("°C"),
|
|
|
|
PRESSURE_PA("Pa"),
|
|
PRESSURE_HPA("hPa", 100, PRESSURE_PA),
|
|
|
|
HUMIDITY_RELATIVE_PERCENT("%"),
|
|
|
|
HUMIDITY_ABSOLUTE_GM3("g/m³"),
|
|
HUMIDITY_ABSOLUTE_MGL("mg/L", 1, HUMIDITY_ABSOLUTE_GM3),
|
|
|
|
ILLUMINANCE_LUX("lux"),
|
|
|
|
RESISTANCE_OHMS("Ω"),
|
|
|
|
ALTITUDE_M("m"),
|
|
ALTITUDE_KM("km", 1000, ALTITUDE_M),
|
|
|
|
POWER_W("W"),
|
|
POWER_KW("kW", 1000, POWER_W),
|
|
|
|
ENERGY_WH("Wh"),
|
|
ENERGY_KWH("kWh", 1000, ENERGY_WH),
|
|
|
|
IAQ("IAQ"),
|
|
IAQ_CO2_EQUIVALENT("ppm"),
|
|
IAQ_VOC_EQUIVALENT("ppm"),
|
|
|
|
SUN_DC("Δ°C"),
|
|
|
|
UNIT_PERCENT("%"),
|
|
|
|
CLOUD_COVER_PERCENT("%"),
|
|
|
|
IRRADIATION_WH_M2("Wh/m²"),
|
|
IRRADIATION_KWH_M2("kWh/m²", 1000, IRRADIATION_WH_M2),
|
|
|
|
PRECIPITATION_MM("mm"),
|
|
;
|
|
|
|
public final String unit;
|
|
|
|
public final double factor;
|
|
|
|
public final Unit base;
|
|
|
|
Unit(@NonNull final String unit) {
|
|
this.unit = unit;
|
|
this.factor = 1.0;
|
|
this.base = this;
|
|
}
|
|
|
|
Unit(@NonNull final String unit, final double factor, @NonNull final Unit base) {
|
|
this.unit = unit;
|
|
this.factor = factor;
|
|
this.base = base;
|
|
}
|
|
|
|
public static class NotConvertible extends RuntimeException {
|
|
|
|
public NotConvertible(@NonNull final Unit source, final Unit target) {
|
|
super("Cannot convert Units: source=%s, target=%s".formatted(source, target));
|
|
}
|
|
|
|
}
|
|
|
|
public static class ListDeserializer extends JsonDeserializer<List<Unit>> {
|
|
|
|
@Override
|
|
public List<Unit> deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
|
|
final String name = jsonParser.getValueAsString();
|
|
return Arrays.stream(values()).filter(unit -> unit.unit.equals(name)).toList();
|
|
}
|
|
|
|
}
|
|
}
|