hecheng преди 3 години
родител
ревизия
dda17e4f38

+ 1 - 1
zd-modules/zd-netty/src/main/java/com/zd/netty/ZdNettyApplication.java

@@ -17,6 +17,6 @@ import org.springframework.scheduling.annotation.EnableScheduling;
 public class ZdNettyApplication {
     public static void main(String[] args) {
         SpringApplication.run(ZdNettyApplication.class, args);
-        log.info("(♥◠‿◠)ノ゙  消息模块启动成功   ლ(´ڡ`ლ)゙");
+        log.info("(♥◠‿◠)ノ゙  netty模块启动成功   ლ(´ڡ`ლ)゙");
     }
 }

+ 7 - 2
zd-modules/zd-netty/src/main/java/com/zd/netty/sdk/DeJuRFIDClientImpl.java

@@ -7,6 +7,7 @@ import com.gg.reader.api.protocol.gx.MsgBaseSetBaseband;
 import com.gg.reader.api.protocol.gx.MsgBaseSetPower;
 import com.zd.common.core.enums.HardwareOperate;
 import com.zd.common.core.exception.ServiceException;
+import com.zd.common.core.utils.SpringUtils;
 import com.zd.common.redis.service.RedisService;
 import com.zd.netty.service.IService;
 import com.zd.system.api.laboratory.RemoteLaboratoryService;
@@ -18,6 +19,8 @@ import org.springframework.stereotype.Service;
 import javax.annotation.Resource;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 
 /**
@@ -36,6 +39,8 @@ public class DeJuRFIDClientImpl implements IService {
     @Resource
     private RedisTemplate<String,RemoteLabHardware> redisTemplate;
 
+    private static final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
+
     @Override
     public void start(RemoteLabHardware hardware) {
         String ipAddress = hardware.getIpAddress();
@@ -71,9 +76,9 @@ public class DeJuRFIDClientImpl implements IService {
             GServer server = serverMap.get(ipAddress);
             service.close(hardware);
             server.close();
+            serverMap.remove(ipAddress);
             log.info(ipAddress + "---监听结束");
         }
-        serverMap.remove(ipAddress);
     }
 
     @Override
@@ -138,7 +143,7 @@ public class DeJuRFIDClientImpl implements IService {
                 disconnect(labHardware);
                 remoteLaboratoryService.update(HardwareOperate.CLOSE, labHardware.getIpAddress());
                 log.info("连接" + s + "重连中。。。");
-                start(labHardware);
+                scheduledExecutorService.schedule(()-> start(labHardware),30, TimeUnit.SECONDS);
             }
         };
     }

+ 26 - 2
zd-modules/zd-netty/src/main/java/com/zd/netty/sdk/DeJuRFIDServerImpl.java

@@ -2,11 +2,13 @@ package com.zd.netty.sdk;
 
 import com.gg.reader.api.dal.GClient;
 import com.gg.reader.api.protocol.gx.*;
+import com.zd.common.core.enums.HardwareOperate;
 import com.zd.common.core.exception.ServiceException;
 import com.zd.common.core.utils.SpringUtils;
 import com.zd.netty.service.ISendService;
 import com.zd.netty.service.IService;
 import com.zd.system.api.domain.InventoryTag;
+import com.zd.system.api.laboratory.RemoteLaboratoryService;
 import com.zd.system.api.laboratory.domain.RemoteLabHardware;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
@@ -44,6 +46,9 @@ public class DeJuRFIDServerImpl implements IService {
     @Resource
     private RedisTemplate<String, RemoteLabHardware> redisTemplate;
 
+    @Resource
+    private RemoteLaboratoryService remoteLaboratoryService;
+
     @Override
     public void start(RemoteLabHardware hardware) {
         open(hardware);
@@ -77,6 +82,8 @@ public class DeJuRFIDServerImpl implements IService {
                 return;
             }
 
+            // 订阅标签上报事件
+            subscribeHandler(client, hardware);
             MsgBaseSetBaseband msgBaseSetBaseband = setSession(hardware, client);
             if (0 != msgBaseSetBaseband.getRtCode()) {
                 log.info("Session epc error.");
@@ -84,14 +91,30 @@ public class DeJuRFIDServerImpl implements IService {
                 open(hardware);
                 return;
             }
-            // 订阅标签上报事件
-            subscribeHandler(client, hardware);
             redisTemplate.opsForValue().set(client.getSerialNumber(), hardware);
         } else {
             throw new ServiceException("Connect failure.");
         }
     }
 
+    /**
+     * 订阅TCP断开连接上报
+     *
+     * @param client   客户端对象
+     */
+    public void subscribeTcpHandler(GClient client) {
+        RemoteLabHardware labHardware = redisTemplate.opsForValue().get(client.getSerialNumber());
+        client.onDisconnected = s -> {
+            log.info("连接" + s + "已断开");
+            if (labHardware!=null){
+                client.close();
+                remoteLaboratoryService.update(HardwareOperate.CLOSE, labHardware.getIpAddress());
+                log.info("连接" + s + "重连中。。。");
+                start(labHardware);
+            }
+        };
+    }
+
     public static MsgBaseInventoryEpc setInventory(RemoteLabHardware hardware, GClient client) {
         MsgBaseInventoryEpc msgBaseInventoryEpc = new MsgBaseInventoryEpc();
         switch (hardware.getChannels()) {
@@ -214,6 +237,7 @@ public class DeJuRFIDServerImpl implements IService {
             GClient client = clientMap.get(ipAddress);
             changeGpo(ipAddress, 0, client, 0);
             stopMsg(client);
+            log.error("client.close()");
             client.close();
             clientMap.remove(ipAddress);
         }