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