sean.zhou
2023-04-25 694b9483c7a551626244cbc222c602ea9ff74094
src/main/java/com/dji/sample/component/mqtt/service/impl/MessageSenderServiceImpl.java
@@ -1,17 +1,19 @@
package com.dji.sample.component.mqtt.service.impl;
import com.dji.sample.component.mqtt.model.Chan;
import com.dji.sample.component.mqtt.model.CommonTopicReceiver;
import com.dji.sample.component.mqtt.model.CommonTopicResponse;
import com.dji.sample.component.mqtt.model.ServiceReply;
import com.dji.sample.component.mqtt.model.*;
import com.dji.sample.component.mqtt.service.IMessageSenderService;
import com.dji.sample.component.mqtt.service.IMqttMessageGateway;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
/**
@@ -30,17 +32,12 @@
    private ObjectMapper mapper;
    public void publish(String topic, CommonTopicResponse response) {
        try {
            log.info("send topic: {}, payload: {}", topic, response.toString());
            messageGateway.publish(topic, mapper.writeValueAsBytes(response));
        } catch (JsonProcessingException e) {
            log.info("Failed to publish the message. {}", response.toString());
            e.printStackTrace();
        }
        this.publish(topic, 1, response);
    }
    public void publish(String topic, int qos, CommonTopicResponse response) {
        try {
            log.info("send topic: {}, payload: {}", topic, response.toString());
            messageGateway.publish(topic, mapper.writeValueAsBytes(response), qos);
        } catch (JsonProcessingException e) {
            log.info("Failed to publish the message. {}", response.toString());
@@ -48,12 +45,11 @@
        }
    }
    public ServiceReply publishWithReply(String topic, CommonTopicResponse response) {
        return this.publishWithReply(ServiceReply.class, topic, response, 2);
    public <T> T publishWithReply(Class<T> clazz, String topic, CommonTopicResponse response) {
        return this.publishWithReply(clazz, topic, response, 2);
    }
    public <T> T publishWithReply(Class<T> clazz, String topic, CommonTopicResponse response, int retryTime) {
        log.info("send topic: {}, payload: {}", topic, response.toString());
        AtomicInteger time = new AtomicInteger(0);
        // Retry three times
        while (time.getAndIncrement() <= retryTime) {
@@ -62,15 +58,59 @@
            Chan<CommonTopicReceiver<T>> chan = Chan.getInstance();
            // If the message is not received in 0.5 seconds then resend it again.
            CommonTopicReceiver<T> receiver = chan.get(response.getTid());
            if (receiver == null) {
                continue;
            }
            // Need to match tid and bid.
            if (receiver.getTid().equals(response.getTid()) &&
            if (Objects.nonNull(receiver) && receiver.getTid().equals(response.getTid()) &&
                    receiver.getBid().equals(response.getBid())) {
                return receiver.getData();
                if (clazz.isAssignableFrom(receiver.getData().getClass())) {
                    return receiver.getData();
                }
                throw new TypeMismatchException(receiver.getData(), clazz);
            }
            // It must be guaranteed that the tid and bid of each message are different.
            response.setBid(UUID.randomUUID().toString());
            response.setTid(UUID.randomUUID().toString());
        }
        throw new RuntimeException("No message reply received.");
    }
    @Override
    public <T> ServiceReply<T> publishServicesTopic(TypeReference<T> clazz, String sn, String method, Object data, String bid) {
        String topic = TopicConst.THING_MODEL_PRE + TopicConst.PRODUCT + sn + TopicConst.SERVICES_SUF;
        ServiceReply reply = this.publishWithReply(ServiceReply.class, topic,
                CommonTopicResponse.builder()
                        .tid(UUID.randomUUID().toString())
                        .bid(StringUtils.hasText(bid) ? bid : UUID.randomUUID().toString())
                        .timestamp(System.currentTimeMillis())
                        .method(method)
                        .data(Objects.requireNonNullElse(data, ""))
                        .build());
        if (Objects.isNull(clazz)) {
            return reply;
        }
        // put together in "output"
        if (Objects.nonNull(reply.getInfo())) {
            reply.setOutput(mapper.convertValue(reply.getInfo(), clazz));
        }
        if (Objects.nonNull(reply.getOutput())) {
            reply.setOutput(mapper.convertValue(reply.getOutput(), clazz));
        }
        return reply;
    }
    @Override
    public ServiceReply publishServicesTopic(String sn, String method, Object data, String bid) {
        return this.publishServicesTopic(null, sn, method, data, bid);
    }
    @Override
    public <T> ServiceReply<T> publishServicesTopic(TypeReference<T> clazz, String sn, String method, Object data) {
        return this.publishServicesTopic(clazz, sn, method, data, null);
    }
    @Override
    public ServiceReply publishServicesTopic(String sn, String method, Object data) {
        return this.publishServicesTopic(null, sn, method, data, null);
    }
}