Value + EspHomeHandler + SmartMeterHandler
This commit is contained in:
parent
147bff7927
commit
8f727572fa
@ -1,11 +1,11 @@
|
|||||||
logging.level.de.ph87=DEBUG
|
logging.level.de.ph87.data.series=DEBUG
|
||||||
#-
|
#-
|
||||||
spring.datasource.url=jdbc:h2:./database;AUTO_SERVER=TRUE
|
spring.datasource.url=jdbc:h2:./database;AUTO_SERVER=TRUE
|
||||||
spring.datasource.driverClassName=org.h2.Driver
|
spring.datasource.driverClassName=org.h2.Driver
|
||||||
spring.datasource.username=sa
|
spring.datasource.username=sa
|
||||||
spring.datasource.password=password
|
spring.datasource.password=password
|
||||||
#-
|
#-
|
||||||
#spring.jpa.hibernate.ddl-auto=create
|
spring.jpa.hibernate.ddl-auto=create
|
||||||
#-
|
#-
|
||||||
de.ph87.data.message.receive.mqtt.host=10.0.0.50
|
de.ph87.data.message.receive.mqtt.host=10.0.0.50
|
||||||
de.ph87.data.message.receive.mqtt.topic=#
|
de.ph87.data.message.receive.mqtt.topic=#
|
||||||
|
|||||||
@ -1,4 +1,4 @@
|
|||||||
package de.ph87.data.series;
|
package de.ph87.data;
|
||||||
|
|
||||||
public enum Action {
|
public enum Action {
|
||||||
CREATED, CHANGED, DELETED
|
CREATED, CHANGED, DELETED
|
||||||
@ -1,13 +1,13 @@
|
|||||||
package de.ph87.data;
|
package de.ph87.data;
|
||||||
|
|
||||||
import org.springframework.boot.SpringApplication;
|
import org.springframework.boot.*;
|
||||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
import org.springframework.boot.autoconfigure.*;
|
||||||
|
|
||||||
@SpringBootApplication
|
@SpringBootApplication
|
||||||
public class Backend {
|
public class Backend {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(Backend.class, args);
|
SpringApplication.run(Backend.class, args);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1,9 +1,9 @@
|
|||||||
package de.ph87.data.message;
|
package de.ph87.data.message;
|
||||||
|
|
||||||
import lombok.NonNull;
|
import lombok.*;
|
||||||
|
|
||||||
public interface IMessageHandler {
|
public interface IMessageHandler {
|
||||||
|
|
||||||
void handle(@NonNull final Message message);
|
void handle(@NonNull final Message message) throws Exception;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,26 +1,22 @@
|
|||||||
package de.ph87.data.message;
|
package de.ph87.data.message;
|
||||||
|
|
||||||
import lombok.Getter;
|
import lombok.*;
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
public class Message {
|
public class Message {
|
||||||
|
|
||||||
public final ZonedDateTime date = ZonedDateTime.now();
|
|
||||||
|
|
||||||
public final String topic;
|
public final String topic;
|
||||||
|
|
||||||
|
@ToString.Exclude
|
||||||
public final String payload;
|
public final String payload;
|
||||||
|
|
||||||
public final String payloadLoggable;
|
public final String payloadLoggable;
|
||||||
|
|
||||||
public Message(final String topic, final String payload) {
|
public Message(final String topic, final String payload) {
|
||||||
this.topic = topic;
|
this.topic = topic;
|
||||||
this.payload = payload;
|
this.payload = payload;
|
||||||
this.payloadLoggable = payload.replace("\n", "\\n").replace("\r", "\\r");
|
this.payloadLoggable = payload.replace("\n", "\\n").replace("\r", "\\r");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +1,26 @@
|
|||||||
package de.ph87.data.message;
|
package de.ph87.data.message;
|
||||||
|
|
||||||
import lombok.NonNull;
|
import lombok.*;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.*;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.*;
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class MessageService {
|
public class MessageService {
|
||||||
|
|
||||||
private final List<IMessageHandler> messageHandlers;
|
private final List<IMessageHandler> messageHandlers;
|
||||||
|
|
||||||
public void onMessage(@NonNull final Message message) {
|
public void handle(@NonNull final Message message) {
|
||||||
messageHandlers.forEach(handler -> {
|
messageHandlers.forEach(handler -> {
|
||||||
try {
|
try {
|
||||||
handler.handle(message);
|
handler.handle(message);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.warn("Message handler error: message={}", e.getMessage(), e);
|
log.error("Failed handling message: message={}, error={}", message, e.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,92 @@
|
|||||||
|
package de.ph87.data.message.handler;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.*;
|
||||||
|
import com.fasterxml.jackson.databind.*;
|
||||||
|
import com.fasterxml.jackson.databind.annotation.*;
|
||||||
|
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.*;
|
||||||
|
import java.util.regex.*;
|
||||||
|
import java.util.stream.*;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class EspHomeHandler implements IMessageHandler {
|
||||||
|
|
||||||
|
private static final Pattern REGEX = Pattern.compile("^(?<area>\\w+)/sensor/(?<property>\\w+)$");
|
||||||
|
|
||||||
|
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 area = matcher.group("area");
|
||||||
|
final String property = propertyWorkaround(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 "pressure" -> Unit.PRESSURE_HPA;
|
||||||
|
case "temperature" -> Unit.TEMPERATURE_C;
|
||||||
|
case "humidity_relative" -> Unit.HUMIDITY_RELATIVE_PERCENT;
|
||||||
|
case "humidity_absolute" -> Unit.HUMIDITY_ABSOLUTE_GM3;
|
||||||
|
case "sun" -> Unit.SUN_DC;
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
if (targetUnit == null) {
|
||||||
|
log.debug("Skipping property: {}", name);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
|
||||||
|
final Unit unitFromPayload = inbound.units.stream().filter(ufp -> ufp.base == targetUnit.base).findFirst().orElse(null);
|
||||||
|
if (unitFromPayload == null) {
|
||||||
|
log.error("Unit mismatch: fromTopic={}, fromPayload=[{}]", targetUnit, inbound.getUnits().stream().map(Enum::name).collect(Collectors.joining(",")));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final Value value = new Value(inbound.value, unitFromPayload);
|
||||||
|
seriesService.receive(new SeriesInbound(name, inbound.date, value.as(targetUnit)));
|
||||||
|
}
|
||||||
|
|
||||||
|
private String propertyWorkaround(final String property) {
|
||||||
|
if ("iaq_equivalent_voc".equals(property)) {
|
||||||
|
return "iaq_voc_equivalent";
|
||||||
|
}
|
||||||
|
return property;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
private static class Inbound {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public final ZonedDateTime date;
|
||||||
|
|
||||||
|
public final double value;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@JsonDeserialize(using = UnitListDeserializer.class)
|
||||||
|
public final List<Unit> units;
|
||||||
|
|
||||||
|
public Inbound(final long timestamp, final double value, @JsonProperty("unit") @NonNull final List<Unit> units) {
|
||||||
|
this.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
|
||||||
|
this.value = value;
|
||||||
|
this.units = units;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,58 +1,55 @@
|
|||||||
package de.ph87.data.message.handler;
|
package de.ph87.data.message.handler;
|
||||||
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.databind.*;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import de.ph87.data.message.*;
|
||||||
import de.ph87.data.message.IMessageHandler;
|
import de.ph87.data.series.*;
|
||||||
import de.ph87.data.message.Message;
|
import de.ph87.data.unit.*;
|
||||||
import de.ph87.data.series.SeriesInbound;
|
import lombok.*;
|
||||||
import de.ph87.data.series.SeriesService;
|
import lombok.extern.slf4j.*;
|
||||||
import de.ph87.data.unit.Unit;
|
import org.springframework.stereotype.*;
|
||||||
import lombok.Getter;
|
|
||||||
import lombok.NonNull;
|
|
||||||
import lombok.RequiredArgsConstructor;
|
|
||||||
import lombok.ToString;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import java.time.Instant;
|
import java.time.*;
|
||||||
import java.time.ZoneId;
|
|
||||||
import java.time.ZonedDateTime;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SimpleJsonHandler implements IMessageHandler {
|
public class SimpleJsonHandler implements IMessageHandler {
|
||||||
|
|
||||||
private final ObjectMapper objectMapper;
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
private final SeriesService seriesService;
|
private final SeriesService seriesService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handle(@NonNull final Message message) {
|
public void handle(@NonNull final Message message) throws Exception {
|
||||||
try {
|
if (!message.topic.endsWith("/SimpleJson")) {
|
||||||
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
|
return;
|
||||||
seriesService.receive(new SeriesInbound(message.topic, inbound.date, inbound.value, inbound.unit));
|
|
||||||
} catch (JsonProcessingException e) {
|
|
||||||
log.debug("Failed to parse inbound message: topic={}, message={}, error={}", message.topic, message, e.toString());
|
|
||||||
}
|
}
|
||||||
|
final Inbound inbound = objectMapper.readValue(message.payload, Inbound.class);
|
||||||
|
seriesService.receive(new SeriesInbound(inbound.name, inbound.date, inbound.value, inbound.unit));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
private static class Inbound {
|
private static class Inbound {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public final String name;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
public final ZonedDateTime date;
|
public final ZonedDateTime date;
|
||||||
|
|
||||||
public final double value;
|
public final double value;
|
||||||
|
|
||||||
|
@NonNull
|
||||||
public final Unit unit;
|
public final Unit unit;
|
||||||
|
|
||||||
public Inbound(final long timestamp, final double value, final Unit unit) {
|
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.date = ZonedDateTime.ofInstant(Instant.ofEpochSecond(timestamp), ZoneId.systemDefault());
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.unit = unit;
|
this.unit = unit;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,56 @@
|
|||||||
|
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 SmartMeterHandler implements IMessageHandler {
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
|
||||||
|
private final SeriesService seriesService;
|
||||||
|
|
||||||
|
@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);
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@ToString
|
||||||
|
private static class Inbound {
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
public final ZonedDateTime date;
|
||||||
|
|
||||||
|
public final Value energyPurchased;
|
||||||
|
|
||||||
|
public final Value energyDelivered;
|
||||||
|
|
||||||
|
public final Value powerDifference;
|
||||||
|
|
||||||
|
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);
|
||||||
|
this.energyDelivered = new Value(deliveryWh, Unit.ENERGY_WH);
|
||||||
|
this.powerDifference = new Value(powerW, Unit.POWER_W);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,5 +1,6 @@
|
|||||||
package de.ph87.data.message.receive.mqtt;
|
package de.ph87.data.message.receive.mqtt;
|
||||||
|
|
||||||
|
import de.ph87.data.message.*;
|
||||||
import jakarta.annotation.*;
|
import jakarta.annotation.*;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
import lombok.extern.slf4j.*;
|
import lombok.extern.slf4j.*;
|
||||||
@ -19,6 +20,8 @@ public class MqttReceiver {
|
|||||||
|
|
||||||
private final MqttConfig config;
|
private final MqttConfig config;
|
||||||
|
|
||||||
|
private final MessageService messageService;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private MqttClient client;
|
private MqttClient client;
|
||||||
|
|
||||||
@ -108,8 +111,9 @@ public class MqttReceiver {
|
|||||||
private void _receive(final String topic, final MqttMessage mqttMessage) {
|
private void _receive(final String topic, final MqttMessage mqttMessage) {
|
||||||
Thread.currentThread().setName("MQTT-RECEIVE");
|
Thread.currentThread().setName("MQTT-RECEIVE");
|
||||||
final String payload = new String(mqttMessage.getPayload(), StandardCharsets.UTF_8);
|
final String payload = new String(mqttMessage.getPayload(), StandardCharsets.UTF_8);
|
||||||
final String payloadLoggable = payload.replace("\n", "\\n").replace("\r", "\\r");
|
final Message message = new Message(topic, payload);
|
||||||
log.debug("received: topic={}, message={}", topic, payloadLoggable);
|
log.debug("received: {}", message);
|
||||||
|
messageService.handle(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,34 +1,39 @@
|
|||||||
package de.ph87.data.series;
|
package de.ph87.data.series;
|
||||||
|
|
||||||
import de.ph87.data.unit.Unit;
|
import de.ph87.data.unit.*;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.*;
|
import lombok.*;
|
||||||
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
public class Series {
|
public class Series {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
private String uuid = UUID.randomUUID().toString();
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
@NonNull
|
||||||
|
@Column(nullable = false, unique = true)
|
||||||
|
private String name;
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
@NonNull
|
@NonNull
|
||||||
@Column(nullable = false, unique = true)
|
@Column(nullable = false, unique = true)
|
||||||
private String title;
|
private String title;
|
||||||
|
|
||||||
@Setter
|
@Setter
|
||||||
@NonNull
|
@NonNull
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
@Enumerated(EnumType.STRING)
|
@Enumerated(EnumType.STRING)
|
||||||
private Unit unit;
|
private Unit unit;
|
||||||
|
|
||||||
@Setter
|
public Series(@NonNull final String name, @NonNull final Unit unit) {
|
||||||
@NonNull
|
this.name = name;
|
||||||
@Column(nullable = false, unique = true)
|
this.title = name;
|
||||||
private String source;
|
this.unit = unit;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,27 +1,25 @@
|
|||||||
package de.ph87.data.series;
|
package de.ph87.data.series;
|
||||||
|
|
||||||
import de.ph87.data.unit.Unit;
|
import de.ph87.data.unit.*;
|
||||||
import lombok.Getter;
|
import lombok.*;
|
||||||
import lombok.NonNull;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
public class SeriesDto {
|
public class SeriesDto {
|
||||||
|
|
||||||
public final String uuid;
|
public final long id;
|
||||||
|
|
||||||
|
public final String name;
|
||||||
|
|
||||||
public final String title;
|
public final String title;
|
||||||
|
|
||||||
public final Unit unit;
|
public final Unit unit;
|
||||||
|
|
||||||
public final String source;
|
|
||||||
|
|
||||||
public SeriesDto(@NonNull final Series series) {
|
public SeriesDto(@NonNull final Series series) {
|
||||||
this.uuid = series.getUuid();
|
this.id = series.getId();
|
||||||
|
this.name = series.getName();
|
||||||
this.title = series.getTitle();
|
this.title = series.getTitle();
|
||||||
this.unit = series.getUnit();
|
this.unit = series.getUnit();
|
||||||
this.source = series.getSource();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,32 +1,33 @@
|
|||||||
package de.ph87.data.series;
|
package de.ph87.data.series;
|
||||||
|
|
||||||
import de.ph87.data.unit.Unit;
|
import de.ph87.data.unit.Value;
|
||||||
import lombok.Getter;
|
import de.ph87.data.unit.*;
|
||||||
import lombok.NonNull;
|
import lombok.*;
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.*;
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
public class SeriesInbound {
|
public class SeriesInbound {
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public final String name;
|
public final String name;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
public final ZonedDateTime date;
|
public final ZonedDateTime date;
|
||||||
|
|
||||||
public final double value;
|
public final Value value;
|
||||||
|
|
||||||
@NonNull
|
public SeriesInbound(@NonNull final String name, @NonNull final ZonedDateTime date, final Value value) {
|
||||||
public final Unit unit;
|
|
||||||
|
|
||||||
public SeriesInbound(@NonNull final String name, @NonNull final ZonedDateTime date, final double value, @NonNull final Unit unit) {
|
|
||||||
this.name = name;
|
this.name = name;
|
||||||
this.date = date;
|
this.date = date;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
this.unit = unit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,12 @@
|
|||||||
package de.ph87.data.series;
|
package de.ph87.data.series;
|
||||||
|
|
||||||
import org.springframework.data.repository.ListCrudRepository;
|
import lombok.*;
|
||||||
|
import org.springframework.data.repository.*;
|
||||||
|
|
||||||
public interface SeriesRepository extends ListCrudRepository<Series, String> {
|
import java.util.*;
|
||||||
|
|
||||||
|
public interface SeriesRepository extends ListCrudRepository<Series, Long> {
|
||||||
|
|
||||||
|
Optional<Series> findByName(@NonNull String name);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,81 +1,62 @@
|
|||||||
package de.ph87.data.series;
|
package de.ph87.data.series;
|
||||||
|
|
||||||
import de.ph87.data.series.entry.EntryService;
|
import de.ph87.data.*;
|
||||||
import jakarta.annotation.PostConstruct;
|
import de.ph87.data.series.entry.*;
|
||||||
import lombok.NonNull;
|
import de.ph87.data.unit.*;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.*;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.*;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.*;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.function.*;
|
||||||
import java.util.Map;
|
|
||||||
import java.util.function.Consumer;
|
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@Transactional
|
@Transactional
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class SeriesService {
|
public class SeriesService {
|
||||||
|
|
||||||
private final Map<String, String> sources = new HashMap<>();
|
|
||||||
|
|
||||||
private final SeriesRepository seriesRepository;
|
private final SeriesRepository seriesRepository;
|
||||||
|
|
||||||
private final EntryService entryService;
|
private final EntryService entryService;
|
||||||
|
|
||||||
@PostConstruct
|
public SeriesDto modify(@NonNull final long id, @NonNull final Consumer<Series> modifier) {
|
||||||
public void cacheInit() {
|
final Series series = getById(id);
|
||||||
synchronized (sources) {
|
|
||||||
seriesRepository.findAll().forEach(this::cachePut);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cachePut(@NonNull final Series series) {
|
|
||||||
synchronized (sources) {
|
|
||||||
sources.put(series.getUuid(), series.getSource());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void cacheRemove(final Series series) {
|
|
||||||
synchronized (sources) {
|
|
||||||
sources.remove(series.getUuid());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public SeriesDto modify(@NonNull final String uuid, @NonNull final Consumer<Series> modifier) {
|
|
||||||
final Series series = getByUuid(uuid);
|
|
||||||
modifier.accept(series);
|
modifier.accept(series);
|
||||||
cachePut(series);
|
|
||||||
return publish(series, Action.CHANGED);
|
return publish(series, Action.CHANGED);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void delete(@NonNull final String uuid) {
|
public void delete(@NonNull final long id) {
|
||||||
final Series series = getByUuid(uuid);
|
final Series series = getById(id);
|
||||||
cacheRemove(series);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private Series getByUuid(@NonNull final String uuid) {
|
private Series getById(@NonNull final long id) {
|
||||||
return seriesRepository.findById(uuid).orElseThrow();
|
return seriesRepository.findById(id).orElseThrow();
|
||||||
}
|
}
|
||||||
|
|
||||||
private SeriesDto publish(@NonNull final Series series, @NonNull final Action action) {
|
private SeriesDto publish(@NonNull final Series series, @NonNull final Action action) {
|
||||||
final SeriesDto dto = toDto(series);
|
final SeriesDto dto = toDto(series);
|
||||||
log.info("Series {}: {}", action, series);
|
log.info("Series {}: {}", action, series);
|
||||||
return dto;
|
return dto;
|
||||||
}
|
}
|
||||||
|
|
||||||
private SeriesDto toDto(@NonNull final Series series) {
|
private SeriesDto toDto(@NonNull final Series series) {
|
||||||
return new SeriesDto(series);
|
return new SeriesDto(series);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void receive(@NonNull final SeriesInbound measure) {
|
public void receive(@NonNull final SeriesInbound measure) throws Unit.NotConvertible {
|
||||||
final String uuid = sources.get(measure.name);
|
final Series series = getOrCreate(measure.name, measure.value.unit);
|
||||||
if (uuid == null) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final Series series = getByUuid(uuid);
|
|
||||||
entryService.write(series, measure);
|
entryService.write(series, measure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Series getOrCreate(@NonNull final String name, @NonNull final Unit unit) {
|
||||||
|
return seriesRepository
|
||||||
|
.findByName(name)
|
||||||
|
.orElseGet(() -> {
|
||||||
|
final Series series = seriesRepository.save(new Series(name, unit));
|
||||||
|
publish(series, Action.CREATED);
|
||||||
|
return series;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,40 +1,45 @@
|
|||||||
package de.ph87.data.series.entry;
|
package de.ph87.data.series.entry;
|
||||||
|
|
||||||
import de.ph87.data.series.Series;
|
import de.ph87.data.series.*;
|
||||||
|
import de.ph87.data.unit.Value;
|
||||||
|
import de.ph87.data.unit.*;
|
||||||
import jakarta.persistence.*;
|
import jakarta.persistence.*;
|
||||||
import lombok.Getter;
|
import lombok.*;
|
||||||
import lombok.NoArgsConstructor;
|
|
||||||
import lombok.NonNull;
|
|
||||||
import lombok.ToString;
|
|
||||||
|
|
||||||
import java.time.ZonedDateTime;
|
import java.time.*;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.*;
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Getter
|
@Getter
|
||||||
@ToString
|
@ToString
|
||||||
@NoArgsConstructor
|
@NoArgsConstructor
|
||||||
|
@Table(
|
||||||
|
uniqueConstraints = {
|
||||||
|
@UniqueConstraint(columnNames = {"series_id", "date"})
|
||||||
|
}
|
||||||
|
)
|
||||||
public class Entry {
|
public class Entry {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@ManyToOne
|
@ManyToOne
|
||||||
private Series series;
|
private Series series;
|
||||||
|
|
||||||
@NonNull
|
@NonNull
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
private ZonedDateTime date;
|
private ZonedDateTime date;
|
||||||
|
|
||||||
@Column(nullable = false,name = "`value`")
|
@Setter
|
||||||
|
@Column(nullable = false, name = "`value`")
|
||||||
private double value;
|
private double value;
|
||||||
|
|
||||||
public Entry(@NonNull final Series series, @NonNull final ZonedDateTime date, final double value) {
|
public Entry(@NonNull final Series series, @NonNull final ZonedDateTime date, final Value value) throws Unit.NotConvertible {
|
||||||
this.series = series;
|
this.series = series;
|
||||||
this.date = date.truncatedTo(ChronoUnit.MINUTES);
|
this.date = date.truncatedTo(ChronoUnit.MINUTES);
|
||||||
this.value = value;
|
this.value = value.as(series.getUnit()).value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,7 +1,14 @@
|
|||||||
package de.ph87.data.series.entry;
|
package de.ph87.data.series.entry;
|
||||||
|
|
||||||
import org.springframework.data.repository.ListCrudRepository;
|
import de.ph87.data.series.*;
|
||||||
|
import lombok.*;
|
||||||
|
import org.springframework.data.repository.*;
|
||||||
|
|
||||||
|
import java.time.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
public interface EntryRepository extends ListCrudRepository<Entry, Long> {
|
public interface EntryRepository extends ListCrudRepository<Entry, Long> {
|
||||||
|
|
||||||
|
Optional<Entry> findBySeriesAndDate(@NonNull Series series, @NonNull ZonedDateTime truncated);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,23 +1,29 @@
|
|||||||
package de.ph87.data.series.entry;
|
package de.ph87.data.series.entry;
|
||||||
|
|
||||||
import de.ph87.data.series.Series;
|
import de.ph87.data.series.*;
|
||||||
import de.ph87.data.series.SeriesInbound;
|
import de.ph87.data.unit.*;
|
||||||
import lombok.NonNull;
|
import lombok.*;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.extern.slf4j.*;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.*;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.*;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
|
||||||
|
import java.time.*;
|
||||||
|
import java.time.temporal.*;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@Transactional
|
@Transactional
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class EntryService {
|
public class EntryService {
|
||||||
|
|
||||||
private final EntryRepository entryRepository;
|
private final EntryRepository entryRepository;
|
||||||
|
|
||||||
public void write(@NonNull final Series series, @NonNull final SeriesInbound measure) {
|
public void write(@NonNull final Series series, @NonNull final SeriesInbound measure) throws Unit.NotConvertible {
|
||||||
// TODO
|
final ZonedDateTime truncated = measure.date.truncatedTo(ChronoUnit.MINUTES);
|
||||||
|
if (entryRepository.findBySeriesAndDate(series, truncated).isEmpty()) {
|
||||||
|
final Entry created = entryRepository.save(new Entry(series, truncated, measure.value));
|
||||||
|
log.debug("Created: {}", created);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
package de.ph87.data.unit;
|
package de.ph87.data.unit;
|
||||||
|
|
||||||
import lombok.NonNull;
|
import lombok.*;
|
||||||
|
|
||||||
public enum Unit {
|
public enum Unit {
|
||||||
TEMPERATURE_C("°C"),
|
TEMPERATURE_C("°C"),
|
||||||
@ -15,24 +15,36 @@ public enum Unit {
|
|||||||
POWER_KW("kW", 1000, POWER_W),
|
POWER_KW("kW", 1000, POWER_W),
|
||||||
ENERGY_WH("W"),
|
ENERGY_WH("W"),
|
||||||
ENERGY_KWH("kWh", 1000, ENERGY_WH),
|
ENERGY_KWH("kWh", 1000, ENERGY_WH),
|
||||||
|
IAQ("IAQ"),
|
||||||
|
IAQ_CO2_EQUIVALENT("ppm"),
|
||||||
|
IAQ_VOC_EQUIVALENT("ppm"),
|
||||||
|
SUN_DC("Δ°C"),
|
||||||
;
|
;
|
||||||
|
|
||||||
public final String unit;
|
public final String unit;
|
||||||
|
|
||||||
public final double factor;
|
public final double factor;
|
||||||
|
|
||||||
public final Unit base;
|
public final Unit base;
|
||||||
|
|
||||||
Unit(@NonNull final String unit) {
|
Unit(@NonNull final String unit) {
|
||||||
this.unit = unit;
|
this.unit = unit;
|
||||||
this.factor = 1.0;
|
this.factor = 1.0;
|
||||||
this.base = this;
|
this.base = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
Unit(@NonNull final String unit, double factor, @NonNull Unit base) {
|
Unit(@NonNull final String unit, final double factor, @NonNull final Unit base) {
|
||||||
this.unit = unit;
|
this.unit = unit;
|
||||||
this.factor = factor;
|
this.factor = factor;
|
||||||
this.base = base;
|
this.base = base;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class NotConvertible extends Exception {
|
||||||
|
|
||||||
|
public NotConvertible(final @NonNull Unit source, final Unit target) {
|
||||||
|
super("Cannot convert Units: source=%s, target=%s".formatted(source, target));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
17
src/main/java/de/ph87/data/unit/UnitListDeserializer.java
Normal file
17
src/main/java/de/ph87/data/unit/UnitListDeserializer.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package de.ph87.data.unit;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.*;
|
||||||
|
import com.fasterxml.jackson.databind.*;
|
||||||
|
|
||||||
|
import java.io.*;
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class UnitListDeserializer extends JsonDeserializer<List<Unit>> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<Unit> deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException {
|
||||||
|
final String name = jsonParser.getValueAsString();
|
||||||
|
return Arrays.stream(Unit.values()).filter(unit -> unit.unit.equals(name)).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
23
src/main/java/de/ph87/data/unit/Value.java
Normal file
23
src/main/java/de/ph87/data/unit/Value.java
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
package de.ph87.data.unit;
|
||||||
|
|
||||||
|
import lombok.*;
|
||||||
|
|
||||||
|
public class Value {
|
||||||
|
|
||||||
|
public final double value;
|
||||||
|
|
||||||
|
public final Unit unit;
|
||||||
|
|
||||||
|
public Value(final double value, @NonNull final Unit unit) {
|
||||||
|
this.value = value;
|
||||||
|
this.unit = unit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Value as(@NonNull final Unit target) throws Unit.NotConvertible {
|
||||||
|
if (this.unit.base != target.base) {
|
||||||
|
throw new Unit.NotConvertible(this.unit, target);
|
||||||
|
}
|
||||||
|
return new Value(value * this.unit.factor / target.factor, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user