org.jetlinks.supports.protocol.DefaultMqttMessageCodecDescription Maven / Gradle / Ivy
The newest version!
package org.jetlinks.supports.protocol;
import lombok.Builder;
import lombok.Getter;
import org.jetlinks.core.message.codec.MessagePayloadType;
import org.jetlinks.core.message.codec.MqttMessageCodecDescription;
import reactor.core.publisher.Flux;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DefaultMqttMessageCodecDescription implements MqttMessageCodecDescription {
private List upstream = new ArrayList<>();
private List downstream = new ArrayList<>();
@Override
public Flux getUpStreamTopics() {
return Flux.fromIterable(upstream);
}
@Override
public Flux getDownStreamTopics() {
return Flux.fromIterable(downstream);
}
public DefaultMqttMessageCodecDescription addUpstream(DefaultTopic.Builder builder) {
upstream.add(builder.build());
return this;
}
public DefaultMqttMessageCodecDescription addDownstream(DefaultTopic.Builder builder) {
downstream.add(builder.build());
return this;
}
@Getter
@Builder(builderClassName = "Builder")
public static class DefaultTopic implements Topic {
private String topic;
private List variables;
private MessagePayloadType payloadType;
private String templatePayload;
private String description;
@Override
public MessagePayloadType getPayloadType() {
return payloadType;
}
@Override
public String getTopic() {
return topic;
}
@Override
public String getTopic(Map variables) {
String tp = topic;
for (String variable : this.variables) {
tp = tp.replace("{".concat(variable).concat("}"), String.valueOf(variables.get(variable)));
}
return tp;
}
}
}