shenyijian
2023-12-14 0346ed9f9760bfd136ec01bf14df7fe95f59a603
src/main/java/com/dji/sample/component/mqtt/handler/InboundMessageRouter.java
@@ -6,6 +6,7 @@
import com.dji.sample.component.rabbitmq.config.MqttMsgProxyProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.annotation.Router;
import org.springframework.integration.mqtt.support.MqttHeaders;
import org.springframework.integration.router.AbstractMessageRouter;
@@ -29,6 +30,9 @@
    @Autowired
    private MqttMsgProxyProducer mqttMsgProxyProducer;
    @Value("${spring.rabbitmq.is-open}")
    private Boolean isOpen;
    /**
     * All mqtt broker messages will arrive here before distributing them to different channels.
     * @param message message from mqtt broker
@@ -41,13 +45,15 @@
        String topic = headers.get(MqttHeaders.RECEIVED_TOPIC).toString();
        byte[] payload = (byte[])message.getPayload();
        log.debug("received topic :{} \t payload :{}", topic, new String(payload));
//        log.debug("received topic :{} \t payload :{}", topic, new String(payload));
        DeviceTopicEnum topicEnum = DeviceTopicEnum.find(topic);
        MessageChannel bean = (MessageChannel) SpringBeanUtils.getBean(topicEnum.getBeanName());
        String msg = new String(payload);
        try {
            mqttMsgProxyProducer.publish(topic.replace("/","."), msg);
            if (isOpen){
                mqttMsgProxyProducer.publish(topic.replace("/","."), msg);
            }
        } catch (Exception e) {
            log.error("消息发送失败:", e);
        }