Search in sources :

Example 1 with DataValueListener

use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem.DataValueListener in project milo by eclipse.

the class ManagedSubscription method createDataItemsAsync.

/**
 * Create {@link ManagedDataItem}s monitoring the Nodes and Attributes identified by {@code readValueIds}.
 * <p>
 * {@code consumer} will receive each item as it is created to provide an opportunity to add a
 * {@link DataValueListener} before the first value change has arrived.
 * <p>
 * The operation results should be checked before each item is used further.
 * See {@link ManagedDataItem#getStatusCode()}.
 * <p>
 * This call completes asynchronously.
 *
 * @param samplingInterval the sampling interval to request.
 * @param readValueIds     the {@link ReadValueId}s identifying the Nodes and Attributes.
 * @param consumer         a {@link Consumer} that will receive each item as it is created.
 * @return a {@link CompletableFuture} that completes successfully with a List of {@link ManagedDataItem} or
 * completes exceptionally if a service-level error occurs.
 */
public CompletableFuture<List<ManagedDataItem>> createDataItemsAsync(double samplingInterval, List<ReadValueId> readValueIds, Consumer<ManagedDataItem> consumer) {
    final ExtensionObject filter = getDefaultDataFilter();
    final UInteger queueSize = getDefaultQueueSize();
    final boolean discardOldest = getDefaultDiscardOldest();
    List<MonitoredItemCreateRequest> createRequests = readValueIds.stream().map(readValueId -> {
        MonitoringParameters parameters = new MonitoringParameters(subscription.nextClientHandle(), samplingInterval, filter, queueSize, discardOldest);
        return new MonitoredItemCreateRequest(readValueId, getDefaultMonitoringMode(), parameters);
    }).collect(Collectors.toList());
    CompletableFuture<List<UaMonitoredItem>> monitoredItems = subscription.createMonitoredItems(getDefaultTimestamps(), createRequests, (item, id) -> {
        ManagedDataItem dataItem = createAndTrackDataItem(item);
        consumer.accept(dataItem);
    });
    return monitoredItems.thenApply(items -> items.stream().map(item -> dataItems.get(item.getClientHandle())).filter(Objects::nonNull).collect(Collectors.toList()));
}
Also used : MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters) CallMethodRequest(org.eclipse.milo.opcua.stack.core.types.structured.CallMethodRequest) EventFilter(org.eclipse.milo.opcua.stack.core.types.structured.EventFilter) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) CompletableFuture.completedFuture(java.util.concurrent.CompletableFuture.completedFuture) CompletableFuture(java.util.concurrent.CompletableFuture) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) DateTime(org.eclipse.milo.opcua.stack.core.types.builtin.DateTime) ArrayList(java.util.ArrayList) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) Collections.singletonList(java.util.Collections.singletonList) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) Unit(org.eclipse.milo.opcua.stack.core.util.Unit) Map(java.util.Map) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) BiConsumer(java.util.function.BiConsumer) DataValueListener(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem.DataValueListener) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) DataChangeFilter(org.eclipse.milo.opcua.stack.core.types.structured.DataChangeFilter) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) FutureUtils.failedUaFuture(org.eclipse.milo.opcua.stack.core.util.FutureUtils.failedUaFuture) ExecutionQueue(org.eclipse.milo.opcua.stack.core.util.ExecutionQueue) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) Objects(java.util.Objects) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) UaException(org.eclipse.milo.opcua.stack.core.UaException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) Objects(java.util.Objects) MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList)

Aggregations

ArrayList (java.util.ArrayList)1 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 CompletableFuture.completedFuture (java.util.concurrent.CompletableFuture.completedFuture)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 BiConsumer (java.util.function.BiConsumer)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)1 UaMonitoredItem (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem)1 UaSubscription (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription)1 DataValueListener (org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem.DataValueListener)1 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)1 Identifiers (org.eclipse.milo.opcua.stack.core.Identifiers)1 StatusCodes (org.eclipse.milo.opcua.stack.core.StatusCodes)1