Search in sources :

Example 1 with AbstractLifecycle

use of org.eclipse.milo.opcua.sdk.server.AbstractLifecycle 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)

Example 2 with AbstractLifecycle

use of org.eclipse.milo.opcua.sdk.server.AbstractLifecycle in project milo by eclipse.

the class SubscriptionDiagnosticsVariableArray method onShutdown.

@Override
protected void onShutdown() {
    AttributeObserver observer = attributeObserver;
    if (observer != null) {
        ServerDiagnosticsTypeNode diagnosticsNode = (ServerDiagnosticsTypeNode) server.getAddressSpaceManager().getManagedNode(Identifiers.Server_ServerDiagnostics).orElseThrow(() -> new NoSuchElementException("NodeId: " + Identifiers.Server_ServerDiagnostics));
        diagnosticsNode.getEnabledFlagNode().removeAttributeObserver(observer);
        attributeObserver = null;
    }
    if (eventSubscriber != null) {
        // noinspection UnstableApiUsage
        server.getEventBus().unregister(eventSubscriber);
        eventSubscriber = null;
    }
    subscriptionDiagnosticsVariables.forEach(AbstractLifecycle::shutdown);
    subscriptionDiagnosticsVariables.clear();
    node.delete();
}
Also used : ServerDiagnosticsTypeNode(org.eclipse.milo.opcua.sdk.server.model.nodes.objects.ServerDiagnosticsTypeNode) AttributeObserver(org.eclipse.milo.opcua.sdk.server.nodes.AttributeObserver) NoSuchElementException(java.util.NoSuchElementException) AbstractLifecycle(org.eclipse.milo.opcua.sdk.server.AbstractLifecycle)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)2 AbstractLifecycle (org.eclipse.milo.opcua.sdk.server.AbstractLifecycle)2 ServerDiagnosticsTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.objects.ServerDiagnosticsTypeNode)2 AttributeObserver (org.eclipse.milo.opcua.sdk.server.nodes.AttributeObserver)2 Subscribe (com.google.common.eventbus.Subscribe)1 ArrayList (java.util.ArrayList)1 Collections (java.util.Collections)1 List (java.util.List)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 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 SubscriptionDiagnosticsArrayTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SubscriptionDiagnosticsArrayTypeNode)1 SubscriptionDiagnosticsTypeNode (org.eclipse.milo.opcua.sdk.server.model.nodes.variables.SubscriptionDiagnosticsTypeNode)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