Procházet zdrojové kódy

重构huimv-eartag2-input子工程

zhuoning před 3 roky
rodič
revize
dc58f2c2f0

+ 23 - 0
huimv-eartag2-platform/huimv-eartag2-input/pom.xml

@@ -17,11 +17,28 @@
         <!--        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>-->
     </properties>
     <dependencies>
+<!--        <dependency>-->
+<!--            <groupId>org.springframework.boot</groupId>-->
+<!--            <artifactId>spring-boot-starter-web</artifactId>-->
+<!--        </dependency>-->
+        <!-- 排除Tomcat容器 -->
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
+            <!-- 移除掉默认支持的 Tomcat -->
+            <exclusions>
+                <exclusion>
+                    <groupId>org.springframework.boot</groupId>
+                    <artifactId>spring-boot-starter-tomcat</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
 
+        <!-- 添加 Undertow 容器 -->
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-undertow</artifactId>
+        </dependency>
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-test</artifactId>
@@ -101,6 +118,12 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-actuator</artifactId>
         </dependency>
+
+<!--        &lt;!&ndash; 引入Actuator监控依赖 &ndash;&gt;-->
+<!--        <dependency>-->
+<!--            <groupId>org.springframework.boot</groupId>-->
+<!--            <artifactId>spring-boot-starter-actuator</artifactId>-->
+<!--        </dependency>-->
     </dependencies>
 
         <build>

+ 2 - 2
huimv-eartag2-platform/huimv-eartag2-input/src/main/java/com/huimv/eartag2/HuimvEartagApplication.java

@@ -14,10 +14,10 @@ import org.springframework.context.ApplicationContext;
  * @Create : 2020-12-25
  **/
 @SpringBootApplication
