liubo лет назад: 2
Родитель
Сommit
6fa6955f92

+ 5 - 0
zd-modules/zd-base/pom.xml

@@ -24,6 +24,11 @@
             <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+        </dependency>
+
         <!-- SpringCloud Alibaba Sentinel -->
         <!--<dependency>
             <groupId>com.alibaba.cloud</groupId>

+ 2 - 2
zd-modules/zd-base/src/main/java/com/zd/base/BaseApplicaion.java

@@ -15,8 +15,8 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 @SpringBootApplication
 @EnableScheduling
 @ComponentScan(basePackages = BaseConstants.BASE_PACKAGE)
-public class BaseApplicaion {
+public class ZdBaseApplicaion {
     public static void main(String[] args) {
-        ZdStartApplication.run(ApplicationConstants.BASE_SERVICE, BaseApplicaion.class, args);
+        ZdStartApplication.run(ApplicationConstants.BASE_SERVICE, ZdBaseApplicaion.class, args);
     }
 }

+ 2 - 0
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/mapper/LabHardwareMapper.java

@@ -58,6 +58,8 @@ public interface LabHardwareMapper {
      */
     List<LabHardware> selectLabHardwareStatusByRelay(LabHardware labHardware);
 
+    List<LabHardwareVO> selectHardwareAndSubInfoByRelay(LabHardware labHardware);
+
     /**
      * 查询硬件列表
      *

+ 63 - 4
zd-modules/zd-modules-laboratory/src/main/java/com/zd/laboratory/netty/NettyServerHandler.java

@@ -1,8 +1,15 @@
 package com.zd.laboratory.netty;
 
+import com.zd.common.core.alert.DingTalkAlert;
 import com.zd.common.core.redis.RedisService;
+import com.zd.common.core.utils.DateUtils;
 import com.zd.common.core.utils.ReUtil;
 import com.zd.common.core.utils.SpringUtils;
+import com.zd.laboratory.domain.LabAbnormal;
+import com.zd.laboratory.domain.LabHardware;
+import com.zd.laboratory.domain.vo.LabHardwareVO;
+import com.zd.laboratory.mapper.LabAbnormalMapper;
+import com.zd.laboratory.mapper.LabHardwareMapper;
 import com.zd.laboratory.socket.runner.TCPServer;
 import com.zd.laboratory.utils.CRCCHECK;
 import io.netty.channel.ChannelHandlerContext;
@@ -10,6 +17,10 @@ import io.netty.channel.ChannelInboundHandler;
 import lombok.extern.slf4j.Slf4j;
 
 import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -17,7 +28,7 @@ import java.util.concurrent.TimeUnit;
  */
 @Slf4j
 public class NettyServerHandler implements ChannelInboundHandler {
-
+    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
     /**
     * 读取客户端发送的消息
     */
@@ -146,11 +157,59 @@ public class NettyServerHandler implements ChannelInboundHandler {
     @Override
     public void handlerRemoved(ChannelHandlerContext channelHandlerContext) throws Exception {
         log.info("netty客户端断开连接" + channelHandlerContext.toString());
-        ChannelMap.getChannelMap().entrySet().forEach(a -> {
-            if(a.getValue() == channelHandlerContext.channel()){
+
+        for (Map.Entry<String, ChannelHandlerContext> a : ChannelMap.getChannelMap().entrySet()) {
+
+            if(a.getValue() == channelHandlerContext){
                 ChannelMap.getChannelMap().remove(a.getKey());
+                log.info("netty客户端断开连接清除key: " + a.getKey());
+
+                executorService.schedule(new Runnable() {
+                    @Override
+                    public void run() {
+
+                        String relayCode = a.getKey();
+
+                        if(ChannelMap.getChannelMap().get(relayCode) != null){
+                            return;
+                        }
+
+                        LabHardwareMapper labHardwareMapper = SpringUtils.getBean(LabHardwareMapper.class);
+
+                        LabHardware labHardware = new LabHardware();
+                        labHardware.setRelayCode(relayCode);
+
+                        List<LabHardwareVO> list = labHardwareMapper.selectHardwareAndSubInfoByRelay(labHardware);
+
+                        if(list.size() == 0){
+                            return;
+                        }
+
+                        LabAbnormalMapper labAbnormalMapper = SpringUtils.getBean(LabAbnormalMapper.class);
+                        for (LabHardwareVO hardwareVO : list) {
+                            LabAbnormal labAbnormal = new LabAbnormal();
+                            labAbnormal.setName(hardwareVO.getName());
+                            labAbnormal.setType(1);
+                            labAbnormal.setAbnormalReason("netty断开链接超过三分钟");
+                            labAbnormal.setHardwareId(hardwareVO.getId());
+                            labAbnormal.setDeptId(hardwareVO.getDeptId());
+                            labAbnormal.setDeptName(hardwareVO.getDeptName());
+                            labAbnormal.setCreateBy("系统");
+                            labAbnormal.setCreateTime(DateUtils.getNowDate());
+                            labAbnormalMapper.insertLabAbnormal(labAbnormal);
+
+
+                            String content = hardwareVO.getDeptName() + hardwareVO.getSubjectName() + hardwareVO.getName()
+                                    + "netty连接已掉线三分钟未连接!请确认处理!";
+                            DingTalkAlert.sendAlert(content);
+                        }
+
+                    }
+                }, 180, TimeUnit.SECONDS);
+
+                break;
             }
-        });
+        }
     }
 
 

+ 20 - 0
zd-modules/zd-modules-laboratory/src/main/resources/mapper/laboratory/LabHardwareMapper.xml

@@ -133,6 +133,26 @@
         </where>
     </select>
 
+    <select id="selectHardwareAndSubInfoByRelay" parameterType="com.zd.laboratory.domain.LabHardware"
+            resultType="com.zd.laboratory.domain.vo.LabHardwareVO">
+        select
+        h.id,
+        h.name,
+        h.subject_id,
+        s.name as subjectName,
+        h.hardware_num,
+        s.dept_id,
+        d.dept_name,
+        h.relay_code
+        from
+        lab_hardware h left join lab_subject s on h.subject_id = s.id
+        left join sys_dept d on s.dept_id = d.dept_id
+        <where>
+            <if test="relayCode != null ">and relay_code = #{relayCode}</if>
+            <if test="hardwareNum != null">and hardware_num = #{hardwareNum}</if>
+        </where>
+    </select>
+
     <select id="selectLabHardwareList" parameterType="com.zd.laboratory.domain.LabHardware"
             resultMap="LabHardwareVOResult">
         <include refid="selectLabHardwareVo"/>