From 191b3de92e362123e6d4d77d56e76c6d44fa4374 Mon Sep 17 00:00:00 2001
From: houzhongjian <houzhongyi@126.com>
Date: 星期四, 07 十一月 2024 09:20:22 +0800
Subject: [PATCH] 增加截图成功后发送rabbitMQ消息

---
 iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/package-info.java                              |    1 
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageMessage.java             |   37 +++++++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/package-info.java                      |    1 
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageConsumer.java            |   60 +++++++++++++++
 iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/package-info.java                         |    1 
 iailab-framework/iailab-common-mq/src/main/java/com/iailab/framework/mq/common/RoutingConstant.java                      |    9 ++
 iailab-module-data/iailab-module-data-biz/pom.xml                                                                        |    8 +-
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/config/TopicImageRabbitConfig.java     |   48 ++++++++++++
 iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/video/service/impl/CameraServiceImpl.java |   18 ++-
 iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java              |   36 +++++++++
 10 files changed, 208 insertions(+), 11 deletions(-)

diff --git a/iailab-framework/iailab-common-mq/src/main/java/com/iailab/framework/mq/common/RoutingConstant.java b/iailab-framework/iailab-common-mq/src/main/java/com/iailab/framework/mq/common/RoutingConstant.java
index ecc1a52..47d1c14 100644
--- a/iailab-framework/iailab-common-mq/src/main/java/com/iailab/framework/mq/common/RoutingConstant.java
+++ b/iailab-framework/iailab-common-mq/src/main/java/com/iailab/framework/mq/common/RoutingConstant.java
@@ -10,4 +10,13 @@
 public interface RoutingConstant {
 
     String Iailab_Data_PointCollectFinish = "Iailab.Data.PointCollectFinish";
+
+    // 摄像头通配路由
+    String Iailab_Data_Image = "Iailab.Data.Image.*";
+
+    // 大华摄像头路由
+    String Iailab_Data_Image_Dahua = "Iailab.Data.Image.Dahua";
+
+    // 海康摄像头路由
+    String Iailab_Data_Image_Hikvision = "Iailab.Data.Image.Hikvision";
 }
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/pom.xml b/iailab-module-data/iailab-module-data-biz/pom.xml
index 69a4f91..82daa6a 100644
--- a/iailab-module-data/iailab-module-data-biz/pom.xml
+++ b/iailab-module-data/iailab-module-data-biz/pom.xml
@@ -60,10 +60,10 @@
             <artifactId>iailab-common-biz-ip</artifactId>
         </dependency>
 
