Search in sources :

Example 1 with BaseMonitoredItem

use of org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem in project milo by eclipse.

the class SubscriptionManager method modifyMonitoredItems.

public void modifyMonitoredItems(ServiceRequest service) throws UaException {
    ModifyMonitoredItemsRequest request = (ModifyMonitoredItemsRequest) service.getRequest();
    UInteger subscriptionId = request.getSubscriptionId();
    Subscription subscription = subscriptions.get(subscriptionId);
    TimestampsToReturn timestamps = request.getTimestampsToReturn();
    List<MonitoredItemModifyRequest> itemsToModify = l(request.getItemsToModify());
    if (subscription == null) {
        throw new UaException(StatusCodes.Bad_SubscriptionIdInvalid);
    }
    if (timestamps == null) {
        throw new UaException(StatusCodes.Bad_TimestampsToReturnInvalid);
    }
    if (itemsToModify.isEmpty()) {
        throw new UaException(StatusCodes.Bad_NothingToDo);
    }
    List<NodeId> distinctNodeIds = itemsToModify.stream().map(item -> {
        UInteger itemId = item.getMonitoredItemId();
        BaseMonitoredItem<?> monitoredItem = subscription.getMonitoredItems().get(itemId);
        return monitoredItem != null ? monitoredItem.getReadValueId().getNodeId() : NodeId.NULL_VALUE;
    }).filter(NodeId::isNotNull).distinct().collect(toList());
    CompletableFuture<Map<NodeId, AttributeGroup>> attributesFuture = readMonitoringAttributes(distinctNodeIds);
    attributesFuture.thenAccept(attributeGroups -> {
        MonitoredItemModifyResult[] modifyResults = new MonitoredItemModifyResult[itemsToModify.size()];
        List<BaseMonitoredItem<?>> monitoredItems = new ArrayList<>();
        for (int i = 0; i < itemsToModify.size(); i++) {
            MonitoredItemModifyRequest modifyRequest = itemsToModify.get(i);
            try {
                BaseMonitoredItem<?> monitoredItem = modifyMonitoredItem(modifyRequest, timestamps, subscription, attributeGroups);
                monitoredItems.add(monitoredItem);
                modifyResults[i] = new MonitoredItemModifyResult(StatusCode.GOOD, monitoredItem.getSamplingInterval(), uint(monitoredItem.getQueueSize()), monitoredItem.getFilterResult());
            } catch (UaException e) {
                modifyResults[i] = new MonitoredItemModifyResult(e.getStatusCode(), 0.0, UInteger.MIN, null);
            }
        }
        subscription.resetLifetimeCounter();
        /*
             * Notify AddressSpaces of the items we just modified.
             */
        byMonitoredItemType(monitoredItems, dataItems -> server.getAddressSpaceManager().onDataItemsModified(dataItems), eventItems -> server.getAddressSpaceManager().onEventItemsModified(eventItems));
        /*
             * AddressSpaces have been notified; send response.
             */
        ResponseHeader header = service.createResponseHeader();
        ModifyMonitoredItemsResponse response = new ModifyMonitoredItemsResponse(header, modifyResults, new DiagnosticInfo[0]);
        service.setResponse(response);
    });
}
Also used : AttributeKey(io.netty.util.AttributeKey) MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters) ReadContext(org.eclipse.milo.opcua.sdk.server.api.services.AttributeServices.ReadContext) DeadbandType(org.eclipse.milo.opcua.stack.core.types.enumerated.DeadbandType) DataItem(org.eclipse.milo.opcua.sdk.server.api.DataItem) LoggerFactory(org.slf4j.LoggerFactory) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) PublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.PublishRequest) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) Map(java.util.Map) AttributeId(org.eclipse.milo.opcua.stack.core.AttributeId) CreateSubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionResponse) ModifySubscriptionRequest(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionRequest) EnumSet(java.util.EnumSet) Reference(org.eclipse.milo.opcua.sdk.core.Reference) EventItem(org.eclipse.milo.opcua.sdk.server.api.EventItem) DataChangeFilter(org.eclipse.milo.opcua.stack.core.types.structured.DataChangeFilter) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) UByte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UByte) ModifyMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsRequest) NumericRange(org.eclipse.milo.opcua.sdk.core.NumericRange) MonitoringFilter(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringFilter) RepublishResponse(org.eclipse.milo.opcua.stack.core.types.structured.RepublishResponse) UaNode(org.eclipse.milo.opcua.sdk.server.nodes.UaNode) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) OpcUaServer(org.eclipse.milo.opcua.sdk.server.OpcUaServer) SetPublishingModeResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetPublishingModeResponse) SubscriptionAcknowledgement(org.eclipse.milo.opcua.stack.core.types.structured.SubscriptionAcknowledgement) DiagnosticInfo(org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SetMonitoringModeRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeRequest) Stream(java.util.stream.Stream) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) StreamUtil.opt2stream(org.eclipse.milo.opcua.sdk.core.util.StreamUtil.opt2stream) DeleteSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsResponse) CreateSubscriptionRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateSubscriptionRequest) Identifiers(org.eclipse.milo.opcua.stack.core.Identifiers) MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode) State(org.eclipse.milo.opcua.sdk.server.subscriptions.Subscription.State) SetMonitoringModeResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeResponse) EventFilter(org.eclipse.milo.opcua.stack.core.types.structured.EventFilter) DataValue(org.eclipse.milo.opcua.stack.core.types.builtin.DataValue) NotificationMessage(org.eclipse.milo.opcua.stack.core.types.structured.NotificationMessage) MonitoredEventItem(org.eclipse.milo.opcua.sdk.server.items.MonitoredEventItem) CreateMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsResponse) RepublishRequest(org.eclipse.milo.opcua.stack.core.types.structured.RepublishRequest) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ModifyMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Session(org.eclipse.milo.opcua.sdk.server.Session) ArrayList(java.util.ArrayList) AccessLevel(org.eclipse.milo.opcua.sdk.core.AccessLevel) Lists(com.google.common.collect.Lists) MonitoredItemModifyRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemModifyRequest) ServiceRequest(org.eclipse.milo.opcua.stack.server.services.ServiceRequest) ConversionUtil.l(org.eclipse.milo.opcua.stack.core.util.ConversionUtil.l) DeleteSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsRequest) SetTriggeringResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringResponse) DeleteMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsRequest) DeleteMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsResponse) StatusCodes(org.eclipse.milo.opcua.stack.core.StatusCodes) Logger(org.slf4j.Logger) Lists.newArrayListWithCapacity(com.google.common.collect.Lists.newArrayListWithCapacity) Iterator(java.util.Iterator) MonitoredDataItem(org.eclipse.milo.opcua.sdk.server.items.MonitoredDataItem) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) MonitoredItemModifyResult(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemModifyResult) UaSerializationException(org.eclipse.milo.opcua.stack.core.UaSerializationException) SetTriggeringRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringRequest) Maps(com.google.common.collect.Maps) Unsigned.ubyte(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.ubyte) MonitoredItemCreateResult(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateResult) Consumer(java.util.function.Consumer) MonitoredItem(org.eclipse.milo.opcua.sdk.server.api.MonitoredItem) CreateMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.CreateMonitoredItemsRequest) AtomicLong(java.util.concurrent.atomic.AtomicLong) BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) SetPublishingModeRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetPublishingModeRequest) Collectors.toList(java.util.stream.Collectors.toList) ModifySubscriptionResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifySubscriptionResponse) UaException(org.eclipse.milo.opcua.stack.core.UaException) ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) MonitoredItemModifyRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemModifyRequest) ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) MonitoredItemModifyResult(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemModifyResult) UaException(org.eclipse.milo.opcua.stack.core.UaException) TimestampsToReturn(org.eclipse.milo.opcua.stack.core.types.enumerated.TimestampsToReturn) ArrayList(java.util.ArrayList) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ModifyMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsRequest) ModifyMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.ModifyMonitoredItemsResponse) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) NodeId(org.eclipse.milo.opcua.stack.core.types.builtin.NodeId) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with BaseMonitoredItem

