/// <summary> /// Constructs an event object from a notification. /// </summary> /// <param name="session">The session.</param> /// <param name="monitoredItem">The monitored item that produced the notification.</param> /// <param name="notification">The notification.</param> /// <param name="eventTypeMappings">Mapping between event types and known event types.</param> /// <returns> /// The event object. Null if the notification is not a valid event type. /// </returns> public static BaseEventState ConstructEvent( Session session, MonitoredItem monitoredItem, EventFieldList notification, Dictionary <NodeId, NodeId> eventTypeMappings) { // find the event type. NodeId eventTypeId = FindEventType(monitoredItem, notification); if (eventTypeId == null) { return(null); } // look up the known event type. NodeId knownTypeId = null; if (!eventTypeMappings.TryGetValue(eventTypeId, out knownTypeId)) { // check for a known type for (int jj = 0; jj < KnownEventTypes.Length; jj++) { if (KnownEventTypes[jj] == eventTypeId) { knownTypeId = eventTypeId; eventTypeMappings.Add(eventTypeId, eventTypeId); break; } } // browse for the supertypes of the event type. if (knownTypeId == null) { ReferenceDescriptionCollection supertypes = FormUtils.BrowseSuperTypes(session, eventTypeId, false); // can't do anything with unknown types. if (supertypes == null) { return(null); } // find the first supertype that matches a known event type. for (int ii = 0; ii < supertypes.Count; ii++) { for (int jj = 0; jj < KnownEventTypes.Length; jj++) { if (KnownEventTypes[jj] == supertypes[ii].NodeId) { knownTypeId = KnownEventTypes[jj]; eventTypeMappings.Add(eventTypeId, knownTypeId); break; } } if (knownTypeId != null) { break; } } } } if (knownTypeId == null) { return(null); } // all of the known event types have a UInt32 as identifier. uint?id = knownTypeId.Identifier as uint?; if (id == null) { return(null); } // construct the event based on the known event type. BaseEventState e = null; switch (id.Value) { case ObjectTypes.ConditionType: { e = new ConditionState(null); break; } case ObjectTypes.DialogConditionType: { e = new DialogConditionState(null); break; } case ObjectTypes.AlarmConditionType: { e = new AlarmConditionState(null); break; } case ObjectTypes.ExclusiveLimitAlarmType: { e = new ExclusiveLimitAlarmState(null); break; } case ObjectTypes.NonExclusiveLimitAlarmType: { e = new NonExclusiveLimitAlarmState(null); break; } case ObjectTypes.AuditEventType: { e = new AuditEventState(null); break; } case ObjectTypes.AuditUpdateMethodEventType: { e = new AuditUpdateMethodEventState(null); break; } default: { e = new BaseEventState(null); break; } } // get the filter which defines the contents of the notification. EventFilter filter = monitoredItem.Status.Filter as EventFilter; // initialize the event with the values in the notification. e.Update(session.SystemContext, filter.SelectClauses, notification); // save the orginal notification. e.Handle = notification; return(e); }
/// <summary> /// Updates the display with a new value for a monitored variable. /// </summary> private void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke(new MonitoredItemNotificationEventHandler(MonitoredItem_Notification), monitoredItem, e); return; } try { EventFieldList notification = e.NotificationValue as EventFieldList; if (notification == null) { return; } // check the type of event. NodeId eventTypeId = FormUtils.FindEventType(monitoredItem, notification); // ignore unknown events. if (NodeId.IsNull(eventTypeId)) { return; } // check for refresh start. if (eventTypeId == ObjectTypeIds.RefreshStartEventType) { ConditionsLV.Items.Clear(); return; } // check for refresh end. if (eventTypeId == ObjectTypeIds.RefreshEndEventType) { return; } // construct the condition object. ConditionState condition = FormUtils.ConstructEvent( m_session, monitoredItem, notification, m_eventTypeMappings) as ConditionState; if (condition == null) { return; } // look for existing entry. ListViewItem item = null; for (int ii = 0; ii < ConditionsLV.Items.Count; ii++) { ConditionState current = (ConditionState)ConditionsLV.Items[ii].Tag; // the combination of a condition and branch id uniquely identify an item in the display. if (current.NodeId == condition.NodeId && BaseVariableState.GetValue(current.BranchId) == BaseVariableState.GetValue(condition.BranchId)) { // match found but watch out for out of order events (async processing can cause this to happen). if (BaseVariableState.GetValue(current.Time) > BaseVariableState.GetValue(condition.Time)) { return; } item = ConditionsLV.Items[ii]; break; } } // create a new entry. if (item == null) { item = new ListViewItem(String.Empty); item.SubItems.Add(String.Empty); // Condition item.SubItems.Add(String.Empty); // Branch item.SubItems.Add(String.Empty); // Type item.SubItems.Add(String.Empty); // Severity item.SubItems.Add(String.Empty); // Time item.SubItems.Add(String.Empty); // State item.SubItems.Add(String.Empty); // Message item.SubItems.Add(String.Empty); // Comment ConditionsLV.Items.Add(item); } // look up the condition type metadata in the local cache. INode type = m_session.NodeCache.Find(condition.TypeDefinitionId); // Source if (condition.SourceName != null) { item.SubItems[0].Text = Utils.Format("{0}", condition.SourceName.Value); } else { item.SubItems[0].Text = null; } // Condition if (condition.ConditionName != null) { item.SubItems[1].Text = Utils.Format("{0}", condition.ConditionName.Value); } else { item.SubItems[1].Text = null; } // Branch if (condition.BranchId != null && !NodeId.IsNull(condition.BranchId.Value)) { item.SubItems[2].Text = Utils.Format("{0}", condition.BranchId.Value); } else { item.SubItems[2].Text = null; } // Type if (type != null) { item.SubItems[3].Text = Utils.Format("{0}", type); } else { item.SubItems[3].Text = null; } // Severity if (condition.Severity != null) { item.SubItems[4].Text = Utils.Format("{0}", (EventSeverity)condition.Severity.Value); } else { item.SubItems[4].Text = null; } // Time if (condition.Time != null) { item.SubItems[5].Text = Utils.Format("{0:HH:mm:ss.fff}", condition.Time.Value.ToLocalTime()); } else { item.SubItems[5].Text = null; } // State if (condition.EnabledState != null && condition.EnabledState.EffectiveDisplayName != null) { item.SubItems[6].Text = Utils.Format("{0}", condition.EnabledState.EffectiveDisplayName.Value); } else { item.SubItems[6].Text = null; } // Message if (condition.Message != null) { item.SubItems[7].Text = Utils.Format("{0}", condition.Message.Value); } else { item.SubItems[7].Text = null; } // Comment if (condition.Comment != null) { item.SubItems[8].Text = Utils.Format("{0}", condition.Comment.Value); } else { item.SubItems[8].Text = null; } item.Tag = condition; // set the color based on the retain bit. if (!BaseVariableState.GetValue(condition.Retain)) { item.ForeColor = Color.DimGray; } else { if (NodeId.IsNull(BaseVariableState.GetValue(condition.BranchId))) { item.ForeColor = Color.Empty; } else { item.ForeColor = Color.DarkGray; } } // adjust the width of the columns. for (int ii = 0; ii < ConditionsLV.Columns.Count; ii++) { ConditionsLV.Columns[ii].Width = -2; } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }
/// <summary> /// Updates the display with a new value for a monitored variable. /// </summary> private void MonitoredItem_Notification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e) { if (this.InvokeRequired) { this.BeginInvoke(new MonitoredItemNotificationEventHandler(MonitoredItem_Notification), monitoredItem, e); return; } try { EventFieldList notification = e.NotificationValue as EventFieldList; if (notification == null) { return; } // check the type of event. NodeId eventTypeId = FormUtils.FindEventType(monitoredItem, notification); // ignore unknown events. if (NodeId.IsNull(eventTypeId)) { return; } // check for refresh start. if (eventTypeId == ObjectTypeIds.RefreshStartEventType) { ConditionsLV.Items.Clear(); return; } // check for refresh end. if (eventTypeId == ObjectTypeIds.RefreshEndEventType) { return; } // construct the condition object. ConditionState condition = FormUtils.ConstructEvent( m_session, monitoredItem, notification, m_eventTypeMappings) as ConditionState; if (condition == null) { return; } ListViewItem item = null; item = new ListViewItem(String.Empty); item.SubItems.Add(String.Empty); // Condition item.SubItems.Add(String.Empty); // Branch item.SubItems.Add(String.Empty); // Type item.SubItems.Add(String.Empty); // Severity item.SubItems.Add(String.Empty); // Time item.SubItems.Add(String.Empty); // State item.SubItems.Add(String.Empty); // Message item.SubItems.Add(String.Empty); // Comment //ConditionsLV.Items.Add(item); // look up the condition type metadata in the local cache. INode type = m_session.NodeCache.Find(condition.TypeDefinitionId); // Source if (condition.SourceName != null) { item.SubItems[0].Text = Utils.Format("{0}", condition.SourceName.Value); } else { item.SubItems[0].Text = null; } // Condition if (condition.ConditionName != null) { item.SubItems[1].Text = Utils.Format("{0}", condition.ConditionName.Value); } else { item.SubItems[1].Text = null; } // Branch if (condition.BranchId != null && !NodeId.IsNull(condition.BranchId.Value)) { item.SubItems[2].Text = Utils.Format("{0}", condition.BranchId.Value); } else { item.SubItems[2].Text = null; } // Type if (type != null) { item.SubItems[3].Text = Utils.Format("{0}", type); } else { item.SubItems[3].Text = null; } // Severity if (condition.Severity != null) { item.SubItems[4].Text = Utils.Format("{0}", (EventSeverity)condition.Severity.Value); } else { item.SubItems[4].Text = null; } // Time if (condition.Time != null) { item.SubItems[5].Text = Utils.Format("{0:HH:mm:ss.fff}", condition.Time.Value.ToLocalTime()); } else { item.SubItems[5].Text = null; } // State if (condition.EnabledState != null && condition.EnabledState.EffectiveDisplayName != null) { item.SubItems[6].Text = Utils.Format("{0}", condition.EnabledState.EffectiveDisplayName.Value); } else { item.SubItems[6].Text = null; } // Message if (condition.Message != null) { item.SubItems[7].Text = Utils.Format("{0}", condition.Message.Value); } else { item.SubItems[7].Text = null; } // Comment if (condition.Comment != null) { item.SubItems[8].Text = Utils.Format("{0}", condition.Comment.Value); } else { item.SubItems[8].Text = null; } item.Tag = condition; // set the color based on the retain bit. if (!BaseVariableState.GetValue(condition.Retain)) { item.ForeColor = Color.DimGray; } else { if (NodeId.IsNull(BaseVariableState.GetValue(condition.BranchId))) { item.ForeColor = Color.Empty; } else { item.ForeColor = Color.DarkGray; } } //ConditionsLV.Items.Add(item); ConditionsLV.Items.Insert(0, item); // adjust the width of the columns. if (this.ColumnAutoAdjust) { for (int ii = 0; ii < ConditionsLV.Columns.Count; ii++) { ConditionsLV.Columns[ii].Width = -2; } } //TODO: Add this as a menu option //ConditionsLV.EnsureVisible(ConditionsLV.Items.Count -1); while (ConditionsLV.Items.Count > MaximumItems) { ConditionsLV.Items.RemoveAt(ConditionsLV.Items.Count - 1); } } catch (Exception exception) { ClientUtils.HandleException(this.Text, exception); } }