62 lines
1.8 KiB
Java
62 lines
1.8 KiB
Java
package de.ph87.data.message.handler;
|
|
|
|
import com.fasterxml.jackson.databind.*;
|
|
import de.ph87.data.message.*;
|
|
import de.ph87.data.meter.*;
|
|
import de.ph87.data.series.entry.*;
|
|
import de.ph87.data.value.Value;
|
|
import de.ph87.data.value.*;
|
|
import lombok.*;
|
|
import lombok.extern.slf4j.*;
|
|
import org.springframework.stereotype.*;
|
|
|
|
import java.time.*;
|
|
|
|
@Slf4j
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class SmartMeterHandler implements IMessageHandler {
|
|
|
|
private static final String METER_NUMBER = "1ZPA0020300305";
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
private final EntryService entryService;
|
|
|
|
private final MeterService meterService;
|
|
|
|
@Override
|
|
public void handle(@NonNull final Message message) throws Exception {
|
|
if (!"electricity/grid/json".equals(message.topic)) {
|
|
return;
|
|
}
|
|
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
|
|
entryService.receive("power/balance", inbound.date, inbound.powerBalance.as(Unit.POWER_W));
|
|
meterService.receive("energy/purchased", METER_NUMBER, inbound.date, inbound.energyPurchased);
|
|
meterService.receive("energy/delivered", METER_NUMBER, inbound.date, inbound.energyDelivered);
|
|
}
|
|
|
|
@Getter
|
|
@ToString
|
|
public static class Inbound {
|
|
|
|
@NonNull
|
|
public final ZonedDateTime date;
|
|
|
|
public final Value energyPurchased;
|
|
|
|
public final Value energyDelivered;
|
|
|
|
public final Value powerBalance;
|
|
|
|
public Inbound(final long timestamp, final double purchaseWh, final double deliveryWh, final double powerW) {
|
|
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
|
|
this.energyPurchased = new Value(purchaseWh, Unit.ENERGY_WH).as(Unit.ENERGY_KWH);
|
|
this.energyDelivered = new Value(deliveryWh, Unit.ENERGY_WH).as(Unit.ENERGY_KWH);
|
|
this.powerBalance = new Value(powerW, Unit.POWER_W);
|
|
}
|
|
|
|
}
|
|
|
|
}
|