hecheng 3 lat temu
rodzic
commit
4acbb33e91

+ 9 - 0
zd-api/zd-api-system/src/main/java/com/zd/system/api/domain/InventoryTag.java

@@ -3,6 +3,10 @@ package com.zd.system.api.domain;
 import com.zd.system.api.laboratory.domain.RemoteLabHardware;
 import lombok.Data;
 
+/**
+ *
+ * @author Administrator
+ */
 @Data
 public class InventoryTag {
 
@@ -25,5 +29,10 @@ public class InventoryTag {
      */
     private String freq;
 
+    /**
+     * 设备码
+     */
+    private String serialNumber;
+
     private RemoteLabHardware remoteLabHardware;
 }

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

@@ -32,8 +32,8 @@ public class NettyStartListener implements ApplicationRunner {
 
     @Override
     public void run(ApplicationArguments args) {
+        R<List<RemoteLabHardware>> listStart = laboratoryService.listStart();
         synchronized (this){
-            R<List<RemoteLabHardware>> listStart = laboratoryService.listStart();
             while (listStart.getCode() != HttpStatus.SUCCESS) {
                 try {
                     log.error("com.zd.system.api.laboratory.RemoteLaboratoryService:远程接口调用异常");

+ 17 - 6
zd-modules/zd-netty/src/main/java/com/zd/netty/sdk/DeJuRFIDClientImpl.java

@@ -7,10 +7,12 @@ 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.redis.service.RedisService;
 import com.zd.netty.service.IService;
 import com.zd.system.api.laboratory.RemoteLaboratoryService;
 import com.zd.system.api.laboratory.domain.RemoteLabHardware;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
@@ -31,6 +33,9 @@ public class DeJuRFIDClientImpl implements IService {
     @Resource
     private DeJuRFIDServerImpl service;
 
+    @Resource
+    private RedisTemplate<String,RemoteLabHardware> redisTemplate;
+
     @Override
     public void start(RemoteLabHardware hardware) {
         String ipAddress = hardware.getIpAddress();
@@ -45,11 +50,16 @@ public class DeJuRFIDClientImpl implements IService {
             server = new GServer();
             serverMap.put(ipAddress, server);
         }
-        if (server.open(DeJuRFIDServerImpl.PORT)) {
-            log.info("开始监听"+ipAddress);
+        String[] ip = ipAddress.split(":");
+        int port=8160;
+        if (ip.length>1){
+            port= Integer.parseInt(ip[1]);
+        }
+        if (server.open(port)) {
+            log.info("开始监听:"+ipAddress);
             subscribeServerHandler(server, hardware);
         } else {
-            log.info("监听失败"+ipAddress);
+            log.info("监听失败:"+ipAddress);
             throw new ServiceException("监听失败,请稍后进行设备重连。。。");
         }
     }
@@ -59,7 +69,7 @@ public class DeJuRFIDClientImpl implements IService {
         String ipAddress = hardware.getIpAddress();
         if (serverMap.containsKey(ipAddress)) {
             GServer server = serverMap.get(ipAddress);
-            DeJuRFIDServerImpl.close(hardware);
+            service.close(hardware);
             server.close();
             log.info(ipAddress + "---监听结束");
         }
@@ -77,13 +87,14 @@ public class DeJuRFIDClientImpl implements IService {
      * @param server   服务对象
      * @param hardware 设备对象
      */
-    private void subscribeServerHandler(GServer server, RemoteLabHardware hardware) {
+    public void subscribeServerHandler(GServer server, RemoteLabHardware hardware) {
         server.onGClientConnected = (client, serialNumber) -> {
             client.setSendHeartBeat(true);//开启心跳检测Tcp连接状态
             client.setPrint(true);
             subscribeTcpHandler(client, hardware);//订阅Tcp断连上报
             reSet(hardware, client);
-            DeJuRFIDServerImpl.subscribeHandler(client, hardware);
+            redisTemplate.opsForValue().set(client.getSerialNumber(),hardware);
+            service.subscribeHandler(client, hardware);
             remoteLaboratoryService.update(HardwareOperate.OPEN, hardware.getIpAddress());
             log.info(client.getName() + "---监听成功");
         };

+ 44 - 47
zd-modules/zd-netty/src/main/java/com/zd/netty/sdk/DeJuRFIDServerImpl.java

@@ -10,9 +10,11 @@ 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.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
+import javax.annotation.Resource;
 import java.util.*;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ScheduledExecutorService;
@@ -28,9 +30,9 @@ public class DeJuRFIDServerImpl implements IService {
 
     private static final ISendService sendService = SpringUtils.getBean("sendService");
 
-    private static final Map<String, GClient> CLIENT_MAP = new ConcurrentHashMap<>();
+    private final Map<String, GClient> clientMap = new ConcurrentHashMap<>();
 
-    private static final Map<String, Boolean> ALARM_MAP = new ConcurrentHashMap<>();
+    private final Map<String, Boolean> alarmMap = new ConcurrentHashMap<>();
     static AtomicInteger stopCount = new AtomicInteger();
 
     public static final Integer PORT = 8160;
@@ -42,26 +44,29 @@ public class DeJuRFIDServerImpl implements IService {
 
     private static final ScheduledExecutorService scheduledExecutorService = SpringUtils.getBean("scheduledExecutorService");
 
+    @Resource
+    private RedisTemplate<String, RemoteLabHardware> redisTemplate;
+
     @Override
     public void start(RemoteLabHardware hardware) {
         open(hardware);
     }
 
-    private static void open(RemoteLabHardware hardware) {
+    private void open(RemoteLabHardware hardware) {
         GClient client;
         String ipAddress = hardware.getIpAddress();
-        if (CLIENT_MAP.containsKey(ipAddress)) {
-            client = CLIENT_MAP.get(ipAddress);
+        if (clientMap.containsKey(ipAddress)) {
+            client = clientMap.get(ipAddress);
         } else {
             client = new GClient();
-            CLIENT_MAP.put(ipAddress, client);
+            clientMap.put(ipAddress, client);
         }
         if (client.openTcp(ipAddress + HTTP_SEPARATOR + PORT, TIME_OUT)) {
             stopMsg(client);
             // 功率配置, 将4个天线功率都设置为30dBm.
             MsgBaseSetPower msgBaseSetPower = setPower(hardware, client);
             if (0 != msgBaseSetPower.getRtCode()) {
-                log.error("Power configuration error.");
+                log.info("Power configuration error.");
                 close(hardware);
                 open(hardware);
                 return;
@@ -69,7 +74,7 @@ public class DeJuRFIDServerImpl implements IService {
             //天线读卡, 读取EPC数据区以及TID数据区
             MsgBaseInventoryEpc msgBaseInventoryEpc = setInventory(hardware, client);
             if (0 != msgBaseInventoryEpc.getRtCode()) {
-                log.error("Inventory epc error.");
+                log.info("Inventory epc error.");
                 close(hardware);
                 open(hardware);
                 return;
@@ -77,13 +82,14 @@ public class DeJuRFIDServerImpl implements IService {
 
             MsgBaseSetBaseband msgBaseSetBaseband = setSession(hardware, client);
             if (0 != msgBaseSetBaseband.getRtCode()) {
-                log.error("Session epc error.");
+                log.info("Session epc error.");
                 close(hardware);
                 open(hardware);
                 return;
             }
             // 订阅标签上报事件
             subscribeHandler(client, hardware);
+            redisTemplate.opsForValue().set(client.getSerialNumber(), hardware);
         } else {
             throw new ServiceException("Connect failure.");
         }
@@ -179,14 +185,6 @@ public class DeJuRFIDServerImpl implements IService {
         return msgBaseSetBaseband;
     }
 
-    public static void reset(GClient client) {
-        MsgAppReset msgAppReset = new MsgAppReset();
-        client.sendSynMsg(msgAppReset);
-        if (0 != msgAppReset.getRtCode()) {
-            log.error("Reset epc error.");
-        }
-    }
-
     @Override
     public void disconnect(RemoteLabHardware hardware) {
         close(hardware);
@@ -196,28 +194,28 @@ public class DeJuRFIDServerImpl implements IService {
     public void alarm(RemoteLabHardware hardware) {
         //灯带设置
         String ipAddress = hardware.getIpAddress();
-        if (CLIENT_MAP.containsKey(ipAddress)) {
-            GClient client = CLIENT_MAP.get(ipAddress);
-            alarm(hardware,client);
-        }
-    }
-
-    public static void alarm(RemoteLabHardware hardware, GClient client) {
-        String ipAddress = hardware.getIpAddress();
-        if (!ALARM_MAP.containsKey(ipAddress)) {
-            log.info("报警状态:==============》{},设备IP:{}", false, ipAddress);
-            changeGpo(ipAddress,1, client, 10);
+        if (clientMap.containsKey(ipAddress)) {
+            GClient client = clientMap.get(ipAddress);
+            boolean alarm = false;
+            if (alarmMap.containsKey(ipAddress)) {
+                alarm = alarmMap.get(ipAddress);
+            }
+            log.info("报警状态:==============》{},设备IP:{}", alarm, ipAddress);
+            if (!alarm) {
+                alarmMap.putIfAbsent(ipAddress,true);
+                changeGpo(ipAddress, 1, client, 10);
+            }
         }
     }
 
-    public static void close(RemoteLabHardware hardware) {
+    public void close(RemoteLabHardware hardware) {
         String ipAddress = hardware.getIpAddress();
-        if (CLIENT_MAP.containsKey(ipAddress)) {
-            GClient client = CLIENT_MAP.get(ipAddress);
+        if (clientMap.containsKey(ipAddress)) {
+            GClient client = clientMap.get(ipAddress);
             changeGpo(ipAddress, 0, client, 0);
             stopMsg(client);
             client.close();
-            CLIENT_MAP.remove(ipAddress);
+            clientMap.remove(ipAddress);
         }
     }
 
@@ -229,7 +227,7 @@ public class DeJuRFIDServerImpl implements IService {
         log.info("客户端停止读卡{}", result);
     }
 
-    private static void changeGpo(String ipAddress, int state, GClient client, int delayTime) {
+    private void changeGpo(String ipAddress, int state, GClient client, int delayTime) {
         MsgAppSetGpo msgAppSetGpo = new MsgAppSetGpo();
         msgAppSetGpo.setGpo1(state);
         msgAppSetGpo.setGpo2(state);
@@ -240,20 +238,19 @@ public class DeJuRFIDServerImpl implements IService {
         if (0 == msgAppSetGpo.getRtCode()) {
             log.info("============》灯带{}成功.", status);
             if (state == 1) {
-                ALARM_MAP.put(ipAddress,true);
-                stopGpo(ipAddress,client, delayTime);
-            }else {
-                ALARM_MAP.remove(ipAddress);
+                stopGpo(ipAddress, client, delayTime);
+            } else {
+                alarmMap.remove(ipAddress);
             }
         } else {
-            log.error("Gpo epc {} error.", status);
+            log.info("Gpo epc {} error.", status);
             if (stopCount.getAndIncrement() < MAX_RETRY) {
-                stopGpo(ipAddress,client, delayTime);
+                stopGpo(ipAddress, client, delayTime);
             }
         }
     }
 
-    public static void stopGpo(String ipAddress,GClient client, int delayTime) {
+    public void stopGpo(String ipAddress, GClient client, int delayTime) {
         scheduledExecutorService.schedule(new TimerTask() {
             @Override
             public void run() {
@@ -268,19 +265,19 @@ public class DeJuRFIDServerImpl implements IService {
      * @param client   客户端
      * @param hardware 设备数据
      */
-    public static void subscribeHandler(GClient client, RemoteLabHardware hardware) {
+    public void subscribeHandler(GClient client, RemoteLabHardware hardware) {
         String ipAddress = hardware.getIpAddress();
-        CLIENT_MAP.computeIfAbsent(ipAddress, f -> client);
+        clientMap.computeIfAbsent(ipAddress, f -> client);
         client.onTagEpcLog = (s, logBaseEpcInfo) -> {
             if (logBaseEpcInfo.getResult() == 0) {
                 log.info("===========》{},index: {}", logBaseEpcInfo.getEpc(), logBaseEpcInfo.getAntId());
                 InventoryTag tag = new InventoryTag();
                 BeanUtils.copyProperties(logBaseEpcInfo, tag);
-                tag.setRemoteLabHardware(hardware);
-                boolean send = sendService.send(tag);
-                if (send){
-                    alarm(hardware,client);
-                }
+                String readerSerialNumber = logBaseEpcInfo.getReaderSerialNumber();
+                Long replySerialNumber = logBaseEpcInfo.getReplySerialNumber();
+                log.info("===========》读取器设备编码:{},replySerialNumber: {}", readerSerialNumber, replySerialNumber);
+                tag.setSerialNumber(client.getSerialNumber());
+                sendService.send(tag);
             }
         };
         client.onTagEpcOver = (s, logBaseEpcOver) -> log.info("HandlerTagEpcOver");

+ 8 - 5
zd-modules/zd-netty/src/main/java/com/zd/netty/service/impl/FridServiceImpl.java

@@ -31,11 +31,14 @@ public class FridServiceImpl implements IFridService {
 
     @Override
     public void start(RemoteLabHardware hardware) {
-        taskExecutor.execute(() -> {
-            rfidService(hardware.getManufacturerType()).start(hardware);
-            log.info("===================thread name:{}===============", Thread.currentThread().getName());
-            threadMap.computeIfAbsent(hardware.getIpAddress(), f -> Thread.currentThread());
-        });
+        synchronized (this){
+            taskExecutor.execute(() -> {
+                rfidService(hardware.getManufacturerType()).start(hardware);
+                log.info("===================thread name:{}===============", Thread.currentThread().getName());
+                threadMap.computeIfAbsent(hardware.getIpAddress(), f -> Thread.currentThread());
+                log.info("==============>线程数量:{}",threadMap.size());
+            });
+        }
     }
 
     @Override

+ 28 - 10
zd-modules/zd-netty/src/main/java/com/zd/netty/service/impl/SendServiceImpl.java

@@ -1,15 +1,16 @@
 package com.zd.netty.service.impl;
 
-import com.gg.reader.api.dal.GClient;
 import com.zd.bottle.feign.service.RemoteBottleService;
 import com.zd.common.core.constant.HttpStatus;
 import com.zd.common.core.domain.R;
+import com.zd.netty.service.IFridService;
 import com.zd.netty.service.ISendService;
 import com.zd.system.api.airbottle.RemoteAirBottleService;
 import com.zd.system.api.chemical.RemoteStockService;
 import com.zd.system.api.domain.InventoryTag;
 import com.zd.system.api.laboratory.domain.RemoteLabHardware;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Service;
 import org.springframework.util.StringUtils;
 
@@ -32,19 +33,33 @@ public class SendServiceImpl implements ISendService {
     @Resource
     private RemoteBottleService remoteBottleService;
 
+    @Resource
+    private RedisTemplate<String,RemoteLabHardware> redisTemplate;
+
+    @Resource
+    private IFridService fridService;
+
 
     @Override
     public boolean send(InventoryTag tag) {
-
-        RemoteLabHardware hardware = tag.getRemoteLabHardware();
-        if (hardware != null) {
-            int antId = tag.getAntId();
-            String uniformPower = hardware.getUniformPower();
-            String[] uniformPowers = uniformPower.split(",");
-            return handleTag(tag, antId, uniformPowers);
-        } else {
-            return sendTag(tag,null);
+        String serialNumber = tag.getSerialNumber();
+        if (StringUtils.hasLength(serialNumber)){
+            log.info("===============》设备编码:{}",serialNumber);
+            RemoteLabHardware hardware = redisTemplate.opsForValue().get(serialNumber);
+            if (hardware!=null){
+                log.info("=========ip:{},设备编码:{}=========",hardware.getIpAddress(),serialNumber);
+                int antId = tag.getAntId();
+                String uniformPower = hardware.getUniformPower();
+                String[] uniformPowers = uniformPower.split(",");
+                boolean b = handleTag(tag, antId, uniformPowers);
+                if (b){
+                    fridService.alarm(hardware);
+                }
+                return b;
+            }
+            log.info("=========未获取到设备信息=========");
         }
+        return sendTag(tag,null);
     }
 
     private boolean handleTag(InventoryTag tag, int antId, String[] uniformPowers) {
@@ -91,6 +106,7 @@ public class SendServiceImpl implements ISendService {
     private boolean sendStock(InventoryTag tag) {
         try {
             R<Boolean> result = remoteStockService.rfidCheck(tag);
+            log.info("化学品服务调用:" + result.getData());
             if (result.getCode() == HttpStatus.SUCCESS) {
                 return result.getData();
             }
@@ -104,6 +120,7 @@ public class SendServiceImpl implements ISendService {
         R<Boolean> result;
         try {
             result = bottleService.remoteAdd(tag);
+            log.info("气瓶服务调用:" + result.getData());
             if (result.getCode() == HttpStatus.SUCCESS) {
                return result.getData();
             }
@@ -112,6 +129,7 @@ public class SendServiceImpl implements ISendService {
         }
         try {
             result = remoteBottleService.remoteAdd(tag);
+            log.info("气瓶服务调用:" + result.getData());
             if (result.getCode() == HttpStatus.SUCCESS) {
                 return result.getData();
             }