code clean

This commit is contained in:
Patrick Haßel 2025-02-24 11:45:52 +01:00
parent 908134600c
commit e005f1ef8c
23 changed files with 256 additions and 268 deletions

View File

@ -6,8 +6,7 @@ import com.fasterxml.jackson.databind.annotation.*;
import de.ph87.data.message.*; import de.ph87.data.message.*;
import de.ph87.data.series.*; import de.ph87.data.series.*;
import de.ph87.data.series.entry.*; import de.ph87.data.series.entry.*;
import de.ph87.data.unit.Value; import de.ph87.data.value.Value;
import de.ph87.data.unit.*;
import lombok.*; import lombok.*;
import lombok.extern.slf4j.*; import lombok.extern.slf4j.*;
import org.springframework.stereotype.*; import org.springframework.stereotype.*;
@ -37,15 +36,15 @@ public class EspHomeHandler implements IMessageHandler {
final String area = matcher.group("area"); final String area = matcher.group("area");
final String property = propertyReplace(matcher.group("property")); final String property = propertyReplace(matcher.group("property"));
final String name = "%s/%s".formatted(area, property.replace("_", "/")); final String name = "%s/%s".formatted(area, property.replace("_", "/"));
final Unit targetUnit = switch (property) { final Value.Unit targetUnit = switch (property) {
case "iaq" -> Unit.IAQ; case "iaq" -> Value.Unit.IAQ;
case "iaq_co2" -> Unit.IAQ_CO2_EQUIVALENT; case "iaq_co2" -> Value.Unit.IAQ_CO2_EQUIVALENT;
case "iaq_voc" -> Unit.IAQ_VOC_EQUIVALENT; case "iaq_voc" -> Value.Unit.IAQ_VOC_EQUIVALENT;
case "pressure" -> Unit.PRESSURE_HPA; case "pressure" -> Value.Unit.PRESSURE_HPA;
case "temperature" -> Unit.TEMPERATURE_C; case "temperature" -> Value.Unit.TEMPERATURE_C;
case "humidity_relative" -> Unit.HUMIDITY_RELATIVE_PERCENT; case "humidity_relative" -> Value.Unit.HUMIDITY_RELATIVE_PERCENT;
case "humidity_absolute" -> Unit.HUMIDITY_ABSOLUTE_GM3; case "humidity_absolute" -> Value.Unit.HUMIDITY_ABSOLUTE_GM3;
case "sun" -> Unit.SUN_DC; case "sun" -> Value.Unit.SUN_DC;
default -> null; default -> null;
}; };
if (targetUnit == null) { if (targetUnit == null) {
@ -53,7 +52,7 @@ public class EspHomeHandler implements IMessageHandler {
return; return;
} }
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class); final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
final Unit unitFromPayload = inbound.units.stream().filter(ufp -> ufp.base == targetUnit.base).findFirst().orElse(null); final Value.Unit unitFromPayload = inbound.units.stream().filter(ufp -> ufp.base == targetUnit.base).findFirst().orElse(null);
if (unitFromPayload == null) { if (unitFromPayload == null) {
log.error("Unit mismatch: fromTopic={}, fromPayload=[{}]", targetUnit, inbound.getUnits().stream().map(Enum::name).collect(Collectors.joining(","))); log.error("Unit mismatch: fromTopic={}, fromPayload=[{}]", targetUnit, inbound.getUnits().stream().map(Enum::name).collect(Collectors.joining(",")));
return; return;
@ -82,10 +81,10 @@ public class EspHomeHandler implements IMessageHandler {
public final double value; public final double value;
@NonNull @NonNull
@JsonDeserialize(using = UnitListDeserializer.class) @JsonDeserialize(using = Value.Unit.ListDeserializer.class)
public final List<Unit> units; public final List<Value.Unit> units;
public Inbound(final long timestamp, final double value, @JsonProperty("unit") @NonNull final List<Unit> units) { public Inbound(final long timestamp, final double value, @JsonProperty("unit") @NonNull final List<Value.Unit> units) {
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
this.value = value; this.value = value;
this.units = units; this.units = units;

View File

@ -4,8 +4,7 @@ import com.fasterxml.jackson.databind.*;
import de.ph87.data.message.*; import de.ph87.data.message.*;
import de.ph87.data.series.*; import de.ph87.data.series.*;
import de.ph87.data.series.entry.*; import de.ph87.data.series.entry.*;
import de.ph87.data.unit.Value; import de.ph87.data.value.Value;
import de.ph87.data.unit.*;
import lombok.*; import lombok.*;
import lombok.extern.slf4j.*; import lombok.extern.slf4j.*;
import org.springframework.stereotype.*; import org.springframework.stereotype.*;
@ -46,7 +45,7 @@ public class HeizungHandler implements IMessageHandler {
public Inbound(final long timestamp, final double sum, final int count) { public Inbound(final long timestamp, final double sum, final int count) {
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
this.value = new Value(sum / count, Unit.TEMPERATURE_C); this.value = new Value(sum / count, Value.Unit.TEMPERATURE_C);
} }
} }

View File

@ -4,8 +4,7 @@ import com.fasterxml.jackson.databind.*;
import de.ph87.data.message.*; import de.ph87.data.message.*;
import de.ph87.data.series.*; import de.ph87.data.series.*;
import de.ph87.data.series.entry.*; import de.ph87.data.series.entry.*;
import de.ph87.data.unit.Value; import de.ph87.data.value.Value;
import de.ph87.data.unit.*;
import lombok.*; import lombok.*;
import lombok.extern.slf4j.*; import lombok.extern.slf4j.*;
import org.springframework.stereotype.*; import org.springframework.stereotype.*;
@ -27,8 +26,8 @@ public class OpenDTUHandler implements IMessageHandler {
return; return;
} }
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class); final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
entryService.receive(new SeriesInbound("electricity/energy/produced", inbound.date, inbound.energy.as(Unit.ENERGY_KWH))); entryService.receive(new SeriesInbound("electricity/energy/produced", inbound.date, inbound.energy.as(Value.Unit.ENERGY_KWH)));
entryService.receive(new SeriesInbound("electricity/power/produced", inbound.date, inbound.power.as(Unit.POWER_W))); entryService.receive(new SeriesInbound("electricity/power/produced", inbound.date, inbound.power.as(Value.Unit.POWER_W)));
} }
@Getter @Getter
@ -44,8 +43,8 @@ public class OpenDTUHandler implements IMessageHandler {
public Inbound(final long timestamp, final double energyProducedKWh, final double powerW) { public Inbound(final long timestamp, final double energyProducedKWh, final double powerW) {
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
this.energy = new Value(energyProducedKWh, Unit.ENERGY_KWH); this.energy = new Value(energyProducedKWh, Value.Unit.ENERGY_KWH);
this.power = new Value(powerW, Unit.POWER_W); this.power = new Value(powerW, Value.Unit.POWER_W);
} }
} }

