/// <summary> /// Updates the audit event. /// </summary> private void UpdateAuditEvent(AuditEventState instance, EventType eventType, ONEVENTSTRUCT e) { instance.SetChildValue(m_defaultContext, Opc.Ua.BrowseNames.ActionTimeStamp, instance.Time.Value, false); instance.SetChildValue(m_defaultContext, Opc.Ua.BrowseNames.Status, true, false); instance.SetChildValue(m_defaultContext, Opc.Ua.BrowseNames.ServerId, m_defaultContext.NamespaceUris.GetString(m_namespaceIndex), false); instance.SetChildValue(m_defaultContext, Opc.Ua.BrowseNames.ClientUserId, e.szActorID, false); }
private void OnRaiseSystemEvents(object state) { try { var e = new SystemEventState(null); e.Initialize( SystemContext, null, EventSeverity.Medium, new LocalizedText("Raising Events")); e.SetChildValue(SystemContext, BrowseNames.SourceNode, ObjectIds.Server, false); e.SetChildValue(SystemContext, BrowseNames.SourceName, "Internal", false); Server.ReportEvent(e); var ae = new AuditEventState(null); ae.Initialize( SystemContext, null, EventSeverity.Medium, new LocalizedText("Events Raised"), true, DateTime.UtcNow); ae.SetChildValue(SystemContext, BrowseNames.SourceNode, ObjectIds.Server, false); ae.SetChildValue(SystemContext, BrowseNames.SourceName, "Internal", false); Server.ReportEvent(ae); } catch (Exception e) { Utils.Trace(e, "Unexpected error in OnRaiseSystemEvents"); } }
/// <summary> /// Creates an instance of an event. /// </summary> public BaseEventState CreateInstance(ServerSystemContext context, AeEventTypeState eventType) { BaseEventState instance = null; switch (eventType.EventType.EventTypeMapping) { case EventTypeMapping.AlarmConditionType: { instance = new AlarmConditionState(null); break; } case EventTypeMapping.AuditEventType: { instance = new AuditEventState(null); break; } case EventTypeMapping.BaseEventType: { instance = new BaseEventState(null); break; } case EventTypeMapping.DeviceFailureEventType: { instance = new DeviceFailureEventState(null); break; } case EventTypeMapping.DiscreteAlarmType: { instance = new DiscreteAlarmState(null); break; } case EventTypeMapping.NonExclusiveDeviationAlarmType: { instance = new NonExclusiveDeviationAlarmState(null); break; } case EventTypeMapping.ExclusiveLevelAlarmType: { instance = new ExclusiveLevelAlarmState(null); break; } case EventTypeMapping.LimitAlarmType: { instance = new LimitAlarmState(null); break; } case EventTypeMapping.NonExclusiveLevelAlarmType: { instance = new NonExclusiveLevelAlarmState(null); break; } case EventTypeMapping.OffNormalAlarmType: { instance = new OffNormalAlarmState(null); break; } case EventTypeMapping.SystemEventType: { instance = new SystemEventState(null); break; } case EventTypeMapping.TripAlarmType: { instance = new TripAlarmState(null); break; } } return(instance); }
/// <inheritdoc/> public void ReportAuditEvent(ISystemContext context, AuditEventState e) { if (Auditing == false) { // do not report auditing events if server Auditing flag is false return; } ReportEvent(context, e); }
/// <summary> /// Initializes a session audit event. /// </summary> private void InitializeSessionAuditEvent(ServerSystemContext systemContext, AuditEventState e, TranslationInfo message) { e.Initialize( systemContext, null, EventSeverity.MediumLow, new LocalizedText(message), true, DateTime.UtcNow); e.SetChildValue(systemContext, BrowseNames.SourceNode, m_sessionId, false); e.SetChildValue(systemContext, BrowseNames.SourceName, m_sessionName, false); e.SetChildValue(systemContext, BrowseNames.SessionId, m_sessionId, false); e.SetChildValue(systemContext, BrowseNames.ServerId, m_server.ServerUris.GetString(0), false); e.SetChildValue(systemContext, BrowseNames.ClientUserId, m_identity.DisplayName, false); e.SetChildValue(systemContext, BrowseNames.ClientAuditEntryId, systemContext.OperationContext.AuditEntryId, false); }
/// <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> /// 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); }
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}"); } }