-<!--        <dependency>-->
-<!--            <groupId>com.iailab</groupId>-->
-<!--            <artifactId>iailab-common-mq</artifactId>-->
-<!--        </dependency>-->
+        <dependency>
+            <groupId>com.iailab</groupId>
+            <artifactId>iailab-common-mq</artifactId>
+        </dependency>
 
         <!-- Web 相关 -->
         <dependency>
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/config/TopicImageRabbitConfig.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/config/TopicImageRabbitConfig.java
new file mode 100644
index 0000000..8749109
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/config/TopicImageRabbitConfig.java
@@ -0,0 +1,48 @@
+package com.iailab.module.data.mq.config;
+
+import com.iailab.module.data.mq.consumer.ImageMessage;
+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 Houzhongjian
+ * @Description
+ * @createTime 2024年11月06日
+ */
+@Configuration
+public class TopicImageRabbitConfig {
+    //绑定键
+    public final static String routingDahua = ImageMessage.DAHUA_ROUTING_KEY;
+    public final static String routingHikvison = ImageMessage.HIKVISION_ROUTING_KEY;
+
+    @Bean
+    public Queue imageDahuaQueue() {
+        return new Queue(TopicImageRabbitConfig.routingDahua);
+    }
+
+    @Bean
+    public Queue imageHikvisionQueue() {
+        return new Queue(TopicImageRabbitConfig.routingHikvison);
+    }
+
+    @Bean
+    TopicExchange exchange() {
+        return new TopicExchange(ImageMessage.EXCHANGE);
+    }
+
+    // 注意通配符*和#的用法
+    @Bean
+    Binding bindingExchangeMessageDH() {
+        return BindingBuilder.bind(imageDahuaQueue()).to(exchange()).with(routingDahua);
+    }
+
+    @Bean
+    Binding bindingExchangeMessageHK() {
+        return BindingBuilder.bind(imageHikvisionQueue()).to(exchange()).with(routingHikvison);
+    }
+
+}
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageConsumer.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageConsumer.java
new file mode 100644
index 0000000..8b664dc
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageConsumer.java
@@ -0,0 +1,60 @@
+package com.iailab.module.data.mq.consumer;
+
+import org.springframework.amqp.core.ExchangeTypes;
+import org.springframework.amqp.rabbit.annotation.*;
+import org.springframework.stereotype.Component;
+
+import java.util.Date;
+
+/**
+ * @author Houzhongjian
+ * @Description
+ * @createTime 2024年11月05日
+ */
+@Component
+@RabbitListener(
+        bindings = @QueueBinding(
+                value = @Queue(
+                        // 在 Queue 的名字上,使用 UUID 生成其后缀。这样,启动的 Consumer 的 Queue 不同,以达到广播消费的目的
+                        name = ImageMessage.DAHUA_ROUTING_KEY,
+                        // Consumer 关闭时,该队列就可以被自动删除了
+                        autoDelete = "true"
+                ),
+                exchange = @Exchange(
+                        name = ImageMessage.EXCHANGE,
+                        type = ExchangeTypes.TOPIC,
+                        declare = "false",
+                        autoDelete = "true"
+                )
+        )
+)
+@RabbitListener(
+        bindings = @QueueBinding(
+                value = @Queue(
+                        // 在 Queue 的名字上,使用 UUID 生成其后缀。这样,启动的 Consumer 的 Queue 不同,以达到广播消费的目的
+                        name = ImageMessage.HIKVISION_ROUTING_KEY,
+                        // Consumer 关闭时,该队列就可以被自动删除了
+                        autoDelete = "true"
+                ),
+                exchange = @Exchange(
+                        name = ImageMessage.EXCHANGE,
+                        type = ExchangeTypes.TOPIC,
+                        declare = "false",
+                        autoDelete = "true"
+                )
+        )
+)
+public class ImageConsumer {
+
+//    @RabbitHandler
+    public void process(ImageMessage imageMessage) throws InterruptedException {
+        System.out.println("1111111111111111111111111");
+        Thread.sleep(5000);
+        System.out.println(imageMessage.toString());
+        System.out.println(new Date());
+        System.out.println(imageMessage.getCreateTime());
+        System.out.println(imageMessage.getMessageId());
+        System.out.println(imageMessage.getImageDTO().getImagePath());
+    }
+
+}
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageMessage.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageMessage.java
new file mode 100644
index 0000000..afd7a3c
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/consumer/ImageMessage.java
@@ -0,0 +1,37 @@
+package com.iailab.module.data.mq.consumer;
+
+import com.iailab.framework.mq.common.RoutingConstant;
+import com.iailab.module.data.video.dto.ImageDTO;
+import lombok.Data;
+
+import java.io.Serializable;
+
+@Data
+public class ImageMessage implements Serializable {
+
+    //暂时取消queue,使用routing自动生成
+//    public static final String QUEUE = "topic.image";
+
+    public static final String EXCHANGE = "imageExchange";
+
+    public static final String ROUTING_KEY = RoutingConstant.Iailab_Data_Image;
+
+    public static final String DAHUA_ROUTING_KEY = RoutingConstant.Iailab_Data_Image_Dahua;
+
+    public static final String HIKVISION_ROUTING_KEY = RoutingConstant.Iailab_Data_Image_Hikvision;
+
+    private String messageId;
+
+    private String createTime;
+
+    private ImageDTO imageDTO;
+
+    @Override
+    public String toString() {
+        return "ImageMessage{" +
+                "messageId='" + messageId + '\'' +
+                ", createTime='" + createTime + '\'' +
+                ", imageDTO=" + imageDTO +
+                '}';
+    }
+}
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/package-info.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/package-info.java
new file mode 100644
index 0000000..474707a
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/mq/package-info.java
@@ -0,0 +1 @@
+package com.iailab.module.data.mq;
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/video/service/impl/CameraServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/video/service/impl/CameraServiceImpl.java
index a79ee68..12f46e9 100644
--- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/video/service/impl/CameraServiceImpl.java
+++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/video/service/impl/CameraServiceImpl.java
@@ -106,13 +106,7 @@
         image.setCreateDate(captureDate);
         imageDao.insert(image);
         //存入rabbitmq消息队列,等待模型处理
