Search in sources :

Example 1 with ManagedSubscription

use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.

the class ManagedSubscriptionDataExample method run.

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
    client.connect().get();
    ManagedSubscription subscription = ManagedSubscription.create(client);
    subscription.addDataChangeListener((items, values) -> {
        for (int i = 0; i < items.size(); i++) {
            logger.info("subscription value received: item={}, value={}", items.get(i).getNodeId(), values.get(i).getValue());
        }
    });
    ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
    if (dataItem.getStatusCode().isGood()) {
        logger.info("item created for nodeId={}", dataItem.getNodeId());
        // let the example run for 5 seconds before completing
        Thread.sleep(5000);
        dataItem.delete();
    } else {
        logger.warn("failed to create item for nodeId={} (status={})", dataItem.getNodeId(), dataItem.getStatusCode());
    }
    future.complete(client);
}
Also used : ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription)

Example 2 with ManagedSubscription

use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.

the class UaMethodTest method findMethod.

@Test
public void findMethod() throws UaException {
    ManagedSubscription subscription = ManagedSubscription.create(client);
    ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
    AddressSpace addressSpace = client.getAddressSpace();
    UaObjectNode serverNode = addressSpace.getObjectNode(Identifiers.Server);
    UaMethod getMonitoredItems = serverNode.getMethod("GetMonitoredItems");
    assertNotNull(getMonitoredItems);
    Argument[] inputArguments = getMonitoredItems.getInputArguments();
    Argument[] outputArguments = getMonitoredItems.getOutputArguments();
    assertEquals(1, inputArguments.length);
    assertEquals("SubscriptionId", inputArguments[0].getName());
    assertEquals(2, outputArguments.length);
    assertEquals("ServerHandles", outputArguments[0].getName());
    assertEquals("ClientHandles", outputArguments[1].getName());
    Variant[] outputs = getMonitoredItems.call(new Variant[] { new Variant(subscription.getSubscription().getSubscriptionId()) });
    UInteger[] expected0 = { dataItem.getMonitoredItem().getMonitoredItemId() };
    UInteger[] expected1 = { dataItem.getMonitoredItem().getClientHandle() };
    assertArrayEquals(expected0, (UInteger[]) outputs[0].getValue());
    assertArrayEquals(expected1, (UInteger[]) outputs[1].getValue());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) Argument(org.eclipse.milo.opcua.stack.core.types.structured.Argument) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Example 3 with ManagedSubscription

use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.

the class UaMethodTest method callMethod.

@Test
public void callMethod() throws UaException {
    ManagedSubscription subscription = ManagedSubscription.create(client);
    ManagedDataItem dataItem = subscription.createDataItem(Identifiers.Server_ServerStatus_CurrentTime);
    AddressSpace addressSpace = client.getAddressSpace();
    UaObjectNode serverNode = addressSpace.getObjectNode(Identifiers.Server);
    Variant[] outputs = serverNode.callMethod("GetMonitoredItems", new Variant[] { new Variant(subscription.getSubscription().getSubscriptionId()) });
    UInteger[] expected0 = { dataItem.getMonitoredItem().getMonitoredItemId() };
    UInteger[] expected1 = { dataItem.getMonitoredItem().getClientHandle() };
    assertArrayEquals(expected0, (UInteger[]) outputs[0].getValue());
    assertArrayEquals(expected1, (UInteger[]) outputs[1].getValue());
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) AddressSpace(org.eclipse.milo.opcua.sdk.client.AddressSpace) UaObjectNode(org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode) UInteger(org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger) ManagedDataItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) Test(org.junit.jupiter.api.Test) AbstractClientServerTest(org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)

Example 4 with ManagedSubscription

use of org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription in project milo by eclipse.

the class ManagedSubscriptionEventExample method run.

@Override
public void run(OpcUaClient client, CompletableFuture<OpcUaClient> future) throws Exception {
    client.connect().get();
    final CountDownLatch eventLatch = new CountDownLatch(3);
    ManagedSubscription subscription = ManagedSubscription.create(client);
    subscription.addEventChangeListener((eventItems, variants) -> {
        for (int i = 0; i < eventItems.size(); i++) {
            ManagedEventItem eventItem = eventItems.get(i);
            Variant[] eventFieldValues = variants.get(i);
            logger.info("Event Received from {}", eventItem.getNodeId());
            for (int j = 0; j < eventFieldValues.length; j++) {
                logger.info("\tvariant[{}]: {}", j, eventFieldValues[j].getValue());
            }
        }
        eventLatch.countDown();
    });
    EventFilter eventFilter = new EventFilterBuilder().select(Identifiers.BaseEventType, new QualifiedName(0, "EventId")).select(Identifiers.BaseEventType, new QualifiedName(0, "EventType")).select(Identifiers.BaseEventType, new QualifiedName(0, "Severity")).select(Identifiers.BaseEventType, new QualifiedName(0, "Time")).select(Identifiers.BaseEventType, new QualifiedName(0, "Message")).build();
    ManagedEventItem eventItem = subscription.createEventItem(Identifiers.Server, eventFilter);
    // wait for some events to arrive before completing
    eventLatch.await();
    eventItem.delete();
    future.complete(client);
}
Also used : Variant(org.eclipse.milo.opcua.stack.core.types.builtin.Variant) QualifiedName(org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName) ManagedEventItem(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedEventItem) CountDownLatch(java.util.concurrent.CountDownLatch) ManagedSubscription(org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription) EventFilter(org.eclipse.milo.opcua.stack.core.types.structured.EventFilter) EventFilterBuilder(org.eclipse.milo.opcua.sdk.client.subscriptions.EventFilterBuilder)

Aggregations

ManagedSubscription (org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedSubscription)4 ManagedDataItem (org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedDataItem)3 Variant (org.eclipse.milo.opcua.stack.core.types.builtin.Variant)3 AddressSpace (org.eclipse.milo.opcua.sdk.client.AddressSpace)2 UaObjectNode (org.eclipse.milo.opcua.sdk.client.nodes.UaObjectNode)2 AbstractClientServerTest (org.eclipse.milo.opcua.sdk.test.AbstractClientServerTest)2 UInteger (org.eclipse.milo.opcua.stack.core.types.builtin.unsigned.UInteger)2 Test (org.junit.jupiter.api.Test)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 EventFilterBuilder (org.eclipse.milo.opcua.sdk.client.subscriptions.EventFilterBuilder)1 ManagedEventItem (org.eclipse.milo.opcua.sdk.client.subscriptions.ManagedEventItem)1 QualifiedName (org.eclipse.milo.opcua.stack.core.types.builtin.QualifiedName)1 Argument (org.eclipse.milo.opcua.stack.core.types.structured.Argument)1 EventFilter (org.eclipse.milo.opcua.stack.core.types.structured.EventFilter)1