convertersystems / opc-ua-samples

Sample HMIs using OPC Unified Architecture (OPC UA) and Visual Studio.
MIT License
107 stars 43 forks source link

Publish several subscriptions #16

Closed abrasat closed 7 years ago

abrasat commented 7 years ago

Could you please post a sample about how to publish several subscriptions ? In the ConsoleApp sample the PublishRequest class is used as follows

var publishRequest = new PublishRequest { SubscriptionAcknowledgements = new SubscriptionAcknowledgement[0] }; ... var publishResponse = await session.PublishAsync(publishRequest).ConfigureAwait(false);

How does the PublishRequest relate to the previously created Subscription/MonitoringItemRequest ? What if there are several subcriptions ?

awcullen commented 7 years ago

When you call PublishAsync, the response can be for any subscription of the session. PublishResponse has field for SubscriptionId, which will match a SubscriptionId that was previously returned in the CreateSubscriptionResponse.

I edited 'program.cs', adding Subscription ID to the console output to be more clear.

abrasat commented 7 years ago

Thanks for that. But what about the first "dummy" PublishRequest object before the while loop (at this point there is no publish response available yet) , it does not contain any subscription id. The first call to PublishAsync() in your sample uses that "dummy" publish request, and only the subsequent calls use the subscription id from the PublishResponse. Also it would be helpful if you could extend the sample with the usage of 2 different subscriptions for the same session.

Console.WriteLine("Step 8 - Publish the subscription."); var publishRequest = new PublishRequest { SubscriptionAcknowledgements = new SubscriptionAcknowledgement[0] }; Console.WriteLine("Press any key to delete the subscription..."); while (!Console.KeyAvailable) { var publishResponse = await session.PublishAsync(publishRequest).ConfigureAwait(false); // loop thru all the data change notifications var dcns = publishResponse.NotificationMessage.NotificationData.OfType); ....

awcullen commented 7 years ago

I see the confusion. The SubscriptionAcknowledgements field holds the sequence number of the previous PublishResponse. The purpose is to allow the server to release the memory of the previous PublishResponse (Servers have to keep old responses around until they are Acknowledged)