沙钢智慧能源系统后端代码
liriming
2 天以前 cf56ebbecf2c8fc695e95c4d84949182260ee082
shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java
@@ -1,12 +1,30 @@
package com.iailab.module.shasteel.mq.consumer;
import com.alibaba.fastjson.JSONObject;
import com.iailab.framework.common.util.date.DateUtils;
import com.iailab.module.model.api.mcs.McsApi;
import com.iailab.module.model.api.mcs.dto.AlarmConfigRespDTO;
import com.iailab.module.model.api.mcs.dto.AlarmMessageRespDTO;
import com.iailab.module.model.api.mcs.dto.PreDataJsonReqVO;
import com.iailab.module.shasteel.mq.common.constant.RoutingConstant;
import com.iailab.module.shasteel.mq.config.QueuePredictFinishConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 监听预测完成
 *
 * @author PanZhibao
 * @Description
 * @createTime 2024年12月11日
@@ -15,16 +33,84 @@
@Component
public class ModelPredictFinishConsumer {
    @Resource
    private McsApi mcsApi;
    @Resource
    private RabbitTemplate rabbitTemplate;
    /**
     * 监听预测完成,产生预警消息
     *
     * @param message
     */
    @RabbitListener(queues = QueuePredictFinishConfig.QUEUE_NAME)
    public void listen(Message message) {
        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
        log.info("routingKey:" + routingKey);
        String messageBody = new String(message.getBody());
        log.info("messageBody:" + messageBody);
        try {
            String routingKey = message.getMessageProperties().getReceivedRoutingKey();
            log.info("routingKey:" + routingKey);
            String messageBody = new String(message.getBody());
            log.info("messageBody:" + messageBody);
            JSONObject messageJson = JSONObject.parseObject(messageBody);
            if (CollectionUtils.isEmpty(messageJson)) {
                return;
            }
            List<AlarmConfigRespDTO> configList = mcsApi.listAlarmConfig(new HashMap<String, Object>());
            if (CollectionUtils.isEmpty(configList)) {
                return;
            }
            List<String> OutputIdList = new ArrayList<>();
            configList.forEach(item -> {
                OutputIdList.add(item.getOutId());
            });
            configList.forEach(item -> {
                PreDataJsonReqVO reqVO = new PreDataJsonReqVO();
                reqVO.setPredictTime(DateUtils.parse(messageJson.get("predictTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
                reqVO.setOutputIdList(OutputIdList);
                Map<String, List<Object[]>> preData = mcsApi.getPreDataCur(reqVO);
                if (CollectionUtils.isEmpty(preData)) {
                    return;
                }
                List<Object[]> result = preData.get(item.getOutId());
                int toIndex = result.size();
                if (toIndex <= 0) {
                    return;
                }
                int fromIndex = result.size() - item.getCompLength();
                List<Object[]> predictList = result.subList(fromIndex, toIndex);
                for (Object[] data : predictList) {
                    BigDecimal dataValue = new BigDecimal(Double.parseDouble(data[1].toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
                    if (!(dataValue.compareTo(item.getLowerLimit()) >= 0 && dataValue.compareTo(item.getUpperLimit()) <= 0)) {
                        AlarmMessageRespDTO alarmMessage = new AlarmMessageRespDTO();
                        alarmMessage.setConfigId(item.getId());
                        if (dataValue.compareTo(item.getLowerLimit()) < 0) {
                            alarmMessage.setAlarmType("1");//超下限
                        } else if (dataValue.compareTo(item.getUpperLimit()) > 0) {
                            alarmMessage.setAlarmType("2");//超上限
                        }
                        alarmMessage.setAlarmTime(DateUtils.parse(data[0].toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
                        StringBuffer sb = new StringBuffer();
                        sb.append(data[0]);
                        sb.append(" ");
                        sb.append(item.getAlarmObj());
                        if ("1".equals(alarmMessage.getAlarmType())) {
                            sb.append("超下限");
                        } else if ("2".equals(alarmMessage.getAlarmType())) {
                            sb.append("超上限");
                        }
                        sb.append(dataValue);
                        sb.append(item.getUnit());
                        alarmMessage.setContent(sb.toString());
                        System.out.println("预警消息=" + alarmMessage);
                        rabbitTemplate.convertAndSend(RoutingConstant.EXCHANGE, RoutingConstant.Iailab_Model_Alarm, alarmMessage);
                        break;
                    }
                }
                System.out.println(preData);
            });
        } catch (Exception e) {
            return;
        }
    }
}