use of org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateResult in project milo by eclipse.
the class SubscriptionManager method createMonitoredItems.
public void createMonitoredItems(ServiceRequest service) throws UaException {
CreateMonitoredItemsRequest request = (CreateMonitoredItemsRequest) service.getRequest();
UInteger subscriptionId = request.getSubscriptionId();
Subscription subscription = subscriptions.get(subscriptionId);
TimestampsToReturn timestamps = request.getTimestampsToReturn();
List<MonitoredItemCreateRequest> itemsToCreate = l(request.getItemsToCreate());
if (subscription == null) {
throw new UaException(StatusCodes.Bad_SubscriptionIdInvalid);
}
if (timestamps == null) {
throw new UaException(StatusCodes.Bad_TimestampsToReturnInvalid);
}
if (itemsToCreate.isEmpty()) {
throw new UaException(StatusCodes.Bad_NothingToDo);
}
List<NodeId> distinctNodeIds = itemsToCreate.stream().map(item -> item.getItemToMonitor().getNodeId()).distinct().collect(toList());
CompletableFuture<Map<NodeId, AttributeGroup>> attributesFuture = readMonitoringAttributes(distinctNodeIds);
attributesFuture.thenAccept(attributeGroups -> {
MonitoredItemCreateResult[] createResults = new MonitoredItemCreateResult[itemsToCreate.size()];
List<BaseMonitoredItem<?>> monitoredItems = new ArrayList<>();
for (int i = 0; i < itemsToCreate.size(); i++) {
MonitoredItemCreateRequest createRequest = itemsToCreate.get(i);
try {
BaseMonitoredItem<?> monitoredItem = createMonitoredItem(createRequest, subscription, timestamps, attributeGroups);
monitoredItems.add(monitoredItem);
createResults[i] = new MonitoredItemCreateResult(StatusCode.GOOD, monitoredItem.getId(), monitoredItem.getSamplingInterval(), uint(monitoredItem.getQueueSize()), monitoredItem.getFilterResult());
} catch (UaException e) {
createResults[i] = new MonitoredItemCreateResult(e.getStatusCode(), UInteger.MIN, 0.0, UInteger.MIN, null);
}
}
subscription.addMonitoredItems(monitoredItems);
// Notify AddressSpaces of the items we just created.
byMonitoredItemType(monitoredItems, dataItems -> server.getAddressSpaceManager().onDataItemsCreated(dataItems), eventItems -> server.getAddressSpaceManager().onEventItemsCreated(eventItems));
ResponseHeader header = service.createResponseHeader();
CreateMonitoredItemsResponse response = new CreateMonitoredItemsResponse(header, createResults, new DiagnosticInfo[0]);
service.setResponse(response);
});
}
use of org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateResult 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;
});
}
Aggregations