50 lines
1.5 KiB
Java
50 lines
1.5 KiB
Java
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));
|
|
}
|
|
|
|
}
|