A filter to apply when monitoring event.
        internal static Opc.Ua.EventFilter GetEventFilter(OpcUAQuery query, NamespaceTable namespaceTable)
        {
            var eventFilter = new Opc.Ua.EventFilter();

            if (query.eventQuery?.eventColumns != null)
            {
                foreach (var column in query.eventQuery.eventColumns)
                {
                    var bp   = Converter.GetBrowsePath(column.browsePath, namespaceTable);
                    var path = SimpleAttributeOperand.Format(bp);
                    eventFilter.AddSelectClause(ObjectTypes.BaseEventType, path, Attributes.Value);
                }
            }


            if (query.eventQuery?.eventFilters != null)
            {
                for (int i = 0; i < query.eventQuery.eventFilters.Length; i++)
                {
                    var filter = query.eventQuery.eventFilters[i];
                    eventFilter.WhereClause.Push(filter.oper, GetOperands(filter, namespaceTable));
                }
            }
            return(eventFilter);
        }
Example #2
0
 internal EventFilterValues(MonitoredItem monitoredItem, OpcUAQuery query, Opc.Ua.EventFilter filter)
 {
     MonitoredItem = monitoredItem;
     Query         = query;
     Filter        = filter;
     Values        = new Dictionary <SourceAndEventTypeKey, VariantCollection>();
     LastRead      = DateTimeOffset.UtcNow;
 }
Example #3
0
        private MonitoredItem AddMonitorItem(NodeId startNodeId, Opc.Ua.EventFilter eventFilter)
        {
            var monitorItem = CreateMonitoredItem(startNodeId, eventFilter);

            monitorItem.Notification += MonitorItem_Notification;
            _subscription.AddItem(monitorItem);
            _subscription.ApplyChanges();
            monitorItem.Subscription.ConditionRefresh();
            return(monitorItem);
        }
Example #4
0
 private EventFilterValues GetEventFilterValues(NodeId startNodeId, Opc.Ua.EventFilter eventFilter)
 {
     lock (_eventData)
     {
         if (_eventData.TryGetValue(startNodeId, out List <EventFilterValues> values))
         {
             var vals = values.FirstOrDefault(a => a.Filter.IsEqual(eventFilter));
             if (vals != null)
             {
                 vals.LastRead = DateTimeOffset.UtcNow;
                 return(vals);
             }
         }
     }
     return(null);
 }
