mcwl-ai/mcwl-resource/src/main/java/com/mcwl/resource/handler/IMessageHandler.java

38 lines
965 B
Java

package com.mcwl.resource.handler;
import lombok.AllArgsConstructor;
import lombok.Getter;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import java.lang.annotation.*;
import java.util.Collections;
import java.util.List;
public interface IMessageHandler {
void handleMessage(String topic, MqttMessage message);
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@interface Topic {
String value();
int qos() default 1;
}
default List<TopicSubscription> getTopics() {
Topic annotation = this.getClass().getAnnotation(Topic.class);
if (annotation != null) {
return Collections.singletonList(
new TopicSubscription(annotation.value(), annotation.qos())
);
}
return Collections.emptyList();
}
@Getter
@AllArgsConstructor
class TopicSubscription {
private String topicFilter;
private int qos;
}
}