package com.usky.transfer.service.listener;


import com.usky.transfer.service.config.mqtt.MqttInConfig;
import com.usky.transfer.service.enums.TopListener;
import com.usky.transfer.service.mqtt.SimpleContext;
import com.usky.transfer.service.vo.MqttBaseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.integration.annotation.ServiceActivator;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHandler;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @author yq
 * @date 2021/11/3 8:13
 */
@ConditionalOnProperty(prefix = "mqtt", value = {"enabled"}, havingValue = "true")
@Slf4j
@Component
public class MqttListener {

    public static final String MESSAGE_NAME = "messageInput";

    @Autowired
    private SimpleContext simpleContext;

    /**
     * 处理消息-消费者
     *
     * @return
     */
    @Bean(MESSAGE_NAME)
    @ServiceActivator(inputChannel = MqttInConfig.CHANNEL_NAME_INPUT)
    public MessageHandler handler() {
        return message -> {
            if (message.getPayload() == null || message.getPayload().toString().isEmpty()) {
                System.out.println("Received empty message, skipping processing.");
                return;
            }
            String payload = message.getPayload().toString();
            //进行接口推送
//            String[] infoCode = TopListener.DEVICE_INFO.getCode().split("/");
//            String[] controlCode = TopListener.DEVICE_CONTROLRESPONSE.getCode().split("/");
            Object mqttReceivedTopic = message.getHeaders().get("mqtt_receivedTopic");
            if (null != mqttReceivedTopic) {
                String topic = mqttReceivedTopic.toString();
                MqttBaseVO mqttBaseVO = new MqttBaseVO();
                mqttBaseVO.setTopic(topic);
                if (topic.indexOf("info") != -1 ) {
                    mqttBaseVO.setDescribe("info");
                    mqttBaseVO.setData(payload);
                }else if(topic.indexOf("controlResponse") != -1 ) {
                    mqttBaseVO.setDescribe("controlResponse");
                    mqttBaseVO.setData(payload);
                }
                else if(topic.indexOf("add") != -1){
                    mqttBaseVO.setDescribe("add");
                    mqttBaseVO.setData(payload);
                }
                else if(topic.indexOf("data-collector") != -1){
                    mqttBaseVO.setDescribe("dataCollector");
                    mqttBaseVO.setData(payload);
                }
                else if(topic.indexOf("alarm") != -1){
                    mqttBaseVO.setDescribe("alarm");
                    mqttBaseVO.setData(payload);
                }
                //统一处理数据
                simpleContext.getResource(mqttBaseVO);
            }
        };
    }
}
