提交 | 用户 | 时间
|
e4c3b0
|
1 |
package com.jcdm.main.da.opcuaconfig.client; |
Y |
2 |
|
|
3 |
import com.jcdm.main.da.opcuaconfig.cert.KeyStoreLoader; |
|
4 |
import com.jcdm.main.da.opcuaconfig.init.Properties; |
|
5 |
import lombok.extern.slf4j.Slf4j; |
|
6 |
import org.eclipse.milo.opcua.sdk.client.OpcUaClient; |
|
7 |
import org.eclipse.milo.opcua.sdk.client.api.config.OpcUaClientConfig; |
|
8 |
import org.eclipse.milo.opcua.sdk.client.api.identity.AnonymousProvider; |
|
9 |
import org.eclipse.milo.opcua.stack.client.DiscoveryClient; |
|
10 |
import org.eclipse.milo.opcua.stack.core.Stack; |
|
11 |
import org.eclipse.milo.opcua.stack.core.security.SecurityPolicy; |
|
12 |
import org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText; |
|
13 |
import org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned; |
|
14 |
import org.eclipse.milo.opcua.stack.core.types.structured.EndpointDescription; |
|
15 |
import org.springframework.beans.factory.annotation.Autowired; |
|
16 |
import org.springframework.stereotype.Component; |
|
17 |
|
|
18 |
import java.nio.file.Files; |
|
19 |
import java.nio.file.Path; |
|
20 |
import java.nio.file.Paths; |
|
21 |
import java.util.List; |
|
22 |
import java.util.concurrent.CompletableFuture; |
|
23 |
import java.util.concurrent.ExecutionException; |
|
24 |
import java.util.function.Predicate; |
|
25 |
|
|
26 |
/** |
|
27 |
* @ClassName: ClientRunner |
|
28 |
* @Description: 客户端启动类 |
|
29 |
* @author yyt |
|
30 |
* @date 2023年10月13日 |
|
31 |
*/ |
|
32 |
@Slf4j |
|
33 |
@Component |
|
34 |
public class ClientRunner { |
|
35 |
|
|
36 |
private final CompletableFuture<OpcUaClient> future = new CompletableFuture<>(); |
|
37 |
|
|
38 |
@Autowired |
|
39 |
private Properties properties; |
|
40 |
|
|
41 |
@Autowired |
|
42 |
private KeyStoreLoader keyStoreLoader; |
|
43 |
|
|
44 |
/** |
|
45 |
* @MethodName: run |
|
46 |
* @Description: 启动 |
|
47 |
* @return |
|
48 |
* @throws Exception |
|
49 |
* @CreateTime 2023年10月13日 |
|
50 |
*/ |
|
51 |
public OpcUaClient run() throws Exception { |
|
52 |
|
|
53 |
OpcUaClient client = createClient(); |
|
54 |
|
|
55 |
future.whenCompleteAsync((c, ex) -> { |
|
56 |
if (ex != null) { |
|
57 |
log.error("Error running example: {}", ex.getMessage(), ex); |
|
58 |
} |
|
59 |
|
|
60 |
try { |
|
61 |
c.disconnect().get(); |
|
62 |
Stack.releaseSharedResources(); |
|
63 |
} catch (InterruptedException | ExecutionException e) { |
|
64 |
log.error("Error disconnecting:", e.getMessage(), e); |
|
65 |
} |
|
66 |
}); |
|
67 |
|
|
68 |
return client; |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* @MethodName: createClient |
|
73 |
* @Description: 创建客户端 |
|
74 |
* @return |
|
75 |
* @throws Exception |
|
76 |
* @CreateTime 2023年10月13日 |
|
77 |
*/ |
|
78 |
private OpcUaClient createClient() throws Exception { |
|
79 |
|
|
80 |
Path securityTempDir = Paths.get(properties.getCertPath(), "security"); |
|
81 |
Files.createDirectories(securityTempDir); |
|
82 |
if (!Files.exists(securityTempDir)) { |
|
83 |
log.error("unable to create security dir: " + securityTempDir); |
|
84 |
return null; |
|
85 |
} |
|
86 |
|
|
87 |
KeyStoreLoader loader = keyStoreLoader.load(securityTempDir); |
|
88 |
|
|
89 |
// 搜索OPC节点 |
|
90 |
List<EndpointDescription> endpoints = null; |
|
91 |
try { |
|
92 |
//获取安全策略 |
|
93 |
endpoints = DiscoveryClient.getEndpoints(properties.getEndpointUrl()).get(); |
|
94 |
} catch (Throwable e) { |
|
95 |
// try the explicit discovery endpoint as well |
|
96 |
String discoveryUrl = properties.getEndpointUrl(); |
|
97 |
|
|
98 |
if (!discoveryUrl.endsWith("/")) { |
|
99 |
discoveryUrl += "/"; |
|
100 |
} |
|
101 |
discoveryUrl += "discovery"; |
|
102 |
|
|
103 |
log.info("Trying explicit discovery URL: {}", discoveryUrl); |
|
104 |
endpoints = DiscoveryClient.getEndpoints(discoveryUrl).get(); |
|
105 |
} |
|
106 |
|
|
107 |
EndpointDescription endpoint = endpoints.stream() |
|
108 |
.filter(e -> e.getSecurityPolicyUri().equals(SecurityPolicy.None.getUri())).filter(endpointFilter()) |
|
109 |
.findFirst().orElseThrow(() -> new Exception("no desired endpoints returned")); |
|
110 |
|
|
111 |
OpcUaClientConfig config = OpcUaClientConfig.builder() |
|
112 |
// opc ua自定义的名称 |
|
113 |
.setApplicationName(LocalizedText.english("plc")) |
|
114 |
// 地址 |
|
115 |
.setApplicationUri(properties.getEndpointUrl()) |
|
116 |
.setCertificate(loader.getClientCertificate()).setKeyPair(loader.getClientKeyPair()) |
|
117 |
// 安全策略等配置 |
|
118 |
//.setEndpoint(endpoint).setIdentityProvider(new UsernameProvider("OPCUA", "yyt@8888888888")) |
|
119 |
.setEndpoint(endpoint) |
|
120 |
// 匿名验证 |
|
121 |
.setIdentityProvider(new AnonymousProvider()) |
|
122 |
//等待时间 |
|
123 |
.setRequestTimeout(Unsigned.uint(5000)).build(); |
|
124 |
|
|
125 |
return OpcUaClient.create(config); |
|
126 |
|
|
127 |
} |
|
128 |
|
|
129 |
/** |
|
130 |
* @MethodName: endpointFilter |
|
131 |
* @Description: endpointFilter |
|
132 |
* @return |
|
133 |
* @CreateTime 2023年10月13日 |
|
134 |
*/ |
|
135 |
private Predicate<EndpointDescription> endpointFilter() { |
|
136 |
return e -> true; |
|
137 |
} |
|
138 |
|
|
139 |
/** |
|
140 |
* @return the future |
|
141 |
*/ |
|
142 |
public CompletableFuture<OpcUaClient> getFuture() { |
|
143 |
return future; |
|
144 |
} |
|
145 |
|
|
146 |
} |