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