From d66e4475926e13188ccbd8f284b293c6eaceacbe Mon Sep 17 00:00:00 2001
From: liriming <1343021927@qq.com>
Date: 星期三, 11 十二月 2024 18:02:54 +0800
Subject: [PATCH] MQ

---
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java         |   33 ++++++++++-
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java |  119 +++++++++++++++++++++------------------
 2 files changed, 94 insertions(+), 58 deletions(-)

diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java
index 7c6c315..48fece0 100644
--- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelAlarmConsumer.java
@@ -1,6 +1,10 @@
 package com.iailab.module.shasteel.mq.consumer;
 
 import com.iailab.module.model.api.mcs.McsApi;
+import com.iailab.module.model.api.mcs.dto.StScheduleSchemeDTO;
+import com.iailab.module.model.api.mdk.MdkApi;
+import com.iailab.module.model.api.mdk.dto.MdkScheduleReqDTO;
+import com.iailab.module.shasteel.mq.common.RoutingConstant;
 import com.iailab.module.shasteel.mq.config.QueueModelAlarmConfig;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.amqp.core.Message;
@@ -8,6 +12,9 @@
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * 监听报警信息
@@ -23,6 +30,9 @@
     @Resource
     private McsApi mcsApi;
 
+    @Resource
+    private MdkApi mdkApi;
+
     /**
      * 事件标识
      */
