50 lines
1.8 KiB
Java
50 lines
1.8 KiB
Java
package de.ph87.data.electricity.photovoltaic;
|
|
|
|
import de.ph87.data.mqtt.MqttEvent;
|
|
import de.ph87.data.series.consumption.ConsumptionEvent;
|
|
import lombok.NonNull;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.context.ApplicationEventPublisher;
|
|
import org.springframework.context.event.EventListener;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.time.ZonedDateTime;
|
|
import java.util.regex.Matcher;
|
|
import java.util.regex.Pattern;
|
|
|
|
import static de.ph87.data.common.DateTimeHelpers.ZDT;
|
|
import static de.ph87.data.electricity.ElectricityHelpers.energyToKWh;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class PhotovoltaicReceiver {
|
|
|
|
public static final String PHOTOVOLTAIC_ENERGY_SERIES_NAME = "photovoltaic.energyKWh";
|
|
|
|
private static final Pattern REGEX = Pattern.compile("^(?<serial>\\S+) (?<epochSeconds>\\d+) (?<power>\\d+(:?\\.\\d+)?)(?<powerUnit>\\S+) (?<energy>\\d+(:?\\.\\d+)?)(?<energyUnit>\\S+)$");
|
|
|
|
private final ApplicationEventPublisher applicationEventPublisher;
|
|
|
|
@EventListener(MqttEvent.class)
|
|
public void onEvent(@NonNull final MqttEvent event) {
|
|
if (!event.topic.equals("OpenDtuFetcher/total")) {
|
|
return;
|
|
}
|
|
final Matcher matcher = REGEX.matcher(event.payload);
|
|
if (!matcher.find()) {
|
|
log.error("Failed to match OpenDtuFetcher payload: {}", event.payloadLoggable);
|
|
return;
|
|
}
|
|
|
|
final String serial = matcher.group("serial");
|
|
final ZonedDateTime date = ZDT(Long.parseLong(matcher.group("epochSeconds")));
|
|
final double energy = Double.parseDouble(matcher.group("energy"));
|
|
final String energyUnit = matcher.group("energyUnit");
|
|
final double energyKWh = energyToKWh(energy, energyUnit);
|
|
applicationEventPublisher.publishEvent(new ConsumptionEvent(PHOTOVOLTAIC_ENERGY_SERIES_NAME, serial, date, energyKWh));
|
|
}
|
|
|
|
}
|