Search in sources :

Example 1 with SessionDiagnosticsDataType

use of org.eclipse.milo.opcua.stack.core.types.structured.SessionDiagnosticsDataType in project milo by eclipse.

the class SessionDiagnosticsVariableArray method onStartup.

@Override
protected void onStartup() {
    ServerDiagnosticsTypeNode diagnosticsNode = (ServerDiagnosticsTypeNode) server.getAddressSpaceManager().getManagedNode(Identifiers.Server_ServerDiagnostics).orElseThrow(() -> new NoSuchElementException("NodeId: " + Identifiers.Server_ServerDiagnostics));
    diagnosticsEnabled.set(diagnosticsNode.getEnabledFlag());
    if (diagnosticsEnabled.get()) {
        addSessionListener();
    }
    attributeObserver = (node, attributeId, value) -> {
        if (attributeId == AttributeId.Value) {
            DataValue dataValue = (DataValue) value;
            Object o = dataValue.getValue().getValue();
            if (o instanceof Boolean) {
                boolean current = (boolean) o;
                boolean previous = diagnosticsEnabled.getAndSet(current);
                if (!previous && current) {
                    server.getSessionManager().getAllSessions().forEach(this::createSessionDiagnosticsVariable);
                    if (sessionListener == null) {
                        addSessionListener();
                    }
                } else if (previous && !current) {
                    if (sessionListener != null) {
                        server.getSessionManager().removeSessionListener(sessionListener);
                        sessionListener = null;
                    }
                    sessionDiagnosticsVariables.forEach(Lifecycle::shutdown);
                    sessionDiagnosticsVariables.clear();
                }
            }
        }
    };
    diagnosticsNode.getEnabledFlagNode().addAttributeObserver(attributeObserver);
    node.getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
        ExtensionObject[] xos = ExtensionObject.encodeArray(server.getSerializationContext(), server.getSessionManager().getAllSessions().stream().map(s -> s.getSessionDiagnostics().getSessionDiagnosticsDataType()).toArray(SessionDiagnosticsDataType[]::new));
        return new DataValue(new Variant(xos));
    }));
}
Also used : NodeManager(org.eclipse.milo.opcua.sdk.server.api.NodeManager) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SessionDiagnosticsVariableTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SessionDiagnosticsVariableTypeNode) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) Util.diagnosticValueFilter(org.eclipse.milo.opcua.sdk.server.diagnostics.variables.Util.diagnosticValueFilter) Session(org.eclipse.milo.opcua.sdk.server.Session) SessionListener(org.eclipse.milo.opcua.sdk.server.SessionListener) ArrayList(java.util.ArrayList) ValueRank(org.eclipse.milo.opcua.sdk.core.ValueRank) AccessLevel(org.eclipse.milo.opcua.sdk.core.AccessLevel) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) NoSuchElementException(java.util.NoSuchElementException) Reference(org.eclipse.milo.opcua.sdk.core.Reference) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) Logger(org.slf4j.Logger) LocalizedText(org.eclipse.milo.opcua.stack.core.types.builtin.LocalizedText) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) AbstractLifecycle(org.eclipse.milo.opcua.sdk.server.AbstractLifecycle) AttributeObserver(org.eclipse.milo.opcua.sdk.server.nodes.AttributeObserver) OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) List(java.util.List) Lifecycle(org.eclipse.milo.opcua.sdk.server.Lifecycle) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) ServerDiagnosticsTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.objects.ServerDiagnosticsTypeNode) UaNodeContext(org.eclipse.milo.opcua.sdk.server.nodes.UaNodeContext) UaException(org.eclipse.milo.opcua.stack.core.UaException) SessionDiagnosticsDataType(org.eclipse.milo.opcua.stack.core.types.structured.SessionDiagnosticsDataType) NodeFactory(org.eclipse.milo.opcua.sdk.server.nodes.factories.NodeFactory) SessionDiagnosticsArrayTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SessionDiagnosticsArrayTypeNode) Collections(java.util.Collections) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) ServerDiagnosticsTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.objects.ServerDiagnosticsTypeNode) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) NoSuchElementException(java.util.NoSuchElementException)

Aggregations

ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)1 NoSuchElementException (java.util.NoSuchElementException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AccessLevel (org.eclipse.milo.opcua.sdk.core.AccessLevel)1 Reference (org.eclipse.milo.opcua.sdk.core.Reference)1 ValueRank (org.eclipse.milo.opcua.sdk.core.ValueRank)1 AbstractLifecycle (org.eclipse.milo.opcua.sdk.server.AbstractLifecycle)1 Lifecycle (org.eclipse.milo.opcua.sdk.server.Lifecycle)1 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)1 Session (org.eclipse.milo.opcua.sdk.server.Session)1 SessionListener (org.eclipse.milo.opcua.sdk.server.SessionListener)1 NodeManager (org.eclipse.milo.opcua.sdk.server.api.NodeManager)1 Util.diagnosticValueFilter (org.eclipse.milo.opcua.sdk.server.diagnostics.variables.Util.diagnosticValueFilter)1 ServerDiagnosticsTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.objects.ServerDiagnosticsTypeNode)1 SessionDiagnosticsArrayTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SessionDiagnosticsArrayTypeNode)1 SessionDiagnosticsVariableTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SessionDiagnosticsVariableTypeNode)1 AttributeObserver (org.eclipse.milo.opcua.sdk.server.nodes.AttributeObserver)1 UaNode (org.eclipse.milo.opcua.sdk.server.nodes.UaNode)1