Search in sources :

Example 1 with SubscriptionDiagnosticsDataType

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

the class SubscriptionDiagnosticsVariableArray 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()) {
        // noinspection UnstableApiUsage
        server.getEventBus().register(eventSubscriber = new EventSubscriber());
    }
    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) {
                    getSubscriptions().forEach(this::createSubscriptionDiagnosticsNode);
                    if (eventSubscriber == null) {
                        // noinspection UnstableApiUsage
                        server.getEventBus().register(eventSubscriber = new EventSubscriber());
                    }
                } else if (previous && !current) {
                    if (eventSubscriber != null) {
                        // noinspection UnstableApiUsage
                        server.getEventBus().unregister(eventSubscriber);
                        eventSubscriber = null;
                    }
                    subscriptionDiagnosticsVariables.forEach(AbstractLifecycle::shutdown);
                    subscriptionDiagnosticsVariables.clear();
                }
            }
        }
    };
    diagnosticsNode.getEnabledFlagNode().addAttributeObserver(attributeObserver);
    node.getFilterChain().addLast(diagnosticValueFilter(diagnosticsEnabled, ctx -> {
        ExtensionObject[] xos = ExtensionObject.encodeArray(server.getSerializationContext(), getSubscriptions().stream().map(s -> s.getSubscriptionDiagnostics().getSubscriptionDiagnosticsDataType()).toArray(SubscriptionDiagnosticsDataType[]::new));
        return new DataValue(new Variant(xos));
    }));
}
Also used : SubscriptionDiagnosticsArrayTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SubscriptionDiagnosticsArrayTypeNode) NodeManager(org.eclipse.milo.opcua.sdk.server.api.NodeManager) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) SubscriptionDiagnosticsTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SubscriptionDiagnosticsTypeNode) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) SubscriptionCreatedEvent(org.eclipse.milo.opcua.sdk.server.subscriptions.SubscriptionCreatedEvent) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) Util.diagnosticValueFilter(org.eclipse.milo.opcua.sdk.server.diagnostics.variables.Util.diagnosticValueFilter) ArrayList(java.util.ArrayList) SubscriptionDiagnosticsDataType(org.eclipse.milo.opcua.stack.core.types.structured.SubscriptionDiagnosticsDataType) 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) Subscribe(com.google.common.eventbus.Subscribe) NoSuchElementException(java.util.NoSuchElementException) SubscriptionDeletedEvent(org.eclipse.milo.opcua.sdk.server.subscriptions.SubscriptionDeletedEvent) Reference(org.eclipse.milo.opcua.sdk.core.Reference) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) Logger(org.slf4j.Logger) Subscription(org.eclipse.milo.opcua.sdk.server.subscriptions.Subscription) 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) 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) NodeFactory(org.eclipse.milo.opcua.sdk.server.nodes.factories.NodeFactory) 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

Subscribe (com.google.common.eventbus.Subscribe)1 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 OpcUaServer (org.eclipse.milo.opcua.sdk.server.OpcUaServer)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 SubscriptionDiagnosticsArrayTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SubscriptionDiagnosticsArrayTypeNode)1 SubscriptionDiagnosticsTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SubscriptionDiagnosticsTypeNode)1 AttributeObserver (org.eclipse.milo.opcua.sdk.server.nodes.AttributeObserver)1 UaNode (org.eclipse.milo.opcua.sdk.server.nodes.UaNode)1 UaNodeContext (org.eclipse.milo.opcua.sdk.server.nodes.UaNodeContext)1 NodeFactory (org.eclipse.milo.opcua.sdk.server.nodes.factories.NodeFactory)1