package com.foreverwin.mesnac.equip.consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

import javax.jms.Queue;


/**
 * 设备交互MQ监听类
 *
 *
 * @author Leon
 * @date 2021-07-21
 */
@Component
@ConditionalOnProperty(prefix = "spring.activemq", value = {"enabled"}, matchIfMissing = true)
public class ResourceMQConsume {

    private static Logger logger = LoggerFactory.getLogger(ResourceMQConsume.class);

    @Autowired
    private ResourceMQProcess resourceMQProcess;

    @Autowired(required = false)
    @Qualifier("defaultJmsTemplate")
    private JmsMessagingTemplate template;

    @JmsListener(destination = "resource.status.process", containerFactory = "defaultFactory")
    public void resourceStatusProcess(String text, MessageHeaders headers) {
        logger.info("接收设备状态变更数据：" + text);

        resourceMQProcess.resourceStatusProcess(text);
        //返回设备处理结果
//        template.convertAndSend((Queue) headers.get("jms_replyTo"), resultString);
    }

    @JmsListener(destination = "resource.alarm.process", containerFactory = "defaultFactory")
    public void resourceAlarmProcess(String text, MessageHeaders headers) {
        logger.info("接收设备报警数据：" + text);
        resourceMQProcess.resourceFaultProcess(text);
//        template.convertAndSend((Queue) headers.get("jms_replyTo"), resultString);
    }

    @JmsListener(destination = "resource.parameter.process", containerFactory = "defaultFactory", concurrency = "5")
    public void resourceEdcProcess(String text) {
        logger.info("接收设备参数数据：" + text);
        resourceMQProcess.resourceParamProcess(text);
    }

    @JmsListener(destination = "resource.runtime.process", containerFactory = "defaultFactory", concurrency = "5")
    public void resourceRunTimeProcess(String text) {
        logger.info("接收设备每日运行时长：" + text);
        resourceMQProcess.resourceRunTimeProcess(text);
    }
}
