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