use of org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem in project milo by eclipse.

the class SubscriptionManager method sessionClosed.

public void sessionClosed(boolean deleteSubscriptions) {
    Iterator<Subscription> iterator = subscriptions.values().iterator();
    while (iterator.hasNext()) {
        Subscription s = iterator.next();
        s.setStateListener(null);
        if (deleteSubscriptions) {
            server.getSubscriptions().remove(s.getId());
            server.getEventBus().post(new SubscriptionDeletedEvent(s));
            List<BaseMonitoredItem<?>> deletedItems = s.deleteSubscription();
            /*
                 * Notify AddressSpaces the items for this subscription are deleted.
                 */
            byMonitoredItemType(deletedItems, dataItems -> server.getAddressSpaceManager().onDataItemsDeleted(dataItems), eventItems -> server.getAddressSpaceManager().onEventItemsDeleted(eventItems));
        }
        iterator.remove();
    }
    if (deleteSubscriptions) {
        while (publishQueue.isNotEmpty()) {
            ServiceRequest publishService = publishQueue.poll();
            if (publishService != null) {
                publishService.setServiceFault(StatusCodes.Bad_SessionClosed);
            }
        }
    }
}
Also used : BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) ServiceRequest(org.eclipse.milo.opcua.stack.server.services.ServiceRequest)

