Search in sources :

Example 1 with MonitoredItemCreateResult

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);
    });
}
Also used : ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) CreateMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsRequest) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) UaException(org.eclipse.milo.opcua.stack.core.UaException) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) ArrayList(java.util.ArrayList) CreateMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsResponse) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) MonitoredItemCreateResult(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateResult) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with MonitoredItemCreateResult

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

Aggregations

Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)2 CreateMonitoredItemsResponse (org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsResponse)2 MonitoredItemCreateRequest (org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest)2 MonitoredItemCreateResult (org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateResult)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 UaMonitoredItem (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem)1 BaseMonitoredItem (org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem)1 UaException (org.eclipse.milo.opcua.stack.core.UaException)1 NodeId (org.eclipse.milo.opcua.stack.core.types.builtin.NodeId)1 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)1 TimestampsToReturn (org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn)1 CreateMonitoredItemsRequest (org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsRequest)1 ResponseHeader (org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader)1