Search in sources :

Example 11 with UaMonitoredItem

use of org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem 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)

Example 12 with UaMonitoredItem

use of org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem in project milo by eclipse.

the class OpcUaSubscription method createMonitoredItems.

@Override
public CompletableFuture<List<UaMonitoredItem>> createMonitoredItems(TimestampsToReturn timestampsToReturn, List<MonitoredItemCreateRequest> itemsToCreate) {
    CompletableFuture<CreateMonitoredItemsResponse> future = client.createMonitoredItems(subscriptionId, timestampsToReturn, itemsToCreate);
    return future.thenApply(response -> {
        List<MonitoredItemCreateResult> results = l(response.getResults());
        List<UaMonitoredItem> createdItems = newArrayListWithCapacity(itemsToCreate.size());
        for (int i = 0; i < itemsToCreate.size(); i++) {
            MonitoredItemCreateRequest request = itemsToCreate.get(i);
            MonitoredItemCreateResult result = results.get(i);
            OpcUaMonitoredItem item = new OpcUaMonitoredItem(client, request.getRequestedParameters().getClientHandle(), request.getItemToMonitor(), result.getMonitoredItemId(), result.getStatusCode(), result.getRevisedSamplingInterval(), result.getRevisedQueueSize(), result.getFilterResult(), request.getMonitoringMode(), request.getRequestedParameters().getFilter(), request.getRequestedParameters().getDiscardOldest(), timestampsToReturn);
            item.setRequestedSamplingInterval(request.getRequestedParameters().getSamplingInterval());
            item.setRequestedQueueSize(request.getRequestedParameters().getQueueSize());
            if (item.getStatusCode().isGood()) {
                itemsByClientHandle.put(item.getClientHandle(), item);
                itemsByServerHandle.put(item.getMonitoredItemId(), item);
            }
            createdItems.add(item);
        }
        return createdItems;
    });
}
Also used : UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) CreateMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsResponse) MonitoredItemCreateResult(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateResult) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)

Example 13 with UaMonitoredItem

use of org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem in project milo by eclipse.

the class TriggeringExample method createMonitoredItem.

private UaMonitoredItem createMonitoredItem(UaSubscription subscription, ReadValueId readValueId, MonitoringMode monitoringMode) throws ExecutionException, InterruptedException {
    // important: client handle must be unique per item
    UInteger clientHandle = uint(clientHandles.getAndIncrement());
    MonitoringParameters parameters = new MonitoringParameters(clientHandle, 1000.0, null, uint(10), true);
    MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, monitoringMode, parameters);
    UaSubscription.ItemCreationCallback onItemCreated = (item, id) -> item.setValueConsumer(this::onSubscriptionValue);
    List<UaMonitoredItem> items = subscription.createMonitoredItems(TimestampsToReturn.Both, newArrayList(request), onItemCreated).get();
    return items.get(0);
}
Also used : MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) Logger(org.slf4j.Logger) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) OpcUaClient(org.eclipse.milo.opcua.sdk.client.OpcUaClient) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) ExecutionException(java.util.concurrent.ExecutionException) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters)

Aggregations

UaMonitoredItem (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem)13 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)10 List (java.util.List)9 UaSubscription (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription)9 OpcUaClient (org.eclipse.milo.opcua.sdk.client.OpcUaClient)8 CompletableFuture (java.util.concurrent.CompletableFuture)7 DataValue (org.eclipse.milo.opcua.stack.core.types.builtin.DataValue)7 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)7 MonitoringMode (org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode)7 MonitoredItemCreateRequest (org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest)7 ReadValueId (org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ExecutionException (java.util.concurrent.ExecutionException)6 AttributeId (org.eclipse.milo.opcua.stack.core.AttributeId)6 UaException (org.eclipse.milo.opcua.stack.core.UaException)6 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)6 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)6 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)6 TimestampsToReturn (org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn)6