/// <summary>
        /// Handles the Click event of the Conditions_MonitorMI control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Events_ViewMI_Click(object sender, EventArgs e)
        {
            try
            {
                if (EventsLV.SelectedItems.Count != 1)
                {
                    return;
                }

                AuditUpdateMethodEventState audit = (AuditUpdateMethodEventState)EventsLV.SelectedItems[0].Tag;
                new ViewEventDetailsDlg().ShowDialog(m_monitoredItem, audit.Handle as EventFieldList);
            }
            catch (Exception exception)
            {
                ClientUtils.HandleException(this.Text, exception);
            }
        }
        /// <summary>
        /// Constructs an event and reports to the server.
        /// </summary>
        private void RaiseAuditUpdateMethodEvent(
            ISystemContext context,
            MethodState method,
            bool status,
            IList <object> inputArguments)
        {
            BaseObjectState parent = method.Parent as BaseObjectState;

            #region Task #D6 - Add Support for Notifiers
            // check if it is even necessary to produce an event.
            if (parent != null && !parent.AreEventsMonitored)
            {
                return;
            }
            #endregion

            // construct translation object with default text.
            TranslationInfo info = new TranslationInfo(
                "AuditUpdateMethodEvent",
                "en-US",
                "A method was called.");

            // intialize the event.
            AuditUpdateMethodEventState e = new AuditUpdateMethodEventState(null);

            e.Initialize(
                SystemContext,
                null,
                EventSeverity.Medium,
                new LocalizedText(info),
                true,
                DateTime.UtcNow);

            // fill in additional fields.
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.SourceNode, method.Parent.NodeId, false);
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.SourceName, "Attribute/Call", false);
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.ServerId, context.ServerUris.GetString(0), false);
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.ClientAuditEntryId, context.AuditEntryId, false);

            if (context.UserIdentity != null)
            {
                e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.ClientUserId, context.UserIdentity.DisplayName, false);
            }

            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.MethodId, method.NodeId, false);

            // need to change data type.
            Variant[] values = new Variant[inputArguments.Count];

            for (int ii = 0; ii < values.Length; ii++)
            {
                values[ii] = new Variant(inputArguments[ii]);
            }

            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.InputArguments, values, false);

            #region Task #D6 - Add Support for Notifiers
            // report the event to the parent. let it propagate up the tree.
            if (parent != null && parent.AreEventsMonitored)
            {
                parent.ReportEvent(context, e);
                return;
            }
            #endregion

            // report the event to the server object.
            Server.ReportEvent(e);
        }
Example #3
0
        private ServiceResult OnStart(ISystemContext context, MethodState method, IList <object> inputArguments, IList <object> outputArguments)
        {
            // all arguments must be provided.
            if (inputArguments.Count < 2)
            {
                return(StatusCodes.BadArgumentsMissing);
            }
            // check the data type of the input arguments.
            uint?initialState = inputArguments[0] as uint?;
            uint?finalState   = inputArguments[1] as uint?;

            if (initialState == null || finalState == null)
            {
                return(StatusCodes.BadTypeMismatch);
            }
            lock (_processLock)
            {
                // check if the process is running.
                if (_processTimer != null)
                {
                    _processTimer.Dispose();
                    _processTimer = null;
                }
                // start the process.
                _state        = initialState.Value;
                _finalState   = finalState.Value;
                _processTimer = new Timer(OnUpdateProcess, null, 1000, 1000);
                // the calling function sets default values for all output arguments.
                // only need to update them here.
                outputArguments[0] = _state;
                outputArguments[1] = _finalState;
            }
            // signal update to state node.
            lock (ApplicationNodeManager.Lock)
            {
                _stateNode.Value = _state;
                _stateNode.ClearChangeMasks(ApplicationNodeManager.SystemContext, true);
            }
            TranslationInfo info = new TranslationInfo(
                "OnStart",
                "en-US",
                "The Confirm method was called.");
            AuditUpdateMethodEventState auditUpdateMethodEventState = new AuditUpdateMethodEventState(method);

            auditUpdateMethodEventState.Initialize(
                context,
                method,
                EventSeverity.Low,
                new LocalizedText(info),
                ServiceResult.IsGood(StatusCodes.Good),
                DateTime.UtcNow);
            auditUpdateMethodEventState.SourceName.Value = "Attribute/Call";
            auditUpdateMethodEventState.MethodId         = new PropertyState <NodeId>(method)
            {
                Value = method.NodeId
            };
            auditUpdateMethodEventState.InputArguments = new PropertyState <object[]>(method)
            {
                Value = new object[] { inputArguments }
            };
            auditUpdateMethodEventState.SetChildValue(context, BrowseNames.InputArguments, inputArguments.ToArray(), true);
            bool valid = auditUpdateMethodEventState.Validate(context);

            if (valid)
            {
                ApplicationNodeManager.Server.ReportEvent(auditUpdateMethodEventState);
            }
            return(ServiceResult.Good);
        }
