沙钢智慧能源系统后端代码
潘志宝
2024-12-24 70ef272e51099f11672a103cdf8e41384bd66489
提交 | 用户 | 时间
250b37 1 package com.iailab.module.shasteel.mq.consumer;
2
0cd823 3 import com.alibaba.fastjson.JSONObject;
L 4 import com.iailab.framework.common.util.date.DateUtils;
5 import com.iailab.module.model.api.mcs.McsApi;
6 import com.iailab.module.model.api.mcs.dto.AlarmConfigRespDTO;
7 import com.iailab.module.model.api.mcs.dto.AlarmMessageRespDTO;
8 import com.iailab.module.model.api.mcs.dto.PreDataJsonReqVO;
456a64 9 import com.iailab.module.shasteel.mq.common.constant.CommonConstant;
cf56eb 10 import com.iailab.module.shasteel.mq.common.constant.RoutingConstant;
250b37 11 import com.iailab.module.shasteel.mq.config.QueuePredictFinishConfig;
12 import lombok.extern.slf4j.Slf4j;
13 import org.springframework.amqp.core.Message;
14 import org.springframework.amqp.rabbit.annotation.RabbitListener;
c9ff6a 15 import org.springframework.amqp.rabbit.core.RabbitTemplate;
250b37 16 import org.springframework.stereotype.Component;
0cd823 17 import org.springframework.util.CollectionUtils;
L 18
19 import javax.annotation.Resource;
20 import java.math.BigDecimal;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
250b37 25
26 /**
c9ff6a 27  * 监听预测完成
28  *
250b37 29  * @author PanZhibao
30  * @Description
31  * @createTime 2024年12月11日
32  */
33 @Slf4j
34 @Component
35 public class ModelPredictFinishConsumer {
36
0cd823 37     @Resource
L 38     private McsApi mcsApi;
39
40     @Resource
c9ff6a 41     private RabbitTemplate rabbitTemplate;
0cd823 42
c9ff6a 43     /**
44      * 监听预测完成,产生预警消息
45      *
46      * @param message
47      */
250b37 48     @RabbitListener(queues = QueuePredictFinishConfig.QUEUE_NAME)
49     public void listen(Message message) {
d66e44 50         try {
L 51             String routingKey = message.getMessageProperties().getReceivedRoutingKey();
52             log.info("routingKey:" + routingKey);
53             String messageBody = new String(message.getBody());
54             log.info("messageBody:" + messageBody);
55             JSONObject messageJson = JSONObject.parseObject(messageBody);
56             if (CollectionUtils.isEmpty(messageJson)) {
57                 return;
58             }
59             List<AlarmConfigRespDTO> configList = mcsApi.listAlarmConfig(new HashMap<String, Object>());
60             if (CollectionUtils.isEmpty(configList)) {
61                 return;
62             }
63             List<String> OutputIdList = new ArrayList<>();
64             configList.forEach(item -> {
65                 OutputIdList.add(item.getOutId());
66             });
67             configList.forEach(item -> {
68                 PreDataJsonReqVO reqVO = new PreDataJsonReqVO();
69                 reqVO.setPredictTime(DateUtils.parse(messageJson.get("predictTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
70                 reqVO.setOutputIdList(OutputIdList);
71                 Map<String, List<Object[]>> preData = mcsApi.getPreDataCur(reqVO);
72                 if (CollectionUtils.isEmpty(preData)) {
73                     return;
74                 }
75                 List<Object[]> result = preData.get(item.getOutId());
76                 int toIndex = result.size();
77                 if (toIndex <= 0) {
78                     return;
79                 }
80                 int fromIndex = result.size() - item.getCompLength();
81                 List<Object[]> predictList = result.subList(fromIndex, toIndex);
82                 for (Object[] data : predictList) {
83                     BigDecimal dataValue = new BigDecimal(Double.parseDouble(data[1].toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
84                     if (!(dataValue.compareTo(item.getLowerLimit()) >= 0 && dataValue.compareTo(item.getUpperLimit()) <= 0)) {
85                         AlarmMessageRespDTO alarmMessage = new AlarmMessageRespDTO();
86                         alarmMessage.setConfigId(item.getId());
87                         if (dataValue.compareTo(item.getLowerLimit()) < 0) {
456a64 88                             alarmMessage.setAlarmType(CommonConstant.EXCEEDING_LOWER_LIMIT);//超下限
d66e44 89                         } else if (dataValue.compareTo(item.getUpperLimit()) > 0) {
456a64 90                             alarmMessage.setAlarmType(CommonConstant.EXCEEDING_UPPER_LIMIT);//超上限
d66e44 91                         }
L 92                         alarmMessage.setAlarmTime(DateUtils.parse(data[0].toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
93                         StringBuffer sb = new StringBuffer();
94                         sb.append(data[0]);
95                         sb.append(" ");
96                         sb.append(item.getAlarmObj());
97                         if ("1".equals(alarmMessage.getAlarmType())) {
456a64 98                             sb.append(CommonConstant.EXCEEDING_LOWER_LIMIT);
d66e44 99                         } else if ("2".equals(alarmMessage.getAlarmType())) {
456a64 100                             sb.append(CommonConstant.EXCEEDING_UPPER_LIMIT);
d66e44 101                         }
L 102                         sb.append(dataValue);
103                         sb.append(item.getUnit());
104                         alarmMessage.setContent(sb.toString());
105                         System.out.println("预警消息=" + alarmMessage);
106                         rabbitTemplate.convertAndSend(RoutingConstant.EXCHANGE, RoutingConstant.Iailab_Model_Alarm, alarmMessage);
107                         break;
108                     }
109                 }
110                 System.out.println(preData);
111             });
112         } catch (Exception e) {
0cd823 113             return;
L 114         }
d66e44 115
250b37 116     }
117 }