Example 3 with BaseMonitoredItem

use of org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem in project milo by eclipse.

the class SubscriptionManager method setMonitoringMode.

public void setMonitoringMode(ServiceRequest service) {
    SetMonitoringModeRequest request = (SetMonitoringModeRequest) service.getRequest();
    UInteger subscriptionId = request.getSubscriptionId();
    try {
        Subscription subscription = subscriptions.get(subscriptionId);
        List<UInteger> itemsToModify = l(request.getMonitoredItemIds());
        if (subscription == null) {
            throw new UaException(StatusCodes.Bad_SubscriptionIdInvalid);
        }
        if (itemsToModify.isEmpty()) {
            throw new UaException(StatusCodes.Bad_NothingToDo);
        }
        /*
             * Set MonitoringMode on each monitored item, if it exists.
             */
        MonitoringMode monitoringMode = request.getMonitoringMode();
        StatusCode[] results = new StatusCode[itemsToModify.size()];
        List<MonitoredItem> modified = newArrayListWithCapacity(itemsToModify.size());
        for (int i = 0; i < itemsToModify.size(); i++) {
            UInteger itemId = itemsToModify.get(i);
            BaseMonitoredItem<?> item = subscription.getMonitoredItems().get(itemId);
            if (item != null) {
                item.setMonitoringMode(monitoringMode);
                modified.add(item);
                results[i] = StatusCode.GOOD;
            } else {
                results[i] = new StatusCode(StatusCodes.Bad_MonitoredItemIdInvalid);
            }
        }
        /*
             * Notify AddressSpace of the items whose MonitoringMode has been modified.
             */
        server.getAddressSpaceManager().onMonitoringModeChanged(modified);
        /*
             * Build and return results.
             */
        ResponseHeader header = service.createResponseHeader();
        SetMonitoringModeResponse response = new SetMonitoringModeResponse(header, results, new DiagnosticInfo[0]);
        service.setResponse(response);
    } catch (UaException e) {
        service.setServiceFault(e);
    }
}
Also used : ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) SetMonitoringModeResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeResponse) UaException(org.eclipse.milo.opcua.stack.core.UaException) SetMonitoringModeRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetMonitoringModeRequest) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) MonitoredItem(org.eclipse.milo.opcua.sdk.server.api.MonitoredItem) BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) MonitoringMode(org.eclipse.milo.opcua.stack.core.types.enumerated.MonitoringMode)

Example 4 with BaseMonitoredItem

use of org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem in project milo by eclipse.

the class SubscriptionManager method deleteMonitoredItems.

