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