/// <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); }
/// <summary> /// Returns the where clause defined by the filter declaration. /// </summary> private ContentFilter GetWhereClause() { ContentFilter whereClause = new ContentFilter(); ContentFilterElement element1 = null; // filter by source. if (SelectedSources != null && SelectedSources.Count > 0) { SimpleAttributeOperand operand1 = new SimpleAttributeOperand(); operand1.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType; operand1.AttributeId = Attributes.Value; operand1.BrowsePath.Add(new QualifiedName(Opc.Ua.BrowseNames.SourceNode)); for (int ii = 0; ii < SelectedSources.Count; ii++) { LiteralOperand operand2 = new LiteralOperand(); operand2.Value = SelectedSources[ii]; ContentFilterElement element2 = whereClause.Push(FilterOperator.Equals, operand1, operand2); element1 = (element1 != null)?whereClause.Push(FilterOperator.Or, element1, element2):element2; } } // add condition/tracking categories if no other categories selected. if (RevisedCategories == null || RevisedCategories.Count == 0) { if (EventTypes == (OpcRcw.Ae.Constants.SIMPLE_EVENT | OpcRcw.Ae.Constants.TRACKING_EVENT)) { ContentFilterElement element2 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.ConditionType); ContentFilterElement element3 = whereClause.Push(FilterOperator.Not, element2); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element3) : element3; } else if (EventTypes == (OpcRcw.Ae.Constants.SIMPLE_EVENT | OpcRcw.Ae.Constants.CONDITION_EVENT)) { ContentFilterElement element2 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.AuditEventType); ContentFilterElement element3 = whereClause.Push(FilterOperator.Not, element2); ContentFilterElement element4 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.TransitionEventType); ContentFilterElement element5 = whereClause.Push(FilterOperator.Not, element4); ContentFilterElement element6 = whereClause.Push(FilterOperator.Or, element3, element5); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element6) : element6; } else if (EventTypes == (OpcRcw.Ae.Constants.TRACKING_EVENT | OpcRcw.Ae.Constants.CONDITION_EVENT)) { ContentFilterElement element2 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.AuditEventType); ContentFilterElement element3 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.TransitionEventType); ContentFilterElement element4 = whereClause.Push(FilterOperator.Or, element2, element3); ContentFilterElement element5 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.ConditionType); ContentFilterElement element6 = whereClause.Push(FilterOperator.Or, element4, element5); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element6) : element6; } else if (EventTypes == OpcRcw.Ae.Constants.TRACKING_EVENT) { ContentFilterElement element2 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.AuditEventType); ContentFilterElement element3 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.TransitionEventType); ContentFilterElement element4 = whereClause.Push(FilterOperator.Or, element2, element3); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element4) : element4; } else if (EventTypes == OpcRcw.Ae.Constants.CONDITION_EVENT) { ContentFilterElement element2 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.ConditionType); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element2) : element2; } else if (EventTypes == OpcRcw.Ae.Constants.SIMPLE_EVENT) { ContentFilterElement element2 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.AuditEventType); ContentFilterElement element3 = whereClause.Push(FilterOperator.Not, element2); ContentFilterElement element4 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.TransitionEventType); ContentFilterElement element5 = whereClause.Push(FilterOperator.Not, element4); ContentFilterElement element6 = whereClause.Push(FilterOperator.Or, element3, element5); ContentFilterElement element7 = whereClause.Push(FilterOperator.OfType, Opc.Ua.ObjectTypeIds.ConditionType); ContentFilterElement element8 = whereClause.Push(FilterOperator.Not, element7); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element8) : element8; } } // filter by event type. if (RevisedCategories.Count > 0) { SimpleAttributeOperand operand1 = new SimpleAttributeOperand(); operand1.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType; operand1.AttributeId = Attributes.Value; operand1.BrowsePath.Add(new QualifiedName(Opc.Ua.BrowseNames.EventType)); ContentFilterElement element3 = null; for (int ii = 0; ii < RevisedCategories.Count; ii++) { ContentFilterElement element2 = whereClause.Push(FilterOperator.Equals, operand1, RevisedCategories[ii].TypeId); element3 = (element3 != null) ? whereClause.Push(FilterOperator.Or, element2, element3) : element2; } element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element3) : element3; } // filter by severity. if (LowSeverity > 1 || HighSeverity < 1000) { SimpleAttributeOperand operand1 = new SimpleAttributeOperand(); operand1.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType; operand1.AttributeId = Attributes.Value; operand1.BrowsePath.Add(new QualifiedName(Opc.Ua.BrowseNames.Severity)); if (LowSeverity > 1) { LiteralOperand operand2 = new LiteralOperand(); operand2.Value = LowSeverity; ContentFilterElement element2 = whereClause.Push(FilterOperator.GreaterThanOrEqual, operand1, operand2); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element2) : element2; } if (HighSeverity < 1000) { LiteralOperand operand2 = new LiteralOperand(); operand2.Value = HighSeverity; ContentFilterElement element2 = whereClause.Push(FilterOperator.LessThanOrEqual, operand1, operand2); element1 = (element1 != null) ? whereClause.Push(FilterOperator.And, element1, element2) : element2; } } return(whereClause); }
/// <summary> /// Displays the dialog. /// </summary> public FilterOperand ShowDialog( Session session, IList <ContentFilterElement> elements, int index, FilterOperand operand) { if (session == null) { throw new ArgumentNullException("session"); } if (elements == null) { throw new ArgumentNullException("elements"); } m_session = session; TypeDefinitionIdCTRL.Browser = new Browser(session); TypeDefinitionIdCTRL.RootId = ObjectTypes.BaseEventType; OperandTypeCB.SelectedItem = typeof(LiteralOperand).Name; if (operand != null) { OperandTypeCB.SelectedItem = operand.GetType().Name; } ElementsCB.Items.Clear(); for (int ii = index + 1; ii < elements.Count; ii++) { ElementsCB.Items.Add(elements[ii].ToString(m_session.NodeCache)); } ElementOperand elementOperand = operand as ElementOperand; if (elementOperand != null) { ElementsCB.SelectedIndex = (int)elementOperand.Index - index - 1; } AttributeOperand attributeOperand = operand as AttributeOperand; if (attributeOperand != null) { TypeDefinitionIdCTRL.Identifier = attributeOperand.NodeId; BrowsePathTB.Text = attributeOperand.BrowsePath.Format(session.NodeCache.TypeTree); AttributeIdCB.SelectedItem = Attributes.GetBrowseName(attributeOperand.AttributeId); IndexRangeTB.Text = attributeOperand.IndexRange; AliasTB.Text = attributeOperand.Alias; } LiteralOperand literalOperand = operand as LiteralOperand; if (literalOperand != null) { NodeId datatypeId = TypeInfo.GetDataTypeId(literalOperand.Value.Value); DataTypeCB.SelectedItem = TypeInfo.GetBuiltInType(datatypeId); StringBuilder buffer = new StringBuilder(); Array array = literalOperand.Value.Value as Array; if (array != null) { for (int ii = 0; ii < array.Length; ii++) { if (ii > 0) { buffer.Append("\r\n"); } buffer.AppendFormat("{0}", new Variant(array.GetValue(ii))); } } else { buffer.AppendFormat("{0}", literalOperand.Value); } ValueTB.Text = buffer.ToString(); } if (ShowDialog() != DialogResult.OK) { return(null); } return(operand); }