-public class HuimvEartagApplication {
+public class HuimvEartag2InputApplication {
     public static void main(String[] args) throws InterruptedException {
 //        SpringApplication.run(HuimvEartagApplication.class, args);
-        ApplicationContext applicationContext = SpringApplication.run(HuimvEartagApplication.class, args);
+        ApplicationContext applicationContext = SpringApplication.run(HuimvEartag2InputApplication.class, args);
         //EartagServer
 //        applicationContext.getBean(EartagServer.class).start();
         //EartagServer2

+ 18 - 11
huimv-eartag2-platform/huimv-eartag2-input/src/main/java/com/huimv/eartag2/config/TopicRabbitMQConfig.java

@@ -20,25 +20,32 @@ import org.springframework.context.annotation.Configuration;
 public class TopicRabbitMQConfig {
     //原始数据队列
     @Bean
-//    public Queue askQueue() {
-//        return new Queue(ask);
-//    }
-    public Queue askQueue() {
-        return new Queue(Const.QUEUE_ASK);
+    public Queue askDeviceQueue() {
+        return new Queue(Const.QUEUE_ASK_DEVICE);
+    }
+    @Bean
+    public Queue askEartagQueue() {
+        return new Queue(Const.QUEUE_ASK_EARTAG);
     }
 
     //原始交换机
     @Bean
-//    TopicExchange askExchange() {
-//        return new TopicExchange("askExchange");
-//    }
-    TopicExchange askExchange() {
-        return new TopicExchange(Const.EXCHANGE_ASK);
+    TopicExchange askDeviceExchange() {
+        return new TopicExchange(Const.EXCHANGE_ASK_DEVICE);
+    }
+    @Bean
+    TopicExchange askEartagExchange() {
+        return new TopicExchange(Const.EXCHANGE_ASK_EARTAG);
     }
 
+    //绑定关系
+    @Bean
+    Binding bindingDeviceExchangeMessage() {
+        return BindingBuilder.bind(askDeviceQueue()).to(askDeviceExchange()).with(Const.ROUTING_KEY_ASK_DEVICE);
+    }
     @Bean
     Binding bindingEartagExchangeMessage() {
-        return BindingBuilder.bind(askQueue()).to(askExchange()).with(Const.ROUTING_KEY_ASK);
+        return BindingBuilder.bind(askEartagQueue()).to(askEartagExchange()).with(Const.ROUTING_KEY_ASK_EARTAG);
     }
 
 //    //采集器注册队列路由键

+ 59 - 4
huimv-eartag2-platform/huimv-eartag2-input/src/main/java/com/huimv/eartag2/producer/Producer.java

@@ -1,5 +1,6 @@
 package com.huimv.eartag2.producer;
 
+import com.alibaba.fastjson.JSONObject;
 import com.huimv.eartag2.common.mq.Const;
 import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -33,13 +34,67 @@ public class Producer {
      */
     public String sendClientAsk(String askText){
         Map map = new HashMap();
-        System.out.println("000000000000000000 >>"+askText);
+//        System.out.println("000000000000000000 >>"+askText);
         map.put("askText",askText);
-        map.put("test","askText2222222");
-        System.out.println("111111111111111111>>"+map.toString());
+//        System.out.println("111111111111111111>>"+map.toString());
 //        rabbitTemplate.convertAndSend("askExchange", "topic.askText.key", map);
         rabbitTemplate.convertAndSend(Const.EXCHANGE_ASK, Const.ROUTING_KEY_ASK, map);
-        System.out.println("保存原始数据到rabbitMQ>>"+map.toString());
+//        System.out.println("保存原始数据到rabbitMQ>>"+map.toString());
+        return "ok";
+    }
+
+    //处理心跳数据
+    public String sendHeartbeat(JSONObject dataJo) {
+        Map map = new HashMap();
+        map.put("heartbeat",dataJo.toString());
+        rabbitTemplate.convertAndSend(Const.EXCHANGE_DEVICE, Const.ROUTING_KEY_DEVICE_HEARTBEAT, map);
+        System.out.println("处理心跳 >>"+dataJo);
+        return "ok";
+    }
+
+    //处理环境温度数据
+    public String sendDeviceTemp(JSONObject dataJo) {
+        Map map = new HashMap();
+        map.put("deviceTemp",dataJo);
+        rabbitTemplate.convertAndSend(Const.EXCHANGE_DEVICE, Const.ROUTING_KEY_DEVICE_TEMP, map);
+        System.out.println("处理环境温度 >>"+dataJo);
+        return "ok";
+    }
+
+    //处理耳标数据
+    public String sendEartag(JSONObject dataJo) {
+        Map map = new HashMap();
+        map.put("eartag",dataJo);
+        rabbitTemplate.convertAndSend(Const.EXCHANGE_EARTAG, Const.ROUTING_KEY_EARTAG, map);
+        System.out.println("处理耳标数据 >>"+dataJo);
+        return "ok";
+    }
+    
+    /**
+     * @Method      : sendClientAsk2
+     * @Description : 发送耳标数据队列和设备数据队列
+     * @Params      : [askText]
+     * @Return      : void
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2022/3/12       
+     * @Time        : 18:30
+     */
+    public String sendClientAsk2(String askText) {
+        Map map = new HashMap();
+        map.put("askText",askText);
+
+        String[] dataArray = askText.split("\\+");
+        String cmdHeader = dataArray[0];
+        if (cmdHeader.trim().equalsIgnoreCase("hm")) {
+            //发送到设备处理消息队列
+            rabbitTemplate.convertAndSend(Const.EXCHANGE_ASK_DEVICE, Const.ROUTING_KEY_ASK_DEVICE, map);
+            System.out.println("## >> 发送设备处理 ...");
+        } else if (cmdHeader.trim().equalsIgnoreCase("zj")) {
+            //发送到耳标处理消息队列
+            rabbitTemplate.convertAndSend(Const.EXCHANGE_ASK_EARTAG, Const.ROUTING_KEY_ASK_EARTAG, map);
+            System.out.println("## >> 发送耳标处理 ...");
+        }
         return "ok";
     }
 }

+ 2 - 0
huimv-eartag2-platform/huimv-eartag2-input/src/main/java/com/huimv/eartag2/service/IEartagService.java

@@ -3,4 +3,6 @@ package com.huimv.eartag2.service;
 public interface IEartagService {
     //处理耳标信息
     void handleEartagInfo();
+
+    Integer countAct(String deviceCode, String nowAct);
 }

+ 7 - 4
huimv-eartag2-platform/huimv-eartag2-input/src/main/java/com/huimv/eartag2/service/impl/DataServiceImpl.java

@@ -73,10 +73,13 @@ public class DataServiceImpl implements IDataService {
         System.out.println("000拆分粘包之后数据>>"+askText);
         //--对接收的上报数据解析
         String answer = parseAskText2(askText);
-//        int countPlus = regexUtil.countPlus(askText);
-//        System.out.println("############### countPlus>>"+countPlus);
-        //--将原始数据保存到rabbitMQ
-        producer.sendClientAsk(askText);
+
+        //--将原始数据保存到rabbitMQ(重构之前代码)
+//        producer.sendClientAsk(askText);
+
+        //--将原始数据保存到rabbitMQ(重构之后代码)
+        producer.sendClientAsk2(askText);
+
         return answer;
     }
 

+ 68 - 0
huimv-eartag2-platform/huimv-eartag2-input/src/main/java/com/huimv/eartag2/service/impl/EartagServiceImpl.java

@@ -1,10 +1,17 @@
 package com.huimv.eartag2.service.impl;
 
+import com.huimv.eartag2.common.dao.entity.EartagDataEntity;
+import com.huimv.eartag2.common.dao.repo.EartagDataRepo;
 import com.huimv.eartag2.service.IEartagService;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * @Project : huimv.shiwan
  * @Package : com.huimv.biosafety.uface.controller
@@ -14,9 +21,14 @@ import org.springframework.stereotype.Service;
  * @Create : 2020-12-25
  **/
 @Service
+@Slf4j
 public class EartagServiceImpl implements IEartagService {
     @Autowired
     RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
+    @Autowired
+    private RedisTemplate redisTemplate;
+    @Autowired
+    private EartagDataRepo eartagDataRepo;
 
     //处理耳标信息
     @Override
@@ -43,4 +55,60 @@ public class EartagServiceImpl implements IEartagService {
     private void handleDeviceRegister() {
 
     }
+
+    /**
+     * @Method : countAct
+     * @Description : 计算act数值
+     * @Params : [deviceCode, nowAct0]
+     * @Return : java.lang.Integer
+     * @Author : ZhuoNing
+     * @Date : 2022/1/18
+     * @Time : 15:25
+     */
+    @Override
+    public Integer countAct(String deviceCode, String nowAct) {
+        //读取hash
+        Object actObj = redisTemplate.opsForHash().get(deviceCode, "act");
+        if (actObj == null) {
+            //--初始化最新的redis记录
+            initRedisObj(deviceCode);
+            Object lastAct = redisTemplate.opsForHash().get(deviceCode, "act");
+            if (lastAct == null) {
+                log.error("#--- redis数据库有问题,请检查redis是否能正常连接 ---# ");
+                return 0;
+            }
+        }
+        Object lastAct = redisTemplate.opsForHash().get(deviceCode, "act");
+        Integer act1 = Integer.parseInt(nowAct) - Integer.parseInt(lastAct.toString());
+//        redisTemplate.opsForHash().put(deviceCode, "act1", act1);
+        return act1;
+    }
+
+    /**
+     * 初始化redis最新记录
+     */
+    private void initRedisObj(String deviceCode) {
+        EartagDataEntity lastEartagDataEntity = eartagDataRepo.getLastEartagData(deviceCode);
+        if (lastEartagDataEntity != null) {
+            System.out.println("## lastEartagDataEntity.toString>>" + lastEartagDataEntity.toString());
+            //--从数据库读取最新记录并覆盖redis
+            Map<String, String> map = new HashMap<>();
+            map.put("cmdHeader", lastEartagDataEntity.getCmdHeader());
+            map.put("device", lastEartagDataEntity.getDevice());
+            map.put("earmark", lastEartagDataEntity.getEarmark());
+            map.put("bat", lastEartagDataEntity.getBat().toString());
+            map.put("earTemp", lastEartagDataEntity.getEarTemp().toString());
+            map.put("earTemp1", lastEartagDataEntity.getEarTemp1().toString());
+            map.put("envTemp", lastEartagDataEntity.getEnvTemp().toString());
+            map.put("envTemp1", lastEartagDataEntity.getEnvTemp1().toString());
+            map.put("act", lastEartagDataEntity.getAct().toString());
+            map.put("act1", lastEartagDataEntity.getAct1().toString());
+            map.put("signal1", lastEartagDataEntity.getSignal1().toString());
+            map.put("askTime", lastEartagDataEntity.getAskTime());
+            map.put("other", lastEartagDataEntity.getOther());
+            map.put("addTime", lastEartagDataEntity.getAddTime().toString());
+            //为hash结构设置多个键值对(hmset)
+            redisTemplate.opsForHash().putAll(deviceCode, map);
+        }
+    }
 }

+ 7 - 2
huimv-eartag2-platform/huimv-eartag2-input/src/main/resources/application-dev.yml

@@ -35,7 +35,8 @@ spring:
     virtual-host: /
 
     #确认消息已发送到交换机(Exchange)
-    publisher-confirms: true
+#    publisher-confirms: true #(过时、弃用)
+    publisher-confirm-type: correlated
     #确认消息已发送到队列(Queue)
     publisher-returns: true
 
@@ -95,5 +96,9 @@ spring:
 #      ddl-auto: update
 #    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
 #    open-in-view: true
-
+management:
+  endpoints:
+    web:
+      exposure:
+        include: "*"   # * 在yaml 文件属于关键字,所以需要加引号
 

+ 18 - 0
huimv-eartag2-platform/huimv-eartag2-input/src/main/resources/application.properties

@@ -2,3 +2,21 @@ spring.profiles.active=dev
 
 #开启健康监控
 management.endpoints.web.exposure.include=*
+
+###################################################配置undertow取代tomcat
+# 是否打开 undertow 日志,默认为 false
+server.undertow.accesslog.enabled=false
+# 设置访问日志所在目录
+server.undertow.accesslog.dir=logs
+# 指定工作者线程的 I/0 线程数,默认为 2 或者 CPU 的个数
+server.undertow.io-threads=
+# 指定工作者线程个数,默认为 I/O 线程个数的 8 倍
+server.undertow.worker-threads=
+# 设置 HTTP POST 内容的最大长度,默认不做限制
+server.undertow.max-http-post-size=0
+
+#########################################################
+###   Actuator Monitor  --   Actuator configuration   ###
+#########################################################
+management.security.enabled=false
+