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/AlarmMessage.java | 17 +++++ /dev/null | 0 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishConsumer.java | 86 ++++++++++++++++++++++++++++ shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/common/RoutingConstant.java | 11 +++ shasteel-biz/src/main/java/com/iailab/module/shasteel/framework/rpc/config/RpcConfiguration.java | 3 shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicPredictFinishRabbitConfig.java | 37 ++++++++++++ shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishMessage.java | 17 +++++ shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/package-info.java | 1 8 files changed, 171 insertions(+), 1 deletions(-) diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/framework/rpc/config/RpcConfiguration.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/framework/rpc/config/RpcConfiguration.java index 2c00b2f..bed8227 100644 --- a/shasteel-biz/src/main/java/com/iailab/module/shasteel/framework/rpc/config/RpcConfiguration.java +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/framework/rpc/config/RpcConfiguration.java @@ -5,11 +5,12 @@ import com.iailab.module.infra.api.file.FileApi; import com.iailab.module.infra.api.websocket.WebSocketSenderApi; import com.iailab.module.model.api.mcs.McsApi; +import com.iailab.module.model.api.mdk.MdkApi; import com.iailab.module.system.api.tenant.TenantApi; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Configuration; @Configuration(proxyBeanMethods = false) -@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, DataSourceConfigServiceApi.class, ConfigApi.class, TenantApi.class, McsApi.class}) +@EnableFeignClients(clients = {FileApi.class, WebSocketSenderApi.class, DataSourceConfigServiceApi.class, ConfigApi.class, TenantApi.class, McsApi.class, MdkApi.class}) public class RpcConfiguration { } diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/common/RoutingConstant.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/common/RoutingConstant.java new file mode 100644 index 0000000..b7981e2 --- /dev/null +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/common/RoutingConstant.java @@ -0,0 +1,11 @@ +package com.iailab.module.shasteel.mq.common; + +public interface RoutingConstant { + + // 预测模型完成路由 + String Iailab_Model_PredictFinish = "Iailab.Model.PredictFinish"; + + //预警消息路由 + String Iailab_Model_Alarm = "Iailab.Model.Alarm"; + +} \ No newline at end of file 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 new file mode 100644 index 0000000..c67a5b6 --- /dev/null +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/config/TopicPredictFinishRabbitConfig.java @@ -0,0 +1,37 @@ +package com.iailab.module.shasteel.mq.config; + +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 TopicPredictFinishRabbitConfig { + //绑定键 + public final static String routingPredictFinish = PredictFinishMessage.PREDICTFINISH_ROUTING_KEY; + + @Bean + public Queue predictFinishQueue() { + return new Queue(TopicPredictFinishRabbitConfig.routingPredictFinish); + } + + + @Bean + TopicExchange exchange() { + return new TopicExchange(PredictFinishMessage.EXCHANGE); + } + + // 注意通配符*和#的用法 + @Bean + Binding bindingExchangeMessageDH() { + return BindingBuilder.bind(predictFinishQueue()).to(exchange()).with(routingPredictFinish); + } +} 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 new file mode 100644 index 0000000..be1cc3e --- /dev/null +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/AlarmMessage.java @@ -0,0 +1,17 @@ +package com.iailab.module.shasteel.mq.consumer; + +import com.iailab.module.model.api.mcs.dto.AlarmMessageRespDTO; +import com.iailab.module.shasteel.mq.common.RoutingConstant; +import lombok.Data; + +import java.io.Serializable; + +@Data +public class AlarmMessage implements Serializable { + + public static final String EXCHANGE = "alarm-exchange"; + + public static final String ALARM_ROUTING_KEY = RoutingConstant.Iailab_Model_Alarm; + // 预警消息DTO + private AlarmMessageRespDTO alarmMessageRespDTO; +} \ No newline at end of file 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); + }); + } + } +} diff --git a/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishMessage.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishMessage.java new file mode 100644 index 0000000..4bf1c36 --- /dev/null +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/consumer/PredictFinishMessage.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 PredictFinishMessage implements Serializable { + + public static final String EXCHANGE = "predict-finish-exchange"; + + public static final String PREDICTFINISH_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/mq/package-info.java b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/package-info.java new file mode 100644 index 0000000..cd739fb --- /dev/null +++ b/shasteel-biz/src/main/java/com/iailab/module/shasteel/mq/package-info.java @@ -0,0 +1 @@ +package com.iailab.module.shasteel.mq; \ No newline at end of file diff --git a/shasteel-biz/src/test/resources/application-unit-test.yaml b/shasteel-biz/src/test/resources/application-unit-test.yaml deleted file mode 100644 index 4d129b4..0000000 --- a/shasteel-biz/src/test/resources/application-unit-test.yaml +++ /dev/null @@ -1,57 +0,0 @@ -spring: - main: - lazy-initialization: true # 开启懒加载,加快速度 - banner-mode: off # 单元测试,禁用 Banner - ---- #################### 数据库相关配置 #################### - -spring: - # 数据源配置项 - datasource: - name: iailab_fast - url: jdbc:h2:mem:testdb;MODE=MYSQL;DATABASE_TO_UPPER=false;NON_KEYWORDS=value; # MODE 使用 MySQL 模式;DATABASE_TO_UPPER 配置表和字段使用小写 - driver-class-name: org.h2.Driver - username: sa - password: - druid: - async-init: true # 单元测试,异步初始化 Druid 连接池,提升启动速度 - initial-size: 1 # 单元测试,配置为 1,提升启动速度 - sql: - init: - schema-locations: classpath:/sql/create_tables.sql - - # Redis 配置。Redisson 默认的配置足够使用,一般不需要进行调优 - redis: - host: 127.0.0.1 # 地址 - port: 16379 # 端口(单元测试,使用 16379 端口) - database: 0 # 数据库索引 - -mybatis: - lazy-initialization: true # 单元测试,设置 MyBatis Mapper 延迟加载,加速每个单元测试 - -mybatis-plus: - global-config: - db-config: - id-type: AUTO # H2 主键递增 - ---- #################### 定时任务相关配置 #################### - ---- #################### 配置中心相关配置 #################### - ---- #################### 服务保障相关配置 #################### - -# Lock4j 配置项(单元测试,禁用 Lock4j) - ---- #################### 监控相关配置 #################### - ---- #################### 平台相关配置 #################### - -# 平台配置项,设置当前项目所有自定义的配置 -iailab: - info: - base-package: com.iailab.module - captcha: - timeout: 5m - width: 160 - height: 60 - enable: true diff --git a/shasteel-biz/src/test/resources/logback.xml b/shasteel-biz/src/test/resources/logback.xml deleted file mode 100644 index daf756b..0000000 --- a/shasteel-biz/src/test/resources/logback.xml +++ /dev/null @@ -1,4 +0,0 @@ -<configuration> - <!-- 引用 Spring Boot 的 logback 基础配置 --> - <include resource="org/springframework/boot/logging/logback/defaults.xml" /> -</configuration> diff --git a/shasteel-biz/src/test/resources/sql/create_tables.sql b/shasteel-biz/src/test/resources/sql/create_tables.sql deleted file mode 100644 index e69de29..0000000 --- a/shasteel-biz/src/test/resources/sql/create_tables.sql +++ /dev/null -- Gitblit v1.9.3