/// <summary>
        /// Creates a Subscription.
        /// </summary>
        /// <param name="client">A instance of <see cref="ISessionClient"/>.</param>
        /// <param name="request">A <see cref="CreateSubscriptionRequest"/>.</param>
        /// <returns>A <see cref="Task"/> representing the asynchronous operation that returns a <see cref="CreateSubscriptionResponse"/>.</returns>
        public static async Task<CreateSubscriptionResponse> CreateSubscriptionAsync(this ISessionClient client, CreateSubscriptionRequest request)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            return (CreateSubscriptionResponse)await client.RequestAsync(request).ConfigureAwait(false);
        }
Exemplo n.º 2
0
        public async Task TransferSubscriptions()
        {
            // get or add application certificate.
            var localCertificate = this.localDescription.GetCertificate();
            if (localCertificate == null)
            {
                throw new ServiceResultException(StatusCodes.BadSecurityChecksFailed, "Application certificate is missing.");
            }

            // discover available endpoints of server.
            var getEndpointsRequest = new GetEndpointsRequest
            {
                EndpointUrl = this.endpointUrl,
                ProfileUris = new[] { TransportProfileUris.UaTcpTransport }
            };
            Console.WriteLine($"Discovering endpoints of '{getEndpointsRequest.EndpointUrl}'.");
            var getEndpointsResponse = await UaTcpDiscoveryClient.GetEndpointsAsync(getEndpointsRequest);
            var selectedEndpoint = getEndpointsResponse.Endpoints.OrderBy(e => e.SecurityLevel).Last();

            IUserIdentity selectedUserIdentity = new UserNameIdentity("root", "secret");

            var client = new UaTcpSessionClient(
                this.localDescription,
                localCertificate,
                selectedUserIdentity,
                selectedEndpoint);
            Console.WriteLine($"Creating session with endpoint '{client.RemoteEndpoint.EndpointUrl}'.");
            Console.WriteLine($"SecurityPolicy: '{client.RemoteEndpoint.SecurityPolicyUri}'.");
            Console.WriteLine($"SecurityMode: '{client.RemoteEndpoint.SecurityMode}'.");
            await client.OpenAsync();
            Console.WriteLine($"Activated session '{client.SessionId}'.");
            var req = new CreateSubscriptionRequest
            {
                RequestedPublishingInterval = 1000,
                RequestedMaxKeepAliveCount = 20,
                PublishingEnabled = true
            };
            var res = await client.CreateSubscriptionAsync(req);
            Console.WriteLine($"Created subscription '{res.SubscriptionId}'.");

            Console.WriteLine($"Aborting session '{client.SessionId}'.");
            await client.AbortAsync();

            var client2 = new UaTcpSessionClient(
                this.localDescription,
                localCertificate,
                selectedUserIdentity,
                selectedEndpoint);
            await client2.OpenAsync();
            Console.WriteLine($"Activated session '{client2.SessionId}'.");

            var req2 = new TransferSubscriptionsRequest
            {
                SubscriptionIds = new[] { res.SubscriptionId }
            };
            var res2 = await client2.TransferSubscriptionsAsync(req2);
            Console.WriteLine($"Transferred subscription result '{res2.Results[0].StatusCode}'.");

            Assert.IsTrue(StatusCode.IsGood(res2.Results[0].StatusCode));

            Console.WriteLine($"Closing session '{client2.SessionId}'.");
            await client2.CloseAsync();
        }
        /// <summary>
        /// Creates new subscriptions on the server.
        /// </summary>
        /// <param name="cancellationToken">A cancellation token. </param>
        /// <returns>A task.</returns>
        private async Task CreateNewSubscriptionsAsync(CancellationToken cancellationToken = default(CancellationToken))
        {
            var subscriptionsChanged = new AsyncAutoResetEvent();
            var handler = new NotifyCollectionChangedEventHandler((s, e) => subscriptionsChanged.Set());
            this.Subscriptions.CollectionChanged += handler;

            try
            {
                while (!cancellationToken.IsCancellationRequested)
                {
                    foreach (var subscription in this.Subscriptions)
                    {
                        if (subscription.Id == 0)
                        {
                            try
                            {
                                // create the subscription.
                                var subscriptionRequest = new CreateSubscriptionRequest
                                {
                                    RequestedPublishingInterval = subscription.PublishingInterval,
                                    RequestedMaxKeepAliveCount = subscription.KeepAliveCount,
                                    RequestedLifetimeCount = subscription.LifetimeCount > 0 ? subscription.LifetimeCount : (uint)(this.SessionTimeout / subscription.PublishingInterval),
                                    PublishingEnabled = false, // initially
                                    Priority = subscription.Priority
                                };
                                var subscriptionResponse = await this.CreateSubscriptionAsync(subscriptionRequest);
                                var id = subscription.Id = subscriptionResponse.SubscriptionId;

                                // add the items.
                                if (subscription.MonitoredItems.Count > 0)
                                {
                                    var items = subscription.MonitoredItems.ToList();
                                    var requests = items.Select(m => new MonitoredItemCreateRequest { ItemToMonitor = new ReadValueId { NodeId = m.NodeId, AttributeId = m.AttributeId, IndexRange = m.IndexRange }, MonitoringMode = m.MonitoringMode, RequestedParameters = new MonitoringParameters { ClientHandle = m.ClientId, DiscardOldest = m.DiscardOldest, QueueSize = m.QueueSize, SamplingInterval = m.SamplingInterval, Filter = m.Filter } }).ToArray();
                                    var itemsRequest = new CreateMonitoredItemsRequest
                                    {
                                        SubscriptionId = id,
                                        ItemsToCreate = requests,
                                    };
                                    var itemsResponse = await this.CreateMonitoredItemsAsync(itemsRequest);
                                    for (int i = 0; i < itemsResponse.Results.Length; i++)
                                    {
                                        var item = items[i];
                                        var result = itemsResponse.Results[i];
                                        item.ServerId = result.MonitoredItemId;
                                        if (StatusCode.IsBad(result.StatusCode))
                                        {
                                            Log.Warn($"Error response from MonitoredItemCreateRequest for {item.NodeId}. {result.StatusCode}");
                                        }
                                    }
                                }

                                // start publishing.
                                if (subscription.PublishingEnabled)
                                {
                                    var modeRequest = new SetPublishingModeRequest
                                    {
                                        SubscriptionIds = new[] { id },
                                        PublishingEnabled = true,
                                    };
                                    var modeResponse = await this.SetPublishingModeAsync(modeRequest);
                                }
                            }
                            catch (ServiceResultException ex)
                            {
                                Log.Warn($"Error creating subscription '{subscription.GetType().Name}'. {ex.Message}");
                            }
                        }
                    }

                    await subscriptionsChanged.WaitAsync().WithCancellation(cancellationToken);
                }
            }
            catch (OperationCanceledException)
            {
            }
            finally
            {
                this.Subscriptions.CollectionChanged -= handler;
            }
        }