Example #5
0
        private MonitoredItem CreateMonitoredItem(NodeId startNodeId, Opc.Ua.EventFilter eventFilter)
        {
            // create the item with the filter.
            MonitoredItem monitoredItem = new MonitoredItem();

            monitoredItem.DisplayName      = null;
            monitoredItem.StartNodeId      = startNodeId;
            monitoredItem.RelativePath     = null;
            monitoredItem.NodeClass        = NodeClass.Object;
            monitoredItem.AttributeId      = Attributes.EventNotifier;
            monitoredItem.IndexRange       = null;
            monitoredItem.Encoding         = null;
            monitoredItem.MonitoringMode   = MonitoringMode.Reporting;
            monitoredItem.SamplingInterval = 0;
            monitoredItem.QueueSize        = UInt32.MaxValue;
            monitoredItem.DiscardOldest    = true;
            monitoredItem.Filter           = eventFilter;

            // save the definition as the handle.
            //monitoredItem.Handle = this;

            return(monitoredItem);
        }
        /// <summary>
        /// Constructs the event filter for the subscription.
        /// </summary>
        /// <returns>The event filter.</returns>
        public EventFilter ConstructFilter(Session session)
        {
            EventFilter filter = new EventFilter();

            // the select clauses specify the values returned with each event notification.
            filter.SelectClauses = SelectClauses;

            // the where clause restricts the events returned by the server.
            // it works a lot like the WHERE clause in a SQL statement and supports
            // arbitrary expession trees where the operands are literals or event fields.
            ContentFilter whereClause = new ContentFilter();

            // the code below constructs a filter that looks like this:
            // (Severity >= X OR LastSeverity >= X) AND (SuppressedOrShelved == False) AND (OfType(A) OR OfType(B))

            // add the severity.
            ContentFilterElement element1 = null;
            ContentFilterElement element2 = null;

            if (Severity > EventSeverity.Min)
            {
                // select the Severity property of the event.
                SimpleAttributeOperand operand1 = new SimpleAttributeOperand();
                operand1.TypeDefinitionId = ObjectTypeIds.BaseEventType;
                operand1.BrowsePath.Add(BrowseNames.Severity);
                operand1.AttributeId = Attributes.Value;

                // specify the value to compare the Severity property with.
                LiteralOperand operand2 = new LiteralOperand();
                operand2.Value = new Variant((ushort)Severity);

                // specify that the Severity property must be GreaterThanOrEqual the value specified.
                element1 = whereClause.Push(FilterOperator.GreaterThanOrEqual, operand1, operand2);
            }

            // add the suppressed or shelved.
            if (!IgnoreSuppressedOrShelved)
            {
                // select the SuppressedOrShelved property of the event.
                SimpleAttributeOperand operand1 = new SimpleAttributeOperand();
                operand1.TypeDefinitionId = ObjectTypeIds.BaseEventType;
                operand1.BrowsePath.Add(BrowseNames.SuppressedOrShelved);
                operand1.AttributeId = Attributes.Value;

                // specify the value to compare the Severity property with.
                LiteralOperand operand2 = new LiteralOperand();
                operand2.Value = new Variant(false);

                // specify that the Severity property must Equal the value specified.
                element2 = whereClause.Push(FilterOperator.Equals, operand1, operand2);
                
                // chain multiple elements together with an AND clause.
                if (element1 != null)
                {
                    element1 = whereClause.Push(FilterOperator.And, element1, element2);
                }
                else
                {
                    element1 = element2;
                }
            }

            // add the event types.
            if (EventTypes != null && EventTypes.Count > 0)
            {
                element2 = null;

                // save the last element.
                for (int ii = 0; ii < EventTypes.Count; ii++)
                {
                    // for this example uses the 'OfType' operator to limit events to thoses with specified event type. 
                    LiteralOperand operand1 = new LiteralOperand();
                    operand1.Value = new Variant(EventTypes[ii]);
                    ContentFilterElement element3 = whereClause.Push(FilterOperator.OfType, operand1);

                    // need to chain multiple types together with an OR clause.
                    if (element2 != null)
                    {
                        element2 = whereClause.Push(FilterOperator.Or, element2, element3);
                    }
                    else
                    {
                        element2 = element3;
                    }
                }

                // need to link the set of event types with the previous filters.
                if (element1 != null)
                {
                    whereClause.Push(FilterOperator.And, element1, element2);
                }
            }

            filter.WhereClause = whereClause;

            // return filter.
            return filter;
        }
Example #7
0
        /// <summary>
        /// Returns the subscription filter to use.
        /// </summary>
        public EventFilter GetFilter()
        {
            ContentFilter whereClause = new ContentFilter();
            ContentFilterElement element1 = whereClause.Push(FilterOperator.OfType, EventTypeId);

            EventFilter filter = new EventFilter();

            for (int ii = 0; ii < Fields.Count; ii++)
            {
                filter.SelectClauses.Add(Fields[ii].Operand);

                if (Fields[ii].FilterValue != Variant.Null)
                {
                    LiteralOperand operand = new LiteralOperand();
                    operand.Value = Fields[ii].FilterValue;
                    ContentFilterElement element2 = whereClause.Push(Fields[ii].FilterOperator, Fields[ii].Operand, operand);
                    element1 = whereClause.Push(FilterOperator.And, element1, element2);
                }
            }

            filter.WhereClause = whereClause;

            return filter;
        }
Example #8
0
        public Result <DataResponse> GetEventData(OpcUAQuery query, NodeId startNodeId, Opc.Ua.EventFilter eventFilter)
        {
            var nodeCache = _nodeCacheFactory.Create(_session);

            eventFilter.AddSelectClause(ObjectTypeIds.BaseEventType, "SourceNode");
            eventFilter.AddSelectClause(ObjectTypeIds.BaseEventType, "EventType");

            var eventFilterValues = GetEventFilterValues(startNodeId, eventFilter);

            if (eventFilterValues == null)
            {
                var monitorItem = AddMonitorItem(startNodeId, eventFilter);
                eventFilterValues = new EventFilterValues(monitorItem, query, eventFilter);
                if (!TryAddEventFilterValues(startNodeId, eventFilterValues))
                {
                    RemoveMonitorItem(monitorItem);
                    eventFilterValues = null;
                }
            }

            lock (_eventData)
            {
                if (eventFilterValues == null)
                {
                    eventFilterValues = GetEventFilterValues(startNodeId, eventFilter);
                }
                if (eventFilterValues != null)
                {
                    return(_eventDataResponse.CreateEventSubscriptionDataResponse(eventFilterValues.Values.Values, query, nodeCache));
                }
            }
            return(new Result <DataResponse>(StatusCodes.BadUnexpectedError, ""));
        }