EventHandler OPC UA

i'm trying to communicate a server to a client using opc ua, it works fine until the point I want my program to return values. Im using a OnNotification function:

public void OnNotification(MonitoredItem item, MonitoredItemNotificationEventArgs e)
{
   // Debug.Log("Entre aqui");

    foreach (var value in item.DequeueValues())
    {
       // Debug.Log("Entre aqui otra vez");
        //Console.WriteLine("{0}: {1}, {2}, {3}", item.DisplayName, value.Value, value.SourceTimestamp, value.StatusCode);
        Debug.Log("{0}: {1}, {2}, {3}" + item.DisplayName + value.Value + value.SourceTimestamp + value.StatusCode);
        //rotZ = float.Parse(value.Value.ToString());
        //J1.Rotate(0, 0, rotZ);
    }
}

This is where I create the Monitored Item and I add the MonitoredItem EventHandler to the function, but I don't why the event of i.Notification never occurs, so it never runs the OnNotification function. Should I trigger the event to occur? What am I doing wrong?

        var filter = new EventFilter();


        var triggeringItemId = new MonitoredItem(subscription.DefaultItem)
        {
            NodeClass = NodeClass.Object,
            StartNodeId = ObjectIds.Server,
            AttributeId = Attributes.EventNotifier,
            MonitoringMode = MonitoringMode.Reporting,
            SamplingInterval = -1,
            QueueSize = 100,
            CacheQueueSize = 100,
            Filter = filter 
        };
        // Log("Step 5 - Add a list of items you wish to monitor to the subscription.");
        var list = new List<MonitoredItem> {
            triggeringItemId,
            /*
            monitoredItem,
            monitoredItem2
            */
        };

        list.ForEach(i => i.Notification += OnNotification);

        Debug.Log(list);

        subscription.AddItems(list);

        // Log("Step 6 - Add the subscription to the session.");
        session.AddSubscription(subscription);
        subscription.Create();

Upvotes: 2

Views: 1832

Answers (2)

Andrew Cullen
Andrew Cullen

Reputation: 868

Try adding some 'Select' clauses to the EventFilter. The following list corresponds to the basic event type. The AlarmCondition types provide many more fields.

var filter = new EventFilter();

filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.EventId); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.EventType); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.SourceNode); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.SourceName); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.Time); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.ReceiveTime); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.LocalTime); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.Message); filter.AddSelectClause(ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.Severity);

Upvotes: 2

greenoldman
greenoldman

Reputation: 21062

In this order it works for me:

session.AddSubscription(subscription);
subscription.Create();

then I create MonitoredItems and then add them to subscription and the last step which you miss:

subscription.ApplyChanges();

Upvotes: 0

Related Questions