zhoupeng před 1 rokem
rodič
revize
6c74c7dc56

+ 141 - 6
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/Test.java

@@ -1,18 +1,153 @@
 package com.example.opc_da.task;
 
+import com.alibaba.fastjson.JSONObject;
+import com.example.opc_common.entity.DataSource;
+import com.example.opc_common.util.Blank;
+import com.example.opc_da.util.OpcDaUtil;
 import lombok.extern.slf4j.Slf4j;
+import org.jinterop.dcom.common.JIException;
+import org.openscada.opc.lib.common.AlreadyConnectedException;
+import org.openscada.opc.lib.da.Server;
+import org.openscada.opc.lib.da.browser.Branch;
+import org.openscada.opc.lib.da.browser.FlatBrowser;
+import org.openscada.opc.lib.da.browser.Leaf;
+import org.openscada.opc.lib.da.browser.TreeBrowser;
 
-import java.util.concurrent.atomic.AtomicReference;
+import java.net.UnknownHostException;
+import java.util.*;
+
+import static com.example.opc_da.util.OpcDaUtil.genFullPath;
 
 @Slf4j
 public class Test {
 
-    public static void main(String[] args) {
-        AtomicReference<Long> index = new AtomicReference<>(0L);
-        for (int i = 0; i < 10; i++) {
-            index.getAndSet(index.get() + 1);
-            System.out.println(index);
+    public static void main(String[] args) throws UnknownHostException, AlreadyConnectedException, JIException {
+        DataSource dataSource = new DataSource();
+//        dataSource.setClsId(OpcDaDriverEnum.PAS300.getValue());
+//        dataSource.setIpAddress("192.168.1.99");
+//        dataSource.setClsId(OpcDaDriverEnum.YOKOGAWA.getValue());
+//        dataSource.setIpAddress("192.168.1.24");
+//        dataSource.setClsId(OpcDaDriverEnum.OPCIFIX.getValue());
+//        dataSource.setIpAddress("192.168.1.111");
+//        dataSource.setClsId(OpcDaDriverEnum.KEPSERVER.getValue());
+        dataSource.setIpAddress("192.168.0.5");
+        dataSource.setIpUserName("administrator");
+        dataSource.setIpPassword("jd123456");
+//        String itemStr = "";
+//        String itemName1 = "AI";
+//        String itemName2 = "AV";
+//        String itemStr = "FCS0101";
+//        String itemName1 = "AA";
+//        String itemName2 = "PV";
+//        String itemStr = "FIX!@AI";
+//        String itemName1 = "TEST";
+//        String itemName2 = "F_CV";
+        String itemStr = "通道 1!@设备 1";
+        String itemName1 = "_System";
+        String itemName2 = " _Description ";
+        Server server = OpcDaUtil.createServer(dataSource);
+        server.connect();
+//        test1(server,itemStr,itemName1,itemName2);
+        test2(server,itemStr,itemName1,itemName2);
+    }
+
+    public static void test1(Server server, String itemStr, String itemName1, String itemName2) throws JIException {
+        long l = System.currentTimeMillis();
+        TreeBrowser treeBrowser = server.getTreeBrowser();
+        System.out.println(System.currentTimeMillis() - l + "ms");
+        List<Map<String, String>> mapList = new ArrayList<>();
+        if (Blank.isEmpty(itemStr)) {
+            mapList = xxx(treeBrowser, new Branch(), itemName1, itemName2);
+        } else {
+            String[] split = itemStr.split("!@");
+            Branch branch = new Branch();
+            for (int i = 0; i < split.length; i++) {
+                String itemId = split[i];
+                if (i == split.length - 1) {
+                    mapList = xxx(treeBrowser, new Branch(branch, itemId), itemName1, itemName2);
+                } else {
+                    branch = new Branch(branch, itemId);
+                }
+            }
+        }
+        System.out.println("-----" + (System.currentTimeMillis() - l) + "ms");
+        System.out.println("--------------");
+        System.out.println(mapList.size());
+        mapList.forEach(map->{
+            System.out.println(map.toString());
+        });
+    }
+
+    public static void test2(Server server, String itemStr, String itemName1, String itemName2) throws UnknownHostException, JIException {
+        long l = System.currentTimeMillis();
+        FlatBrowser flatBrowser = server.getFlatBrowser();
+        Collection<String> browse = flatBrowser.browse();
+//        Collection<String> browse = flatBrowser.browse(itemName1);
+//        Collection<String> browse = flatBrowser.browse("通道 1.设备 1."+itemName1);
+//        Collection<String> browse = flatBrowser.getAccessPaths("通道 1.设备 1."+itemName1);
+        System.out.println(System.currentTimeMillis() - l + "ms");
+        System.out.println("-----" + (System.currentTimeMillis() - l) + "ms");
+        System.out.println("--------------");
+        System.out.println(browse.toString());
+        browse.forEach(e->{
+            System.out.println(e);
+        });
+    }
+
+    public static List<Map<String, String>> xxx(TreeBrowser treeBrowser, Branch branch, String itemName1, String itemName2) {
+        JSONObject jsonObject = new JSONObject();
+        try {
+            treeBrowser.fillBranches(branch);
+        } catch (Exception ignored) {
         }
+//        try {
+//            treeBrowser.fillLeaves(branch);
+//        } catch (Exception ignored) {
+//        }
+
+        List<Map<String, String>> mapList = new ArrayList<>();
+        Iterator iter = branch.getBranches().iterator();
+        while (iter.hasNext()) {
+            Branch subBranch = (Branch) iter.next();
+//            map.put(subBranch.getName(), subBranch.getName());
+            String name = subBranch.getName();
+            if (name.contains(itemName1)) {
+                List<Map<String, String>> yyy = yyy(treeBrowser, new Branch(branch, name), itemName2);
+                if (Blank.notBlank(yyy)) {
+                    mapList.addAll(yyy);
+                }
+            }
+        }
+//        jsonObject.put("leaves", mapList);
+        return mapList;
+    }
+
+    public static List<Map<String, String>> yyy(TreeBrowser treeBrowser, Branch branch, String itemName2) {
+//        try {
+//            treeBrowser.fillBranches(branch);
+//        } catch (Exception ignored) {
+//        }
+        try {
+            treeBrowser.fillLeaves(branch);
+        } catch (Exception ignored) {
+        }
+
+        Iterator iter = branch.getLeaves().iterator();
+        List<Map<String, String>> mapList = new ArrayList<>();
+        while (iter.hasNext()) {
+            Leaf leaf = (Leaf) iter.next();
+//            map.put(leaf.getName(), leaf.getItemId());
+            Map<String, String> map = new HashMap<>();
+            String name = leaf.getName();
+            if (name.contains(itemName2)) {
+                map.put("fullPath", genFullPath(leaf));
+                map.put("itemName", leaf.getName());
+                map.put("itemReadName", leaf.getItemId());
+                mapList.add(map);
+            }
+        }
+        return mapList;
     }
 
+
 }