Series.unit

This commit is contained in:
Patrick Haßel 2024-10-16 13:32:21 +02:00
parent 0667357bf2
commit 0926e949fa
12 changed files with 46 additions and 16 deletions

View File

@ -6,7 +6,7 @@ import static de.ph87.data.heating.HeatingConstants.*;
public class AggregationConstants {
public static final Map<String, String> HEATING_TOPIC_MAP = Map.of(
public static final Map<String, String> HEATING_SERIES_MAP = Map.of(
"aggregation/heizung/abgas/temperatur", HEATING_EXHAUST_TEMPERATURE_SERIES_NAME,
"aggregation/heizung/puffer/vorlauf/temperatur", HEATING_BUFFER_SUPPLY_TEMPERATURE_SERIES_NAME,
"aggregation/heizung/puffer/ruecklauf/temperatur", HEATING_BUFFER_RETURN_TEMPERATURE_SERIES_NAME,
@ -18,4 +18,18 @@ public class AggregationConstants {
"aggregation/heizung/heizkreis/ruecklauf/temperatur", HEATING_LOOP_RETURN_TEMPERATURE_SERIES_NAME
);
public static final String TEMPERATURE_UNIT = "°C";
public static final Map<String, String> HEATING_UNIT_MAP = Map.of(
"aggregation/heizung/abgas/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/puffer/vorlauf/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/puffer/ruecklauf/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/puffer/eingang/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/puffer/speicher/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/puffer/ausgang/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/puffer/zirkulation/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/heizkreis/vorlauf/temperatur", TEMPERATURE_UNIT,
"aggregation/heizung/heizkreis/ruecklauf/temperatur", TEMPERATURE_UNIT
);
}

View File

@ -11,7 +11,8 @@ import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Service;
import static de.ph87.data.aggregation.AggregationConstants.HEATING_TOPIC_MAP;
import static de.ph87.data.aggregation.AggregationConstants.HEATING_SERIES_MAP;
import static de.ph87.data.aggregation.AggregationConstants.HEATING_UNIT_MAP;
@Slf4j
@Service
@ -24,7 +25,8 @@ public class AggregationReceiver {
@EventListener(MqttEvent.class)
public void onEvent(@NonNull final MqttEvent event) {
final String seriesName = HEATING_TOPIC_MAP.get(event.topic);
final String seriesName = HEATING_SERIES_MAP.get(event.topic);
final String unit = HEATING_UNIT_MAP.get(event.topic);
if (seriesName == null) {
return;
}
@ -37,7 +39,7 @@ public class AggregationReceiver {
return;
}
applicationEventPublisher.publishEvent(new MeasureEvent(seriesName, aggregation.lastTime, aggregation.lastValue));
applicationEventPublisher.publishEvent(new MeasureEvent(seriesName, unit, aggregation.lastTime, aggregation.lastValue));
}
}

View File

@ -18,9 +18,9 @@ import static de.ph87.data.electricity.ElectricityHelpers.energyToKWh;
@RequiredArgsConstructor
public class GridReceiver {
public static final String GRID_PURCHASE_SERIES_NAME = "grid.purchaseKWh";
public static final String GRID_PURCHASE_SERIES_NAME = "electricity.grid.purchase";
public static final String GRID_DELIVERY_SERIES_NAME = "grid.deliveryKWh";
public static final String GRID_DELIVERY_SERIES_NAME = "electricity.grid.delivery";
private final ApplicationEventPublisher applicationEventPublisher;
@ -41,10 +41,10 @@ public class GridReceiver {
}
final double purchaseKWh = energyToKWh(inbound.purchaseWh, "Wh");
applicationEventPublisher.publishEvent(new ConsumptionEvent(GRID_PURCHASE_SERIES_NAME, true, inbound.meter, inbound.date, purchaseKWh));
applicationEventPublisher.publishEvent(new ConsumptionEvent(GRID_PURCHASE_SERIES_NAME, "kWh", true, inbound.meter, inbound.date, purchaseKWh));
final double deliveryKWh = energyToKWh(inbound.deliveryWh, "Wh");
applicationEventPublisher.publishEvent(new ConsumptionEvent(GRID_DELIVERY_SERIES_NAME, true, inbound.meter, inbound.date, deliveryKWh));
applicationEventPublisher.publishEvent(new ConsumptionEvent(GRID_DELIVERY_SERIES_NAME, "kWh", true, inbound.meter, inbound.date, deliveryKWh));
}
}

View File

@ -21,7 +21,7 @@ import static de.ph87.data.electricity.ElectricityHelpers.energyToKWh;
@RequiredArgsConstructor
public class PhotovoltaicReceiver {
public static final String PHOTOVOLTAIC_ENERGY_SERIES_NAME = "photovoltaic.energyKWh";
public static final String PHOTOVOLTAIC_ENERGY_SERIES_NAME = "electricity.photovoltaic.energy";
private static final Pattern REGEX = Pattern.compile("^(?<serial>\\S+) (?<epochSeconds>\\d+) (?<powerValue>\\d+(:?\\.\\d+)?)(?<powerUnit>\\S+) (?<producedValue>\\d+(:?\\.\\d+)?)(?<producedUnit>\\S+)$");
@ -43,7 +43,7 @@ public class PhotovoltaicReceiver {
final double producedValue = Double.parseDouble(matcher.group("producedValue"));
final String producedUnit = matcher.group("producedUnit");
final double producedKWh = energyToKWh(producedValue, producedUnit);
applicationEventPublisher.publishEvent(new ConsumptionEvent(PHOTOVOLTAIC_ENERGY_SERIES_NAME, true, serial, date, producedKWh));
applicationEventPublisher.publishEvent(new ConsumptionEvent(PHOTOVOLTAIC_ENERGY_SERIES_NAME, "kWh", true, serial, date, producedKWh));
}
}

View File

@ -22,6 +22,10 @@ public class Series {
@Column(nullable = false, unique = true)
private String name;
@NonNull
@Column(nullable = false)
private String unit;
@NonNull
@Enumerated(EnumType.STRING)
private SeriesMode mode;
@ -36,9 +40,10 @@ public class Series {
@ElementCollection
private Set<String> aliases = new HashSet<>();
public Series(@NonNull final String name, @NonNull final SeriesMode mode) {
public Series(@NonNull final String name, @NonNull final SeriesMode mode, @NonNull final String unit) {
this.name = name;
this.mode = mode;
this.unit = unit;
}
}

View File

@ -15,8 +15,8 @@ public class SeriesService {
private final SeriesRepository seriesRepository;
@NonNull
public Series getOrCreateByName(@NonNull final String name, @NonNull final SeriesMode mode) {
final Series series = seriesRepository.findByNameOrAliasesContains(name, name).orElseGet(() -> seriesRepository.save(new Series(name, mode)));
public Series getOrCreateByName(@NonNull final String name, @NonNull final SeriesMode mode, @NonNull final String unit) {
final Series series = seriesRepository.findByNameOrAliasesContains(name, name).orElseGet(() -> seriesRepository.save(new Series(name, mode, unit)));
if (series.getMode() != mode) {
throw new RuntimeException("'mode' argument for getOrCreateByName does not match 'mode' of existing Series: mode=%s, series=%s".formatted(mode, series));
}

View File

@ -15,6 +15,9 @@ public class ConsumptionEvent {
@NonNull
private final String name;
@NonNull
private final String unit;
private final boolean increasing;
@NonNull

View File

@ -31,7 +31,7 @@ public class ConsumptionService {
@EventListener(ConsumptionEvent.class)
public void onConsumptionEvent(@NonNull final ConsumptionEvent event) {
log.debug("Handling ConsumptionEvent: {}", event);
final Series series = seriesService.getOrCreateByName(event.getName(), event.isIncreasing() ? SeriesMode.INCREASING : SeriesMode.DECREASING);
final Series series = seriesService.getOrCreateByName(event.getName(), event.isIncreasing() ? SeriesMode.INCREASING : SeriesMode.DECREASING, event.getUnit());
final Period period = periodService.getOrCreatePeriod(series, event);
period.setLastDate(event.getDate());
period.setLastValue(event.getValue());

View File

@ -15,6 +15,9 @@ public class CounterEvent {
@NonNull
private final String name;
@NonNull
private final String unit;
@NonNull
private final ZonedDateTime date;

View File

@ -24,7 +24,7 @@ public class CounterService {
@EventListener(CounterEvent.class)
public void onCounterEvent(@NonNull final CounterEvent event) {
final Series series = seriesService.getOrCreateByName(event.getName(), SeriesMode.COUNTER);
final Series series = seriesService.getOrCreateByName(event.getName(), SeriesMode.COUNTER, event.getUnit());
for (final Unit unit : Unit.values()) {
final SeriesIntervalKey id = new SeriesIntervalKey(series, unit, event.getDate());
counterRepository.findById(id)

View File

@ -15,6 +15,9 @@ public class MeasureEvent {
@NonNull
private final String name;
@NonNull
private final String unit;
@NonNull
private final ZonedDateTime date;

View File

@ -24,7 +24,7 @@ public class MeasureService {
@EventListener(MeasureEvent.class)
public void onMeasureEvent(@NonNull final MeasureEvent event) {
final Series series = seriesService.getOrCreateByName(event.getName(), SeriesMode.MEASURE);
final Series series = seriesService.getOrCreateByName(event.getName(), SeriesMode.MEASURE, event.getUnit());
for (final Unit unit : Unit.values()) {
final SeriesIntervalKey id = new SeriesIntervalKey(series, unit, event.getDate());
measureRepository.findById(id)