소스 검색

新增opcua一层一层获取数据结构

zhoupeng 1 년 전
부모
커밋
63c24bc202

+ 1 - 1
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/service/impl/DataSourceServiceImpl.java

@@ -222,7 +222,7 @@ public class DataSourceServiceImpl implements DataSourceService {
                     return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "选择不匿名方式,需要填写账号");
                 }
             }
-            return restTemplate.postForObject(opcUaUrl + "/dataSource/getDataSourceItemTree", new HttpEntity<>(dataSource, new HttpHeaders()), Result.class);
+            return restTemplate.postForObject(opcUaUrl + "/dataSource/getNextAllItem", new HttpEntity<>(dataSource, new HttpHeaders()), Result.class);
         } else if (OpcUtil.isOpcDa(dataSourceTypeKey)) {
             if (Blank.isEmpty(dataSource.getIpAddress(), dataSource.getIpUserName(), dataSource.getClsId())) {
                 return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "ip地址,账号,opc驱动都不能为空");

+ 1 - 1
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaChangeTask.java

@@ -84,7 +84,7 @@ public class OpcDaChangeTask extends TimerTask {
         Integer dataSourceId = dataSource.getId();
         try {
             server.connect();
-            SyncAccess access = new SyncAccess(server, 1000);
+            SyncAccess access = new SyncAccess(server, 500);
             for (com.example.opc_common.entity.Item item : itemList) {
                 String itemId = item.getItemReadName();
                 access.addItem(itemId, new DataCallback() {

+ 1 - 1
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/task/OpcDaExceedTask.java

@@ -82,7 +82,7 @@ public class OpcDaExceedTask extends TimerTask {
         Integer dataSourceId = dataSource.getId();
         try {
             server.connect();
-            SyncAccess access = new SyncAccess(server, 1000);
+            SyncAccess access = new SyncAccess(server, 500);
             for (com.example.opc_common.entity.Item item : itemList) {
                 String itemId = item.getItemReadName();
                 Integer isDriverItem = item.getIsDriverItem();

+ 4 - 3
chaunyi_opc/opc_da/src/main/java/com/example/opc_da/util/OpcDaUtil.java

@@ -480,7 +480,7 @@ public class OpcDaUtil {
         } else if (message.contains("0x8001FFFF")) {
             return "ip连接不可用";
         } else if (message.contains("Index 16 out of bounds for length 16") ||
-                message.contains("java.lang.ArrayIndexOutOfBoundsException: 16")) {
+                message.contains("java.lang.ArrayIndexOutOfBoundsException")) {
             return "服务未启动";
         } else if (message.contains("0x80070005")) {
             return "访问被拒绝,可能是权限未配置,配置可在组件服务中进行配置";
@@ -554,16 +554,17 @@ public class OpcDaUtil {
             Branch branch = new Branch();
             List<Branch> branchList = getBranch(treeBrowser, branch);
             for (int i = 0; i < split.length; i++) {
+                String itemId = split[i];
                 if (i == split.length - 1) {
                     for (Branch branch1 : branchList) {
-                        if (split[i].equals(branch1.getName())) {
+                        if (itemId.equals(branch1.getName())) {
                             jsonObject = browserTree(treeBrowser, branch1);
                             break;
                         }
                     }
                 } else {
                     for (Branch branch1 : branchList) {
-                        if (split[i].equals(branch1.getName())) {
+                        if (itemId.equals(branch1.getName())) {
                             branchList = getBranch(treeBrowser, branch1);
                             break;
                         }

+ 9 - 0
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/controller/DataSourceController.java

@@ -3,6 +3,7 @@ package com.example.opc_ua.controller;
 import com.alibaba.fastjson.JSON;
 import com.alibaba.fastjson.JSONObject;
 import com.example.opc_common.entity.DataSource;
+import com.example.opc_common.enums.ResultEnum;
 import com.example.opc_common.util.Blank;
 import com.example.opc_common.util.ConstantStr;
 import com.example.opc_common.util.Result;
@@ -60,6 +61,14 @@ public class DataSourceController {
         }
     }
 
+    @PostMapping("/getNextAllItem")
+    public Result getNextAllItem(Integer id,String itemStr) {
+        if (Blank.isEmpty(id)) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "数据源不能为空");
+        }
+        return dataSourceService.getNextAllItem(id, itemStr);
+    }
+
     @PostMapping("/genDataSourceItemTree")
     public void genDataSourceItemTree(@RequestBody DataSource dataSource) {
         opcAsyncTask.opcUaGetTree(dataSource);

+ 3 - 0
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/service/DataSourceService.java

@@ -1,4 +1,7 @@
 package com.example.opc_ua.service;
 
+import com.example.opc_common.util.Result;
+
 public interface DataSourceService {
+    Result getNextAllItem(Integer id, String itemStr);
 }

+ 34 - 0
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/service/impl/DataSourceServiceimpl.java

@@ -1,9 +1,18 @@
 package com.example.opc_ua.service.impl;
 
+import com.example.opc_common.entity.DataSource;
+import com.example.opc_common.entity.DataSourceType;
+import com.example.opc_common.enums.ResultEnum;
+import com.example.opc_common.exception.CustomException;
+import com.example.opc_common.util.Blank;
+import com.example.opc_common.util.ConstantStr;
+import com.example.opc_common.util.OpcUtil;
+import com.example.opc_common.util.Result;
 import com.example.opc_ua.dao.DataModelDao;
 import com.example.opc_ua.dao.DataSourceDao;
 import com.example.opc_ua.dao.ItemGroupDao;
 import com.example.opc_ua.service.DataSourceService;
+import com.example.opc_ua.util.OpcUaUtil;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
@@ -22,4 +31,29 @@ public class DataSourceServiceimpl implements DataSourceService {
     @Resource
     private DataModelDao dataModelDao;
 
+    @Override
+    public Result getNextAllItem(Integer id, String itemStr) {
+        DataSource dataSource = dataSourceDao.getDataSourceById(id);
+        if (Blank.isEmpty(dataSource)) {
+            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "没有此数据源配置,请刷新");
+        }
+        if (Blank.isNotEmpty(dataSource.getIpPassword())) {
+            dataSource = DataSource.convertPassword(dataSource);
+        }
+        DataSourceType dataSourceType = dataSourceDao.getDataSourceTypeById(dataSource.getTypeId());
+        String dataSourceTypeKey = dataSourceType.getDataSourceTypeKey();
+        if (OpcUtil.isOpcUa(dataSourceTypeKey)) {
+            if (Blank.isEmpty(dataSource.getIpAddress(), dataSource.getIpPort(), dataSource.getIsAnonymous())) {
+                return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "ip地址,端口号,匿名方式都不能为空");
+            }
+            if (dataSource.getIsAnonymous() == ConstantStr.NOT_ANONYMOUS) {
+                if (Blank.isEmpty(dataSource.getIpUserName())) {
+                    return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "选择不匿名方式,需要填写账号");
+                }
+            }
+            return OpcUaUtil.getNextAllItem(dataSource, itemStr);
+        } else {
+            throw new CustomException(ResultEnum.SERVER_ERROR.getRespCode(), "目前还没有此种类型的连接方式");
+        }
+    }
 }

+ 2 - 2
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/task/OpcUaTask.java

@@ -143,7 +143,7 @@ public class OpcUaTask {
                             timer,
                             timeFormat,
                             endDate.getTime()),
-                    startDate, (int) (Math.round(itemGroup.getModeValue() * 1000)));
+                    startDate, 500);
         } else if (readMode == ConstantStr.EXCEED_SET_VALUE) {
             timer.schedule(new OpcUaExceedTask(redisUtil,
                             opcAsyncTask,
@@ -159,7 +159,7 @@ public class OpcUaTask {
                             timer,
                             timeFormat,
                             endDate.getTime()),
-                    startDate, (int) (Math.round(itemGroup.getModeValue() * 1000)));
+                    startDate, 500);
         } else {
             cronTaskRegister.removeCronTask(cronId);
             throw new CustomException(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), "目前未适配此种采样模式");

+ 56 - 1
chaunyi_opc/opc_ua/src/main/java/com/example/opc_ua/util/OpcUaUtil.java

@@ -21,7 +21,6 @@ import org.eclipse.milo.opcua.stack.client.DiscoveryClient;
 import org.eclipse.milo.opcua.stack.core.BuiltinDataType;
 import org.eclipse.milo.opcua.stack.core.Identifiers;
 import org.eclipse.milo.opcua.stack.core.UaException;
-import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy;
 import org.eclipse.milo.opcua.stack.core.types.builtin.*;
 import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
 import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
@@ -341,4 +340,60 @@ public class OpcUaUtil {
         }
         return "通信异常(" + message + ")";
     }
+
+    public static Result getNextAllItem(DataSource dataSource, String itemStr) {
+        OpcUaClient opcUaClient = null;
+        try {
+            opcUaClient = createClient(dataSource);
+            if (Blank.isEmpty(opcUaClient)) {
+                return Result.no(ResultEnum.REQUEST_TIME_OUT.getRespCode(), "客户端创建失败");
+            }
+            opcUaClient.connect().get();
+            return Result.ok(getNextItem(opcUaClient, null));
+        } catch (Exception e) {
+            throw new CustomException(ResultEnum.REQUEST_TIME_OUT.getRespCode(), OpcUaUtil.genException(e.getMessage()));
+        } finally {
+            if (Blank.isNotEmpty(opcUaClient)) {
+                opcUaClient.disconnect();
+            }
+        }
+    }
+
+    public static List<? extends UaNode> getNextItem(OpcUaClient client, String itemStr) throws UaException {
+        if (Blank.isEmpty(client)) {
+            return null;
+        }
+        if (Blank.isEmpty(itemStr)) {
+            return browserTree(client, null);
+        } else {
+            String[] split = itemStr.split("\\.");
+            List<? extends UaNode> nodes = browserTree(client, null);
+            for (int i = 0; i < split.length; i++) {
+                String itemId = split[i];
+                for (UaNode nd : nodes) {
+                    if (itemId.equals(nd.getNodeId().getIdentifier())) {
+                        nodes = browserTree(client, nd);
+                        break;
+                    }
+                }
+            }
+            return nodes;
+        }
+    }
+
+    /**
+     * 通过上一级,获取下一次的所有item
+     *
+     * @param client
+     * @param uaNode
+     * @return
+     */
+    public static List<? extends UaNode> browserTree(OpcUaClient client, UaNode uaNode) throws UaException {
+        if (uaNode == null) {
+//            nodes = client.getAddressSpace().browseNodes(Identifiers.ObjectsFolder);//从根目录
+            return client.getAddressSpace().browseNodes(Identifiers.ViewsFolder);
+        } else {
+            return client.getAddressSpace().browseNodes(uaNode);
+        }
+    }
 }