Search in sources :

Example 1 with SetTriggeringRequest

use of org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringRequest in project milo by eclipse.

the class SubscriptionManager method setTriggering.

public void setTriggering(ServiceRequest service) {
    SetTriggeringRequest request = (SetTriggeringRequest) service.getRequest();
    UInteger subscriptionId = request.getSubscriptionId();
    Subscription subscription = subscriptions.get(subscriptionId);
    if (subscription == null) {
        service.setServiceFault(StatusCodes.Bad_SubscriptionIdInvalid);
        return;
    }
    UInteger triggerId = request.getTriggeringItemId();
    List<UInteger> linksToAdd = l(request.getLinksToAdd());
    List<UInteger> linksToRemove = l(request.getLinksToRemove());
    if (linksToAdd.isEmpty() && linksToRemove.isEmpty()) {
        service.setServiceFault(StatusCodes.Bad_NothingToDo);
        return;
    }
    StatusCode[] addResults;
    StatusCode[] removeResults;
    synchronized (subscription) {
        Map<UInteger, BaseMonitoredItem<?>> itemsById = subscription.getMonitoredItems();
        BaseMonitoredItem<?> triggerItem = itemsById.get(triggerId);
        if (triggerItem == null) {
            service.setServiceFault(StatusCodes.Bad_MonitoredItemIdInvalid);
            return;
        }
        removeResults = linksToRemove.stream().map(linkedItemId -> {
            BaseMonitoredItem<?> item = itemsById.get(linkedItemId);
            if (item != null) {
                if (triggerItem.getTriggeredItems().remove(linkedItemId) != null) {
                    return StatusCode.GOOD;
                } else {
                    return new StatusCode(StatusCodes.Bad_MonitoredItemIdInvalid);
                }
            } else {
                return new StatusCode(StatusCodes.Bad_MonitoredItemIdInvalid);
            }
        }).toArray(StatusCode[]::new);
        addResults = linksToAdd.stream().map(linkedItemId -> {
            BaseMonitoredItem<?> linkedItem = itemsById.get(linkedItemId);
            if (linkedItem != null) {
                triggerItem.getTriggeredItems().put(linkedItemId, linkedItem);
                return StatusCode.GOOD;
            } else {
                return new StatusCode(StatusCodes.Bad_MonitoredItemIdInvalid);
            }
        }).toArray(StatusCode[]::new);
    }
    SetTriggeringResponse response = new SetTriggeringResponse(service.createResponseHeader(), addResults, new DiagnosticInfo[0], removeResults, new DiagnosticInfo[0]);
    service.setResponse(response);
}
Also used : BaseMonitoredItem(org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem) SetTriggeringRequest(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringRequest) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) SetTriggeringResponse(org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringResponse)

Aggregations

BaseMonitoredItem (org.eclipse.milo.opcua.sdk.server.items.BaseMonitoredItem)1 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)1 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)1 SetTriggeringRequest (org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringRequest)1 SetTriggeringResponse (org.eclipse.milo.opcua.stack.core.types.structured.SetTriggeringResponse)1