public void deleteMonitoredItems(ServiceRequest service) throws UaException {
    DeleteMonitoredItemsRequest request = (DeleteMonitoredItemsRequest) service.getRequest();
    UInteger subscriptionId = request.getSubscriptionId();
    Subscription subscription = subscriptions.get(subscriptionId);
    List<UInteger> itemsToDelete = l(request.getMonitoredItemIds());
    if (subscription == null) {
        throw new UaException(StatusCodes.Bad_SubscriptionIdInvalid);
    }
    if (itemsToDelete.isEmpty()) {
        throw new UaException(StatusCodes.Bad_NothingToDo);
    }
    StatusCode[] deleteResults = new StatusCode[itemsToDelete.size()];
    List<BaseMonitoredItem<?>> deletedItems = newArrayListWithCapacity(itemsToDelete.size());
    synchronized (subscription) {
        for (int i = 0; i < itemsToDelete.size(); i++) {
            UInteger itemId = itemsToDelete.get(i);
            BaseMonitoredItem<?> item = subscription.getMonitoredItems().get(itemId);
            if (item == null) {
                deleteResults[i] = new StatusCode(StatusCodes.Bad_MonitoredItemIdInvalid);
            } else {
                deletedItems.add(item);
                deleteResults[i] = StatusCode.GOOD;
            }
        }
        subscription.removeMonitoredItems(deletedItems);
    }
    /*
         * Notify AddressSpaces of the items that have been deleted.
         */
    byMonitoredItemType(deletedItems, dataItems -> server.getAddressSpaceManager().onDataItemsDeleted(dataItems), eventItems -> server.getAddressSpaceManager().onEventItemsDeleted(eventItems));
    /*
         * Build and return results.
         */
    ResponseHeader header = service.createResponseHeader();
    DeleteMonitoredItemsResponse response = new DeleteMonitoredItemsResponse(header, deleteResults, new DiagnosticInfo[0]);
    service.setResponse(response);
}
Also used : DeleteMonitoredItemsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsResponse) ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) UaException(org.eclipse.milo.opcua.stack.core.UaException) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) DeleteMonitoredItemsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteMonitoredItemsRequest) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)

Example 5 with BaseMonitoredItem

use of org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem in project milo by eclipse.

the class SubscriptionManager method deleteSubscription.

public void deleteSubscription(ServiceRequest service) throws UaException {
    DeleteSubscriptionsRequest request = (DeleteSubscriptionsRequest) service.getRequest();
    List<UInteger> subscriptionIds = l(request.getSubscriptionIds());
    if (subscriptionIds.isEmpty()) {
        throw new UaException(StatusCodes.Bad_NothingToDo);
    }
    StatusCode[] results = new StatusCode[subscriptionIds.size()];
    for (int i = 0; i < subscriptionIds.size(); i++) {
        UInteger subscriptionId = subscriptionIds.get(i);
        Subscription subscription = subscriptions.remove(subscriptionId);
        if (subscription != null) {
            server.getSubscriptions().remove(subscription.getId());
            server.getEventBus().post(new SubscriptionDeletedEvent(subscription));
            List<BaseMonitoredItem<?>> deletedItems = subscription.deleteSubscription();
            /*
                 * Notify AddressSpaces of the items we just deleted.
                 */
            byMonitoredItemType(deletedItems, dataItems -> server.getAddressSpaceManager().onDataItemsDeleted(dataItems), eventItems -> server.getAddressSpaceManager().onEventItemsDeleted(eventItems));
            results[i] = StatusCode.GOOD;
        } else {
            results[i] = new StatusCode(StatusCodes.Bad_SubscriptionIdInvalid);
        }
    }
    ResponseHeader header = service.createResponseHeader();
    DeleteSubscriptionsResponse response = new DeleteSubscriptionsResponse(header, results, new DiagnosticInfo[0]);
    service.setResponse(response);
    while (subscriptions.isEmpty() && publishQueue.isNotEmpty()) {
        ServiceRequest publishService = publishQueue.poll();
        if (publishService != null) {
            publishService.setServiceFault(StatusCodes.Bad_NoSubscription);
        }
    }
}
Also used : ResponseHeader(org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader) BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) UaException(org.eclipse.milo.opcua.stack.core.UaException) DeleteSubscriptionsResponse(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsResponse) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ServiceRequest(org.eclipse.milo.opcua.stack.server.services.ServiceRequest) DeleteSubscriptionsRequest(org.eclipse.milo.opcua.stack.core.types.structured.DeleteSubscriptionsRequest) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)

Aggregations

BaseMonitoredItem (org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem)7 UaException (org.eclipse.milo.opcua.stack.core.UaException)5 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)5 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)4 Unsigned.uint (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint)4 ResponseHeader (org.eclipse.milo.opcua.stack.core.types.structured.ResponseHeader)4 ServiceRequest (org.eclipse.milo.opcua.stack.server.services.ServiceRequest)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MonitoredItem (org.eclipse.milo.opcua.sdk.server.api.MonitoredItem)2 Lists (com.google.common.collect.Lists)1 Lists.newArrayListWithCapacity (com.google.common.collect.Lists.newArrayListWithCapacity)1 Maps (com.google.common.collect.Maps)1 AttributeKey (io.netty.util.AttributeKey)1 EnumSet (java.util.EnumSet)1 Iterator (java.util.Iterator)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1