提交 | 用户 | 时间
|
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) { |
|
49 |
String routingKey = message.getMessageProperties().getReceivedRoutingKey(); |
|
50 |
log.info("routingKey:" + routingKey); |
|
51 |
String messageBody = new String(message.getBody()); |
|
52 |
log.info("messageBody:" + messageBody); |
0cd823
|
53 |
JSONObject messageJson = JSONObject.parseObject(messageBody); |
L |
54 |
List<AlarmConfigRespDTO> configList = mcsApi.listAlarmConfig(new HashMap<String, Object>()); |
|
55 |
if (CollectionUtils.isEmpty(configList)) { |
|
56 |
return; |
|
57 |
} |
|
58 |
List<String> OutputIdList = new ArrayList<>(); |
|
59 |
configList.forEach(item -> { |
|
60 |
OutputIdList.add(item.getOutId()); |
|
61 |
}); |
|
62 |
configList.forEach(item -> { |
|
63 |
PreDataJsonReqVO reqVO = new PreDataJsonReqVO(); |
|
64 |
reqVO.setPredictTime(DateUtils.parse(messageJson.get("predictTime").toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); |
|
65 |
reqVO.setOutputIdList(OutputIdList); |
|
66 |
Map<String, List<Object[]>> preData = mcsApi.getPreDataCur(reqVO); |
|
67 |
if (CollectionUtils.isEmpty(preData)) { |
|
68 |
return; |
|
69 |
} |
|
70 |
List<Object[]> result = preData.get(item.getOutId()); |
|
71 |
int toIndex = result.size(); |
|
72 |
if (toIndex <= 0) { |
|
73 |
return; |
|
74 |
} |
|
75 |
int fromIndex = result.size() - item.getCompLength(); |
|
76 |
List<Object[]> predictList = result.subList(fromIndex, toIndex); |
|
77 |
for (Object[] data : predictList) { |
|
78 |
BigDecimal dataValue = new BigDecimal(Double.parseDouble(data[1].toString())).setScale(2, BigDecimal.ROUND_HALF_UP); |
|
79 |
if (!(dataValue.compareTo(item.getLowerLimit()) >= 0 && dataValue.compareTo(item.getUpperLimit()) <= 0)) { |
c9ff6a
|
80 |
AlarmMessageRespDTO alarmMessage = new AlarmMessageRespDTO(); |
潘 |
81 |
alarmMessage.setConfigId(item.getId()); |
0cd823
|
82 |
if (dataValue.compareTo(item.getLowerLimit()) < 0) { |
c9ff6a
|
83 |
alarmMessage.setAlarmType("1");//超下限 |
0cd823
|
84 |
} else if (dataValue.compareTo(item.getUpperLimit()) > 0) { |
c9ff6a
|
85 |
alarmMessage.setAlarmType("2");//超上限 |
0cd823
|
86 |
} |
c9ff6a
|
87 |
alarmMessage.setAlarmTime(DateUtils.parse(data[0].toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND)); |
0cd823
|
88 |
JSONObject content = new JSONObject(); |
L |
89 |
content.put("alarmObj", item.getAlarmObj()); |
c9ff6a
|
90 |
content.put("alarmType", alarmMessage.getAlarmType()); |
0cd823
|
91 |
content.put("unit", item.getUnit()); |
L |
92 |
content.put("overLimitTime", data[0]); |
|
93 |
content.put("overLimitValue", dataValue); |
c9ff6a
|
94 |
alarmMessage.setContent(JSONObject.toJSONString(content)); |
潘 |
95 |
System.out.println("预警消息=" + alarmMessage); |
|
96 |
rabbitTemplate.convertAndSend(RoutingConstant.EXCHANGE, RoutingConstant.Iailab_Model_Alarm, alarmMessage); |
0cd823
|
97 |
break; |
L |
98 |
} |
|
99 |
} |
|
100 |
System.out.println(preData); |
|
101 |
}); |
250b37
|
102 |
} |
潘 |
103 |
} |