Пример #1
0
        // Full constructor
        public OPCClient(string OPCAddress, string username, string password, ILogger logger, string clientName, SecurityPolicy securityPolicy, MessageSecurity messageSecurity)
        {
            //Initialize Logger
            Logger = logger;
            Logger?.Init();

            _Address      = OPCAddress; //Example:  opc.tcp://localhost:4840
            _ClientName   = clientName;
            _OPC_User     = username;
            _OPC_Password = password;

            SecurityPolicy  = securityPolicy;
            MessageSecurity = messageSecurity;

            //Create opc client session
            Init();

            if (IsAvailable)
            {
                Logger?.LogDebug($"OPC - Created new client: {clientName}. Address: {_Address}.");
            }
            else
            {
                Logger?.LogDebug($"OPC - Client: {clientName} initialization failure. Started reconnection procedure.");
                //Check Connection Every X seconds
                Set_CheckConnection();
            }
        }
Пример #2
0
        public static EndpointDescription SelectEndpointBySecurity(string discoveryUrl, SecurityPolicy securityPolicy, MessageSecurity messageSecurity, int operationTimeout = -1)
        {
            Uri uri = new Uri(discoveryUrl);


            EndpointConfiguration configuration = EndpointConfiguration.Create();

            if (operationTimeout > 0)
            {
                configuration.OperationTimeout = operationTimeout;
            }

            EndpointDescription selectedEndpoint = null;
            MessageSecurityMode actualMessageSecurity;

            switch (messageSecurity)
            {
            case MessageSecurity.SignAndEncrypt:
                actualMessageSecurity = MessageSecurityMode.SignAndEncrypt;
                break;

            case MessageSecurity.Sign:
                actualMessageSecurity = MessageSecurityMode.Sign;
                break;

            case MessageSecurity.None:
            default:
                actualMessageSecurity = MessageSecurityMode.None;
                break;
            }

            // Connect to the server's discovery endpoint and find the available configuration.
            using (DiscoveryClient client = DiscoveryClient.Create(uri, configuration))
            {
                EndpointDescriptionCollection endpoints = client.GetEndpoints(null);

                selectedEndpoint = endpoints.FirstOrDefault(ep => ep.SecurityMode == actualMessageSecurity && ep.SecurityPolicyUri.Contains(securityPolicy.ToString()));
            }

            return(selectedEndpoint);
        }