@Component public class ClientGen { //加载加密类型 static { Security.addProvider(new BouncyCastleProvider()); } private final Logger log = LoggerFactory.getLogger(getClass()); // milo用来连接opc ua的client public static OpcUaClient opcUaClient; @Autowired private OpcUaConfig opcUaConfig; / * 创建opc ua客户端(在项目启动的时候执行该方法) * */ @PostConstruct public void createClient_() throws UaException { String endPoint = opcUaConfig.getEndpointUrl(); opcUaClient = OpcUaClient.create( endPoint, endpointDescriptions -> { final List<EndpointDescription> collect = endpointDescriptions.stream().collect(Collectors.toList()); final Optional<EndpointDescription> first = endpointDescriptions .stream() .findFirst(); System.out.println(first.get().getSecurityPolicyUri()); return Optional.of(collect.get(0)); }, configBuilder -> configBuilder .setApplicationName(LocalizedText.english("opc-ua client")) .setApplicationUri(endPoint) // .setIdentityProvider(new UsernameProvider(username, password)) .setIdentityProvider(new AnonymousProvider()) .setRequestTimeout(uint(5000)) .build() ); log.info("创建客户端:{} 成功!", opcUaClient); } / * 创建opc ua客户端(在项目启动的时候执行该方法) * */ public void createClient(){ try { Path securityTempDir = Paths.get(System.getProperty("java.io.tmpdir"), "security"); Files.createDirectories(securityTempDir); if (!Files.exists(securityTempDir)) { throw new Exception("没有创建安全目录: " + securityTempDir); } log.info("安全目录: {}", securityTempDir.toAbsolutePath()); //加载秘钥 KeyStoreLoader loader = new KeyStoreLoader().load(securityTempDir); //安全策略 None、Basic256、Basic128Rsa15、Basic256Sha256 SecurityPolicy securityPolicy = SecurityPolicy.None; List<EndpointDescription> endpoints; try { endpoints = DiscoveryClient.getEndpoints(opcUaConfig.getEndpointUrl()).get(); } catch (Throwable ex) { String discoveryUrl = opcUaConfig.getEndpointUrl(); if (!discoveryUrl.endsWith("/")) { discoveryUrl += "/"; } discoveryUrl += "discovery"; log.info("开始连接 URL: {}", discoveryUrl); endpoints = DiscoveryClient.getEndpoints(discoveryUrl).get(); } EndpointDescription endpoint = endpoints.stream() .filter(e -> e.getEndpointUrl().equals(opcUaConfig.getEndpointUrl())) .findFirst().orElseThrow(() -> new Exception("没有节点返回")); //.filter(e -> e.getSecurityPolicyUri().equals(securityPolicy.getUri())) //.filter(opcUaConfig.endpointFilter()) //.findFirst() //.orElseThrow(() -> new Exception("没有连接上端点")); log.info("使用端点: {} [{}/{}]", endpoint.getEndpointUrl(), securityPolicy, endpoint.getSecurityMode()); OpcUaClientConfig config = OpcUaClientConfig.builder() .setApplicationName(LocalizedText.english("eclipse milo opc-ua client")) .setApplicationUri("urn:eclipse:milo:examples:client") .setCertificate(loader.getClientCertificate()) // 证书 .setKeyPair(loader.getClientKeyPair()) .setEndpoint(endpoint) //根据匿名验证和第三个用户名验证方式设置传入对象 AnonymousProvider(匿名方式)UsernameProvider(账户密码) .setIdentityProvider(new AnonymousProvider()) .setRequestTimeout(uint()) .build(); opcUaClient = OpcUaClient.create(config); log.info("创建客户端:{} 成功!", opcUaClient); } catch (Exception e) { log.error("创建客户端失败" + e.getMessage()); } } }
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/java-jiao-cheng/17927.html