From 77a5ab85c55cdd84b311b1d67d35ba4663be8b93 Mon Sep 17 00:00:00 2001
From: dengzedong <dengzedong@email>
Date: 星期五, 06 十二月 2024 12:00:35 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/master'

---
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmMessage.java                 |    2 
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ScheduleConsumer.java             |   56 +++++++++++
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicAlarmRabbitConfig.java         |   37 +++++++
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishConsumer.java        |   74 ++++++++------
 shasteel-biz/src/test/java/com/iailab/module/package-info.java                                      |    1 
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicPredictFinishRabbitConfig.java |    7 
 shasteel-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java                      |   33 ++++++
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ScheduleMessage.java              |   17 +++
 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmConsumer.java                |   37 +++++++
 shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java                  |    2 
 10 files changed, 226 insertions(+), 40 deletions(-)

diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicAlarmRabbitConfig.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicAlarmRabbitConfig.java
new file mode 100644
index 0000000..da4f90e
--- /dev/null
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicAlarmRabbitConfig.java
@@ -0,0 +1,37 @@
+package com.iailab.module.shasteel.mq.config;
+
+import com.iailab.module.shasteel.mq.consumer.AlarmMessage;
+import com.iailab.module.shasteel.mq.consumer.PredictFinishMessage;
+import org.springframework.amqp.core.Binding;
+import org.springframework.amqp.core.BindingBuilder;
+import org.springframework.amqp.core.Queue;
+import org.springframework.amqp.core.TopicExchange;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author lirm
+ * @Description
+ * @createTime 2024年11月27日
+ */
+@Configuration
+public class TopicAlarmRabbitConfig {
+    //绑定键
+    public final static String routingAlarm = AlarmMessage.ALARM_ROUTING_KEY;
+
+    @Bean
+    public Queue alarmQueue() {
+        return new Queue(TopicAlarmRabbitConfig.routingAlarm);
+    }
+
+    @Bean
+    TopicExchange alarmExchange() {
+        return new TopicExchange(AlarmMessage.EXCHANGE);
+    }
+
+    // 注意通配符*和#的用法
+    @Bean
+    Binding bindingExchangeMessageAlarm() {
+        return BindingBuilder.bind(alarmQueue()).to(alarmExchange()).with(routingAlarm);
+    }
+}
diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicPredictFinishRabbitConfig.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicPredictFinishRabbitConfig.java
index c67a5b6..110fbe8 100644
--- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicPredictFinishRabbitConfig.java
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicPredictFinishRabbitConfig.java
@@ -23,15 +23,14 @@
         return new Queue(TopicPredictFinishRabbitConfig.routingPredictFinish);
     }
 
-
     @Bean