@@ -35,10 +45,27 @@
      */
     @RabbitListener(queues = QueueModelAlarmConfig.QUEUE_NAME)
     public void listen(Message message) {
+        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
+        log.info("routingKey:" + routingKey);
+        String messageBody = new String(message.getBody());
+        log.info("messageBody:" + messageBody);
         // 查找需要执行的调度方案
-
-
-        // 开始执行调度方案
+        Map<String, Object> params = new HashMap<>();
+        params.put("trigger_condition", routingKey);
+        List<StScheduleSchemeDTO> scheduleSchemeList = mcsApi.listScheduleScheme(params);//根据触发条件获取调度方案列表
+        for(StScheduleSchemeDTO stScheduleSchemeDTO : scheduleSchemeList){
+            MdkScheduleReqDTO mdkScheduleReqDTO = new MdkScheduleReqDTO();
+            mdkScheduleReqDTO.setScheduleCode(stScheduleSchemeDTO.getCode());
+            mdkScheduleReqDTO.setScheduleTime(stScheduleSchemeDTO.getScheduleTime());
+//            MdkScheduleRespDTO mdkScheduleRespDTO = mdkApi.doSchedule(mdkScheduleReqDTO);//执行调度方案
+//            String result = mdkScheduleRespDTO.getResult().toString();
+//            System.out.println(result);
+//            AlarmMessageRespDTO alarmMessageRespDTO = new AlarmMessageRespDTO();
+//            mcsApi.createAlarmMessage(alarmMessageRespDTO);
+        }
+//        AlarmMessageRespDTO alarmMessageRespDTO = alarmMessage.getAlarmMessageRespDTO();
+//        JSONObject contentJson = JSONObject.parseObject(alarmMessageRespDTO.getContent());
+//        String overLimitTime = contentJson.get("overLimitTime").toString();
 
         // 根据模型返回结果,保存调度建议
 
diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java
index aed70ef..ba037aa 100644
--- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ModelPredictFinishConsumer.java
@@ -46,62 +46,71 @@
      */
     @RabbitListener(queues = QueuePredictFinishConfig.QUEUE_NAME)
     public void listen(Message message) {
-        String routingKey = message.getMessageProperties().getReceivedRoutingKey();
-        log.info("routingKey:" + routingKey);
-        String messageBody = new String(message.getBody());
-        log.info("messageBody:" + messageBody);
-        JSONObject messageJson = JSONObject.parseObject(messageBody);
-        List<AlarmConfigRespDTO> configList = mcsApi.listAlarmConfig(new HashMap<String, Object>());
-        if (CollectionUtils.isEmpty(configList)) {
+        try {
+            String routingKey = message.getMessageProperties().getReceivedRoutingKey();
+            log.info("routingKey:" + routingKey);
+            String messageBody = new String(message.getBody());
+            log.info("messageBody:" + messageBody);
+            JSONObject messageJson = JSONObject.parseObject(messageBody);
+            if (CollectionUtils.isEmpty(messageJson)) {
+                return;
+            }
+            List<AlarmConfigRespDTO> configList = mcsApi.listAlarmConfig(new HashMap<String, Object>());
+            if (CollectionUtils.isEmpty(configList)) {
+                return;
+            }
+            List<String> OutputIdList = new ArrayList<>();
+            configList.forEach(item -> {
+                OutputIdList.add(item.getOutId());
+            });
+            configList.forEach(item -> {
+                PreDataJsonReqVO reqVO = new PreDataJsonReqVO();
+                reqVO.setPredictTime(DateUtils.parse(messageJson.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)) {
+                    return;
+                }
+                List<Object[]> result = preData.get(item.getOutId());
+                int toIndex = result.size();
+                if (toIndex <= 0) {
+                    return;
+                }
+                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)) {
+                        AlarmMessageRespDTO alarmMessage = new AlarmMessageRespDTO();
+                        alarmMessage.setConfigId(item.getId());
+                        if (dataValue.compareTo(item.getLowerLimit()) < 0) {
+                            alarmMessage.setAlarmType("1");//超下限
+                        } else if (dataValue.compareTo(item.getUpperLimit()) > 0) {
+                            alarmMessage.setAlarmType("2");//超上限
+                        }
+                        alarmMessage.setAlarmTime(DateUtils.parse(data[0].toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
+                        StringBuffer sb = new StringBuffer();
+                        sb.append(data[0]);
+                        sb.append(" ");
+                        sb.append(item.getAlarmObj());
+                        if ("1".equals(alarmMessage.getAlarmType())) {
+                            sb.append("超下限");
+                        } else if ("2".equals(alarmMessage.getAlarmType())) {
+                            sb.append("超上限");
+                        }
+                        sb.append(dataValue);
+                        sb.append(item.getUnit());
+                        alarmMessage.setContent(sb.toString());
+                        System.out.println("预警消息=" + alarmMessage);
+                        rabbitTemplate.convertAndSend(RoutingConstant.EXCHANGE, RoutingConstant.Iailab_Model_Alarm, alarmMessage);
+                        break;
+                    }
+                }
+                System.out.println(preData);
+            });
+        } catch (Exception e) {
             return;
         }
-        List<String> OutputIdList = new ArrayList<>();
-        configList.forEach(item -> {
-            OutputIdList.add(item.getOutId());
-        });
-        configList.forEach(item -> {
-            PreDataJsonReqVO reqVO = new PreDataJsonReqVO();
-            reqVO.setPredictTime(DateUtils.parse(messageJson.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)) {
-                return;
-            }
-            List<Object[]> result = preData.get(item.getOutId());
-            int toIndex = result.size();
-            if (toIndex <= 0) {
-                return;
-            }
-            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)) {
-                    AlarmMessageRespDTO alarmMessage = new AlarmMessageRespDTO();
-                    alarmMessage.setConfigId(item.getId());
-                    if (dataValue.compareTo(item.getLowerLimit()) < 0) {
-                        alarmMessage.setAlarmType("1");//超下限
-                    } else if (dataValue.compareTo(item.getUpperLimit()) > 0) {
-                        alarmMessage.setAlarmType("2");//超上限
-                    }
-                    alarmMessage.setAlarmTime(DateUtils.parse(data[0].toString(), DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND));
-                    StringBuffer sb = new StringBuffer();
-                    sb.append(data[0]);
-                    sb.append(item.getAlarmObj());
-                    if("1".equals(alarmMessage.getAlarmType())){
-                        sb.append("超下限");
-                    }else if("2".equals(alarmMessage.getAlarmType())){
-                        sb.append("超上限");
-                    }
-                    sb.append(dataValue);
-                    sb.append(item.getUnit());
-                    alarmMessage.setContent(sb.toString());
-                    System.out.println("预警消息=" + alarmMessage);
-                    rabbitTemplate.convertAndSend(RoutingConstant.EXCHANGE, RoutingConstant.Iailab_Model_Alarm, alarmMessage);
-                    break;
-                }
-            }
-            System.out.println(preData);
-        });
+
     }
 }
\ No newline at end of file

--
Gitblit v1.9.3