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