-    TopicExchange exchange() {
+    TopicExchange predictFinishExchange() {
         return new TopicExchange(PredictFinishMessage.EXCHANGE);
     }
 
     // 注意通配符*和#的用法
     @Bean
-    Binding bindingExchangeMessageDH() {
-        return BindingBuilder.bind(predictFinishQueue()).to(exchange()).with(routingPredictFinish);
+    Binding bindingExchangeMessagePF() {
+        return BindingBuilder.bind(predictFinishQueue()).to(predictFinishExchange()).with(routingPredictFinish);
     }
 }
diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmConsumer.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmConsumer.java
new file mode 100644
index 0000000..af5c1c3
--- /dev/null
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmConsumer.java
@@ -0,0 +1,37 @@
+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.amqp.rabbit.core.RabbitTemplate;
+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 = AlarmMessage.ALARM_ROUTING_KEY)
+public class AlarmConsumer {
+
+    @Resource
+    private RabbitTemplate rabbitTemplate;
+
+    public void process(AlarmMessage alarmMessage) throws InterruptedException {
+        rabbitTemplate.convertAndSend(AlarmMessage.EXCHANGE, AlarmMessage.ALARM_ROUTING_KEY, alarmMessage);
+    }
+}
diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmMessage.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmMessage.java
index be1cc3e..2587386 100644
--- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmMessage.java
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmMessage.java
@@ -9,7 +9,7 @@
 @Data
 public class AlarmMessage implements Serializable {
 
-    public static final String EXCHANGE = "alarm-exchange";
+    public static final String EXCHANGE = "predict-alarm-exchange";
 
     public static final String ALARM_ROUTING_KEY = RoutingConstant.Iailab_Model_Alarm;
     // 预警消息DTO
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
index 0937244..d8f1918 100644
--- 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
@@ -1,6 +1,5 @@
 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;
@@ -9,14 +8,15 @@
 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.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 /**
  * @author lirm
@@ -31,13 +31,12 @@
     private McsApi mcsApi;
 
     @Resource
-    private RabbitTemplate rabbitTemplate;
+    private AlarmConsumer alarmConsumer;
 
     @RabbitHandler
-    public void process(PredictFinishMessage finishMessage) throws InterruptedException {
+    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<>();
@@ -45,37 +44,44 @@
                 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);
+                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();
-                    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");//超上限
+                    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;
                             }
-                            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;
                         }
                     }
                 }
diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ScheduleConsumer.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ScheduleConsumer.java
new file mode 100644
index 0000000..78a2acb
--- /dev/null
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ScheduleConsumer.java
@@ -0,0 +1,56 @@
+package com.iailab.module.shasteel.mq.consumer;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.iailab.module.model.api.mcs.McsApi;
+import com.iailab.module.model.api.mcs.dto.AlarmMessageRespDTO;
+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.model.api.mdk.dto.MdkScheduleRespDTO;
+import org.springframework.amqp.rabbit.annotation.RabbitHandler;
+import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.stereotype.Component;
+
+import javax.annotation.Resource;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * @author lirm
+ * @Description
+ * @createTime 2024年11月27日
+ */
+@Component
+@RabbitListener(queues = AlarmMessage.ALARM_ROUTING_KEY)
+public class ScheduleConsumer {
+
+    @Resource
+    private McsApi mcsApi;
+
+    @Resource
+    private MdkApi mdkApi;
+
+    @RabbitHandler
+    public void process(AlarmMessage alarmMessage) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("trigger_condition", AlarmMessage.ALARM_ROUTING_KEY);
+        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/ScheduleMessage.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ScheduleMessage.java
new file mode 100644
index 0000000..b5e9a8a
--- /dev/null
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/ScheduleMessage.java
@@ -0,0 +1,17 @@
+package com.iailab.module.shasteel.mq.consumer;
+
+import com.iailab.module.shasteel.mq.common.RoutingConstant;
+import lombok.Data;
+
+import java.io.Serializable;
+import java.util.Date;
+
+@Data
+public class ScheduleMessage implements Serializable {
+
+    public static final String EXCHANGE = "schedule-exchange";
+
+    public static final String SCHEDULE_ROUTING_KEY = RoutingConstant.Iailab_Model_PredictFinish;
+    // 预测时间
+    private Date predictTime;
+}
\ No newline at end of file
diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java
index fe3fcc5..8337a83 100644
--- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java
+++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/util/token/IailabClient.java
@@ -63,7 +63,7 @@
 //        PASSWORD = PlatApplicationContext.getProperty("iailab.password");
 
         BASE_URL = "http://127.0.0.1:48080/admin-api/system";
-        TENANT_ID = 172L;
+        TENANT_ID = 176L;
         CLIENT_ID = "shasteel";
         CLIENT_SECRET = "shasteel111111111111111";
         USERNAME = "shasteel";
diff --git a/shasteel-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java b/shasteel-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java
new file mode 100644
index 0000000..29d86fe
--- /dev/null
+++ b/shasteel-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java
@@ -0,0 +1,33 @@
+package com.iailab.module.data.controller;
+
+import com.alibaba.fastjson.JSONObject;
+import com.iailab.framework.common.util.date.DateUtils;
+import com.iailab.framework.test.core.ut.BaseMockitoUnitTest;
+import com.iailab.module.shasteel.ShasteelServerApplication;
+import com.iailab.module.shasteel.mq.consumer.PredictFinishMessage;
+import org.junit.jupiter.api.Test;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import javax.annotation.Resource;
+import java.util.Date;
+
+/**
+ * @author lirm
+ * @Description
+ * @createTime 2024年11月27日
+ */
+@SpringBootTest(classes = ShasteelServerApplication.class)
+public class RabbitMQTest extends BaseMockitoUnitTest {
+
+    @Resource
+    private RabbitTemplate rabbitTemplate;
+
+    @Test
+    public void testProducer() {
+        JSONObject content = new JSONObject();
+        content.put("moduleType", "NET_LDG");
+        content.put("predictTime", "2024-12-04 06:30:00");
+        rabbitTemplate.convertAndSend(PredictFinishMessage.EXCHANGE, PredictFinishMessage.PREDICTFINISH_ROUTING_KEY, content);
+    }
+}
diff --git a/shasteel-biz/src/test/java/com/iailab/module/package-info.java b/shasteel-biz/src/test/java/com/iailab/module/package-info.java
new file mode 100644
index 0000000..c343803
--- /dev/null
+++ b/shasteel-biz/src/test/java/com/iailab/module/package-info.java
@@ -0,0 +1 @@
+package com.iailab.module;
\ No newline at end of file

--
Gitblit v1.9.3