Exemple #1
0
        /// <summary>
        /// Sets a default filter.
        /// </summary>
        public void SetDefault()
        {
            EventTypeId = Opc.Ua.ObjectTypeIds.BaseEventType;
            Fields = new List<FilterDefinitionField>();

            FilterDefinitionField field = new FilterDefinitionField();
            field.DisplayName = Opc.Ua.BrowseNames.EventType;
            field.DataType = Opc.Ua.DataTypeIds.NodeId;
            field.ValueRank = ValueRanks.Scalar;
            field.BuiltInType = BuiltInType.NodeId;
            field.DataTypeDisplayName = field.BuiltInType.ToString();
            field.Operand = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.EventType);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn = true;
            Fields.Add(field);

            field = new FilterDefinitionField();
            field.DisplayName = Opc.Ua.BrowseNames.SourceNode;
            field.DataType = Opc.Ua.DataTypeIds.NodeId;
            field.ValueRank = ValueRanks.Scalar;
            field.BuiltInType = BuiltInType.NodeId;
            field.DataTypeDisplayName = field.BuiltInType.ToString();
            field.Operand = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.SourceNode);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn = true;
            Fields.Add(field);

            field = new FilterDefinitionField();
            field.DisplayName = Opc.Ua.BrowseNames.Time;
            field.DataType = Opc.Ua.DataTypeIds.DateTime;
            field.ValueRank = ValueRanks.Scalar;
            field.BuiltInType = BuiltInType.DateTime;
            field.DataTypeDisplayName = field.BuiltInType.ToString();
            field.Operand = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.Time);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn = true;
            Fields.Add(field);

            field = new FilterDefinitionField();
            field.DisplayName = Opc.Ua.BrowseNames.EventId;
            field.DataType = Opc.Ua.DataTypeIds.ByteString;
            field.ValueRank = ValueRanks.Scalar;
            field.BuiltInType = BuiltInType.ByteString;
            field.DataTypeDisplayName = field.BuiltInType.ToString();
            field.Operand = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.EventId);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn = false;
            Fields.Add(field);

            field = new FilterDefinitionField();
            field.DisplayName = Opc.Ua.BrowseNames.Message;
            field.DataType = Opc.Ua.DataTypeIds.LocalizedText;
            field.ValueRank = ValueRanks.Scalar;
            field.BuiltInType = BuiltInType.LocalizedText;
            field.DataTypeDisplayName = field.BuiltInType.ToString();
            field.Operand = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.Message);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn = true;
            Fields.Add(field);
        }
        /// <summary>
        /// Handles the AfterSelect event of the BrowseTV control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.Forms.TreeViewEventArgs"/> instance containing the event data.</param>
        private void BrowseTV_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {
                EventFieldsLV.Items.Clear();

                FilterDefinition filter = m_newFilter = new FilterDefinition();
                filter.EventTypeId = null;
                filter.Fields = new List<FilterDefinitionField>();

                if (e.Node == null)
                {
                    OkBTN.Enabled = false;
                    return;
                }

                OkBTN.Enabled = true;

                // get the currently selected event.
                NodeId eventTypeId = Opc.Ua.ObjectTypeIds.BaseEventType;
                ReferenceDescription reference = e.Node.Tag as ReferenceDescription;

                if (reference != null)
                {
                    eventTypeId = (NodeId)reference.NodeId;
                }

                filter.EventTypeId = eventTypeId;

                // collect all of the fields defined for the event.
                SimpleAttributeOperandCollection fields = new SimpleAttributeOperandCollection();
                List<NodeId> declarationIds = new List<NodeId>();
                FormUtils.CollectFieldsForType(m_session, eventTypeId, fields, declarationIds);
                
                // need to read the description and datatype for each field. 
                ReadValueIdCollection valuesToRead = new ReadValueIdCollection();

                for (int ii = 0; ii < declarationIds.Count; ii++)
                {
                    ReadValueId valueToRead = new ReadValueId();
                    valueToRead.NodeId = declarationIds[ii];
                    valueToRead.AttributeId = Attributes.Description;
                    valuesToRead.Add(valueToRead);

                    valueToRead = new ReadValueId();
                    valueToRead.NodeId = declarationIds[ii];
                    valueToRead.AttributeId = Attributes.DataType;
                    valuesToRead.Add(valueToRead);

                    valueToRead = new ReadValueId();
                    valueToRead.NodeId = declarationIds[ii];
                    valueToRead.AttributeId = Attributes.ValueRank;
                    valuesToRead.Add(valueToRead);
                }

                DataValueCollection results = null;
                DiagnosticInfoCollection diagnosticInfos = null;

                m_session.Read(
                    null,
                    0,
                    TimestampsToReturn.Neither,
                    valuesToRead,
                    out results,
                    out diagnosticInfos);

                ClientBase.ValidateResponse(results, valuesToRead);
                ClientBase.ValidateDiagnosticInfos(diagnosticInfos, valuesToRead);

                // collect values. ignore errors since data used for display only.
                List<LocalizedText> descriptions = new List<LocalizedText>();
                List<NodeId> datatypes = new List<NodeId>();
                List<int> valueRanks = new List<int>();

                for (int ii = 0; ii < declarationIds.Count*3; ii += 3)
                {
                    descriptions.Add(results[ii].GetValue<LocalizedText>(LocalizedText.Null));
                    datatypes.Add(results[ii+1].GetValue<NodeId>(NodeId.Null));
                    valueRanks.Add(results[ii+2].GetValue<int>(ValueRanks.Any));
                }

                // populate the list box.
                for (int ii = 0; ii < fields.Count; ii++)
                {
                    FilterDefinitionField field = new FilterDefinitionField();
                    filter.Fields.Add(field);

                    field.Operand = fields[ii];

                    StringBuilder displayName = new StringBuilder();

                    for (int jj = 0; jj < field.Operand.BrowsePath.Count; jj++)
                    {
                        if (displayName.Length > 0)
                        {
                            displayName.Append('/');
                        }

                        displayName.Append(field.Operand.BrowsePath[jj].Name);
                    }

                    field.DisplayName = displayName.ToString();
                    field.DataType = datatypes[ii];
                    field.ValueRank = valueRanks[ii];
                    field.BuiltInType = DataTypes.GetBuiltInType(field.DataType, m_session.TypeTree);
                    field.Description = descriptions[ii].ToString();

                    // preserve previous settings.
                    for (int jj = 0; jj < m_filter.Fields.Count; jj++)
                    {
                        if (m_filter.Fields[jj].DisplayName == field.DisplayName)
                        {
                            field.ShowColumn = m_filter.Fields[jj].ShowColumn;
                            field.FilterValue = m_filter.Fields[jj].FilterValue;
                            break;
                        }
                    }

                    ListViewItem item = new ListViewItem(field.DisplayName);
                    item.SubItems.Add(String.Empty);
                    item.SubItems.Add(String.Empty);
                    item.Checked = field.ShowColumn;
                    item.Tag = field;

                    INode dataType = m_session.NodeCache.Find(datatypes[ii]);

                    if (dataType != null)
                    {
                        displayName = new StringBuilder();
                        displayName.Append(dataType.ToString());

                        if (valueRanks[ii] >= 0)
                        {
                            displayName.Append("[]");
                        }

                        field.DataTypeDisplayName = displayName.ToString();
                        item.SubItems[1].Text = field.DataTypeDisplayName;
                    }

                    item.SubItems[2].Text = descriptions[ii].ToString();
                    EventFieldsLV.Items.Add(item);
                }

                // resize columns to fit text.
                for (int ii = 0; ii < EventFieldsLV.Columns.Count; ii++)
                {
                    EventFieldsLV.Columns[ii].Width = -2;
                } 
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        /// <summary>
        /// Sets a default filter.
        /// </summary>
        public void SetDefault()
        {
            EventTypeId = Opc.Ua.ObjectTypeIds.BaseEventType;
            Fields      = new List <FilterDefinitionField>();

            FilterDefinitionField field = new FilterDefinitionField();

            field.DisplayName              = Opc.Ua.BrowseNames.EventType;
            field.DataType                 = Opc.Ua.DataTypeIds.NodeId;
            field.ValueRank                = ValueRanks.Scalar;
            field.BuiltInType              = BuiltInType.NodeId;
            field.DataTypeDisplayName      = field.BuiltInType.ToString();
            field.Operand                  = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.EventType);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn          = true;
            Fields.Add(field);

            field                          = new FilterDefinitionField();
            field.DisplayName              = Opc.Ua.BrowseNames.SourceNode;
            field.DataType                 = Opc.Ua.DataTypeIds.NodeId;
            field.ValueRank                = ValueRanks.Scalar;
            field.BuiltInType              = BuiltInType.NodeId;
            field.DataTypeDisplayName      = field.BuiltInType.ToString();
            field.Operand                  = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.SourceNode);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn          = true;
            Fields.Add(field);

            field                          = new FilterDefinitionField();
            field.DisplayName              = Opc.Ua.BrowseNames.Time;
            field.DataType                 = Opc.Ua.DataTypeIds.DateTime;
            field.ValueRank                = ValueRanks.Scalar;
            field.BuiltInType              = BuiltInType.DateTime;
            field.DataTypeDisplayName      = field.BuiltInType.ToString();
            field.Operand                  = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.Time);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn          = true;
            Fields.Add(field);

            field                          = new FilterDefinitionField();
            field.DisplayName              = Opc.Ua.BrowseNames.EventId;
            field.DataType                 = Opc.Ua.DataTypeIds.ByteString;
            field.ValueRank                = ValueRanks.Scalar;
            field.BuiltInType              = BuiltInType.ByteString;
            field.DataTypeDisplayName      = field.BuiltInType.ToString();
            field.Operand                  = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.EventId);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn          = false;
            Fields.Add(field);

            field                          = new FilterDefinitionField();
            field.DisplayName              = Opc.Ua.BrowseNames.Message;
            field.DataType                 = Opc.Ua.DataTypeIds.LocalizedText;
            field.ValueRank                = ValueRanks.Scalar;
            field.BuiltInType              = BuiltInType.LocalizedText;
            field.DataTypeDisplayName      = field.BuiltInType.ToString();
            field.Operand                  = new SimpleAttributeOperand();
            field.Operand.TypeDefinitionId = Opc.Ua.ObjectTypeIds.BaseEventType;
            field.Operand.BrowsePath.Add(Opc.Ua.BrowseNames.Message);
            field.Operand.AttributeId = Attributes.Value;
            field.ShowColumn          = true;
            Fields.Add(field);
        }