Search in sources :

Example 1 with SimpleAttributeOperand

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

the class ManagedSubscriptionTest method eventChangeListener.

@Test
public void eventChangeListener() throws UaException, InterruptedException {
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    subscription.addChangeListener(new ChangeListener() {

        @Override
        public void onEventReceived(List<ManagedEventItem> eventItems, List<Variant[]> eventFields) {
            if (eventItems.get(0).getNodeId().equals(Identifiers.Server)) {
                latch1.countDown();
            }
        }
    });
    subscription.addEventChangeListener((eventItems, eventFields) -> {
        if (eventItems.get(0).getNodeId().equals(Identifiers.Server)) {
            latch2.countDown();
        }
    });
    EventFilter eventFilter = new EventFilter(new SimpleAttributeOperand[] { new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventId") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Time") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Message") }, AttributeId.Value.uid(), null) }, new ContentFilter(null));
    ManagedEventItem eventItem = subscription.createEventItem(Identifiers.Server, eventFilter);
    assertTrue(eventItem.getStatusCode().isGood());
    assertTrue(latch1.await(5, TimeUnit.SECONDS));
    assertTrue(latch2.await(5, TimeUnit.SECONDS));
}
Also used : SimpleAttributeOperand(org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) ChangeListener(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription.ChangeListener) CountDownLatch(java.util.concurrent.CountDownLatch) EventFilter(org.eclipse.milo.opcua.stack.core.types.structured.EventFilter) ContentFilter(org.eclipse.milo.opcua.stack.core.types.structured.ContentFilter) Test(org.junit.jupiter.api.Test)

Example 2 with SimpleAttributeOperand

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

the class EventSubscriptionExample method run.

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
    // synchronous connect
    client.connect().get();
    // create a subscription and a monitored item
    UaSubscription subscription = client.getSubscriptionManager().createSubscription(1000.0).get();
    ReadValueId readValueId = new ReadValueId(Identifiers.Server, AttributeId.EventNotifier.uid(), null, QualifiedName.NULL_VALUE);
    // client handle must be unique per item
    UInteger clientHandle = uint(clientHandles.getAndIncrement());
    EventFilter eventFilter = new EventFilter(new SimpleAttributeOperand[] { new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventId") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventType") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Severity") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Time") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Message") }, AttributeId.Value.uid(), null) }, new ContentFilter(null));
    MonitoringParameters parameters = new MonitoringParameters(clientHandle, 0.0, ExtensionObject.encode(client.getStaticSerializationContext(), eventFilter), uint(10), true);
    MonitoredItemCreateRequest request = new MonitoredItemCreateRequest(readValueId, MonitoringMode.Reporting, parameters);
    List<UaMonitoredItem> items = subscription.createMonitoredItems(TimestampsToReturn.Both, newArrayList(request)).get();
    // do something with the value updates
    UaMonitoredItem monitoredItem = items.get(0);
    final AtomicInteger eventCount = new AtomicInteger(0);
    monitoredItem.setEventConsumer((item, vs) -> {
        logger.info("Event Received from {}", item.getReadValueId().getNodeId());
        for (int i = 0; i < vs.length; i++) {
            logger.info("\tvariant[{}]: {}", i, vs[i].getValue());
        }
        if (eventCount.incrementAndGet() == 3) {
            future.complete(client);
        }
    });
}
Also used : UaMonitoredItem(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem) UaSubscription(org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaSubscription) MonitoredItemCreateRequest(org.eclipse.milo.opcua.stack.core.types.structured.MonitoredItemCreateRequest) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) EventFilter(org.eclipse.milo.opcua.stack.core.types.structured.EventFilter) ContentFilter(org.eclipse.milo.opcua.stack.core.types.structured.ContentFilter) Unsigned.uint(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.Unsigned.uint) ReadValueId(org.eclipse.milo.opcua.stack.core.types.structured.ReadValueId) SimpleAttributeOperand(org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) MonitoringParameters(org.eclipse.milo.opcua.stack.core.types.structured.MonitoringParameters)

Example 3 with SimpleAttributeOperand

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

the class EventContentFilter method validateFilterElement.