View File

@ -4,8 +4,7 @@ import com.fasterxml.jackson.databind.*;
import de.ph87.data.message.*; import de.ph87.data.message.*;
import de.ph87.data.series.*; import de.ph87.data.series.*;
import de.ph87.data.series.entry.*; import de.ph87.data.series.entry.*;
import de.ph87.data.unit.Value; import de.ph87.data.value.Value;
import de.ph87.data.unit.*;
import lombok.*; import lombok.*;
import lombok.extern.slf4j.*; import lombok.extern.slf4j.*;
import org.springframework.stereotype.*; import org.springframework.stereotype.*;
@ -42,7 +41,7 @@ public class SimpleJsonHandler implements IMessageHandler {
public final Value value; public final Value value;
public Inbound(@NonNull final String name, final long timestamp, final double value, @NonNull final Unit unit) { public Inbound(@NonNull final String name, final long timestamp, final double value, @NonNull final Value.Unit unit) {
this.name = name; this.name = name;
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
this.value = new Value(value, unit); this.value = new Value(value, unit);

View File

@ -4,8 +4,7 @@ import com.fasterxml.jackson.databind.*;
import de.ph87.data.message.*; import de.ph87.data.message.*;
import de.ph87.data.series.*; import de.ph87.data.series.*;
import de.ph87.data.series.entry.*; import de.ph87.data.series.entry.*;
import de.ph87.data.unit.Value; import de.ph87.data.value.Value;
import de.ph87.data.unit.*;
import lombok.*; import lombok.*;
import lombok.extern.slf4j.*; import lombok.extern.slf4j.*;
import org.springframework.stereotype.*; import org.springframework.stereotype.*;
@ -27,9 +26,9 @@ public class SmartMeterHandler implements IMessageHandler {
return; return;
} }
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class); final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
entryService.receive(new SeriesInbound("electricity/energy/purchased", inbound.date, inbound.energyPurchased.as(Unit.ENERGY_KWH))); entryService.receive(new SeriesInbound("electricity/energy/purchased", inbound.date, inbound.energyPurchased.as(Value.Unit.ENERGY_KWH)));
entryService.receive(new SeriesInbound("electricity/energy/delivered", inbound.date, inbound.energyDelivered.as(Unit.ENERGY_KWH))); entryService.receive(new SeriesInbound("electricity/energy/delivered", inbound.date, inbound.energyDelivered.as(Value.Unit.ENERGY_KWH)));
entryService.receive(new SeriesInbound("electricity/power/difference", inbound.date, inbound.powerDifference.as(Unit.POWER_W))); entryService.receive(new SeriesInbound("electricity/power/difference", inbound.date, inbound.powerDifference.as(Value.Unit.POWER_W)));
} }
@Getter @Getter
@ -47,9 +46,9 @@ public class SmartMeterHandler implements IMessageHandler {
public Inbound(final long timestamp, final double purchaseWh, final double deliveryWh, final double powerW) { public Inbound(final long timestamp, final double purchaseWh, final double deliveryWh, final double powerW) {
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
this.energyPurchased = new Value(purchaseWh, Unit.ENERGY_WH); this.energyPurchased = new Value(purchaseWh, Value.Unit.ENERGY_WH);
this.energyDelivered = new Value(deliveryWh, Unit.ENERGY_WH); this.energyDelivered = new Value(deliveryWh, Value.Unit.ENERGY_WH);
this.powerDifference = new Value(powerW, Unit.POWER_W); this.powerDifference = new Value(powerW, Value.Unit.POWER_W);
} }
} }

