From 8766cfc344d3635cd7e50a7c674cd5feb54d5a3d Mon Sep 17 00:00:00 2001
From: liriming <1343021927@qq.com>
Date: 星期一, 02 十二月 2024 09:30:35 +0800
Subject: [PATCH] 预警mq监听并发送

---
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishConsumer.java |   86 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 86 insertions(+), 0 deletions(-)

diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishConsumer.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishConsumer.java
new file mode 100644
index 0000000..0937244
--- /dev/null
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishConsumer.java
@@ -0,0 +1,86 @@
+package com.iailab.module.shasteel.mq.consumer;
+
+import com.alibaba.fastjson.JSONArray;
+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.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.util.CollectionUtils;
+
+import javax.annotation.Resource;
+import java.math.BigDecimal;
+import java.util.*;
+
+/**
+ * @author lirm
+ * @Description
+ * @createTime 2024年11月27日
+ */
+@Component
+@RabbitListener(queues = PredictFinishMessage.PREDICTFINISH_ROUTING_KEY)
+public class PredictFinishConsumer {
+
+    @Resource
+    private McsApi mcsApi;
+
+    @Resource
+    private RabbitTemplate rabbitTemplate;
+
+    @RabbitHandler
+    public void process(PredictFinishMessage finishMessage) 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 preDataJsonReqVO = new PreDataJsonReqVO();
+                preDataJsonReqVO.setPredictTime(finishMessage.getPredictTime());
+                preDataJsonReqVO.setOutputIdList(OutputIdList);
+                Map<String, List<Object[]>> preData = mcsApi.getPreDataCur(preDataJsonReqVO);
+                if (!CollectionUtils.isEmpty(preData)) {
+                    List<Object[]> result = preData.get(item.getOutId());
+                    int toIndex = result.size();
+                    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));
+                            alarmMessage.setAlarmMessageRespDTO(alarmMessageRespDTO);
+                            rabbitTemplate.convertAndSend(AlarmMessage.EXCHANGE, AlarmMessage.ALARM_ROUTING_KEY, alarmMessage);
+                            break;
+                        }
+                    }
+                }
+                System.out.println(preData);
+            });
+        }
+    }
+}

--
Gitblit v1.9.3