Example #4
0
        /// <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.
            if (session is null)
            {
                return(null);
            }
            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;
                }

                // construct the audit object.
                AuditUpdateMethodEventState audit = FormUtils.ConstructEvent(
                    m_session,
                    monitoredItem,
                    notification,
                    m_eventTypeMappings) as AuditUpdateMethodEventState;

                if (audit == null)
                {
                    return;
                }

                ListViewItem item = new ListViewItem(String.Empty);

                item.SubItems.Add(String.Empty); // Source
                item.SubItems.Add(String.Empty); // Type
                item.SubItems.Add(String.Empty); // Method
                item.SubItems.Add(String.Empty); // Status
                item.SubItems.Add(String.Empty); // Time
                item.SubItems.Add(String.Empty); // Message
                item.SubItems.Add(String.Empty); // Arguments

                // look up the condition type metadata in the local cache.
                INode type = m_session.NodeCache.Find(audit.TypeDefinitionId);

                // Source
                if (audit.SourceName != null)
                {
                    item.SubItems[0].Text = Utils.Format("{0}", audit.SourceName.Value);
                }
                else
                {
                    item.SubItems[0].Text = null;
                }

                // Type
                if (type != null)
                {
                    item.SubItems[1].Text = Utils.Format("{0}", type);
                }
                else
                {
                    item.SubItems[1].Text = null;
                }

                // look up the method metadata in the local cache.
                INode method = m_session.NodeCache.Find(BaseVariableState.GetValue(audit.MethodId));

                // Method
                if (method != null)
                {
                    item.SubItems[2].Text = Utils.Format("{0}", method);
                }
                else
                {
                    item.SubItems[2].Text = null;
                }

                // Status
                if (audit.Status != null)
                {
                    item.SubItems[3].Text = Utils.Format("{0}", audit.Status.Value);
                }
                else
                {
                    item.SubItems[3].Text = null;
                }

                // Time
                if (audit.Time != null)
                {
                    item.SubItems[4].Text = Utils.Format("{0:HH:mm:ss.fff}", audit.Time.Value.ToLocalTime());
                }
                else
                {
                    item.SubItems[4].Text = null;
                }

                // Message
                if (audit.Message != null)
                {
                    item.SubItems[5].Text = Utils.Format("{0}", audit.Message.Value);
                }
                else
                {
                    item.SubItems[5].Text = null;
                }

                // Arguments
                if (audit.InputArguments != null)
                {
                    item.SubItems[6].Text = Utils.Format("{0}", new Variant(audit.InputArguments.Value));
                }
                else
                {
                    item.SubItems[6].Text = null;
                }

                item.Tag = audit;
                EventsLV.Items.Add(item);

                // adjust the width of the columns.
                for (int ii = 0; ii < EventsLV.Columns.Count; ii++)
                {
                    EventsLV.Columns[ii].Width = -2;
                }
            }
            catch (Exception exception)
            {
                ClientUtils.HandleException(this.Text, exception);
            }
        }
