/// <summary>
        /// Returns value of the field name containing the event type.
        /// </summary>
        public static T GetEventValue <T>(this EventFieldList eventFields, string name,
                                          MonitoredItem monitoredItem, T defaultValue = default)
        {
            // get value
            var value = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, name);

            if (value != null)
            {
                return(value.As <T>());
            }
            return(defaultValue);
        }
Esempio n. 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="monitoredItem"></param>
        /// <param name="e"></param>
        protected virtual void ItemNotificationHandler(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                if (monitoredItem != null)
                {
                    Opc.Ua.MonitoredItemNotification change = e.NotificationValue as Opc.Ua.MonitoredItemNotification;
                    if (change != null)
                    {
                        DataValue dv = change.Value;
                        if (dv != null)
                        {
                            ListViewItem[] lvis = ItemsLV.Items.Find(monitoredItem.ClientHandle.ToString(), false);
                            if (lvis.Length > 0)
                            {
                                DiagnosticListViewItem dlvi = lvis[0].Tag as DiagnosticListViewItem;
                                dlvi.UpdateInListView(lvis[0], dv, m_Session);
                            }
                        }
                        else
                        {
                            Utils.Trace("dv is null: {0}", MethodBase.GetCurrentMethod());
                        }
                    }
                    else
                    {
                        EventFieldList eventFields = e.NotificationValue as EventFieldList;
                        if (eventFields != null)
                        {
                            // get the event fields.
                            NodeId        eventType  = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.EventType) as NodeId;
                            string        sourceName = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.SourceName) as string;
                            DateTime?     time       = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.Time) as DateTime?;
                            ushort?       severity   = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.Severity) as ushort?;
                            LocalizedText message    = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.Message) as LocalizedText;
                            NodeId        sourceNode = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.SourceNode) as NodeId;

                            if (eventType == new NodeId(ObjectTypes.AuditAddNodesEventType))
                            {
                            }
                            else
                            {
                            }
                        }
                        else
                        {
                            Utils.Trace("eventFields is null " + MethodBase.GetCurrentMethod());
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }
Esempio n. 3
0
        /// <see cref="BaseListCtrl.UpdateItem" />
        protected override void UpdateItem(ListViewItem listItem, object item)
        {
            EventFieldList eventFields = item as EventFieldList;

            if (eventFields == null)
            {
                base.UpdateItem(listItem, item);
                return;
            }

            MonitoredItem monitoredItem = m_subscription.FindItemByClientHandle(eventFields.ClientHandle);

            if (monitoredItem == null)
            {
                listItem.SubItems[0].Text = String.Format("[{0}]", eventFields.ClientHandle);
                listItem.SubItems[1].Text = "(unknown)";
                listItem.SubItems[2].Text = null;
                listItem.SubItems[3].Text = null;
                listItem.SubItems[4].Text = null;
                listItem.SubItems[5].Text = null;

                listItem.Tag = eventFields;
                return;
            }

            // get the event fields.
            NodeId        eventType  = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.EventType) as NodeId;
            string        sourceName = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.SourceName) as string;
            DateTime?     time       = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.Time) as DateTime?;
            ushort?       severity   = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.Severity) as ushort?;
            LocalizedText message    = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, Opc.Ua.BrowseNames.Message) as LocalizedText;

            // fill in the columns.
            listItem.SubItems[0].Text = String.Format("[{0}]", eventFields.ClientHandle);

            INode typeNode = m_subscription.Session.NodeCache.Find(eventType);

            if (typeNode == null)
            {
                listItem.SubItems[1].Text = String.Format("{0}", eventType);
            }
            else
            {
                listItem.SubItems[1].Text = String.Format("{0}", typeNode);
            }

            listItem.SubItems[2].Text = String.Format("{0}", sourceName);

            if (time != null && time.Value != DateTime.MinValue)
            {
                listItem.SubItems[3].Text = String.Format("{0:HH:mm:ss.fff}", time.Value.ToLocalTime());
            }
            else
            {
                listItem.SubItems[3].Text = String.Empty;
            }

            listItem.SubItems[4].Text = String.Format("{0}", severity);

            if (message != null)
            {
                listItem.SubItems[5].Text = String.Format("{0}", message.Text);
            }
            else
            {
                listItem.SubItems[5].Text = String.Empty;
            }

            listItem.Tag = eventFields;
        }
