Selaa lähdekoodia

创建路由器、队列

home 2 vuotta sitten
vanhempi
commit
3366038251

+ 0 - 4
src/main/java/com/fjhx/rfid/r2000/tcp/callback/TcpCallBackAllData.java

@@ -2,10 +2,8 @@ package com.fjhx.rfid.r2000.tcp.callback;
 
 import com.alibaba.fastjson.JSONObject;
 import com.fjhx.config.RFIDConfig;
-import com.fjhx.rabbitmq.enums.MsgSourceEnum;
 import com.fjhx.rabbitmq.service.RabbitmqService;
 import com.fjhx.redis.RedisCache;
-import com.fjhx.utils.id.IdUtils;
 import com.fjhx.utils.spring.SpringUtils;
 import com.rfid.callBack.CallBack.R2000;
 import lombok.Data;
@@ -14,10 +12,8 @@ import org.apache.commons.lang3.StringUtils;
 import java.io.File;
 import java.io.FileWriter;
 import java.text.SimpleDateFormat;
-import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
-import java.util.concurrent.ConcurrentHashMap;
 
 @Data
 public class TcpCallBackAllData implements R2000 {

+ 0 - 9
src/main/java/com/fjhx/rfid/r2000/tcp/service/TcpService.java

@@ -14,25 +14,16 @@ public interface TcpService {
 
     /**
      * 启动单台
-     *
-     * @param client
-     * @return
      */
     public AjaxResult startOne(RFIDClient client);
 
     /**
      * 关闭单台
-     *
-     * @param client
-     * @return
      */
     public AjaxResult closeOne(RFIDClient client);
 
     /**
      * 启动多台
-     *
-     * @param client
-     * @return
      */
     AjaxResult openMore(RFIDClient client);
 }

+ 10 - 28
src/main/java/com/fjhx/rfid/r2000/tcp/service/impl/TcpServiceImpl.java

@@ -26,7 +26,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
-import java.util.stream.Collectors;
 
 /**
  * @Description:
@@ -44,13 +43,10 @@ public class TcpServiceImpl implements TcpService {
     /**
      * 初始化实例集合
      */
-    private static Map<String, Map<String, Object>> serverMap = new HashMap<>();
+    private static final Map<String, Map<String, Object>> serverMap = new HashMap<>();
 
     /**
      * 启动单台
-     *
-     * @param client
-     * @return
      */
     public AjaxResult startOne(RFIDClient client) {
         if (client == null || StringUtils.isBlank(client.getIp())) {
@@ -92,16 +88,13 @@ public class TcpServiceImpl implements TcpService {
 
     /**
      * 关闭单台
-     *
-     * @param client
-     * @return
      */
     public AjaxResult closeOne(RFIDClient client) {
         if (client == null || StringUtils.isBlank(client.getIp())) {
             throw new BaseException("读写器IP地址不能为空!");
         }
 
-        Map<String, JSONObject> data = new HashMap<>();
+        Map<String, JSONObject> data;
         List<JSONObject> list = new ArrayList<>();
         ReaderR2000Service service;
         ReaderR2000 reader;
@@ -131,7 +124,7 @@ public class TcpServiceImpl implements TcpService {
             redisCache.deleteObject(dataRedisKey);
 
             if (MapUtils.isNotEmpty(data)) {
-                list = data.values().stream().collect(Collectors.toList());
+                list = new ArrayList<>(data.values());
             }
         } catch (Exception e) {
             e.printStackTrace();
@@ -142,9 +135,6 @@ public class TcpServiceImpl implements TcpService {
 
     /**
      * 启动多台
-     *
-     * @param client
-     * @return
      */
     @Override
     public AjaxResult openMore(RFIDClient client) {
@@ -152,7 +142,7 @@ public class TcpServiceImpl implements TcpService {
         ConcurrentHashMap<String, JSONObject> dataMap = new ConcurrentHashMap<>();
         List<JSONObject> list = new ArrayList<>();
 
-        Integer count = 0;
+        int count = 0;
         try {
             if (client == null || CollectionUtils.isEmpty(client.getIpList())) {
                 throw new BaseException("读写器IP地址不能为空!");
@@ -161,8 +151,7 @@ public class TcpServiceImpl implements TcpService {
             // 启动扫描线程
             for (String ip : client.getIpList()) {
                 count++;
-                Integer finalCount = count;
-                CountDownLatch errCountDownLatch = countDownLatch;
+                int finalCount = count;
                 ThreadPoolManager.execute(() -> {
                     try {
                         // redis tcp数据缓存key
@@ -179,9 +168,7 @@ public class TcpServiceImpl implements TcpService {
                         service.beginInvV2(reader);
                     } catch (Exception e) {
                         e.printStackTrace();
-                        if (errCountDownLatch != null) {
-                            errCountDownLatch.countDown();
-                        }
+                        countDownLatch.countDown();
                         throw new BaseException(RFIDConstant.READ_DATA_ERR);
                     }
                 });
@@ -194,8 +181,7 @@ public class TcpServiceImpl implements TcpService {
             // 关闭线程
             for (String ip : client.getIpList()) {
                 count++;
-                Integer finalCount = count;
-                CountDownLatch finalCountDownLatch = countDownLatch;
+                int finalCount = count;
                 try {
                     // redis tcp数据缓存key
                     String dataRedisKey = MessageFormat.format(RedisKeyConstant.RFID_TCP_DATA_REDIS_CACHE_PREFIX, ip + "-" + finalCount);
@@ -228,20 +214,16 @@ public class TcpServiceImpl implements TcpService {
                 } catch (Exception e) {
                     e.printStackTrace();
                 } finally {
-                    if (finalCountDownLatch != null) {
-                        finalCountDownLatch.countDown();
-                    }
+                    countDownLatch.countDown();
                 }
             }
 //            System.err.println("最终总结果数量--:" + dataMap.size());
 
             if (MapUtils.isNotEmpty(dataMap)) {
-                list = dataMap.values().stream().collect(Collectors.toList());
+                list = new ArrayList<>(dataMap.values());
             }
 
-            if (countDownLatch != null) {
-                countDownLatch.await();
-            }
+            countDownLatch.await();
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 15 - 34
src/main/java/com/fjhx/rfid/r2000/tcp/service/impl/TcpServiceImplV2.java

@@ -28,7 +28,6 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.CountDownLatch;
-import java.util.stream.Collectors;
 
 /**
  * @Description:
@@ -49,7 +48,7 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
     /**
      * 初始化实例集合
      */
-    private static ConcurrentHashMap<String, ConcurrentHashMap<String, Object>> serverMap = new ConcurrentHashMap<>();
+    private static final ConcurrentHashMap<String, ConcurrentHashMap<String, Object>> serverMap = new ConcurrentHashMap<>();
 
     /**
      * 启动并返回所有数据
@@ -61,15 +60,14 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
      */
     @Override
     public AjaxResult openAndReturnAllData(List<String> ips, String busUuid, String userId) {
-        Integer count = 0;
+        int count = 0;
         try {
             CountDownLatch countDownLatch = new CountDownLatch(ips.size());
             System.err.println(serverMap);
             // 启动扫描线程
             for (String ip : ips) {
                 count++;
-                Integer finalCount = count;
-                CountDownLatch errCountDownLatch = countDownLatch;
+                int finalCount = count;
                 List<JSONObject> list = new ArrayList<>();
                 ThreadPoolManager.execute(() -> {
                     try {
@@ -106,17 +104,13 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
 
                     } catch (Exception e) {
                         e.printStackTrace();
-                        if (errCountDownLatch != null) {
-                            errCountDownLatch.countDown();
-                        }
+                        countDownLatch.countDown();
                         throw new BaseException(MessageFormat.format(RFIDConstant.SERVER_START_ERR, ip));
                     }
                 });
             }
 
-            if (countDownLatch != null) {
-                countDownLatch.await();
-            }
+            countDownLatch.await();
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -143,15 +137,14 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
 //        rabbitmqService.send(testData);
 
 
-        Integer count = 0;
+        int count = 0;
         try {
             CountDownLatch countDownLatch = new CountDownLatch(ips.size());
             System.err.println(serverMap);
             // 启动扫描线程
             for (String ip : ips) {
                 count++;
-                Integer finalCount = count;
-                CountDownLatch errCountDownLatch = countDownLatch;
+                int finalCount = count;
                 ConcurrentHashMap<String, JSONObject> map = new ConcurrentHashMap<>();
                 ThreadPoolManager.execute(() -> {
                     try {
@@ -177,7 +170,7 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
                             jsonObject.put("messageId", IdUtils.fastSimpleUUID());
                             jsonObject.put("msgSource", MsgSourceEnum.MSG_SOURCE_2.getKey());
                             jsonObject.put("userId", userId);
-                            jsonObject.put("notices", map.values().stream().collect(Collectors.toList()));
+                            jsonObject.put("notices", new ArrayList<>(map.values()));
                             rabbitmqService.send(jsonObject);
                             System.err.println("jsonObject--:" + jsonObject.toJSONString());
 
@@ -186,17 +179,13 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
 
                     } catch (Exception e) {
                         e.printStackTrace();
-                        if (errCountDownLatch != null) {
-                            errCountDownLatch.countDown();
-                        }
+                        countDownLatch.countDown();
                         throw new BaseException(MessageFormat.format(RFIDConstant.SERVER_START_ERR, ip));
                     }
                 });
             }
 
-            if (countDownLatch != null) {
-                countDownLatch.await();
-            }
+            countDownLatch.await();
         } catch (Exception e) {
             e.printStackTrace();
         }
@@ -206,16 +195,13 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
 
     /**
      * 关闭多台
-     *
-     * @param client
-     * @return
      */
     @Override
     public AjaxResult closeMore(RFIDClient client) {
         ConcurrentHashMap<String, JSONObject> dataMap = new ConcurrentHashMap<>();
         List<JSONObject> list = new ArrayList<>();
 
-        Integer count = 0;
+        int count = 0;
         List<String> ips = RFIDConfig.getTcp().getIps();
         try {
             if (client == null) {
@@ -228,8 +214,7 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
             // 关闭线程
             for (String ip : ips) {
                 count++;
-                Integer finalCount = count;
-                CountDownLatch finalCountDownLatch = countDownLatch;
+                int finalCount = count;
                 try {
                     // redis tcp数据缓存key
                     String dataRedisKey = MessageFormat.format(RedisKeyConstant.RFID_TCP_DATA_REDIS_CACHE_PREFIX, ip + "-" + finalCount);
@@ -262,20 +247,16 @@ public class TcpServiceImplV2 implements TcpServiceV2 {
                 } catch (Exception e) {
                     e.printStackTrace();
                 } finally {
-                    if (finalCountDownLatch != null) {
-                        finalCountDownLatch.countDown();
-                    }
+                    countDownLatch.countDown();
                 }
             }
 //            System.err.println("最终总结果数量--:" + dataMap.size());
 
             if (MapUtils.isNotEmpty(dataMap)) {
-                list = dataMap.values().stream().collect(Collectors.toList());
+                list = new ArrayList<>(dataMap.values());
             }
 
-            if (countDownLatch != null) {
-                countDownLatch.await();
-            }
+            countDownLatch.await();
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 1 - 0
src/main/java/com/fjhx/rfid/r2000/udp/callback/UdpCallBack.java

@@ -10,4 +10,5 @@ public class UdpCallBack extends SimpleChannelInboundHandler<DatagramPacket> {
     protected void channelRead0(ChannelHandlerContext channelHandlerContext, DatagramPacket datagramPacket) throws Exception {
 
     }
+
 }

+ 2 - 11
src/main/java/com/fjhx/rfid/r2000/udp/service/UdpService.java

@@ -14,26 +14,17 @@ public interface UdpService {
 
     /**
      * 启动单台
-     *
-     * @param client
-     * @return
      */
-    public AjaxResult startOne(RFIDClient client);
+    AjaxResult startOne(RFIDClient client);
 
     /**
      * 关闭单台
-     *
-     * @param client
-     * @return
      */
-    public AjaxResult closeOne(RFIDClient client);
+    AjaxResult closeOne(RFIDClient client);
 
 
     /**
      * 启动多台
-     *
-     * @param client
-     * @return
      */
     AjaxResult openMore(RFIDClient client);
 }

+ 9 - 17
src/main/java/com/fjhx/rfid/r2000/udp/service/impl/UdpServiceImpl.java

@@ -95,7 +95,7 @@ public class UdpServiceImpl implements UdpService {
      * @return
      */
     public AjaxResult closeOne(RFIDClient client) {
-        Map<String, JSONObject> data = new HashMap<>();
+        Map<String, JSONObject> data;
         List<JSONObject> list = new ArrayList<>();
 
         // redis udp数据缓存key
@@ -121,7 +121,7 @@ public class UdpServiceImpl implements UdpService {
             redisCache.deleteObject(dataRedisKey);
 
             if (MapUtils.isNotEmpty(data)) {
-                list = data.values().stream().collect(Collectors.toList());
+                list = new ArrayList<>(data.values());
             }
 
         } catch (Exception e) {
@@ -143,7 +143,7 @@ public class UdpServiceImpl implements UdpService {
         ConcurrentHashMap<String, JSONObject> dataMap = new ConcurrentHashMap<>();
         List<JSONObject> list = new ArrayList<>();
 
-        Integer count = 0;
+        int count = 0;
         try {
             if (client == null || CollectionUtils.isEmpty(client.getIpList())) {
                 throw new BaseException("读写器IP地址不能为空!");
@@ -152,8 +152,7 @@ public class UdpServiceImpl implements UdpService {
             // 启动扫描线程
             for (String ip : client.getIpList()) {
                 count++;
-                Integer finalCount = count;
-                CountDownLatch errCountDownLatch = countDownLatch;
+                int finalCount = count;
                 ThreadPoolManager.execute(() -> {
                     try {
                         // redis udp数据缓存key
@@ -175,9 +174,7 @@ public class UdpServiceImpl implements UdpService {
                         sMap.put(ip + "-" + finalCount, ch);
                     } catch (Exception e) {
                         e.printStackTrace();
-                        if (errCountDownLatch != null) {
-                            errCountDownLatch.countDown();
-                        }
+                        countDownLatch.countDown();
                         throw new BaseException(RFIDConstant.READ_DATA_ERR);
                     }
                 });
@@ -190,8 +187,7 @@ public class UdpServiceImpl implements UdpService {
             // 关闭线程
             for (String ip : client.getIpList()) {
                 count++;
-                Integer finalCount = count;
-                CountDownLatch finalCountDownLatch = countDownLatch;
+                int finalCount = count;
                 try {
                     // redis udp数据缓存key
                     String dataRedisKey = MessageFormat.format(RedisKeyConstant.RFID_UDP_DATA_REDIS_CACHE_PREFIX, ip + "-" + finalCount);
@@ -221,20 +217,16 @@ public class UdpServiceImpl implements UdpService {
                 } catch (Exception e) {
                     e.printStackTrace();
                 } finally {
-                    if (finalCountDownLatch != null) {
-                        finalCountDownLatch.countDown();
-                    }
+                    countDownLatch.countDown();
                 }
             }
 //            System.err.println("最终总结果数量--:" + dataMap.size());
 
             if (MapUtils.isNotEmpty(dataMap)) {
-                list = dataMap.values().stream().collect(Collectors.toList());
+                list = new ArrayList<>(dataMap.values());
             }
 
-            if (countDownLatch != null) {
-                countDownLatch.await();
-            }
+            countDownLatch.await();
         } catch (Exception e) {
             e.printStackTrace();
         }

+ 3 - 2
src/main/java/com/fjhx/rfid/r2000/udp/utils/Utility.java

@@ -8,6 +8,7 @@ import java.nio.ByteBuffer;
 import java.nio.CharBuffer;
 import java.nio.charset.Charset;
 import java.nio.charset.CharsetDecoder;
+import java.nio.charset.StandardCharsets;
 import java.text.SimpleDateFormat;
 import java.util.Date;
 import java.util.logging.Level;
@@ -85,7 +86,7 @@ public class Utility {
             m = i * 2 + 1;
             n = m + 1;
             int intVal = Integer.decode("0x" + hexString.substring(i * 2, m) + hexString.substring(m, n));
-            ret[i] = Byte.valueOf((byte) intVal);
+            ret[i] = (byte) intVal;
         }
         return ret;
     }
@@ -209,7 +210,7 @@ public class Utility {
         CharsetDecoder decoder = null;
         CharBuffer charBuffer = null;
         try {
-            charset = Charset.forName("UTF-8");
+            charset = StandardCharsets.UTF_8;
             decoder = charset.newDecoder();
             // charBuffer = decoder.decode(buffer);//用这个的话,只能输出来一次结果,第二次显示为空
             charBuffer = decoder.decode(buffer.asReadOnlyBuffer());