|
| 1 | +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. |
| 3 | +// Website: https://www.blazor.zone or https://argozhang.github.io/ |
| 4 | + |
| 5 | +using Microsoft.Extensions.Configuration; |
| 6 | +using Opc.Ua; |
| 7 | +using Opc.Ua.Client; |
| 8 | +using Opc.Ua.Configuration; |
| 9 | + |
| 10 | +namespace UnitTestOpcUa; |
| 11 | + |
| 12 | +public class UnitTest1 |
| 13 | +{ |
| 14 | + [Fact] |
| 15 | + public async Task Connect_Ok() |
| 16 | + { |
| 17 | + var config = new ApplicationConfiguration() |
| 18 | + { |
| 19 | + ApplicationName = "KEPServerEX Client", |
| 20 | + ApplicationType = ApplicationType.Client, |
| 21 | + ApplicationUri = "urn:" + Utils.GetHostName() + ":KEPServerEXClient", |
| 22 | + SecurityConfiguration = new SecurityConfiguration |
| 23 | + { |
| 24 | + ApplicationCertificate = new CertificateIdentifier { StoreType = "X509Store", StorePath = "CurrentUser\\My" }, |
| 25 | + TrustedPeerCertificates = new CertificateTrustList { StoreType = "Directory", StorePath = "OPC Foundation/CertificateStores/UA Applications" }, |
| 26 | + RejectedCertificateStore = new CertificateTrustList { StoreType = "Directory", StorePath = "OPC Foundation/CertificateStores/RejectedCertificates" }, |
| 27 | + AutoAcceptUntrustedCertificates = true // 仅用于测试环境 |
| 28 | + }, |
| 29 | + TransportConfigurations = new TransportConfigurationCollection(), |
| 30 | + TransportQuotas = new TransportQuotas { OperationTimeout = 15000 }, |
| 31 | + ClientConfiguration = new ClientConfiguration { DefaultSessionTimeout = 60000 } |
| 32 | + }; |
| 33 | + |
| 34 | + //await config.Validate(ApplicationType.Client); |
| 35 | + |
| 36 | + // 创建端点描述 |
| 37 | + var endpointDescription = CoreClientUtils.SelectEndpoint("opc.tcp://127.0.0.1:49320", false); |
| 38 | + var endpointConfiguration = EndpointConfiguration.Create(config); |
| 39 | + var endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration); |
| 40 | + |
| 41 | + // 创建会话 |
| 42 | + var identity = new UserIdentity(); // 匿名登录,或提供用户名密码 |
| 43 | + var session = await Session.Create( |
| 44 | + config, |
| 45 | + endpoint, |
| 46 | + false, |
| 47 | + false, |
| 48 | + config.ApplicationName, |
| 49 | + 60000, |
| 50 | + identity, |
| 51 | + null); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact] |
| 55 | + public async Task FindServersAsync() |
| 56 | + { |
| 57 | + // 配置与连接过程同前述基本客户端 |
| 58 | + var application = new ApplicationInstance |
| 59 | + { |
| 60 | + ApplicationName = "OPC UA Basic Client", |
| 61 | + ApplicationType = ApplicationType.Client |
| 62 | + }; |
| 63 | + var applicationConfiguration = new ApplicationConfiguration |
| 64 | + { |
| 65 | + ApplicationName = application.ApplicationName, |
| 66 | + ApplicationType = application.ApplicationType, |
| 67 | + ClientConfiguration = new ClientConfiguration() |
| 68 | + }; |
| 69 | + var endpointURL = "opc.tcp://127.0.0.1:49320"; |
| 70 | + var endpointDescription = CoreClientUtils.SelectEndpoint(applicationConfiguration, endpointURL, false); |
| 71 | + var endpointConfiguration = EndpointConfiguration.Create(); |
| 72 | + var endpoint = new ConfiguredEndpoint(null, endpointDescription, endpointConfiguration); |
| 73 | + |
| 74 | + var session = await Session.Create( |
| 75 | + configuration: applicationConfiguration, |
| 76 | + endpoint: endpoint, |
| 77 | + updateBeforeConnect: false, |
| 78 | + sessionName: "Opc.Session.BootstrapBlazor", |
| 79 | + sessionTimeout: 60000, |
| 80 | + identity: null, |
| 81 | + preferredLocales: null); |
| 82 | + } |
| 83 | +} |
0 commit comments