use of org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsRequest 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);
});
}
Aggregations