Esempio n. 1
0
        /// <summary>
        /// Updates the filter from the controls.
        /// </summary>
        private void UpdateFilter()
        {
            // get selected declarations.
            List <InstanceDeclaration> declarations = new List <InstanceDeclaration>();
            NodeId eventTypeId = CollectInstanceDeclarations(declarations);

            if (m_filter == null)
            {
                m_filter = new FilterDeclaration();
            }

            if (m_filter.Fields == null || m_filter.Fields.Count == 0)
            {
                m_filter.Fields = new List <FilterDeclarationField>();

                // select some default values to display in the list.
                AddDefaultFilter(m_filter.Fields, Opc.Ua.BrowseNames.EventType, true);
                AddDefaultFilter(m_filter.Fields, Opc.Ua.BrowseNames.SourceName, true);
                AddDefaultFilter(m_filter.Fields, Opc.Ua.BrowseNames.SourceNode, true);
                AddDefaultFilter(m_filter.Fields, Opc.Ua.BrowseNames.Time, true);
                AddDefaultFilter(m_filter.Fields, Opc.Ua.BrowseNames.Severity, true);
                AddDefaultFilter(m_filter.Fields, Opc.Ua.BrowseNames.Message, true);
            }

            // copy settings from existing filter.
            List <FilterDeclarationField> fields = new List <FilterDeclarationField>();

            foreach (InstanceDeclaration declaration in declarations)
            {
                if (declaration.NodeClass != NodeClass.Variable)
                {
                    continue;
                }

                FilterDeclarationField field = new FilterDeclarationField(declaration);

                foreach (FilterDeclarationField field2 in m_filter.Fields)
                {
                    if (field2.InstanceDeclaration.BrowsePathDisplayText ==
                        field.InstanceDeclaration.BrowsePathDisplayText)
                    {
                        field.DisplayInList  = field2.DisplayInList;
                        field.FilterEnabled  = field2.FilterEnabled;
                        field.FilterOperator = field2.FilterOperator;
                        field.FilterValue    = field2.FilterValue;
                        break;
                    }
                }

                fields.Add(field);
            }

            // update filter.
            m_filter.EventTypeId = eventTypeId;
            m_filter.Fields      = fields;
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a new instance of a FilterDeclaration.
        /// </summary>
        public FilterDeclaration(FilterDeclaration declaration)
        {
            EventTypeId = declaration.EventTypeId;
            Fields      = new List <FilterDeclarationField>(declaration.Fields.Count);

            for (int ii = 0; ii < declaration.Fields.Count; ii++)
            {
                Fields.Add(new FilterDeclarationField(declaration.Fields[ii]));
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Deletes the subscription.
 /// </summary>
 private void DeleteSubscription()
 {
     if (m_subscription != null)
     {
         m_subscription.Delete(true);
         m_session.RemoveSubscription(m_subscription);
         m_subscription  = null;
         m_filter        = null;
         m_monitoredItem = null;
     }
 }
        /// <summary>
        /// Displays the current filter components in the control.
        /// </summary>
        public void ShowFilter(FilterDeclaration filter)
        {
            m_declarations = new List <InstanceDeclaration>();

            foreach (FilterDeclarationField declaration in filter.Fields)
            {
                DataRow row = m_dataset.Tables[0].NewRow();
                UpdateRow(row, declaration.InstanceDeclaration);
                m_dataset.Tables[0].Rows.Add(row);
                m_declarations.Add(declaration.InstanceDeclaration);
            }
        }
Esempio n. 5
0
        /// <summary>
        /// Changes the filter used to select the events.
        /// </summary>
        public void ChangeFilter(FilterDeclaration filter, bool fetchRecent)
        {
            m_filter = filter;
            EventsLV.Items.Clear();

            int index = 0;

            if (m_filter != null)
            {
                // add or update existing columns.
                for (int ii = 0; ii < m_filter.Fields.Count; ii++)
                {
                    if (m_filter.Fields[ii].DisplayInList)
                    {
                        if (index >= EventsLV.Columns.Count)
                        {
                            EventsLV.Columns.Add(new ColumnHeader());
                        }

                        EventsLV.Columns[index].Text      = m_filter.Fields[ii].InstanceDeclaration.DisplayName;
                        EventsLV.Columns[index].TextAlign = HorizontalAlignment.Left;
                        index++;
                    }
                }
            }

            // remove extra columns.
            while (index < EventsLV.Columns.Count)
            {
                EventsLV.Columns.RemoveAt(EventsLV.Columns.Count - 1);
            }

            // adjust the width of the columns.
            for (int ii = 0; ii < EventsLV.Columns.Count; ii++)
            {
                EventsLV.Columns[ii].Width = -2;
            }

            // fetch recent history.
            if (fetchRecent)
            {
                ReadRecentHistory();
            }

            // update subscription.
            if (m_subscription != null && m_filter != null)
            {
                m_monitoredItem.Filter = m_filter.GetFilter();
                m_subscription.ApplyChanges();
            }
        }
Esempio n. 6
0
        /// <summary>
        /// Sets the filter to edit.
        /// </summary>
        public void SetFilter(FilterDeclaration filter)
        {
            m_dataset.Tables[0].Rows.Clear();

            if (filter != null)
            {
                foreach (FilterDeclarationField field in filter.Fields)
                {
                    DataRow row = m_dataset.Tables[0].NewRow();
                    UpdateRow(row, field);
                    m_dataset.Tables[0].Rows.Add(row);
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Creates the subscription.
        /// </summary>
        private void CreateSubscription()
        {
            // create the default subscription.
            m_subscription = new Subscription();

            m_subscription.DisplayName                = null;
            m_subscription.PublishingInterval         = 1000;
            m_subscription.KeepAliveCount             = 10;
            m_subscription.LifetimeCount              = 100;
            m_subscription.MaxNotificationsPerPublish = 1000;
            m_subscription.PublishingEnabled          = true;
            m_subscription.TimestampsToReturn         = TimestampsToReturn.Both;

            m_session.AddSubscription(m_subscription);
            m_subscription.Create();

            // a table used to track event types.
            m_eventTypeMappings = new Dictionary <NodeId, NodeId>();

            NodeId knownEventId = ExpandedNodeId.ToNodeId(ObjectTypeIds.SystemCycleStatusEventType, m_session.NamespaceUris);

            m_knownEventTypes = new Dictionary <NodeId, Type>();
            m_knownEventTypes.Add(knownEventId, typeof(SystemCycleStatusEventState));

            TypeDeclaration type = new TypeDeclaration();

            type.NodeId       = ExpandedNodeId.ToNodeId(ObjectTypeIds.SystemCycleStatusEventType, m_session.NamespaceUris);
            type.Declarations = ClientUtils.CollectInstanceDeclarationsForType(m_session, type.NodeId);

            // the filter to use.
            m_filter = new FilterDeclaration(type, null);

            // declate callback.
            m_MonitoredItem_Notification = new MonitoredItemNotificationEventHandler(MonitoredItem_Notification);

            // create a monitored item based on the current filter settings.
            m_monitoredItem                  = new MonitoredItem();
            m_monitoredItem.StartNodeId      = Opc.Ua.ObjectIds.Server;
            m_monitoredItem.AttributeId      = Attributes.EventNotifier;
            m_monitoredItem.SamplingInterval = 0;
            m_monitoredItem.QueueSize        = 1000;
            m_monitoredItem.DiscardOldest    = true;
            m_monitoredItem.Filter           = m_filter.GetFilter();

            // set up callback for notifications.
            m_monitoredItem.Notification += m_MonitoredItem_Notification;

            m_subscription.AddItem(m_monitoredItem);
            m_subscription.ApplyChanges();
        }
Esempio n. 8
0
        /// <summary>
        /// Creates a new instance of a FilterDeclaration.
        /// </summary>
        public FilterDeclaration(TypeDeclaration eventType, FilterDeclaration template)
        {
            EventTypeId = eventType.NodeId;
            Fields      = new List <FilterDeclarationField>();

            foreach (InstanceDeclaration instanceDeclaration in eventType.Declarations)
            {
                if (instanceDeclaration.NodeClass == NodeClass.Method)
                {
                    continue;
                }

                if (NodeId.IsNull(instanceDeclaration.ModellingRule))
                {
                    continue;
                }

                FilterDeclarationField element = new FilterDeclarationField(instanceDeclaration);
                Fields.Add(element);

                // set reasonable defaults.
                if (template == null)
                {
                    if (instanceDeclaration.RootTypeId == Opc.Ua.ObjectTypeIds.BaseEventType &&
                        instanceDeclaration.BrowseName != Opc.Ua.BrowseNames.EventId)
                    {
                        element.DisplayInList = true;
                    }
                }

                // preserve filter settings.
                else
                {
                    foreach (FilterDeclarationField field in template.Fields)
                    {
                        if (field.InstanceDeclaration.BrowsePathDisplayText ==
                            element.InstanceDeclaration.BrowsePathDisplayText)
                        {
                            element.DisplayInList  = field.DisplayInList;
                            element.FilterEnabled  = field.FilterEnabled;
                            element.FilterOperator = field.FilterOperator;
                            element.FilterValue    = field.FilterValue;
                            break;
                        }
                    }
                }
            }
        }
Esempio n. 9
0
        /// <summary>
        /// Shows all fields for the current condition.
        /// </summary>
        public bool ShowDialog(FilterDeclaration filter, VariantCollection fields)
        {
            // fill in dialog.
            for (int ii = 0; ii < filter.Fields.Count; ii++)
            {
                InstanceDeclaration instance = filter.Fields[ii].InstanceDeclaration;
                ListViewItem        item     = new ListViewItem(instance.DisplayPath);
                item.SubItems.Add(instance.DataTypeDisplayText);

                string text = null;

                // check for missing fields.
                if (fields.Count <= ii + 1 || fields[ii + 1].Value == null)
                {
                    text = String.Empty;
                }

                // use default string format.
                else
                {
                    text = fields[ii + 1].ToString();
                }

                item.SubItems.Add(text);
                item.Tag = filter.Fields[ii];
                FieldsLV.Items.Add(item);
            }

            // adjust columns.
            for (int ii = 0; ii < FieldsLV.Columns.Count; ii++)
            {
                FieldsLV.Columns[ii].Width = -2;
            }

            // display the dialog.
            if (ShowDialog() != DialogResult.OK)
            {
                return(false);
            }

            return(true);
        }
Esempio n. 10
0
        /// <summary>
        /// Sets the filter to edit.
        /// </summary>
        public void SetFilter(FilterDeclaration filter)
        {
            m_filter  = filter;
            m_dataset = new DataSet();
            m_dataset.Tables.Add("Events");
            m_dataset.Tables[0].Columns.Add("Event", typeof(VariantCollection));

            if (m_filter != null)
            {
                foreach (FilterDeclarationField field in m_filter.Fields)
                {
                    if (field.DisplayInList)
                    {
                        m_dataset.Tables[0].Columns.Add(field.InstanceDeclaration.DisplayName, typeof(string));
                    }
                }
            }

            EventsDV.DataSource = m_dataset.Tables[0];
        }
Esempio n. 11
0
        /// <summary>
        /// Deletes the recent history.
        /// </summary>
        private void DeleteHistory(NodeId areaId, List <VariantCollection> events, FilterDeclaration filter)
        {
            // find the event id.
            int index = 0;

            foreach (FilterDeclarationField field in filter.Fields)
            {
                if (field.InstanceDeclaration.BrowseName == Opc.Ua.BrowseNames.EventId)
                {
                    break;
                }

                index++;
            }

            // can't delete events if no event id.
            if (index >= filter.Fields.Count)
            {
                throw ServiceResultException.Create(StatusCodes.BadEventIdUnknown, "Cannot delete events if EventId was not selected.");
            }

            // build list of nodes to delete.
            DeleteEventDetails details = new DeleteEventDetails();

            details.NodeId = areaId;

            foreach (VariantCollection e in events)
            {
                byte[] eventId = null;

                if (e.Count > index)
                {
                    eventId = e[index].Value as byte[];
                }

                details.EventIds.Add(eventId);
            }

            // delete the events.
            ExtensionObjectCollection nodesToUpdate = new ExtensionObjectCollection();

            nodesToUpdate.Add(new ExtensionObject(details));

            HistoryUpdateResultCollection results         = null;
            DiagnosticInfoCollection      diagnosticInfos = null;

            m_session.HistoryUpdate(
                null,
                nodesToUpdate,
                out results,
                out diagnosticInfos);

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

            if (StatusCode.IsBad(results[0].StatusCode))
            {
                throw new ServiceResultException(results[0].StatusCode);
            }

            // check for item level errors.
            if (results[0].OperationResults.Count > 0)
            {
                int count = 0;

                for (int ii = 0; ii < results[0].OperationResults.Count; ii++)
                {
                    if (StatusCode.IsBad(results[0].OperationResults[ii]))
                    {
                        count++;
                    }
                }

                // raise an error.
                if (count > 0)
                {
                    throw ServiceResultException.Create(
                              StatusCodes.BadEventIdUnknown,
                              "Error deleting events. Only {0} of {1} deletes succeeded.",
                              events.Count - count,
                              events.Count);
                }
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Creates list item for an event.
        /// </summary>
        private ListViewItem CreateListItem(FilterDeclaration filter, VariantCollection fieldValues)
        {
            ListViewItem item = null;

            if (m_displayConditions)
            {
                NodeId conditionId = fieldValues[0].Value as NodeId;

                if (conditionId != null)
                {
                    for (int ii = 0; ii < EventsLV.Items.Count; ii++)
                    {
                        VariantCollection fields = EventsLV.Items[ii].Tag as VariantCollection;

                        if (fields != null && Utils.IsEqual(conditionId, fields[0].Value))
                        {
                            item = EventsLV.Items[ii];
                            break;
                        }
                    }
                }
            }

            if (item == null)
            {
                item = new ListViewItem();
            }

            item.Tag = fieldValues;
            int position = -1;

            for (int ii = 1; ii < filter.Fields.Count; ii++)
            {
                if (!filter.Fields[ii].DisplayInList)
                {
                    continue;
                }

                position++;

                string  text  = null;
                Variant value = fieldValues[ii + 1];

                // check for missing fields.
                if (value.Value == null)
                {
                    text = String.Empty;
                }

                // display the name of a node instead of the node id.
                else if (value.TypeInfo.BuiltInType == BuiltInType.NodeId)
                {
                    INode node = m_session.NodeCache.Find((NodeId)value.Value);

                    if (node != null)
                    {
                        text = node.ToString();
                    }
                }

                // display local time for any time fields.
                else if (value.TypeInfo.BuiltInType == BuiltInType.DateTime)
                {
                    DateTime datetime = (DateTime)value.Value;

                    if (m_filter.Fields[ii].InstanceDeclaration.DisplayName.Contains("Time"))
                    {
                        text = datetime.ToLocalTime().ToString("HH:mm:ss.fff");
                    }
                    else
                    {
                        text = datetime.ToLocalTime().ToString("yyyy-MM-dd");
                    }
                }

                // use default string format.
                else
                {
                    text = value.ToString();
                }

                // update subitem text.
                if (item.Text == String.Empty)
                {
                    item.Text             = text;
                    item.SubItems[0].Text = text;
                }
                else
                {
                    if (item.SubItems.Count <= position)
                    {
                        item.SubItems.Add(text);
                    }
                    else
                    {
                        item.SubItems[position].Text = text;
                    }
                }
            }

            return(item);
        }
Esempio n. 13
0
 /// <summary>
 /// Deletes the subscription.
 /// </summary>
 private void DeleteSubscription()
 {
     if (m_subscription != null)
     {
         m_subscription.Delete(true);
         m_session.RemoveSubscription(m_subscription);
         m_subscription = null;
         m_filter = null;
         m_monitoredItem = null;
     }
 }
Esempio n. 14
0
        /// <summary>
        /// Creates the subscription.
        /// </summary>
        private void CreateSubscription()
        {
            // create the default subscription.
            m_subscription = new Subscription();

            m_subscription.DisplayName = null;
            m_subscription.PublishingInterval = 1000;
            m_subscription.KeepAliveCount = 10;
            m_subscription.LifetimeCount = 100;
            m_subscription.MaxNotificationsPerPublish = 1000;
            m_subscription.PublishingEnabled = true;
            m_subscription.TimestampsToReturn = TimestampsToReturn.Both;

            m_session.AddSubscription(m_subscription);
            m_subscription.Create();

            // a table used to track event types.
            m_eventTypeMappings = new Dictionary<NodeId, NodeId>();

            NodeId knownEventId = ExpandedNodeId.ToNodeId(ObjectTypeIds.SystemCycleStatusEventType, m_session.NamespaceUris);

            m_knownEventTypes = new Dictionary<NodeId, Type>();
            m_knownEventTypes.Add(knownEventId, typeof(SystemCycleStatusEventState));
            
            TypeDeclaration type = new TypeDeclaration();
            type.NodeId = ExpandedNodeId.ToNodeId(ObjectTypeIds.SystemCycleStatusEventType, m_session.NamespaceUris);
            type.Declarations = ClientUtils.CollectInstanceDeclarationsForType(m_session, type.NodeId);

            // the filter to use.
            m_filter = new FilterDeclaration(type, null);

            // declate callback.
            m_MonitoredItem_Notification = new MonitoredItemNotificationEventHandler(MonitoredItem_Notification);

            // create a monitored item based on the current filter settings.            
            m_monitoredItem = new MonitoredItem();
            m_monitoredItem.StartNodeId = Opc.Ua.ObjectIds.Server;
            m_monitoredItem.AttributeId = Attributes.EventNotifier;
            m_monitoredItem.SamplingInterval = 0;
            m_monitoredItem.QueueSize = 1000;
            m_monitoredItem.DiscardOldest = true;
            m_monitoredItem.Filter = m_filter.GetFilter();

            // set up callback for notifications.
            m_monitoredItem.Notification += m_MonitoredItem_Notification;

            m_subscription.AddItem(m_monitoredItem);
            m_subscription.ApplyChanges();
        }