Data/src/main/java/de/ph87/data/message/handler/SimpleJsonHandler.java
2025-02-24 11:47:20 +01:00

53 lines
1.3 KiB
Java

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.series.entry.*;
import de.ph87.data.value.Value;
import lombok.*;
import lombok.extern.slf4j.*;
import org.springframework.stereotype.*;
import java.time.*;
@Slf4j
@Service
@RequiredArgsConstructor
public class SimpleJsonHandler implements IMessageHandler {
private final ObjectMapper objectMapper;
private final EntryService entryService;
@Override
public void handle(@NonNull final Message message) throws Exception {
if (!message.topic.endsWith("/SimpleJson")) {
return;
}
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
entryService.receive(new SeriesInbound(inbound.name, inbound.date, inbound.value));
}
@Getter
@ToString
private static class Inbound {
@NonNull
public final String name;
@NonNull
public final ZonedDateTime date;
public final Value value;
public Inbound(@NonNull final String name, final long timestamp, final double value, @NonNull final Value.Unit unit) {
this.name = name;
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
this.value = new Value(value, unit);
}
}
}