Esempio n. 4
0
        /// <summary>
        /// Processes a Publish response from the server.
        /// </summary>
        ///
        void ItemNotification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MonitoredItemNotificationEventHandler(ItemNotification), monitoredItem, e);
                return;
            }
            else if (!IsHandleCreated)
            {
                return;
            }
            try
            {
                if (monitoredItem != null)
                {
                    string         Key  = monitoredItem.StartNodeId.Identifier.ToString() + "." + monitoredItem.RelativePath;
                    ListViewItem[] lvis = listView1.Items.Find(Key, true);
                    Opc.Ua.MonitoredItemNotification change = e.NotificationValue as Opc.Ua.MonitoredItemNotification;
                    if (change != null)
                    {
                        DataValue dv = change.Value;
                        if (lvis.Length == 1)
                        {
                            ListViewItem lvi                = lvis[0];
                            int          subindex           = lvi.SubItems.IndexOfKey(Key);
                            ListViewItem.ListViewSubItem si = lvi.SubItems[subindex];
                            TypedMonitoredItem           mi = si.Tag as TypedMonitoredItem;

                            if (mi != null)
                            {
                                if (mi.ClientHandle == monitoredItem.ClientHandle)
                                {
                                    if (dv != null && dv.Value != null)
                                    {
                                        if (monitoredItem.Status.Id == StatusCodes.BadNodeIdUnknown)
                                        {
                                            // Randy said we would get this, but we don't
                                            RemoveSessionItem(lvi, true);
                                        }
                                        else
                                        {
                                            si.Text = mi.ToString(dv);
                                        }
                                    }
                                    else
                                    {
                                        // This is what we get
                                        RemoveSessionItem(lvi, true);
                                    }
                                }
                                else
                                {
                                    Utils.Trace("(mi.ClientHandle != monitoredItem.ClientHandle " + MethodBase.GetCurrentMethod());
                                }
                            }
                            else
                            {
                                Utils.Trace("mi is null " + MethodBase.GetCurrentMethod());
                            }
                        }
                        else
                        {
                            Utils.Trace("lvis.Length != 1 " + MethodBase.GetCurrentMethod());
                        }
                    }
                    else
                    {
                        EventFieldList eventFields = e.NotificationValue as EventFieldList;
                        if (eventFields != null)
                        {
                            // get the event fields.
                            NodeId        eventType  = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.EventType) as NodeId;
                            string        sourceName = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.SourceName) as string;
                            DateTime?     time       = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.Time) as DateTime?;
                            ushort?       severity   = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.Severity) as ushort?;
                            LocalizedText message    = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.Message) as LocalizedText;
                            NodeId        sourceNode = monitoredItem.GetFieldValue(eventFields, ObjectTypes.BaseEventType, BrowseNames.SourceNode) as NodeId;
                            //Utils.Trace("eventType: {0}, message: {1}, sourceName: {2} sourceNode: {3}", eventType.ToString(), message.Text.ToString(), sourceName.ToString(), sourceNode.ToString());
                            if (eventType == new NodeId(ObjectTypes.AuditActivateSessionEventType))
                            {
                                Utils.Trace("AuditActivateSessionEventType detected " + MethodBase.GetCurrentMethod());
                                AddSessions();
                                m_Subscription.ModifyItems();
                                m_Subscription.ApplyChanges();
                            }
                        }
                        else
                        {
                            Utils.Trace("eventFields is null " + MethodBase.GetCurrentMethod());
                        }
                    }
                }
                else
                {
                    Utils.Trace("monitoredItem is null " + MethodBase.GetCurrentMethod());
                }
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
        }