/// <summary> /// Initializes a new instance of the class. /// </summary> public FrmSecurityOptions(OpcConnectionOptions connectionOptions) : this() { this.connectionOptions = connectionOptions; FillComboBoxes(); }
/// <summary> /// Connects to the OPC server asynchronously. /// </summary> public async Task <bool> ConnectAsync(OpcConnectionOptions connectionOptions, int operationTimeout = -1) { ApplicationInstance application = new ApplicationInstance { ApplicationName = string.Format("DrvOpcUa_{0} Driver", deviceNumStr), ApplicationType = ApplicationType.Client, ConfigSectionName = "Scada.Comm.Drivers.DrvOpcUa" }; // load the application configuration // TODO: use stream instead of file after updating OPCFoundation.NetStandard.Opc.Ua WriteConfigFile(out string configFileName); ApplicationConfiguration config = await application.LoadApplicationConfiguration(configFileName, false); // check the application certificate bool haveAppCertificate = await application.CheckApplicationInstanceCertificate(false, 0); if (!haveAppCertificate) { throw new ScadaException(Locale.IsRussian ? "Сертификат экземпляра приложения недействителен!" : "Application instance certificate invalid!"); } if (haveAppCertificate) { config.ApplicationUri = X509Utils.GetApplicationUriFromCertificate( config.SecurityConfiguration.ApplicationCertificate.Certificate); if (config.SecurityConfiguration.AutoAcceptUntrustedCertificates) { AutoAccept = true; } config.CertificateValidator.CertificateValidation += CertificateValidator_CertificateValidation; } else { log.WriteLine(Locale.IsRussian ? "Предупреждение: отсутствует сертификат приложения, используется незащищенное соединение." : "Warning: missing application certificate, using unsecure connection."); } // create session EndpointDescription selectedEndpoint = operationTimeout > 0 ? CoreClientUtils.SelectEndpoint(connectionOptions.ServerUrl, haveAppCertificate, operationTimeout) : CoreClientUtils.SelectEndpoint(connectionOptions.ServerUrl, haveAppCertificate); selectedEndpoint.SecurityMode = connectionOptions.SecurityMode; selectedEndpoint.SecurityPolicyUri = connectionOptions.GetSecurityPolicy(); EndpointConfiguration endpointConfiguration = EndpointConfiguration.Create(config); ConfiguredEndpoint endpoint = new ConfiguredEndpoint(null, selectedEndpoint, endpointConfiguration); UserIdentity userIdentity = connectionOptions.AuthenticationMode == AuthenticationMode.Username ? new UserIdentity(connectionOptions.Username, connectionOptions.Password) : new UserIdentity(new AnonymousIdentityToken()); OpcSession = await Session.Create(config, endpoint, false, "Rapid SCADA DrvOpcUa_" + deviceNumStr, (uint)config.ClientConfiguration.DefaultSessionTimeout, userIdentity, null); log.WriteLine(Locale.IsRussian ? "OPC сессия успешно создана: {0}" : "OPC session created successfully: {0}", connectionOptions.ServerUrl); return(true); }