沙钢智慧能源系统后端代码
dengzedong
2024-12-06 3216ed3fb2d9dc9cf61da21bda3175e66ec46426
提交 | 用户 | 时间
8766cf 1 package com.iailab.module.shasteel.mq.consumer;
L 2
3 import com.alibaba.fastjson.JSONObject;
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;
9 import org.springframework.amqp.rabbit.annotation.RabbitHandler;
10 import org.springframework.amqp.rabbit.annotation.RabbitListener;
11 import org.springframework.stereotype.Component;
12 import org.springframework.util.CollectionUtils;
13
14 import javax.annotation.Resource;
15 import java.math.BigDecimal;
bd1e8a 16 import java.util.ArrayList;
L 17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Map;
8766cf 20
L 21 /**
22  * @author lirm
23  * @Description
24  * @createTime 2024年11月27日
25  */
26 @Component
27 @RabbitListener(queues = PredictFinishMessage.PREDICTFINISH_ROUTING_KEY)
28 public class PredictFinishConsumer {
29
30     @Resource
31     private McsApi mcsApi;
32
33     @Resource
f0a800 34     private AlarmConsumer alarmConsumer;
L 35
8766cf 36     @RabbitHandler
bd1e8a 37     public void process(JSONObject json) throws InterruptedException {
8766cf 38         System.out.println("测试消费模型预测完成RabbitMQ消息----------------------");
L 39         Thread.sleep(5000);
40         List<AlarmConfigRespDTO> configList = mcsApi.listAlarmConfig(new HashMap<String, Object>());
41         if (!CollectionUtils.isEmpty(configList)) {
42             List<String> OutputIdList = new ArrayList<>();
43             configList.forEach(item -> {
44                 OutputIdList.add(item.getOutId());
45             });
46             configList.forEach(item -> {
f0a800 47                 PreDataJsonReqVO reqVO = new PreDataJsonReqVO();
bd1e8a 48                 reqVO.setPredictTime(DateUtils.parse(json.get("predictTime").toString(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
f0a800 49                 reqVO.setOutputIdList(OutputIdList);
L 50                 Map<String, List<Object[]>> preData = mcsApi.getPreDataCur(reqVO);
8766cf 51                 if (!CollectionUtils.isEmpty(preData)) {
L 52                     List<Object[]> result = preData.get(item.getOutId());
53                     int toIndex = result.size();
f0a800 54                     if(toIndex > 0){
L 55                         int fromIndex = result.size() - item.getCompLength();
56                         List<Object[]> predictList = result.subList(fromIndex, toIndex);
57                         for(Object[] data : predictList){
58                             BigDecimal dataValue = new BigDecimal(Double.parseDouble(data[1].toString())).setScale(2, BigDecimal.ROUND_HALF_UP);
59                             if (!(dataValue.compareTo(item.getLowerLimit()) >= 0 && dataValue.compareTo(item.getUpperLimit()) <= 0)) {
60                                 AlarmMessage alarmMessage = new AlarmMessage();
61                                 AlarmMessageRespDTO alarmMessageRespDTO = new AlarmMessageRespDTO();
62                                 alarmMessageRespDTO.setConfigId(item.getId());
63                                 if (dataValue.compareTo(item.getLowerLimit()) < 0) {
64                                     alarmMessageRespDTO.setAlarmType("1");//超下限
65                                 }else if (dataValue.compareTo(item.getUpperLimit()) > 0) {
66                                     alarmMessageRespDTO.setAlarmType("2");//超上限
67                                 }
68                                 alarmMessageRespDTO.setAlarmTime(DateUtils.parse(data[0].toString(),DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
69                                 JSONObject content = new JSONObject();
70                                 content.put("alarmObj", item.getAlarmObj());
71                                 content.put("alarmType", alarmMessageRespDTO.getAlarmType());
72                                 content.put("unit", item.getUnit());
73                                 content.put("overLimitTime", data[0]);
74                                 content.put("overLimitValue", dataValue);
75                                 alarmMessageRespDTO.setContent(JSONObject.toJSONString(content));
bd1e8a 76                                 System.out.println("预警消息=" + alarmMessageRespDTO);
f0a800 77                                 alarmMessage.setAlarmMessageRespDTO(alarmMessageRespDTO);
L 78                                 try {
79                                     alarmConsumer.process(alarmMessage);
80                                 } catch (InterruptedException e) {
81                                     throw new RuntimeException(e);
82                                 }
83                                 break;
8766cf 84                             }
L 85                         }
86                     }
87                 }
88                 System.out.println(preData);
89             });
90         }
91     }
92 }