package com.usky.alarm.service.listener;

import com.usky.alarm.service.config.mqtt.MqttInConfig;
import com.usky.alarm.service.enums.TopListener;
import com.usky.alarm.service.mqtt.SimpleContext;
import com.usky.alarm.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.MessageHandler;
import org.springframework.stereotype.Component;

/**
 * @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 -> {
            String payload = message.getPayload().toString();
            //进行接口推送
            String[] code1 = TopListener.FIRE_ALERT.getCode().split("/");
            String[] code2 = TopListener.WATER_ALERT.getCode().split("/");
            String[] code3 = TopListener.RTU_ALERT.getCode().split("/");
            String[] code4 = TopListener.VIDEO_ALERT.getCode().split("/");
            String[] code5 = TopListener.SMOKE_ALERT.getCode().split("/");
            String[] code6 = TopListener.ELECTRICAL_ALERT.getCode().split("/");
            String[] code7 = TopListener.LIQUID_ALERT.getCode().split("/");
            String[] code8 = TopListener.LR_SMOKE_ALERT.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(code1[2]) != -1 && topic.indexOf(code1[5]) != -1) ||
                        (topic.indexOf(code2[2]) != -1 && topic.indexOf(code2[5]) != -1) ||
                        (topic.indexOf(code3[2]) != -1 && topic.indexOf(code3[5]) != -1) ||
                        (topic.indexOf(code4[2]) != -1 && topic.indexOf(code4[5]) != -1) ||
                        (topic.indexOf(code5[2]) != -1 && topic.indexOf(code5[5]) != -1) ||
                        (topic.indexOf(code6[2]) != -1 && topic.indexOf(code6[5]) != -1) ||
                        (topic.indexOf(code7[2]) != -1 && topic.indexOf(code7[5]) != -1) ||
                        (topic.indexOf(code8[2]) != -1 && topic.indexOf(code8[5]) != -1)) {
                    mqttBaseVO.setDescribe("alarm");
                }else {
                    mqttBaseVO.setDescribe("fireInfoAndAlarm");
                }
                mqttBaseVO.setData(payload);
                //统一处理数据
                simpleContext.getResource(mqttBaseVO);
            }
        };
    }
}