Example #6
0
        /// <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.
            var eventTypeId = FindEventType(monitoredItem, notification);

            if (eventTypeId == null)
            {
                return(null);
            }

            // look up the known event type.
            NodeId knownTypeId;

            if (!eventTypeMappings.TryGetValue(eventTypeId, out knownTypeId))
            {
                // check for a known type
                if (KnownEventTypes.Any(t => t == eventTypeId))
                {
                    knownTypeId = eventTypeId;
                    eventTypeMappings.Add(eventTypeId, eventTypeId);
                }

                // browse for the supertypes of the event type.
                if (knownTypeId == null)
                {
                    var supertypes = 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.
                    foreach (var t in supertypes)
                    {
                        foreach (var nodeId in KnownEventTypes)
                        {
                            if (nodeId == t.NodeId)
                            {
                                knownTypeId = nodeId;
                                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.
            var id = knownTypeId.Identifier as uint?;

            if (id == null)
            {
                return(null);
            }

            // construct the event based on the known event type.
            BaseEventState e;

            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.
            var filter = monitoredItem.Status.Filter as EventFilter;

            // initialize the event with the values in the notification.
            if (filter != null)
            {
                e.Update(session.SystemContext, filter.SelectClauses, notification);
            }

            // save the orginal notification.
            e.Handle = notification;
            return(e);
        }
Example #7
0
        /// <summary>
        /// Constructs an event and reports to the server.
        /// </summary>
        private void RaiseAuditUpdateMethodEvent(
            ISystemContext context,
            MethodState method,
            bool status,
            IList<object> inputArguments)
        {
            BaseObjectState parent = method.Parent as BaseObjectState;

            #region Task #D6 - Add Support for Notifiers
            // check if it is even necessary to produce an event.
            if (parent != null && !parent.AreEventsMonitored)
            {
                return;
            }
            #endregion

            // construct translation object with default text.
            TranslationInfo info = new TranslationInfo(
                "AuditUpdateMethodEvent",
                "en-US",
                "A method was called.");

            // intialize the event.
            AuditUpdateMethodEventState e = new AuditUpdateMethodEventState(null);

            e.Initialize(
                SystemContext,
                null,
                EventSeverity.Medium,
                new LocalizedText(info),
                true,
                DateTime.UtcNow);

            // fill in additional fields.
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.SourceNode, method.Parent.NodeId, false);
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.SourceName, "Attribute/Call", false);
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.ServerId, context.ServerUris.GetString(0), false);
            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.ClientAuditEntryId, context.AuditEntryId, false);

            if (context.UserIdentity != null)
            {
                e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.ClientUserId, context.UserIdentity.DisplayName, false);
            }

            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.MethodId, method.NodeId, false);

            // need to change data type.
            Variant[] values = new Variant[inputArguments.Count];

            for (int ii = 0; ii < values.Length; ii++)
            {
                values[ii] = new Variant(inputArguments[ii]);
            }

            e.SetChildValue(SystemContext, Opc.Ua.BrowseNames.InputArguments, values, false);

            #region Task #D6 - Add Support for Notifiers
            // report the event to the parent. let it propagate up the tree. 
            if (parent != null && parent.AreEventsMonitored)
            {
                parent.ReportEvent(context, e);
                return;
            }
            #endregion

            // report the event to the server object.
            Server.ReportEvent(e);
        }
