OpenDTUHandler

This commit is contained in:
Patrick Haßel 2025-02-15 20:53:52 +01:00
parent 8f727572fa
commit 551db4b93d
8 changed files with 125 additions and 23 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -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/(?<property>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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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);
}