-        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
-        ImageMessage imageMessage = new ImageMessage();
-        imageMessage.setMessageId(image.getId());
-        imageMessage.setCreateTime(createTime);
-        imageMessage.setImageDTO(BeanUtils.toBean(image, ImageDTO.class));
-        rabbitTemplate.convertAndSend(ImageMessage.EXCHANGE, ImageMessage.ROUTING_KEY, imageMessage);
-//        rabbitTemplate.convertAndSend("topic.image", imageMap);
+        sendRabbitMQMessage(image);
     }
 
     private QueryWrapper<CameraEntity> getWrapper(CameraPageReqVO cameraPageReqVO){
@@ -155,4 +149,14 @@
         return baseDao.selectCount(queryWrapper);
     }
 
+    // 发送rabbitMQ消息
+    private void sendRabbitMQMessage(ImageEntity image) {
+        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+        ImageMessage imageMessage = new ImageMessage();
+        imageMessage.setMessageId(image.getId());
+        imageMessage.setCreateTime(createTime);
+        imageMessage.setImageDTO(BeanUtils.toBean(image, ImageDTO.class));
+        rabbitTemplate.convertAndSend(ImageMessage.EXCHANGE, ImageMessage.DAHUA_ROUTING_KEY, imageMessage);
+    }
+
 }
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java b/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java
new file mode 100644
index 0000000..f7610a1
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/controller/RabbitMQTest.java
@@ -0,0 +1,36 @@
+package com.iailab.module.data.controller;
+
+import com.iailab.DataWebApplication;
+import com.iailab.framework.test.core.ut.BaseMockitoUnitTest;
+import com.iailab.module.data.mq.consumer.ImageMessage;
+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.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.*;
+
+/**
+ * @author Houzhongjian
+ * @Description
+ * @createTime 2024年11月06日
+ */
+@SpringBootTest(classes = DataWebApplication.class)
+public class RabbitMQTest extends BaseMockitoUnitTest {
+
+    @Resource
+    private RabbitTemplate rabbitTemplate;
+
+
+    @Test
+    public void testProducer() {
+        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
+        Map<String, Object> imageMap = new HashMap<>();
+        imageMap.put("messageId", "111111111111111");
+        imageMap.put("image", "8888888888888888888");
+        imageMap.put("createTime", createTime);
+        rabbitTemplate.convertAndSend(ImageMessage.EXCHANGE, ImageMessage.ROUTING_KEY, imageMap);
+    }
+}
diff --git a/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/package-info.java b/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/package-info.java
new file mode 100644
index 0000000..1b55905
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/data/package-info.java
@@ -0,0 +1 @@
+package com.iailab.module.data;
\ No newline at end of file
diff --git a/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/package-info.java b/iailab-module-data/iailab-module-data-biz/src/test/java/com/iailab/module/package-info.java
new file mode 100644
index 0000000..c343803
--- /dev/null
+++ b/iailab-module-data/iailab-module-data-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