Example #8
0
        private void MonitorMethodUpdateNotification(MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs e)
        {
            try
            {
                if (!(e.NotificationValue is EventFieldList notification))
                {
                    return;
                }
                NodeId eventTypeId = null;
                if (!(monitoredItem.Status.Filter is EventFilter filter))
                {
                    return;
                }
                for (int index = 0; index < filter.SelectClauses.Count; index++)
                {
                    SimpleAttributeOperand simpleAttributeOperand = filter.SelectClauses[index];
                    if (simpleAttributeOperand.BrowsePath.Count != 1 ||
                        simpleAttributeOperand.BrowsePath[0] != BrowseNames.EventType)
                    {
                        continue;
                    }
                    eventTypeId = notification.EventFields[index].Value as NodeId;
                }

                // look up the known event type.
                Dictionary <NodeId, NodeId> eventTypeMappings = new Dictionary <NodeId, NodeId>();
                if (eventTypeId == null || NodeId.IsNull(eventTypeId))
                {
                    return;
                }
                if (!eventTypeMappings.TryGetValue(eventTypeId, out NodeId knownTypeId))
                {
                    // check for a known type
                    if (KnownEventTypes.Any(nodeId => nodeId == eventTypeId))
                    {
                        knownTypeId = eventTypeId;
                        eventTypeMappings.Add(eventTypeId, eventTypeId);
                    }

                    // browse for the supertypes of the event type.
                    if (knownTypeId == null)
                    {
                        ReferenceDescriptionCollection supertypes = new ReferenceDescriptionCollection();
                        // find all of the children of the field.
                        BrowseDescription nodeToBrowse = new BrowseDescription
                        {
                            NodeId          = eventTypeId,
                            BrowseDirection = BrowseDirection.Inverse,
                            ReferenceTypeId = ReferenceTypeIds.HasSubtype,
                            IncludeSubtypes = false, // more efficient to use IncludeSubtypes=False when possible.
                            NodeClassMask   = 0,     // the HasSubtype reference already restricts the targets to Types.
                            ResultMask      = (uint)BrowseResultMask.All
                        };

                        ReferenceDescriptionCollection
                            references = _applicationInstanceManager.Browse(nodeToBrowse);
                        while (references != null && references.Count > 0)
                        {
                            // should never be more than one supertype.
                            supertypes.Add(references[0]);
                            // only follow references within this server.
                            if (references[0].NodeId.IsAbsolute)
                            {
                                break;
                            }

                            // get the references for the next level up.
                            nodeToBrowse.NodeId = (NodeId)references[0].NodeId;
                            references          = _applicationInstanceManager.Browse(nodeToBrowse);
                        }

                        // find the first super type that matches a known event type.
                        foreach (ReferenceDescription referenceDescription in supertypes)
                        {
                            foreach (NodeId nodeId in KnownEventTypes)
                            {
                                if (nodeId != referenceDescription.NodeId)
                                {
                                    continue;
                                }
                                knownTypeId = nodeId;
                                eventTypeMappings.Add(eventTypeId, knownTypeId);
                                break;
                            }

                            if (knownTypeId != null)
                            {
                                break;
                            }
                        }
                    }
                }

                if (knownTypeId == null)
                {
                    return;
                }
                // all of the known event types have a UInt32 as identifier.
                uint?id = knownTypeId.Identifier as uint?;
                if (id == null)
                {
                    return;
                }
                // construct the event based on the known event type.
                BaseEventState baseEventState = null;

                switch (id.Value)
                {
                case ObjectTypes.ConditionType:
                {
                    baseEventState = new ConditionState(null);
                    break;
                }

                case ObjectTypes.DialogConditionType:
                {
                    baseEventState = new DialogConditionState(null);
                    break;
                }

                case ObjectTypes.AlarmConditionType:
                {
                    baseEventState = new AlarmConditionState(null);
                    break;
                }

                case ObjectTypes.ExclusiveLimitAlarmType:
                {
                    baseEventState = new ExclusiveLimitAlarmState(null);
                    break;
                }

                case ObjectTypes.NonExclusiveLimitAlarmType:
                {
                    baseEventState = new NonExclusiveLimitAlarmState(null);
                    break;
                }

                case ObjectTypes.AuditEventType:
                {
                    baseEventState = new AuditEventState(null);
                    break;
                }

                case ObjectTypes.AuditUpdateMethodEventType:
                {
                    baseEventState = new AuditUpdateMethodEventState(null);
                    break;
                }

                default:
                {
                    baseEventState = new BaseEventState(null);
                    break;
                }
                }

                // get the filter which defines the contents of the notification.
                filter = monitoredItem.Status.Filter as EventFilter;
                // initialize the event with the values in the notification.
                baseEventState.Update(_applicationInstanceManager.Session.SystemContext, filter.SelectClauses,
                                      notification);
                // save the original notification.
                baseEventState.Handle = notification;
                // construct the audit object.
                if (baseEventState is AuditUpdateMethodEventState audit)
                {
                    // look up the condition type metadata in the local cache.
                    string sourceName = "";
                    if (audit.SourceName.Value != null)
                    {
                        sourceName = Utils.Format("{0}", audit.SourceName.Value);
                    }
                    string type = "";
                    if (audit.TypeDefinitionId != null)
                    {
                        type = Utils.Format("{0}",
                                            _applicationInstanceManager.Session.NodeCache.Find(audit.TypeDefinitionId));
                    }

                    string method = "";
                    if (audit.MethodId != null)
                    {
                        method = Utils.Format("{0}",
                                              _applicationInstanceManager.Session.NodeCache.Find(
                                                  BaseVariableState.GetValue(audit.MethodId)));
                    }

                    string status = "";
                    if (audit.Status != null)
                    {
                        status = Utils.Format("{0}", audit.Status.Value);
                    }

                    string time = "";
                    if (audit.Time != null)
                    {
                        time = Utils.Format("{0:HH:mm:ss.fff}", audit.Time.Value.ToLocalTime());
                    }

                    string message = "";
                    if (audit.Message != null)
                    {
                        message = Utils.Format("{0}", audit.Message.Value);
                    }

                    string inputArguments = "";
                    if (audit.InputArguments != null)
                    {
                        inputArguments = Utils.Format("{0}", new Variant(audit.InputArguments.Value));
                    }


                    InformationDisplay(
                        $"sourceName: {sourceName}, type:{type}, method:{method}, status:{status}, time:{time}, message:{message}, inputArguments:{inputArguments}");
                }
            }
            catch (Exception ex)
            {
                InformationDisplay($"Monitored Item Notification exception: {ex.StackTrace}");
            }
        }
