EasyEspReceiver
This commit is contained in:
parent
b784f236df
commit
08e3d71344
29
src/main/java/de/ph87/data/easyEsp/EasyEspInbound.java
Normal file
29
src/main/java/de/ph87/data/easyEsp/EasyEspInbound.java
Normal file
@ -0,0 +1,29 @@
|
||||
package de.ph87.data.easyEsp;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NonNull;
|
||||
import lombok.ToString;
|
||||
|
||||
import java.time.ZonedDateTime;
|
||||
|
||||
import static de.ph87.data.common.DateTimeHelpers.ZDT;
|
||||
|
||||
@Getter
|
||||
@ToString
|
||||
public class EasyEspInbound {
|
||||
|
||||
@NonNull
|
||||
public final ZonedDateTime date;
|
||||
|
||||
public final double value;
|
||||
|
||||
@NonNull
|
||||
public final String unit;
|
||||
|
||||
public EasyEspInbound(final long timestamp, final double value, @NonNull final String unit) {
|
||||
this.date = ZDT(timestamp);
|
||||
this.value = value;
|
||||
this.unit = unit;
|
||||
}
|
||||
|
||||
}
|
||||
49
src/main/java/de/ph87/data/easyEsp/EasyEspReceiver.java
Normal file
49
src/main/java/de/ph87/data/easyEsp/EasyEspReceiver.java
Normal file
@ -0,0 +1,49 @@
|
||||
package de.ph87.data.easyEsp;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import de.ph87.data.mqtt.MqttEvent;
|
||||
import de.ph87.data.series.measure.MeasureEvent;
|
||||
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.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class EasyEspReceiver {
|
||||
|
||||
private static final Pattern REGEX = Pattern.compile("^(?<room>[^/]+)/sensor/(?<property>[^/]+)$");
|
||||
|
||||
private final ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@EventListener(MqttEvent.class)
|
||||
public void onEvent(@NonNull final MqttEvent event) {
|
||||
final Matcher matcher = REGEX.matcher(event.topic);
|
||||
if (!matcher.find()) {
|
||||
return;
|
||||
}
|
||||
|
||||
final EasyEspInbound inbound;
|
||||
try {
|
||||
inbound = objectMapper.readValue(event.payload, EasyEspInbound.class);
|
||||
} catch (JsonProcessingException e) {
|
||||
log.error("Failed to parse EasyEspInbound: error={}, event={}", e, event);
|
||||
return;
|
||||
}
|
||||
|
||||
final String room = matcher.group("room");
|
||||
final String property = matcher.group("property");
|
||||
final String name = "%s.%s".formatted(room, property);
|
||||
applicationEventPublisher.publishEvent(new MeasureEvent(name, inbound.unit, inbound.date, inbound.value));
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user