hfxc226 2 rokov pred
rodič
commit
0b20f47325

+ 40 - 34
platform-opc/src/main/java/com/platform/opc/servie/OpcService.java

@@ -54,43 +54,44 @@ public class OpcService {
         Map<Integer, List<RemoteOpc>> listMap = remoteOpcList.stream().collect(Collectors.groupingBy(RemoteOpc::getLine));
         OpcDAClient.connect();
         OpcDAClient.findAllItem();
-        AddFailedException exception = OpcDAClient.addGroupList(listMap);
-        Map<String, Integer> failedItems = exception.getErrors();
-        Map<String, Item> addItems = exception.getItems();
         List<RemoteOpc> remoteOpcFailList = new ArrayList<>();
-        if (failedItems != null) {// 有不存在的item,需要更新对应的点位信息
-            for (Map.Entry<String, Integer> entry : failedItems.entrySet()) {
-                RemoteOpc remoteOpc = new RemoteOpc();
-                // 因为有些标签是:PT_9836_AV,不能用 entry.getKey().split("_")[0],需要找到最后一个_
-                int index = entry.getKey().lastIndexOf("_");
-                if(index == -1){
-                    remoteOpc.setPositionNum(entry.getKey());
-                }else{
-                    remoteOpc.setPositionNum(entry.getKey().substring(0, index));
+        AddFailedException exception = OpcDAClient.addGroupList(listMap);
+        if (exception != null) {
+            Map<String, Integer> failedItems = exception.getErrors();
+            Map<String, Item> addItems = exception.getItems();
+            if (failedItems != null) {// 有不存在的item,需要更新对应的点位信息
+                for (Map.Entry<String, Integer> entry : failedItems.entrySet()) {
+                    RemoteOpc remoteOpc = new RemoteOpc();
+                    // 因为有些标签是:PT_9836_AV,不能用 entry.getKey().split("_")[0],需要找到最后一个_
+                    int index = entry.getKey().lastIndexOf("_");
+                    if (index == -1) {
+                        remoteOpc.setPositionNum(entry.getKey());
+                    } else {
+                        remoteOpc.setPositionNum(entry.getKey().substring(0, index));
+                    }
+                    remoteOpc.setCreatedFlag(0);
+                    remoteOpc.setRemark("opc server未找到改点位。可能原因1:AV/DV配置错误,2:opc server中未配置");
+                    log.error("opc server未找到该点位。key: " + remoteOpc.getPositionNum() + ", value: " + entry.getValue());
+                    remoteOpcFailList.add(remoteOpc);
                 }
-                remoteOpc.setCreatedFlag(0);
-                remoteOpc.setRemark("opc server未找到改点位。可能原因1:AV/DV配置错误,2:opc server中未配置");
-                log.error("opc server未找到该点位。key: " + remoteOpc.getPositionNum() + ", value: " + entry.getValue());
-                remoteOpcFailList.add(remoteOpc);
             }
-        }
-        if (addItems != null) {// 有不存在的item,需要更新对应的点位信息
-            for (Map.Entry<String, Item> entry : addItems.entrySet()) {
-                RemoteOpc remoteOpc = new RemoteOpc();
-                // 因为有些标签是:PT_9836_AV,不能用 entry.getKey().split("_")[0],需要找到最后一个_
-                int index = entry.getKey().lastIndexOf("_");
-                if(index == -1){
-                    remoteOpc.setPositionNum(entry.getKey());
-                }else{
-                    remoteOpc.setPositionNum(entry.getKey().substring(0, index));
+            if (addItems != null) {// 有不存在的item,需要更新对应的点位信息
+                for (Map.Entry<String, Item> entry : addItems.entrySet()) {
+                    RemoteOpc remoteOpc = new RemoteOpc();
+                    // 因为有些标签是:PT_9836_AV,不能用 entry.getKey().split("_")[0],需要找到最后一个_
+                    int index = entry.getKey().lastIndexOf("_");
+                    if (index == -1) {
+                        remoteOpc.setPositionNum(entry.getKey());
+                    } else {
+                        remoteOpc.setPositionNum(entry.getKey().substring(0, index));
+                    }
+                    remoteOpc.setCreatedFlag(1);
+                    remoteOpc.setRemark("opc server已配置,AV/DV配置正确");
+                    log.error("opc server已配置。key: " + remoteOpc.getPositionNum() + ", value: " + entry.getValue());
+                    remoteOpcFailList.add(remoteOpc);
                 }
-                remoteOpc.setCreatedFlag(1);
-                remoteOpc.setRemark("opc server已配置,AV/DV配置正确");
-                log.error("opc server已配置。key: " + remoteOpc.getPositionNum() + ", value: " + entry.getValue());
-                remoteOpcFailList.add(remoteOpc);
             }
         }
-
         if (!CollectionUtils.isEmpty(remoteOpcFailList)) {
             remoteOpcMapper.updateBatch(remoteOpcFailList);
         }
@@ -99,8 +100,13 @@ public class OpcService {
     }
 
     public static void main(String[] args) {
-        String ss = "PT9836";
-        int index = ss.lastIndexOf("_");
-        System.out.println(ss.substring(0, index));
+
+        Item[][] itemArrList = new Item[10][];
+        Item[] itemArr = new Item[10];
+        itemArrList[0] = itemArr;
+
+        Item[] itemArr2 = new Item[5];
+        itemArrList[1] = itemArr2;
+        System.out.println("");
     }
 }

+ 5 - 1
platform-opc/src/main/java/com/platform/opc/servie/OpcTaskService.java

@@ -10,6 +10,8 @@ import com.platform.opc.util.OpcDAClient;
 import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.context.annotation.DependsOn;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.annotation.EnableAsync;
 import org.springframework.scheduling.annotation.EnableScheduling;
 import org.springframework.scheduling.annotation.Scheduled;
 import org.springframework.stereotype.Service;
@@ -23,9 +25,11 @@ import java.util.stream.Collectors;
 @AllArgsConstructor
 @Slf4j
 @EnableScheduling   // 1.开启定时任务
+@EnableAsync
 public class OpcTaskService {
 
-    @Scheduled(fixedDelay = 2000)  //间隔2秒
+    @Async
+    @Scheduled(fixedDelay = 5000)  //间隔2秒
     public void getValue(){
         log.info("开始定时任务");
         List<Map<String, Object>> resultList = OpcDAClient.getItemValuesList();

+ 4 - 2
platform-opc/src/main/java/com/platform/opc/util/OpcDAClient.java

@@ -37,7 +37,7 @@ public class OpcDAClient {
     private static List<Group> groupList = new ArrayList<>();
     private static List<Map<String, Item>> groupItemsList = new ArrayList<>();
     private static List<Set> itemSetList = new ArrayList<>();
-    private static Item[][] itemArrList = new Item[][]{};
+    private static Item[][] itemArrList = null;
 
     /**
      * 初始化连接信息
@@ -130,6 +130,7 @@ public class OpcDAClient {
      */
     public static AddFailedException addGroupList(Map<Integer, List<RemoteOpc>> listMap) {
         Map<String, Object> result = new HashMap<>();
+        itemArrList = new Item[listMap.size()][];
         try {
             if (CollectionUtils.isEmpty(groupItemsList) || CollectionUtils.isEmpty(groupList)) {
                 log.info("开始建组...");
@@ -219,9 +220,10 @@ public class OpcDAClient {
         List<Map<String, Object>> resultList = new ArrayList<>();
         try {
             if (!CollectionUtils.isEmpty(groupItemsList)) {
+                log.info("数据获取总组数:", groupItemsList.size());
                 for (int i = 0; i < groupItemsList.size(); i++) {
                     Map<Item, ItemState> resultMap = groupList.get(i).read(true, itemArrList[i]);
-                    log.info("数据获取完成:{}条", resultMap.size());
+                    log.info("数据获取完成。数量:", resultMap.size() + ", 组序号:" + i);
                     Map<String, Object> result = new HashMap<>();
                     for (Item item : resultMap.keySet()) {
                         ItemState itemMap = resultMap.get(item);