Example #9
0
        /// <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;
        }
Example #10
0
        private ServiceResult AddApplication(ISystemContext context, MethodState method, IList <object> inputArguments, IList <object> outputArguments)
        {
            if (inputArguments.Count != 1)
            {
                return(StatusCodes.BadArgumentsMissing);
            }
            // check the data type of the input arguments.
            string value = inputArguments[0] as string;

            if (value == null)
            {
                return(StatusCodes.BadTypeMismatch);
            }

            if (_applications == null)
            {
                _applications = new List <string>();
            }
            _applications.Add(value);

            if (_processor == null)
            {
                _processor          = new Processor();
                _processor.Name     = "Turbo";
                _processor.MinSpeed = 0;
                _processor.MaxSpeed = 0;
            }
            _processor.MinSpeed  = 1;
            _processor.MaxSpeed += 1;

            //Could not encode outgoing
            outputArguments[0] = _applications.ToArray();
            outputArguments[1] = _processor;

            //Report
            TranslationInfo info = new TranslationInfo(
                "AddApplication",
                "en-US",
                "The Confirm method was called.");
            AuditUpdateMethodEventState auditUpdateMethodEventState = new AuditUpdateMethodEventState(method);

            auditUpdateMethodEventState.Initialize(
                context,
                method,
                EventSeverity.Low,
                new LocalizedText(info),
                ServiceResult.IsGood(StatusCodes.Good),
                DateTime.UtcNow);
            auditUpdateMethodEventState.SourceName.Value = "Attribute/Call";
            auditUpdateMethodEventState.MethodId         = new PropertyState <NodeId>(method)
            {
                Value = method.NodeId
            };
            auditUpdateMethodEventState.InputArguments = new PropertyState <object[]>(method)
            {
                Value = new object[] { inputArguments }
            };
            auditUpdateMethodEventState.SetChildValue(context, BrowseNames.InputArguments, inputArguments.ToArray(), true);
            bool valid = auditUpdateMethodEventState.Validate(context);

            if (valid)
            {
                ApplicationNodeManager.Server.ReportEvent(auditUpdateMethodEventState);
            }
            return(ServiceResult.Good);
        }