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));
}));
}
Aggregations