ty130316261 3 lat temu
rodzic
commit
7b7d9907a1

+ 2 - 1
zd-modules/zd-netty/src/main/java/com/zd/netty/enums/ManufacturerTypeEnum.java

@@ -1,5 +1,6 @@
 package com.zd.netty.enums;
 
+import com.zd.netty.sdk.DeJuRFIDListenerService;
 import com.zd.netty.sdk.DeJuRFIDService;
 import com.zd.netty.sdk.WuYuanRFIDService;
 import com.zd.netty.service.IService;
@@ -11,7 +12,7 @@ import lombok.Getter;
 public enum ManufacturerTypeEnum {
 
     WU_WEI(1,"无源", WuYuanRFIDService.class),
-    DE_JU(2,"惠州德聚",DeJuRFIDService .class);
+    DE_JU(2,"惠州德聚", DeJuRFIDListenerService.class);
 
     private final Integer code;
     private final String name;

+ 148 - 0
zd-modules/zd-netty/src/main/java/com/zd/netty/sdk/DeJuRFIDListenerService.java

@@ -0,0 +1,148 @@
+package com.zd.netty.sdk;
+
+import com.gg.reader.api.dal.GClient;
+import com.gg.reader.api.dal.GServer;
+import com.gg.reader.api.protocol.gx.MsgAppSetGpo;
+import com.gg.reader.api.protocol.gx.MsgBaseStop;
+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.domain.RemoteLabHardware;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.BeanUtils;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.Map;
+import java.util.TimerTask;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicBoolean;
+
+
+@Slf4j
+@Service
+public class DeJuRFIDListenerService implements IService {
+
+    @Resource
+    private ISendService sendService;
+
+    private final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
+
+    AtomicBoolean isAlarm = new AtomicBoolean();
+
+    static GClient client;
+
+    private final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
+
+    @Override
+    public void start(RemoteLabHardware hardware) {
+        String ipAddress = hardware.getIpAddress();
+        if (clientMap.containsKey(ipAddress)){
+            return;
+        }
+        GServer server = new GServer();
+        if (server.open(9190)) {
+            subscribeServerHandler(server);
+            if (client!=null){
+                clientMap.put(ipAddress,client);
+            }
+            log.info("开始监听");
+        } else {
+            log.info("监听失败");
+        }
+    }
+
+    @Override
+    public void disconnect(RemoteLabHardware hardware) {
+        stop();
+        clientMap.remove(hardware.getIpAddress());
+    }
+
+    //订阅监听上报
+    private void subscribeServerHandler(GServer server) {
+        server.onGClientConnected = (gClient, serialNumber) -> {
+            log.info(gClient.getName() + "---监听成功");
+            client = gClient;//切换连接对象
+            client.setSendHeartBeat(true);//开启心跳检测Tcp连接状态
+            client.setPrint(true);
+            subscribeTcpHandler();//订阅Tcp断连上报
+            subscribeHandler(client);
+            stop();//测试监听成功的连接是否通信正常
+        };
+    }
+
+    //订阅TCP断开连接上报
+    private static void subscribeTcpHandler() {
+        client.onDisconnected = s -> {
+            log.info("连接" + s + "已断开");
+            client.close();//释放当前连接资源
+        };
+    }
+
+    private static void stop() {
+        MsgBaseStop msg = new MsgBaseStop();
+        client.sendSynMsg(msg);
+        if (0x00 == msg.getRtCode()) {
+            log.info("Stop success");
+        } else {
+            log.info(msg.getRtMsg());
+        }
+    }
+
+    /**
+     * 订阅6c标签信息上报
+     *
+     * @param client 客户端
+     */
+    private void subscribeHandler(GClient client) {
+        client.onTagEpcLog = (s, logBaseEpcInfo) -> {
+            if (logBaseEpcInfo.getResult() == 0) {
+                log.info("===========》{}", logBaseEpcInfo.getbEpc());
+                //灯带设置
+                if (!isAlarm.get()) {
+                    scheduledExecutorService.execute(() -> {
+                        isAlarm.set(true);
+                        changeGpo(1, client, 10);
+                    });
+                }
+                InventoryTag tag = new InventoryTag();
+                BeanUtils.copyProperties(logBaseEpcInfo, tag);
+                sendService.send(tag);
+            }
+        };
+        client.onTagEpcOver = (s, logBaseEpcOver) -> log.info("HandlerTagEpcOver");
+    }
+
+    private void changeGpo(int state, GClient client, int delayTime) {
+        MsgAppSetGpo msgAppSetGpo = new MsgAppSetGpo();
+        msgAppSetGpo.setGpo1(state);
+        msgAppSetGpo.setGpo2(state);
+
+        client.sendSynMsg(msgAppSetGpo);
+        String status = state == 1 ? "start" : "stop";
+        if (0 == msgAppSetGpo.getRtCode()) {
+            if (state == 1) {
+                stopGpo(client, delayTime);
+            } else {
+                isAlarm.set(false);
+            }
+        } else {
+            log.error("Gpo epc {} error.", status);
+            if (state == 1) {
+                stopGpo(client, delayTime);
+            }
+        }
+    }
+
+    public void stopGpo(GClient client, int delayTime) {
+        scheduledExecutorService.schedule(new TimerTask() {
+            @Override
+            public void run() {
+                changeGpo(0, client, 0);
+            }
+        }, delayTime, TimeUnit.SECONDS);
+    }
+}