/// <summary> /// Updates the session diagnostics summary structure. /// </summary> private bool UpdateSessionDiagnostics( SessionDiagnosticsData diagnostics, SessionDiagnosticsDataType[] sessionArray, int index) { // get the latest snapshot. object value = null; ServiceResult result = diagnostics.UpdateCallback( SystemContext, diagnostics.Value.Variable, ref value); SessionDiagnosticsDataType newValue = value as SessionDiagnosticsDataType; sessionArray[index] = newValue; // check for changes. if (Utils.IsEqual(newValue, diagnostics.Value.Value)) { return false; } diagnostics.Value.Error = null; // check for bad value. if (ServiceResult.IsNotBad(result) && newValue == null) { result = StatusCodes.BadOutOfService; } // check for bad result. if (ServiceResult.IsBad(result)) { diagnostics.Value.Error = result; newValue = null; } // update the value. diagnostics.Value.Value = newValue; diagnostics.Value.Timestamp = DateTime.UtcNow; // notify any monitored items. diagnostics.Value.ChangesComplete(SystemContext); return true; }
/// <summary> /// Creates the diagnostics node for a subscription. /// </summary> public NodeId CreateSessionDiagnostics( ServerSystemContext systemContext, SessionDiagnosticsDataType diagnostics, NodeValueSimpleEventHandler updateCallback, SessionSecurityDiagnosticsDataType securityDiagnostics, NodeValueSimpleEventHandler updateSecurityCallback) { NodeId nodeId = null; lock (Lock) { SessionDiagnosticsObjectState sessionNode = new SessionDiagnosticsObjectState(null); // create a new instance and assign ids. nodeId = CreateNode( systemContext, null, ReferenceTypeIds.HasComponent, new QualifiedName(diagnostics.SessionName), sessionNode); diagnostics.SessionId = nodeId; securityDiagnostics.SessionId = nodeId; // check if diagnostics have been enabled. if (!m_diagnosticsEnabled) { return nodeId; } // add reference to session summary object. sessionNode.AddReference( ReferenceTypeIds.HasComponent, true, ObjectIds.Server_ServerDiagnostics_SessionsDiagnosticsSummary); // add reference from session summary object. SessionsDiagnosticsSummaryState summary = (SessionsDiagnosticsSummaryState)FindPredefinedNode( ObjectIds.Server_ServerDiagnostics_SessionsDiagnosticsSummary, typeof(SessionsDiagnosticsSummaryState)); if (summary != null) { summary.AddReference(ReferenceTypeIds.HasComponent, false, sessionNode.NodeId); } // initialize diagnostics node. SessionDiagnosticsVariableState diagnosticsNode = sessionNode.CreateChild( systemContext, BrowseNames.SessionDiagnostics) as SessionDiagnosticsVariableState; // wrap diagnostics in a thread safe object. SessionDiagnosticsVariableValue diagnosticsValue = new SessionDiagnosticsVariableValue( diagnosticsNode, diagnostics, Lock); // must ensure the first update gets sent. diagnosticsValue.Value = null; diagnosticsValue.Error = StatusCodes.BadWaitingForInitialData; diagnosticsValue.CopyPolicy = Opc.Ua.VariableCopyPolicy.Never; diagnosticsValue.OnBeforeRead = OnBeforeReadDiagnostics; // initialize security diagnostics node. SessionSecurityDiagnosticsState securityDiagnosticsNode = sessionNode.CreateChild( systemContext, BrowseNames.SessionSecurityDiagnostics) as SessionSecurityDiagnosticsState; // wrap diagnostics in a thread safe object. SessionSecurityDiagnosticsValue securityDiagnosticsValue = new SessionSecurityDiagnosticsValue( securityDiagnosticsNode, securityDiagnostics, Lock); // must ensure the first update gets sent. securityDiagnosticsValue.Value = null; securityDiagnosticsValue.Error = StatusCodes.BadWaitingForInitialData; securityDiagnosticsValue.CopyPolicy = Opc.Ua.VariableCopyPolicy.Never; securityDiagnosticsValue.OnBeforeRead = OnBeforeReadDiagnostics; // save the session. SessionDiagnosticsData sessionData = new SessionDiagnosticsData( sessionNode, diagnosticsValue, updateCallback, securityDiagnosticsValue, updateSecurityCallback); m_sessions.Add(sessionData); // send initial update. DoScan(true); } return nodeId; }