Search in sources :

Example 1 with CreateMonitoredItemsRequest

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

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)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 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)1 TimestampsToReturn (org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn)1 CreateMonitoredItemsRequest (org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsRequest)1 CreateMonitoredItemsResponse (org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsResponse)1 MonitoredItemCreateRequest (org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest)1 MonitoredItemCreateResult (org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateResult)1 ResponseHeader (org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader)1