View File

@ -1,6 +1,6 @@
package de.ph87.data.series; package de.ph87.data.series;
import de.ph87.data.unit.*; import de.ph87.data.value.Value;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
@ -28,13 +28,13 @@ public class Series {
@NonNull @NonNull
@Column(nullable = false) @Column(nullable = false)
@Enumerated(EnumType.STRING) @Enumerated(EnumType.STRING)
private Unit unit; private Value.Unit unit;
@Setter @Setter
@Column(nullable = false) @Column(nullable = false)
private int decimals = 1; private int decimals = 1;
public Series(@NonNull final String name, @NonNull final Unit unit) { public Series(@NonNull final String name, @NonNull final Value.Unit unit) {
this.name = name; this.name = name;
this.title = name; this.title = name;
this.unit = unit; this.unit = unit;

View File

@ -1,6 +1,6 @@
package de.ph87.data.series; package de.ph87.data.series;
import de.ph87.data.unit.*; import de.ph87.data.value.Value;
import jakarta.annotation.*; import jakarta.annotation.*;
import lombok.*; import lombok.*;
@ -14,7 +14,7 @@ public class SeriesDto {
public final String title; public final String title;
public final Unit unit; public final Value.Unit unit;
public final int decimals; public final int decimals;

View File

@ -1,6 +1,6 @@
package de.ph87.data.series; package de.ph87.data.series;
import de.ph87.data.unit.Value; import de.ph87.data.value.Value;
import lombok.*; import lombok.*;
import java.time.*; import java.time.*;

View File

@ -1,7 +1,7 @@
package de.ph87.data.series; package de.ph87.data.series;
import de.ph87.data.*; import de.ph87.data.*;
import de.ph87.data.unit.*; import de.ph87.data.value.Value;
import lombok.*; import lombok.*;
import lombok.extern.slf4j.*; import lombok.extern.slf4j.*;
import org.springframework.stereotype.*; import org.springframework.stereotype.*;
@ -49,7 +49,7 @@ public class SeriesService {
return new SeriesDto(series); return new SeriesDto(series);
} }
public Series getOrCreate(@NonNull final String name, @NonNull final Unit unit) { public Series getOrCreate(@NonNull final String name, @NonNull final Value.Unit unit) {
return seriesRepository return seriesRepository
.findByName(name) .findByName(name)
.orElseGet(() -> { .orElseGet(() -> {

View File

@ -1,8 +1,7 @@
package de.ph87.data.series.entry; package de.ph87.data.series.entry;
import de.ph87.data.series.*; import de.ph87.data.series.*;
import de.ph87.data.unit.Value; import de.ph87.data.value.Value;
import de.ph87.data.unit.*;
import jakarta.persistence.*; import jakarta.persistence.*;
import lombok.*; import lombok.*;
@ -36,7 +35,7 @@ public class Entry {
@Column(nullable = false, name = "`value`") @Column(nullable = false, name = "`value`")
private double value; private double value;
public Entry(@NonNull final Series series, @NonNull final ZonedDateTime date, final Value value) throws Unit.NotConvertible { public Entry(@NonNull final Series series, @NonNull final ZonedDateTime date, final Value value) throws Value.Unit.NotConvertible {
this.series = series; this.series = series;
this.date = date.truncatedTo(ChronoUnit.MINUTES); this.date = date.truncatedTo(ChronoUnit.MINUTES);
this.value = value.as(series.getUnit()).value; this.value = value.as(series.getUnit()).value;

View File

@ -1,7 +1,7 @@
package de.ph87.data.series.entry; package de.ph87.data.series.entry;
import de.ph87.data.series.*; import de.ph87.data.series.*;
import de.ph87.data.unit.*; import de.ph87.data.value.Value;
import lombok.*; import lombok.*;
import lombok.extern.slf4j.*; import lombok.extern.slf4j.*;
import org.springframework.stereotype.*; import org.springframework.stereotype.*;
@ -20,7 +20,7 @@ public class EntryService {
private final SeriesService seriesService; private final SeriesService seriesService;
public void write(@NonNull final Series series, @NonNull final SeriesInbound measure) throws Unit.NotConvertible { public void write(@NonNull final Series series, @NonNull final SeriesInbound measure) throws Value.Unit.NotConvertible {
final ZonedDateTime truncated = measure.date.truncatedTo(ChronoUnit.MINUTES).minusMinutes(measure.date.getMinute() % 5); final ZonedDateTime truncated = measure.date.truncatedTo(ChronoUnit.MINUTES).minusMinutes(measure.date.getMinute() % 5);
if (entryRepository.findBySeriesAndDate(series, truncated).isEmpty()) { if (entryRepository.findBySeriesAndDate(series, truncated).isEmpty()) {
final Entry created = entryRepository.save(new Entry(series, truncated, measure.value)); final Entry created = entryRepository.save(new Entry(series, truncated, measure.value));
@ -28,7 +28,7 @@ public class EntryService {
} }
} }
public void receive(@NonNull final SeriesInbound measure) throws Unit.NotConvertible { public void receive(@NonNull final SeriesInbound measure) throws Value.Unit.NotConvertible {
final Series series = seriesService.getOrCreate(measure.name, measure.value.unit); final Series series = seriesService.getOrCreate(measure.name, measure.value.unit);
write(series, measure); write(series, measure);
} }

View File

@ -1,51 +0,0 @@
package de.ph87.data.unit;
import lombok.*;
public enum Unit {
TEMPERATURE_C("°C"),
PRESSURE_HPA("hPa"),
HUMIDITY_RELATIVE_PERCENT("%"),
HUMIDITY_ABSOLUTE_MGL("mg/L"),
HUMIDITY_ABSOLUTE_GM3("g/m³", 1, HUMIDITY_ABSOLUTE_MGL),
ILLUMINANCE_LUX("lux"),
RESISTANCE_OHMS("Ω"),
ALTITUDE_M("m"),
POWER_W("W"),
POWER_KW("kW", 1000, POWER_W),
ENERGY_WH("W"),
ENERGY_KWH("kWh", 1000, ENERGY_WH),
IAQ("IAQ"),
IAQ_CO2_EQUIVALENT("ppm"),
IAQ_VOC_EQUIVALENT("ppm"),
SUN_DC("Δ°C"),
UNIT_PERCENT("%"),
;
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 Exception {
public NotConvertible(final @NonNull Unit source, final Unit target) {
super("Cannot convert Units: source=%s, target=%s".formatted(source, target));
}
}
}

View File

@ -1,17 +0,0 @@
package de.ph87.data.unit;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
import java.util.*;
public class UnitListDeserializer 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(Unit.values()).filter(unit -> unit.unit.equals(name)).toList();
}
}

View File

@ -1,26 +0,0 @@
package de.ph87.data.unit;
import lombok.*;
public class Value {
public final double value;
public final Unit unit;
public Value(final double value, @NonNull final Unit unit) {
this.value = value;
this.unit = unit;
}
public Value as(@NonNull final Unit target) throws Unit.NotConvertible {
if (this.unit == target) {
return this;
}
if (this.unit.base != target.base) {
throw new Unit.NotConvertible(this.unit, target);
}
return new Value(value * this.unit.factor / target.factor, target);
}
}

View File

@ -0,0 +1,88 @@
package de.ph87.data.value;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import lombok.*;
import java.io.*;
import java.util.*;
public class Value {
public final double value;
public final Unit unit;
public Value(final double value, @NonNull final Unit unit) {
this.value = value;
this.unit = unit;
}
public Value as(@NonNull final Unit target) throws Unit.NotConvertible {
if (this.unit == target) {
return this;
}
if (this.unit.base != target.base) {
throw new Unit.NotConvertible(this.unit, target);
}
return new Value(value * this.unit.factor / target.factor, target);
}
public enum Unit {
TEMPERATURE_C("°C"),
PRESSURE_HPA("hPa"),
HUMIDITY_RELATIVE_PERCENT("%"),
HUMIDITY_ABSOLUTE_MGL("mg/L"),
HUMIDITY_ABSOLUTE_GM3("g/m³", 1, HUMIDITY_ABSOLUTE_MGL),
ILLUMINANCE_LUX("lux"),
RESISTANCE_OHMS("Ω"),
ALTITUDE_M("m"),
POWER_W("W"),
POWER_KW("kW", 1000, POWER_W),
ENERGY_WH("W"),
ENERGY_KWH("kWh", 1000, ENERGY_WH),
IAQ("IAQ"),
IAQ_CO2_EQUIVALENT("ppm"),
IAQ_VOC_EQUIVALENT("ppm"),
SUN_DC("Δ°C"),
UNIT_PERCENT("%"),
;
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 Value.Unit base) {
this.unit = unit;
this.factor = factor;
this.base = base;
}
public static class NotConvertible extends Exception {
public NotConvertible(final @NonNull Value.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();
}
}
}
}