private static ContentFilterElementResult validateFilterElement(@NotNull FilterContext context, @NotNull ContentFilterElement filterElement) {
    FilterOperator filterOperator = filterElement.getFilterOperator();
    if (!Operators.SUPPORTED_OPERATORS.contains(filterOperator)) {
        return new ContentFilterElementResult(new StatusCode(StatusCodes.Bad_FilterOperatorUnsupported), new StatusCode[0], new DiagnosticInfo[0]);
    }
    ExtensionObject[] xos = filterElement.getFilterOperands();
    if (xos == null || xos.length == 0) {
        return new ContentFilterElementResult(new StatusCode(StatusCodes.Bad_FilterOperandCountMismatch), new StatusCode[0], new DiagnosticInfo[0]);
    }
    FilterOperand[] operands = new FilterOperand[xos.length];
    StatusCode[] operandStatusCodes = new StatusCode[xos.length];
    for (int i = 0; i < xos.length; i++) {
        Object operand = xos[i].decodeOrNull(context.getServer().getSerializationContext());
        if (operand instanceof FilterOperand) {
            operands[i] = (FilterOperand) operand;
            if (operand instanceof SimpleAttributeOperand) {
                try {
                    validateSimpleOperand(context, (SimpleAttributeOperand) operand);
                    operandStatusCodes[i] = StatusCode.GOOD;
                } catch (ValidationException e) {
                    operandStatusCodes[i] = e.getStatusCode();
                }
            } else if (operand instanceof ElementOperand) {
                operandStatusCodes[i] = StatusCode.GOOD;
            } else if (operand instanceof LiteralOperand) {
                operandStatusCodes[i] = StatusCode.GOOD;
            } else {
                // includes AttributeOperand and any unknown/unhandle subclasses
                operandStatusCodes[i] = new StatusCode(StatusCodes.Bad_FilterOperandInvalid);
            }
        } else {
            operandStatusCodes[i] = new StatusCode(StatusCodes.Bad_FilterOperandInvalid);
        }
    }
    StatusCode operatorStatus = StatusCode.GOOD;
    try {
        Operator<?> operator = getOperator(filterOperator);
        operator.validate(context, operands);
    } catch (ValidationException e) {
        operatorStatus = e.getStatusCode();
    }
    return new ContentFilterElementResult(operatorStatus, operandStatusCodes, new DiagnosticInfo[0]);
}
Also used : LiteralOperand(org.eclipse.milo.opcua.stack.core.types.structured.LiteralOperand) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode) ElementOperand(org.eclipse.milo.opcua.stack.core.types.structured.ElementOperand) FilterOperator(org.eclipse.milo.opcua.stack.core.types.enumerated.FilterOperator) SimpleAttributeOperand(org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand) FilterOperand(org.eclipse.milo.opcua.stack.core.types.structured.FilterOperand) ExtensionObject(org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject) ContentFilterElementResult(org.eclipse.milo.opcua.stack.core.types.structured.ContentFilterElementResult)

Example 4 with SimpleAttributeOperand

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

the class ManagedSubscriptionTest method createAndDeleteEventItem.

@Test
public void createAndDeleteEventItem() throws UaException {
    EventFilter eventFilter = new EventFilter(new SimpleAttributeOperand[] { new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "EventId") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Time") }, AttributeId.Value.uid(), null), new SimpleAttributeOperand(Identifiers.BaseEventType, new QualifiedName[] { new QualifiedName(0, "Message") }, AttributeId.Value.uid(), null) }, new ContentFilter(null));
    ManagedEventItem eventItem = subscription.createEventItem(Identifiers.Server, eventFilter);
    assertTrue(eventItem.getStatusCode().isGood());
    subscription.deleteEventItem(eventItem);
    assertFalse(subscription.getEventItems().contains(eventItem));
}
Also used : SimpleAttributeOperand(org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) EventFilter(org.eclipse.milo.opcua.stack.core.types.structured.EventFilter) ContentFilter(org.eclipse.milo.opcua.stack.core.types.structured.ContentFilter) Test(org.junit.jupiter.api.Test)

Example 5 with SimpleAttributeOperand

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

the class EventContentFilter method validateSelectClauses.

private static SelectClauseValidationResult validateSelectClauses(FilterContext context, SimpleAttributeOperand[] selectClauses) {
    List<StatusCode> statusCodes = new ArrayList<>();
    List<DiagnosticInfo> diagnosticInfos = new ArrayList<>();
    for (SimpleAttributeOperand select : selectClauses) {
        try {
            validateSimpleOperand(context, select);
            statusCodes.add(StatusCode.GOOD);
            diagnosticInfos.add(DiagnosticInfo.NULL_VALUE);
        } catch (ValidationException e) {
            statusCodes.add(e.getStatusCode());
            diagnosticInfos.add(e.getDiagnosticInfo());
        } catch (Throwable t) {
            LOGGER.error("Unexpected error validating operand: {}", select, t);
            statusCodes.add(new StatusCode(StatusCodes.Bad_InternalError));
            diagnosticInfos.add(DiagnosticInfo.NULL_VALUE);
        }
    }
    return new SelectClauseValidationResult(statusCodes.toArray(new StatusCode[0]), diagnosticInfos.toArray(new DiagnosticInfo[0]));
}
Also used : SimpleAttributeOperand(org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand) DiagnosticInfo(org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo) ArrayList(java.util.ArrayList) StatusCode(org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)

Aggregations

SimpleAttributeOperand (org.eclipse.milo.opcua.stack.core.types.structured.SimpleAttributeOperand)7 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)4 ContentFilter (org.eclipse.milo.opcua.stack.core.types.structured.ContentFilter)4 EventFilter (org.eclipse.milo.opcua.stack.core.types.structured.EventFilter)4 StatusCode (org.eclipse.milo.opcua.stack.core.types.builtin.StatusCode)3 ArrayList (java.util.ArrayList)2 UaException (org.eclipse.milo.opcua.stack.core.UaException)2 DiagnosticInfo (org.eclipse.milo.opcua.stack.core.types.builtin.DiagnosticInfo)2 ExtensionObject (org.eclipse.milo.opcua.stack.core.types.builtin.ExtensionObject)2 ContentFilterResult (org.eclipse.milo.opcua.stack.core.types.structured.ContentFilterResult)2 EventFilterResult (org.eclipse.milo.opcua.stack.core.types.structured.EventFilterResult)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Predicate (java.util.function.Predicate)1 UaMonitoredItem (org.eclipse.milo.opcua.sdk.client.api.subscriptions.UaMonitoredItem)1