|
@@ -13,24 +13,21 @@ import com.example.opc_common.util.MathUtil;
|
|
import com.example.opc_common.util.Result;
|
|
import com.example.opc_common.util.Result;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
|
|
import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
|
|
-import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig;
|
|
|
|
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
|
|
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider;
|
|
import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
|
|
import org.eclipse.milo.opcua.sdk.client.api.identity.UsernameProvider;
|
|
import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
|
|
import org.eclipse.milo.opcua.sdk.client.nodes.UaNode;
|
|
-import org.eclipse.milo.opcua.stack.client.DiscoveryClient;
|
|
|
|
import org.eclipse.milo.opcua.stack.core.AttributeId;
|
|
import org.eclipse.milo.opcua.stack.core.AttributeId;
|
|
import org.eclipse.milo.opcua.stack.core.BuiltinDataType;
|
|
import org.eclipse.milo.opcua.stack.core.BuiltinDataType;
|
|
import org.eclipse.milo.opcua.stack.core.Identifiers;
|
|
import org.eclipse.milo.opcua.stack.core.Identifiers;
|
|
import org.eclipse.milo.opcua.stack.core.UaException;
|
|
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.*;
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger;
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
|
|
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UShort;
|
|
-import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned;
|
|
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode;
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass;
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.NodeClass;
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
|
|
import org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn;
|
|
import org.eclipse.milo.opcua.stack.core.types.structured.*;
|
|
import org.eclipse.milo.opcua.stack.core.types.structured.*;
|
|
-import org.eclipse.milo.opcua.stack.core.util.EndpointUtil;
|
|
|
|
|
|
|
|
import java.math.BigDecimal;
|
|
import java.math.BigDecimal;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
@@ -52,48 +49,27 @@ public class OpcUaUtil {
|
|
* @return
|
|
* @return
|
|
*/
|
|
*/
|
|
public static OpcUaClient createClient(DataSource dataSource) throws Exception {
|
|
public static OpcUaClient createClient(DataSource dataSource) throws Exception {
|
|
- String endpointUrl = "opc.tcp://" + dataSource.getIpAddress() + ":" + dataSource.getIpPort();
|
|
|
|
|
|
+ String endPointUrl = "opc.tcp://" + dataSource.getIpAddress() + ":" + dataSource.getIpPort();
|
|
Path securityTempDir = Paths.get(certPath, "security");
|
|
Path securityTempDir = Paths.get(certPath, "security");
|
|
|
|
|
|
Files.createDirectories(securityTempDir);
|
|
Files.createDirectories(securityTempDir);
|
|
if (!Files.exists(securityTempDir)) {
|
|
if (!Files.exists(securityTempDir)) {
|
|
- log.info("无法创建安全目录: " + securityTempDir);
|
|
|
|
- return null;
|
|
|
|
|
|
+ throw new Exception("无法创建安全目录: " + securityTempDir);
|
|
}
|
|
}
|
|
- KeyStoreLoader keyStoreLoader = new KeyStoreLoader();
|
|
|
|
- KeyStoreLoader loader = keyStoreLoader.load(securityTempDir);
|
|
|
|
- // 搜索OPC节点
|
|
|
|
- List<EndpointDescription> endpoints = null;
|
|
|
|
- EndpointDescription endpoint = null;
|
|
|
|
- try {
|
|
|
|
- endpoints = DiscoveryClient.getEndpoints(endpointUrl).get();
|
|
|
|
|
|
+ return OpcUaClient.create(endPointUrl,
|
|
|
|
+ endpoints ->
|
|
|
|
+ endpoints.stream()
|
|
|
|
+ .filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri()))
|
|
|
|
+ .findFirst(),
|
|
|
|
+ configBuilder ->
|
|
|
|
+ configBuilder
|
|
|
|
+ .setApplicationName(LocalizedText.english(""))
|
|
|
|
+ //如果不是匿名则获取账号和密码
|
|
|
|
+ .setIdentityProvider(dataSource.getIsAnonymous() == ConstantStr.NOT_ANONYMOUS ? new UsernameProvider(dataSource.getDataSourceName(), dataSource.getIpPassword()) : new AnonymousProvider())
|
|
|
|
+ .setRequestTimeout(UInteger.valueOf(5000))
|
|
|
|
+ .build()
|
|
|
|
+ );
|
|
|
|
|
|
-// endpoint = endpoints.stream()
|
|
|
|
-// .filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri())).filter(endpointFilter())
|
|
|
|
-// .findFirst().orElseThrow(() -> new Exception("no desired endpoints returned"));
|
|
|
|
- endpoint = EndpointUtil.updateUrl(endpoints.get(0), dataSource.getIpAddress(), Integer.valueOf(dataSource.getIpPort()));
|
|
|
|
- } catch (Throwable e) {
|
|
|
|
- String discoveryUrl = endpointUrl;
|
|
|
|
- if (!discoveryUrl.endsWith("/")) {
|
|
|
|
- discoveryUrl += "/";
|
|
|
|
- }
|
|
|
|
- discoveryUrl += "discovery";
|
|
|
|
- endpoints = DiscoveryClient.getEndpoints(discoveryUrl).get();
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
-// EndpointDescription endpoint = endpoints.stream()
|
|
|
|
-// .filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri())).filter(endpointFilter())
|
|
|
|
-// .findFirst().orElseThrow(() -> new Exception("no desired endpoints returned"));
|
|
|
|
- OpcUaClientConfig config = OpcUaClientConfig.builder()
|
|
|
|
- .setApplicationName(LocalizedText.english("my"))
|
|
|
|
- .setApplicationUri("urn:Jellyleo:UnifiedAutomation:UaExpert@Jellyleo")
|
|
|
|
- .setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair())
|
|
|
|
- .setEndpoint(endpoint)
|
|
|
|
- //如果不是匿名则获取账号和密码
|
|
|
|
- .setIdentityProvider(dataSource.getIsAnonymous() == ConstantStr.NOT_ANONYMOUS ? new UsernameProvider(dataSource.getDataSourceName(), dataSource.getIpPassword()) : new AnonymousProvider())
|
|
|
|
- .setRequestTimeout(Unsigned.uint(5000)).build();
|
|
|
|
-
|
|
|
|
- return OpcUaClient.create(config);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
private static Predicate<EndpointDescription> endpointFilter() {
|
|
private static Predicate<EndpointDescription> endpointFilter() {
|
|
@@ -119,6 +95,7 @@ public class OpcUaUtil {
|
|
log.info("连接耗费时间为:" + (finish - start) + "毫秒");
|
|
log.info("连接耗费时间为:" + (finish - start) + "毫秒");
|
|
return Result.ok("测试连接成功");
|
|
return Result.ok("测试连接成功");
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
throw new CustomException(ResultEnum.REQUEST_TIME_OUT.getRespCode(), OpcUaUtil.genException(e.getMessage()));
|
|
throw new CustomException(ResultEnum.REQUEST_TIME_OUT.getRespCode(), OpcUaUtil.genException(e.getMessage()));
|
|
} finally {
|
|
} finally {
|
|
if (Blank.isNotEmpty(opcUaClient)) {
|
|
if (Blank.isNotEmpty(opcUaClient)) {
|
|
@@ -184,8 +161,9 @@ public class OpcUaUtil {
|
|
List<JSONObject> jsonList = new ArrayList<>();
|
|
List<JSONObject> jsonList = new ArrayList<>();
|
|
List<? extends UaNode> nodes;
|
|
List<? extends UaNode> nodes;
|
|
if (uaNode == null) {
|
|
if (uaNode == null) {
|
|
|
|
+ nodes = client.getAddressSpace().browseNodes(Identifiers.RootFolder);//从根目录
|
|
// nodes = client.getAddressSpace().browseNodes(Identifiers.ObjectsFolder);//从根目录
|
|
// nodes = client.getAddressSpace().browseNodes(Identifiers.ObjectsFolder);//从根目录
|
|
- nodes = client.getAddressSpace().browseNodes(Identifiers.ViewsFolder);
|
|
|
|
|
|
+// nodes = client.getAddressSpace().browseNodes(Identifiers.ViewsFolder);
|
|
} else {
|
|
} else {
|
|
nodes = client.getAddressSpace().browseNodes(uaNode);
|
|
nodes = client.getAddressSpace().browseNodes(uaNode);
|
|
}
|
|
}
|
|
@@ -416,11 +394,6 @@ public class OpcUaUtil {
|
|
String itemId = split[i];
|
|
String itemId = split[i];
|
|
if (i == split.length - 1) {
|
|
if (i == split.length - 1) {
|
|
for (UaNode nd : nodes) {
|
|
for (UaNode nd : nodes) {
|
|
-// if (itemId.equals(nd.getNodeId().getIdentifier())) {
|
|
|
|
-// nodes = browserTree(client, nd);
|
|
|
|
-// jsonObject = genUaNode(nodes, itemStr.replace("!@", "."));
|
|
|
|
-// break;
|
|
|
|
-// }
|
|
|
|
if (itemId.equals(nd.getBrowseName().getName())) {
|
|
if (itemId.equals(nd.getBrowseName().getName())) {
|
|
nodes = browserTree(client, nd);
|
|
nodes = browserTree(client, nd);
|
|
jsonObject = genUaNode(nodes, itemStr.replace("!@", "."));
|
|
jsonObject = genUaNode(nodes, itemStr.replace("!@", "."));
|
|
@@ -429,10 +402,6 @@ public class OpcUaUtil {
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
for (UaNode nd : nodes) {
|
|
for (UaNode nd : nodes) {
|
|
-// if (itemId.equals(nd.getNodeId().getIdentifier())) {
|
|
|
|
-// nodes = browserTree(client, nd);
|
|
|
|
-// break;
|
|
|
|
-// }
|
|
|
|
if (itemId.equals(nd.getBrowseName().getName())) {
|
|
if (itemId.equals(nd.getBrowseName().getName())) {
|
|
nodes = browserTree(client, nd);
|
|
nodes = browserTree(client, nd);
|
|
break;
|
|
break;
|
|
@@ -457,8 +426,6 @@ public class OpcUaUtil {
|
|
// return client.getAddressSpace().browseNodes(Identifiers.ViewsFolder);
|
|
// return client.getAddressSpace().browseNodes(Identifiers.ViewsFolder);
|
|
return client.getAddressSpace().browseNodes(Identifiers.ObjectsFolder);
|
|
return client.getAddressSpace().browseNodes(Identifiers.ObjectsFolder);
|
|
} else {
|
|
} else {
|
|
-// List<? extends UaNode> uaNodes = client.getAddressSpace().browseNodes(uaNode);
|
|
|
|
-// return uaNodes;
|
|
|
|
return client.getAddressSpace().browseNodes(uaNode);
|
|
return client.getAddressSpace().browseNodes(uaNode);
|
|
}
|
|
}
|
|
}
|
|
}
|