diff --git a/application.properties b/application.properties index 78c51d9..f75e180 100644 --- a/application.properties +++ b/application.properties @@ -1,4 +1,4 @@ -logging.level.de.ph87.data.series=DEBUG +#logging.level.de.ph87.data.series=DEBUG #- spring.datasource.url=jdbc:h2:./database;AUTO_SERVER=TRUE spring.datasource.driverClassName=org.h2.Driver diff --git a/src/main/java/de/ph87/data/message/handler/EspHomeHandler.java b/src/main/java/de/ph87/data/message/handler/EspHomeHandler.java index 7f7a271..fd4ef4f 100644 --- a/src/main/java/de/ph87/data/message/handler/EspHomeHandler.java +++ b/src/main/java/de/ph87/data/message/handler/EspHomeHandler.java @@ -34,12 +34,12 @@ public class EspHomeHandler implements IMessageHandler { return; } final String area = matcher.group("area"); - final String property = propertyWorkaround(matcher.group("property")); + final String property = propertyReplace(matcher.group("property")); final String name = "%s/%s".formatted(area, property.replace("_", "/")); final Unit targetUnit = switch (property) { case "iaq" -> Unit.IAQ; - case "iaq_co2_equivalent" -> Unit.IAQ_CO2_EQUIVALENT; - case "iaq_voc_equivalent" -> Unit.IAQ_VOC_EQUIVALENT; + case "iaq_co2" -> Unit.IAQ_CO2_EQUIVALENT; + case "iaq_voc" -> Unit.IAQ_VOC_EQUIVALENT; case "pressure" -> Unit.PRESSURE_HPA; case "temperature" -> Unit.TEMPERATURE_C; case "humidity_relative" -> Unit.HUMIDITY_RELATIVE_PERCENT; @@ -61,9 +61,12 @@ public class EspHomeHandler implements IMessageHandler { seriesService.receive(new SeriesInbound(name, inbound.date, value.as(targetUnit))); } - private String propertyWorkaround(final String property) { + private String propertyReplace(final String property) { + if ("iaq_co2_equivalent".equals(property)) { + return "iaq_co2"; + } if ("iaq_equivalent_voc".equals(property)) { - return "iaq_voc_equivalent"; + return "iaq_voc"; } return property; } diff --git a/src/main/java/de/ph87/data/message/handler/HeizungHandler.java b/src/main/java/de/ph87/data/message/handler/HeizungHandler.java new file mode 100644 index 0000000..4e4ddda --- /dev/null +++ b/src/main/java/de/ph87/data/message/handler/HeizungHandler.java @@ -0,0 +1,53 @@ +package de.ph87.data.message.handler; + +import com.fasterxml.jackson.databind.*; +import de.ph87.data.message.*; +import de.ph87.data.series.*; +import de.ph87.data.unit.Value; +import de.ph87.data.unit.*; +import lombok.*; +import lombok.extern.slf4j.*; +import org.springframework.stereotype.*; + +import java.time.*; +import java.util.regex.*; + +@Slf4j +@Service +@RequiredArgsConstructor +public class HeizungHandler implements IMessageHandler { + + private static final Pattern REGEX = Pattern.compile("^aggregation/(?heizung/.+)/temperatur$"); + + private final ObjectMapper objectMapper; + + private final SeriesService seriesService; + + @Override + public void handle(@NonNull final Message message) throws Exception { + final Matcher matcher = REGEX.matcher(message.topic); + if (!matcher.find()) { + return; + } + final String property = matcher.group("property"); + final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class); + seriesService.receive(new SeriesInbound(property, inbound.date, inbound.value)); + } + + @Getter + @ToString + private static class Inbound { + + @NonNull + public final ZonedDateTime date; + + public final Value value; + + public Inbound(final long timestamp, final double sum, final int count) { + this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); + this.value = new Value(sum / count, Unit.TEMPERATURE_C); + } + + } + +} diff --git a/src/main/java/de/ph87/data/message/handler/OpenDTUHandler.java b/src/main/java/de/ph87/data/message/handler/OpenDTUHandler.java new file mode 100644 index 0000000..0371016 --- /dev/null +++ b/src/main/java/de/ph87/data/message/handler/OpenDTUHandler.java @@ -0,0 +1,53 @@ +package de.ph87.data.message.handler; + +import com.fasterxml.jackson.databind.*; +import de.ph87.data.message.*; +import de.ph87.data.series.*; +import de.ph87.data.unit.Value; +import de.ph87.data.unit.*; +import lombok.*; +import lombok.extern.slf4j.*; +import org.springframework.stereotype.*; + +import java.time.*; + +@Slf4j +@Service +@RequiredArgsConstructor +public class OpenDTUHandler implements IMessageHandler { + + private final ObjectMapper objectMapper; + + private final SeriesService seriesService; + + @Override + public void handle(final @NonNull Message message) throws Exception { + if (!"openDTU/pv/patrix/json".equals(message.topic)) { + return; + } + final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class); + seriesService.receive(new SeriesInbound("electricity/energy/produced", inbound.date, inbound.energy.as(Unit.ENERGY_KWH))); + seriesService.receive(new SeriesInbound("electricity/power/produced", inbound.date, inbound.power.as(Unit.POWER_W))); + } + + + @Getter + @ToString + private static class Inbound { + + @NonNull + public final ZonedDateTime date; + + public final Value energy; + + public final Value power; + + public Inbound(final long timestamp, final double energyProducedKWh, final double powerW) { + this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); + this.energy = new Value(energyProducedKWh, Unit.ENERGY_KWH); + this.power = new Value(powerW, Unit.POWER_W); + } + + } + +} diff --git a/src/main/java/de/ph87/data/message/handler/SimpleJsonHandler.java b/src/main/java/de/ph87/data/message/handler/SimpleJsonHandler.java index 700540f..5c43be1 100644 --- a/src/main/java/de/ph87/data/message/handler/SimpleJsonHandler.java +++ b/src/main/java/de/ph87/data/message/handler/SimpleJsonHandler.java @@ -3,6 +3,7 @@ package de.ph87.data.message.handler; import com.fasterxml.jackson.databind.*; import de.ph87.data.message.*; import de.ph87.data.series.*; +import de.ph87.data.unit.Value; import de.ph87.data.unit.*; import lombok.*; import lombok.extern.slf4j.*; @@ -25,7 +26,7 @@ public class SimpleJsonHandler implements IMessageHandler { return; } final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class); - seriesService.receive(new SeriesInbound(inbound.name, inbound.date, inbound.value, inbound.unit)); + seriesService.receive(new SeriesInbound(inbound.name, inbound.date, inbound.value)); } @Getter @@ -38,16 +39,12 @@ public class SimpleJsonHandler implements IMessageHandler { @NonNull public final ZonedDateTime date; - public final double value; - - @NonNull - public final Unit unit; + public final Value value; public Inbound(@NonNull final String name, final long timestamp, final double value, @NonNull final Unit unit) { this.name = name; this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault()); - this.value = value; - this.unit = unit; + this.value = new Value(value, unit); } } diff --git a/src/main/java/de/ph87/data/message/handler/SmartMeterHandler.java b/src/main/java/de/ph87/data/message/handler/SmartMeterHandler.java index 635b9ed..bdeebe7 100644 --- a/src/main/java/de/ph87/data/message/handler/SmartMeterHandler.java +++ b/src/main/java/de/ph87/data/message/handler/SmartMeterHandler.java @@ -26,9 +26,9 @@ public class SmartMeterHandler implements IMessageHandler { return; } final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class); - seriesService.receive(new SeriesInbound("energy/purchased", inbound.date, inbound.energyPurchased)); - seriesService.receive(new SeriesInbound("energy/delivered", inbound.date, inbound.energyDelivered)); - seriesService.receive(new SeriesInbound("power/difference", inbound.date, inbound.powerDifference)); + seriesService.receive(new SeriesInbound("electricity/energy/purchased", inbound.date, inbound.energyPurchased.as(Unit.ENERGY_KWH))); + seriesService.receive(new SeriesInbound("electricity/energy/delivered", inbound.date, inbound.energyDelivered.as(Unit.ENERGY_KWH))); + seriesService.receive(new SeriesInbound("electricity/power/difference", inbound.date, inbound.powerDifference.as(Unit.POWER_W))); } @Getter diff --git a/src/main/java/de/ph87/data/series/SeriesInbound.java b/src/main/java/de/ph87/data/series/SeriesInbound.java index a5b4649..75f5992 100644 --- a/src/main/java/de/ph87/data/series/SeriesInbound.java +++ b/src/main/java/de/ph87/data/series/SeriesInbound.java @@ -1,7 +1,6 @@ package de.ph87.data.series; import de.ph87.data.unit.Value; -import de.ph87.data.unit.*; import lombok.*; import java.time.*; @@ -24,10 +23,4 @@ public class SeriesInbound { this.value = value; } - public SeriesInbound(@NonNull final String name, @NonNull final ZonedDateTime date, final double value, @NonNull final Unit unit) { - this.name = name; - this.date = date; - this.value = new Value(value, unit); - } - } diff --git a/src/main/java/de/ph87/data/unit/Value.java b/src/main/java/de/ph87/data/unit/Value.java index 6ae55c3..4492e9f 100644 --- a/src/main/java/de/ph87/data/unit/Value.java +++ b/src/main/java/de/ph87/data/unit/Value.java @@ -14,6 +14,9 @@ public class Value { } 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); }